Skip to content

Commit

Permalink
hacking
Browse files Browse the repository at this point in the history
  • Loading branch information
grmoon committed Oct 30, 2020
1 parent 5eb5cb0 commit 9e905cf
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 3 deletions.
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

60 changes: 58 additions & 2 deletions src/autoupdater.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,55 @@
const core = require("@actions/core");

async function getStatusChecks(pr, repo, octokit) {
core.info(`Getting require status checks for ${pr.head.ref}`);

const response = await octokit.repos.getStatusChecksProtection({
...repo,
branch: pr.head.ref,
});
const statusChecks = response.data;

core.info(
`Required Status checks for ${pr.head.ref}: ${statusChecks.contexts.join(
", "
)}`
);

return statusChecks;
}

async function waitForStatusChecks(pr, repo, octokit) {
const requiredChecks = getStatusChecks(pr, repo, octokit);

let completed;
let requiredCheckRuns;
let count = 0;

do {
if (count > 0) {
core.info("Waiting for checks to pass...");
await new Promise((resolve) => setTimeout(resolve, 5000));
}

const response = await octokit.checks.listForRef({
...repo,
ref: pr.head.ref,
});

const checks = response.data;

requiredCheckRuns = checks.check_runs.filter((run) =>
requiredChecks.contexts.includes(run.name)
);

completed = requiredCheckRuns.every(
(run) => run.status == "completed" && run.conclusion == "success"
);

count++;
} while (!completed);
}

async function performUpdate({ pullRequest, repo, octokit }) {
core.startGroup(`Updating PR: Updating ${pullRequest.head.ref}`);

Expand All @@ -9,14 +59,20 @@ async function performUpdate({ pullRequest, repo, octokit }) {
context: `autoupdate from ${pullRequest.base.ref}`,
};

core.debug("Sending pending status");

await octokit.repos.createCommitStatus({
...commitStatusParams,
state: "pending",
description: "Updating",
});

const timeout = setTimeout(() => {
throw Error("Timed out waiting for status checks.");
}, 30000);

await waitForStatusChecks(pullRequest, repo, octokit);

clearTimeout(timeout);

let error;

try {
Expand Down
20 changes: 20 additions & 0 deletions test/autoupdater.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,27 @@ async function expectError(callback, expected) {
}

function createOctokitMock({ prs = [], repos, pulls }) {
const checksMock = {
listForRef: jest.fn().mockReturnValue(
new Promise((resolve) =>
resolve({
data: {
check_runs: [],
},
})
)
),
};

const reposMock = {
createCommitStatus: jest.fn(),
getStatusChecksProtection: jest.fn().mockReturnValue(
new Promise((resolve) =>
resolve({
data: { contexts: [] },
})
)
),
...repos,
};

Expand All @@ -31,6 +50,7 @@ function createOctokitMock({ prs = [], repos, pulls }) {
return {
repos: reposMock,
pulls: pullsMock,
checks: checksMock,
};
}

Expand Down

0 comments on commit 9e905cf

Please sign in to comment.