Skip to content

Commit 4844af0

Browse files
authored
Merge pull request #37 from yacketrj/upstream/console-update-release-source
Fix Console Update release source for forks
2 parents 670dfdc + 28cbaaf commit 4844af0

2 files changed

Lines changed: 156 additions & 19 deletions

File tree

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import test from "node:test";
2+
import assert from "node:assert/strict";
3+
import { spawn, spawnSync } from "node:child_process";
4+
import { createServer } from "node:http";
5+
import { chmodSync, copyFileSync, mkdirSync, mkdtempSync, writeFileSync } from "node:fs";
6+
import { tmpdir } from "node:os";
7+
import { dirname, join, resolve } from "node:path";
8+
import { fileURLToPath } from "node:url";
9+
10+
const repoRoot = resolve(dirname(fileURLToPath(import.meta.url)), "../../..");
11+
12+
test("self-update check prefers the official upstream release repo in fork checkouts", async () => {
13+
const dir = mkdtempSync(join(tmpdir(), "arrakis-self-update-"));
14+
mkdirSync(join(dir, "runtime", "scripts"), { recursive: true });
15+
copyFileSync(join(repoRoot, "runtime", "scripts", "self-update.sh"), join(dir, "runtime", "scripts", "self-update.sh"));
16+
chmodSync(join(dir, "runtime", "scripts", "self-update.sh"), 0o700);
17+
writeFileSync(join(dir, "VERSION"), "v1.3.37\n");
18+
19+
assert.equal(spawnSync("git", ["init", "-q"], { cwd: dir }).status, 0);
20+
assert.equal(spawnSync("git", ["remote", "add", "origin", "git@github.com:yacketrj/dune-awakening-selfhost-docker-WSL.git"], { cwd: dir }).status, 0);
21+
assert.equal(spawnSync("git", ["remote", "add", "upstream", "https://github.com/Red-Blink/dune-awakening-selfhost-docker.git"], { cwd: dir }).status, 0);
22+
23+
const requests = [];
24+
const server = createServer((req, res) => {
25+
requests.push(req.url || "");
26+
if (req.url === "/repos/Red-Blink/dune-awakening-selfhost-docker/releases/latest") {
27+
res.writeHead(200, { "content-type": "application/json" });
28+
res.end(JSON.stringify({ tag_name: "v1.3.37" }));
29+
return;
30+
}
31+
res.writeHead(404, { "content-type": "application/json" });
32+
res.end(JSON.stringify({ message: "not found" }));
33+
});
34+
await new Promise((resolveListen) => server.listen(0, "127.0.0.1", resolveListen));
35+
36+
try {
37+
const address = server.address();
38+
const apiBase = `http://127.0.0.1:${address.port}`;
39+
const result = await runProcess("bash", ["runtime/scripts/self-update.sh", "check"], {
40+
cwd: dir,
41+
timeout: 15000,
42+
env: { ...process.env, DUNE_SELF_UPDATE_API_BASE: apiBase, NO_PROXY: "127.0.0.1,localhost", no_proxy: "127.0.0.1,localhost" }
43+
});
44+
45+
assert.equal(result.status, 0, result.stderr || result.stdout);
46+
assert.match(result.stdout, /GitHub repo:\s+Red-Blink\/dune-awakening-selfhost-docker/);
47+
assert(!result.stdout.includes("yacketrj/dune-awakening-selfhost-docker-WSL"));
48+
assert.deepEqual(requests, ["/repos/Red-Blink/dune-awakening-selfhost-docker/releases/latest"]);
49+
} finally {
50+
server.closeAllConnections();
51+
await new Promise((resolveClose) => server.close(resolveClose));
52+
}
53+
});
54+
55+
function runProcess(command, args, options = {}) {
56+
return new Promise((resolve, reject) => {
57+
const { timeout = 15000, ...spawnOptions } = options;
58+
const child = spawn(command, args, spawnOptions);
59+
let stdout = "";
60+
let stderr = "";
61+
const timer = setTimeout(() => {
62+
child.kill("SIGKILL");
63+
reject(new Error(`${command} ${args.join(" ")} timed out\n${stdout}\n${stderr}`));
64+
}, timeout);
65+
66+
child.stdout?.on("data", (chunk) => {
67+
stdout += chunk.toString();
68+
});
69+
child.stderr?.on("data", (chunk) => {
70+
stderr += chunk.toString();
71+
});
72+
child.on("error", (error) => {
73+
clearTimeout(timer);
74+
reject(error);
75+
});
76+
child.on("close", (status, signal) => {
77+
clearTimeout(timer);
78+
resolve({ status, signal, stdout, stderr });
79+
});
80+
});
81+
}

runtime/scripts/self-update.sh

Lines changed: 75 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,93 @@ ROOT_DIR="$(pwd)"
66

77
CURRENT_VERSION="dev"
88
[ -f VERSION ] && CURRENT_VERSION="$(tr -d '[:space:]' < VERSION)"
9+
DEFAULT_SELF_UPDATE_REPO="Red-Blink/dune-awakening-selfhost-docker"
910

