Skip to content

Commit b55d6e8

Browse files
authored
Improve the experience of testing pull requests (#5942)
1 parent 59d1006 commit b55d6e8

File tree

7 files changed

+112
-34
lines changed

7 files changed

+112
-34
lines changed

docker/dockerfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ USER node
7979
RUN git config --global user.email "[email protected]"
8080
RUN git config --global user.name "PR Tester"
8181
RUN git clone https://github.com/louislam/uptime-kuma.git .
82+
83+
# Hide the warning when running in detached head state
84+
RUN git config --global advice.detachedHead false
85+
8286
RUN npm ci
8387

8488
EXPOSE 3000 3001

extra/checkout-pr.js

Lines changed: 0 additions & 33 deletions
This file was deleted.

extra/checkout-pr.mjs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import childProcess from "child_process";
2+
import { parsePrName } from "./kuma-pr/pr-lib.mjs";
3+
4+
let { name, branch } = parsePrName(process.env.UPTIME_KUMA_GH_REPO);
5+
6+
console.log(`Checking out PR from ${name}:${branch}`);
7+
8+
// Checkout the pr
9+
let result = childProcess.spawnSync("git", [ "remote", "add", name, `https://github.com/${name}/uptime-kuma` ], {
10+
stdio: "inherit"
11+
});
12+
13+
if (result.status !== 0) {
14+
console.error("Failed to add remote repository.");
15+
process.exit(1);
16+
}
17+
18+
result = childProcess.spawnSync("git", [ "fetch", name, branch ], {
19+
stdio: "inherit"
20+
});
21+
22+
if (result.status !== 0) {
23+
console.error("Failed to fetch the branch.");
24+
process.exit(1);
25+
}
26+
27+
result = childProcess.spawnSync("git", [ "checkout", `${name}/${branch}`, "--force" ], {
28+
stdio: "inherit"
29+
});
30+
31+
if (result.status !== 0) {
32+
console.error("Failed to checkout the branch.");
33+
process.exit(1);
34+
}

extra/kuma-pr/index.mjs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env node
2+
import { spawn } from "child_process";
3+
import { parsePrName } from "./pr-lib.mjs";
4+
5+
const prName = process.argv[2];
6+
7+
// Pre-check the prName here, so testers don't need to wait until the Docker image is pulled to see the error.
8+
try {
9+
parsePrName(prName);
10+
} catch (error) {
11+
console.error(error.message);
12+
process.exit(1);
13+
}
14+
15+
spawn("docker", [
16+
"run",
17+
"--rm",
18+
"-it",
19+
"-p", "3000:3000",
20+
"-p", "3001:3001",
21+
"--pull", "always",
22+
"-e", `UPTIME_KUMA_GH_REPO=${prName}`,
23+
"louislam/uptime-kuma:pr-test2"
24+
], {
25+
stdio: "inherit",
26+
});

extra/kuma-pr/package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "kuma-pr",
3+
"version": "1.0.0",
4+
"type": "module",
5+
"bin": {
6+
"kuma-pr": "./index.mjs"
7+
}
8+
}

extra/kuma-pr/pr-lib.mjs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Parse <name>:<branch> to an object.
3+
* @param {string} prName <name>:<branch>
4+
* @returns {object} An object with name and branch properties.
5+
*/
6+
export function parsePrName(prName) {
7+
let name = "louislam";
8+
let branch;
9+
10+
const errorMessage = "Please set a repo to the environment variable 'UPTIME_KUMA_GH_REPO' (e.g. mhkarimi1383:goalert-notification)";
11+
12+
if (!prName) {
13+
throw new Error(errorMessage);
14+
}
15+
16+
prName = prName.trim();
17+
if (prName === "") {
18+
throw new Error(errorMessage);
19+
}
20+
21+
let inputArray = prName.split(":");
22+
23+
// Just realized that owner's prs are not prefixed with "louislam:"
24+
if (inputArray.length === 1) {
25+
branch = inputArray[0];
26+
27+
} else if (inputArray.length === 2) {
28+
name = inputArray[0];
29+
branch = inputArray[1];
30+
31+
} else {
32+
throw new Error("Invalid format. The format is like this: mhkarimi1383:goalert-notification");
33+
}
34+
35+
return {
36+
name,
37+
branch
38+
};
39+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
"release-nightly": "node ./extra/release/nightly.mjs",
5858
"git-remove-tag": "git tag -d",
5959
"build-dist-and-restart": "npm run build && npm run start-server-dev",
60-
"start-pr-test": "node extra/checkout-pr.js && npm install && npm run dev",
60+
"start-pr-test": "node extra/checkout-pr.mjs && npm install && npm run dev",
6161
"build-healthcheck-armv7": "cross-env GOOS=linux GOARCH=arm GOARM=7 go build -x -o ./extra/healthcheck-armv7 ./extra/healthcheck.go",
6262
"deploy-demo-server": "node extra/deploy-demo-server.js",
6363
"sort-contributors": "node extra/sort-contributors.js",

0 commit comments

Comments
 (0)