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 2 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: "Array of booleans which indicate whether a PR is dirty or not and where the index is the PR ID."
baywet marked this conversation as resolved.
Show resolved Hide resolved
runs:
using: "node12"
main: "dist/index.js"
Expand Down
24 changes: 16 additions & 8 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,7 @@ interface CheckDirtyContext {
// number of allowed retries
retryMax: number;
}
async function checkDirty(context: CheckDirtyContext): Promise<void> {
async function checkDirty(context: CheckDirtyContext): Promise<any> {
baywet marked this conversation as resolved.
Show resolved Hide resolved
const {
after,
client,
Expand Down Expand Up @@ -96,7 +97,7 @@ query openPullRequests($owner: String!, $repo: String!, $after: String) {
if (pullRequests.length === 0) {
return;
}

let dirtyStatuses: any = {};
baywet marked this conversation as resolved.
Show resolved Hide resolved
for (const pullRequest of pullRequests) {
core.debug(JSON.stringify(pullRequest, null, 2));

Expand All @@ -111,6 +112,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 +121,16 @@ 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 +142,14 @@ query openPullRequests($owner: String!, $repo: String!, $after: String) {
}

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

/**
Expand Down