10-
detect_github_repo() {
11+
normalize_github_remote_repo() {
12+
local remote="$1"
13+
14+
case "$remote" in
15+
https://github.com/*)
16+
remote="${remote#https://github.com/}"
17+
;;
18+
git@github.com:*)
19+
remote="${remote#git@github.com:}"
20+
;;
21+
ssh://git@github.com/*)
22+
remote="${remote#ssh://git@github.com/}"
23+
;;
24+
*)
25+
return 1
26+
;;
27+
esac
28+
29+
remote="${remote%.git}"
30+
remote="${remote%/}"
31+
[ -n "$remote" ] || return 1
32+
printf '%s\n' "$remote"
33+
}
34+
35+
github_repo_from_git_remote() {
36+
local remote_name="$1"
1137
local remote
1238

39+
remote="$(git remote get-url "$remote_name" 2>/dev/null || true)"
40+
[ -n "$remote" ] || return 1
41+
normalize_github_remote_repo "$remote"
42+
}
43+
44+
detect_github_repo() {
45+
local remote_name repo
46+
1347
if [ -n "${DUNE_SELF_UPDATE_REPO:-}" ]; then
1448
printf '%s\n' "$DUNE_SELF_UPDATE_REPO"
1549
return 0
1650
fi
1751

1852
if command -v git >/dev/null 2>&1; then
19-
remote="$(git remote get-url origin 2>/dev/null || true)"
20-
case "$remote" in
21-
https://github.com/*)
22-
remote="${remote#https://github.com/}"
23-
remote="${remote%.git}"
24-
printf '%s\n' "$remote"
53+
for remote_name in upstream origin; do
54+
repo="$(github_repo_from_git_remote "$remote_name" 2>/dev/null || true)"
55+
if [ "$repo" = "$DEFAULT_SELF_UPDATE_REPO" ]; then
56+
printf '%s\n' "$repo"
2557
return 0
26-
;;
27-
git@github.com:*)
28-
remote="${remote#git@github.com:}"
29-
remote="${remote%.git}"
30-
printf '%s\n' "$remote"
58+
fi
59+
done
60+
61+
repo="$(github_repo_from_git_remote upstream 2>/dev/null || true)"
62+
if [ -n "$repo" ]; then
63+
printf '%s\n' "$repo"
64+
return 0
65+
fi
66+
67+
repo="$(github_repo_from_git_remote origin 2>/dev/null || true)"
68+
if [ -n "$repo" ]; then
69+
printf '%s\n' "$repo"
70+
return 0
71+
fi
72+
fi
73+
74+
printf '%s\n' "$DEFAULT_SELF_UPDATE_REPO"
75+
}
76+
77+
detect_github_fetch_remote() {
78+
local repo="$1"
79+
local remote_name remote_repo
80+
81+
if command -v git >/dev/null 2>&1; then
82+
for remote_name in upstream origin; do
83+
remote_repo="$(github_repo_from_git_remote "$remote_name" 2>/dev/null || true)"
84+
if [ "$remote_repo" = "$repo" ]; then
85+
printf '%s\n' "$remote_name"
3186
return 0
32-
;;
33-
esac
87+
fi
88+
done
3489
fi
3590

36-
printf '%s\n' "Red-Blink/dune-awakening-selfhost-docker"
91+
printf '%s\n' "https://github.com/${repo}.git"
3792
}
3893

3994
GITHUB_REPO="$(detect_github_repo)"
95+
GITHUB_FETCH_REMOTE="$(detect_github_fetch_remote "$GITHUB_REPO")"
4096
GITHUB_API_BASE="${DUNE_SELF_UPDATE_API_BASE:-https://api.github.com}"
4197
GITHUB_TOKEN="${DUNE_SELF_UPDATE_TOKEN:-}"
4298
LATEST_TAG_CACHE_FILE="runtime/generated/self-update-latest-tag.txt"
@@ -846,12 +902,12 @@ install_release_tag_with_git() {
846902
exit 2
847903
}
848904

849-
remote="$(git remote get-url origin 2>/dev/null || true)"
905+
remote="$GITHUB_FETCH_REMOTE"
850906
echo "Updating stack Git checkout from:"
851907
echo " $remote"
852908
echo "Fetching release tag: $tag"
853-
git fetch --force --tags origin
854-
git fetch --force origin "refs/tags/${tag}:refs/tags/${tag}" >/dev/null 2>&1 || true
909+
git fetch --force --tags "$remote"
910+
git fetch --force "$remote" "refs/tags/${tag}:refs/tags/${tag}" >/dev/null 2>&1 || true
855911

856912
target="$(git rev-parse -q --verify "refs/tags/${tag}^{commit}" 2>/dev/null || true)"
857913
if [ -z "$target" ]; then
@@ -945,13 +1001,13 @@ case "$cmd" in
9451001
;;
9461002

9471003
check|status)
948-
echo "Current stack version: $CURRENT_VERSION"
9491004
set +e
9501005
latest="$(latest_release_tag)"
9511006
rc=$?
9521007
set -e
9531008

9541009
if [ "$rc" -ne 0 ] || [ -z "${latest:-}" ]; then
1010+
echo "Current stack version: $CURRENT_VERSION"
9551011
echo "Latest release: unknown"
9561012
echo "GitHub repo: $GITHUB_REPO"
9571013
echo

0 commit comments

Comments
 (0)