Skip to content
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

set PRs and their dirty state as output #17

Merged
merged 6 commits into from
Jul 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
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ inputs:
description: "Number of seconds after which the action runs again if the mergable state is unknown."
retryMax:
description: "Number of times the action retries calculating the mergable state"
ouputs:
prDirtyStatuses:
description: "Object-map. The keys are pull request numbers and their values whether a PR is dirty or not."
runs:
using: "node12"
main: "dist/index.js"
Expand Down
26 changes: 18 additions & 8 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7586,6 +7586,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
Object.defineProperty(exports, "__esModule", { value: true });
const core = __importStar(__webpack_require__(470));
const github = __importStar(__webpack_require__(469));
const prDirtyStatusesOutputKey = `prDirtyStatuses`;
function main() {
return __awaiter(this, void 0, void 0, function* () {
const repoToken = core.getInput("repoToken", { required: true });
Expand All @@ -7596,7 +7597,7 @@ function main() {
const retryAfter = parseInt(core.getInput("retryAfter") || "120", 10);
const retryMax = parseInt(core.getInput("retryMax") || "5", 10);
const client = github.getOctokit(repoToken);
return yield checkDirty({
yield checkDirty({
client,
dirtyLabel,
removeOnDirtyLabel,
Expand All @@ -7611,7 +7612,7 @@ function checkDirty(context) {
const { after, client, dirtyLabel, removeOnDirtyLabel, retryAfter, retryMax, } = context;
if (retryMax <= 0) {
core.warning("reached maximum allowed retries");
return;
return {};
}
const query = `
query openPullRequests($owner: String!, $repo: String!, $after: String) {
Expand Down Expand Up @@ -7646,8 +7647,9 @@ query openPullRequests($owner: String!, $repo: String!, $after: String) {
const { repository: { pullRequests: { nodes: pullRequests, pageInfo }, }, } = pullsResponse;
core.debug(JSON.stringify(pullsResponse, null, 2));
if (pullRequests.length === 0) {
return;
return {};
}
let dirtyStatuses = {};
for (const pullRequest of pullRequests) {
core.debug(JSON.stringify(pullRequest, null, 2));
const info = (message) => core.info(`for PR "${pullRequest.title}": ${message}`);
Expand All @@ -7659,6 +7661,7 @@ query openPullRequests($owner: String!, $repo: String!, $after: String) {
addLabelIfNotExists(dirtyLabel, pullRequest, { client }),
removeLabelIfExists(removeOnDirtyLabel, pullRequest, { client }),
]);
dirtyStatuses[pullRequest.number] = true;
break;
case "MERGEABLE":
info(`remove "${dirtyLabel}"`);
Expand All @@ -7667,23 +7670,30 @@ query openPullRequests($owner: String!, $repo: String!, $after: String) {
// we don't add it again because we assume that the removeOnDirtyLabel
// is used to mark a PR as "merge!".
// So we basically require a manual review pass after rebase.
dirtyStatuses[pullRequest.number] = false;
break;
case "UNKNOWN":
info(`Retrying after ${retryAfter}s.`);
return new Promise((resolve) => {
setTimeout(() => __awaiter(this, void 0, void 0, function* () {
yield new Promise((resolve) => {
setTimeout(() => {
core.info(`retrying with ${retryMax} retries remaining.`);
resolve(yield checkDirty(Object.assign(Object.assign({}, context), { retryMax: retryMax - 1 })));
}), retryAfter * 1000);
resolve(() => __awaiter(this, void 0, void 0, function* () {
dirtyStatuses = Object.assign(Object.assign({}, dirtyStatuses), (yield checkDirty(Object.assign(Object.assign({}, context), { retryMax: retryMax - 1 }))));
}));
}, retryAfter * 1000);
});
break;
default:
throw new TypeError(`unhandled mergeable state '${pullRequest.mergeable}'`);
}
}
if (pageInfo.hasNextPage) {
return checkDirty(Object.assign(Object.assign({}, context), { after: pageInfo.endCursor }));
dirtyStatuses = Object.assign(Object.assign({}, dirtyStatuses), (yield checkDirty(Object.assign(Object.assign({}, context), { after: pageInfo.endCursor }))));
}
else {
core.setOutput(prDirtyStatusesOutputKey, dirtyStatuses);
}
return dirtyStatuses;
});
}
/**
Expand Down
40 changes: 28 additions & 12 deletions sources/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as core from "@actions/core";
import * as github from "@actions/github";

type GitHub = ReturnType<typeof github.getOctokit>;
const prDirtyStatusesOutputKey = `prDirtyStatuses`;

async function main() {
const repoToken = core.getInput("repoToken", { required: true });
Expand All @@ -14,7 +15,7 @@ async function main() {

const client = github.getOctokit(repoToken);

return await checkDirty({
await checkDirty({
client,
dirtyLabel,
removeOnDirtyLabel,
Expand All @@ -37,7 +38,9 @@ interface CheckDirtyContext {
// number of allowed retries
retryMax: number;
}
async function checkDirty(context: CheckDirtyContext): Promise<void> {
async function checkDirty(
context: CheckDirtyContext
): Promise<Record<number, boolean>> {
const {
after,
client,
Expand All @@ -49,7 +52,7 @@ async function checkDirty(context: CheckDirtyContext): Promise<void> {

if (retryMax <= 0) {
core.warning("reached maximum allowed retries");
return;
return {};
}

interface RepositoryResponse {
Expand Down Expand Up @@ -94,9 +97,9 @@ query openPullRequests($owner: String!, $repo: String!, $after: String) {
core.debug(JSON.stringify(pullsResponse, null, 2));

if (pullRequests.length === 0) {
return;
return {};
}

let dirtyStatuses: Record<number, boolean> = {};
for (const pullRequest of pullRequests) {
core.debug(JSON.stringify(pullRequest, null, 2));

Expand All @@ -111,6 +114,7 @@ query openPullRequests($owner: String!, $repo: String!, $after: String) {
addLabelIfNotExists(dirtyLabel, pullRequest, { client }),
removeLabelIfExists(removeOnDirtyLabel, pullRequest, { client }),
]);
dirtyStatuses[pullRequest.number] = true;
break;
case "MERGEABLE":
info(`remove "${dirtyLabel}"`);
Expand All @@ -119,13 +123,19 @@ query openPullRequests($owner: String!, $repo: String!, $after: String) {
// we don't add it again because we assume that the removeOnDirtyLabel
// is used to mark a PR as "merge!".
// So we basically require a manual review pass after rebase.
dirtyStatuses[pullRequest.number] = false;
break;
case "UNKNOWN":
info(`Retrying after ${retryAfter}s.`);
return new Promise((resolve) => {
setTimeout(async () => {
await new Promise((resolve) => {
setTimeout(() => {
core.info(`retrying with ${retryMax} retries remaining.`);
resolve(await checkDirty({ ...context, retryMax: retryMax - 1 }));
resolve(async () => {
dirtyStatuses = {
...dirtyStatuses,
...(await checkDirty({ ...context, retryMax: retryMax - 1 })),
};
});
}, retryAfter * 1000);
});
break;
Expand All @@ -137,11 +147,17 @@ query openPullRequests($owner: String!, $repo: String!, $after: String) {
}

if (pageInfo.hasNextPage) {
return checkDirty({
...context,
after: pageInfo.endCursor,
});
dirtyStatuses = {
...dirtyStatuses,
...(await checkDirty({
...context,
after: pageInfo.endCursor,
})),
};
} else {
core.setOutput(prDirtyStatusesOutputKey, dirtyStatuses);
}
return dirtyStatuses;
}

/**
Expand Down