Skip to content

Commit 0627cd2

Browse files
committed
Update GitHub Pages config
1 parent 5aa9a0d commit 0627cd2

File tree

3 files changed

+159
-0
lines changed

3 files changed

+159
-0
lines changed

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ module.exports = async (argv) => {
4848
let transforms = [
4949
"create-branch",
5050
"update-default-branch",
51+
"github-pages",
5152
"retarget-pull-requests",
5253
"branch-protection",
5354
"delete-old-branch",

transforms/github-pages.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
module.exports = async function ({
2+
owner,
3+
repo,
4+
old,
5+
target,
6+
octokit,
7+
log,
8+
dryRun,
9+
}) {
10+
log = log.extend("transforms:github-pages");
11+
12+
log("Updating GitHub Pages config");
13+
14+
try {
15+
const {
16+
data: { source },
17+
} = await octokit.repos.getPages({
18+
owner,
19+
repo,
20+
});
21+
22+
if (source.branch != old) {
23+
log(
24+
`Skipping GitHub pages as [${source.branch}] does not match [${old}]`
25+
);
26+
return;
27+
}
28+
29+
source.branch = target;
30+
31+
await octokit.repos.updateInformationAboutPagesSite({
32+
owner,
33+
repo,
34+
source,
35+
});
36+
37+
log(
38+
`Updated GitHub pages from [${old}] to [${target}] with path [${source.path}]`
39+
);
40+
} catch (e) {
41+
log(`No GitHub Pages found for [${owner}/${repo}]`);
42+
}
43+
};

transforms/github-pages.test.js

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
const nock = require("nock");
2+
nock.disableNetConnect();
3+
4+
const log = require("../src/mock-debug");
5+
const githubPages = require("./github-pages");
6+
7+
const createBranch = require("./github-pages");
8+
const octokit = require("../src/octokit")();
9+
10+
afterEach(() => {
11+
if (!nock.isDone()) {
12+
throw new Error("Not all nock interceptors were used");
13+
}
14+
log.cleanAll();
15+
nock.cleanAll();
16+
});
17+
18+
describe("#github-pages", () => {
19+
it("continues with a log if pages is not enabled", async () => {
20+
nock("https://api.github.com/").get("/repos/demo/repo/pages").reply(404);
21+
22+
await githubPages({
23+
owner: "demo",
24+
repo: "repo",
25+
old: "master",
26+
target: "main",
27+
octokit,
28+
log,
29+
});
30+
31+
expect(log.logger).toBeCalledWith("No GitHub Pages found for [demo/repo]");
32+
});
33+
34+
it("logs if the configured branch does not match the old branch", async () => {
35+
nock("https://api.github.com/")
36+
.get("/repos/demo/repo/pages")
37+
.reply(200, {
38+
source: {
39+
branch: "custom",
40+
path: "/",
41+
},
42+
});
43+
44+
await githubPages({
45+
owner: "demo",
46+
repo: "repo",
47+
old: "master",
48+
target: "main",
49+
octokit,
50+
log,
51+
});
52+
53+
expect(log.logger).toBeCalledWith(
54+
"Skipping GitHub pages as [custom] does not match [master]"
55+
);
56+
});
57+
58+
it("configures actions with the new branch", async () => {
59+
nock("https://api.github.com/")
60+
.get("/repos/demo/repo/pages")
61+
.reply(200, {
62+
source: {
63+
branch: "master",
64+
path: "/",
65+
},
66+
});
67+
68+
nock("https://api.github.com/")
69+
.put("/repos/demo/repo/pages", { source: { branch: "main", path: "/" } })
70+
.reply(200);
71+
72+
await githubPages({
73+
owner: "demo",
74+
repo: "repo",
75+
old: "master",
76+
target: "main",
77+
octokit,
78+
log,
79+
});
80+
81+
expect(log.logger).toBeCalledWith(
82+
"Updated GitHub pages from [master] to [main] with path [/]"
83+
);
84+
});
85+
86+
it("configures actions with the new branch (custom path)", async () => {
87+
nock("https://api.github.com/")
88+
.get("/repos/demo/repo/pages")
89+
.reply(200, {
90+
source: {
91+
branch: "master",
92+
path: "/docs",
93+
},
94+
});
95+
96+
nock("https://api.github.com/")
97+
.put("/repos/demo/repo/pages", {
98+
source: { branch: "main", path: "/docs" },
99+
})
100+
.reply(200);
101+
102+
await githubPages({
103+
owner: "demo",
104+
repo: "repo",
105+
old: "master",
106+
target: "main",
107+
octokit,
108+
log,
109+
});
110+
111+
expect(log.logger).toBeCalledWith(
112+
"Updated GitHub pages from [master] to [main] with path [/docs]"
113+
);
114+
});
115+
});

0 commit comments

Comments
 (0)