Skip to content

Update GitHub Pages config #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ module.exports = async (argv) => {
let transforms = [
"create-branch",
"update-default-branch",
"github-pages",
"retarget-pull-requests",
"branch-protection",
"delete-old-branch",
Expand Down
43 changes: 43 additions & 0 deletions transforms/github-pages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
module.exports = async function ({
owner,
repo,
old,
target,
octokit,
log,
dryRun,
}) {
log = log.extend("transforms:github-pages");

log("Updating GitHub Pages config");

try {
const {
data: { source },
} = await octokit.repos.getPages({
owner,
repo,
});

if (source.branch != old) {
log(
`Skipping GitHub pages as [${source.branch}] does not match [${old}]`
);
return;
}

source.branch = target;

await octokit.repos.updateInformationAboutPagesSite({
owner,
repo,
source,
});

log(
`Updated GitHub pages from [${old}] to [${target}] with path [${source.path}]`
);
} catch (e) {
log(`No GitHub Pages found for [${owner}/${repo}]`);
}
};
115 changes: 115 additions & 0 deletions transforms/github-pages.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
const nock = require("nock");
nock.disableNetConnect();

const log = require("../src/mock-debug");
const githubPages = require("./github-pages");

const createBranch = require("./github-pages");
const octokit = require("../src/octokit")();

afterEach(() => {
if (!nock.isDone()) {
throw new Error("Not all nock interceptors were used");
}
log.cleanAll();
nock.cleanAll();
});

describe("#github-pages", () => {
it("continues with a log if pages is not enabled", async () => {
nock("https://api.github.com/").get("/repos/demo/repo/pages").reply(404);

await githubPages({
owner: "demo",
repo: "repo",
old: "master",
target: "main",
octokit,
log,
});

expect(log.logger).toBeCalledWith("No GitHub Pages found for [demo/repo]");
});

it("logs if the configured branch does not match the old branch", async () => {
nock("https://api.github.com/")
.get("/repos/demo/repo/pages")
.reply(200, {
source: {
branch: "custom",
path: "/",
},
});

await githubPages({
owner: "demo",
repo: "repo",
old: "master",
target: "main",
octokit,
log,
});

expect(log.logger).toBeCalledWith(
"Skipping GitHub pages as [custom] does not match [master]"
);
});

it("configures actions with the new branch", async () => {
nock("https://api.github.com/")
.get("/repos/demo/repo/pages")
.reply(200, {
source: {
branch: "master",
path: "/",
},
});

nock("https://api.github.com/")
.put("/repos/demo/repo/pages", { source: { branch: "main", path: "/" } })
.reply(200);

await githubPages({
owner: "demo",
repo: "repo",
old: "master",
target: "main",
octokit,
log,
});

expect(log.logger).toBeCalledWith(
"Updated GitHub pages from [master] to [main] with path [/]"
);
});

it("configures actions with the new branch (custom path)", async () => {
nock("https://api.github.com/")
.get("/repos/demo/repo/pages")
.reply(200, {
source: {
branch: "master",
path: "/docs",
},
});

nock("https://api.github.com/")
.put("/repos/demo/repo/pages", {
source: { branch: "main", path: "/docs" },
})
.reply(200);

await githubPages({
owner: "demo",
repo: "repo",
old: "master",
target: "main",
octokit,
log,
});

expect(log.logger).toBeCalledWith(
"Updated GitHub pages from [master] to [main] with path [/docs]"
);
});
});