124 lines
3.9 KiB
JavaScript
124 lines
3.9 KiB
JavaScript
import dotenv from 'dotenv';
|
|
import logger from './lib/logger.mjs';
|
|
import createRepo from './lib/create_repo.mjs';
|
|
import getComments from './lib/get_comments.mjs';
|
|
import getPull from './lib/get_pull.mjs';
|
|
import getCommits from './lib/get_commits.mjs';
|
|
import getCommitStatus from './lib/get_commit_status.mjs';
|
|
import createPullComment from './lib/create_pull_comment.mjs';
|
|
import execAndCheck from './lib/exec_and_check.mjs';
|
|
import History from './lib/history.mjs';
|
|
|
|
// load env vars from .env file
|
|
dotenv.config();
|
|
|
|
const sleep = async ms => new Promise(resolve => setTimeout(resolve, ms));
|
|
|
|
async function ghActionBot() {
|
|
// parse repo name from cli and create repo instance
|
|
const repo = createRepo(process.argv.splice(2)[0]);
|
|
|
|
const {
|
|
COMMENT_BODY_REGEXP,
|
|
COMMENT_BODY_REGEXP_FLAGS,
|
|
COMMENT_ACTOR,
|
|
PULL_LABEL_FILTER,
|
|
PULL_AUTHOR_FILTER,
|
|
PULL_RETEST_BODY,
|
|
ACTION_RETRY_DELAY = 3000,
|
|
} = process.env;
|
|
|
|
// load the history module
|
|
const history = new History();
|
|
|
|
// fetch comment info from event stream, filter for only new comments
|
|
const comments = (await getComments(repo, {
|
|
body: new RegExp(COMMENT_BODY_REGEXP, COMMENT_BODY_REGEXP_FLAGS),
|
|
actor: COMMENT_ACTOR,
|
|
})).filter(comment => !history.get(comment.id)); // skip comments already processed
|
|
|
|
if (comments.length) logger.debug(`Found ${comments.length} comments to process`);
|
|
|
|
// read pull data
|
|
const pulls = (await Promise.all(
|
|
comments.map(async comment => {
|
|
// keep track of comments that have been processed
|
|
history.add(comment);
|
|
|
|
logger.debug(`PROCESS COMMENT ON #${comment.number}`);
|
|
|
|
const pull = await getPull(repo, comment.number);
|
|
|
|
// filter out any closed pulls
|
|
if (pull.state !== 'open') {
|
|
logger.debug(`SKIP PULL #${pull.number}: state is ${pull.state}`);
|
|
return false;
|
|
}
|
|
|
|
// filter on owner
|
|
if (PULL_AUTHOR_FILTER && pull.owner !== PULL_AUTHOR_FILTER) {
|
|
logger.debug(`SKIP PULL #${pull.number}: author is ${pull.owner}`);
|
|
return false;
|
|
}
|
|
|
|
// filter on label
|
|
if (!PULL_LABEL_FILTER.split(',').every(label => pull.labels.includes(label))) {
|
|
logger.debug(
|
|
`SKIP PULL #${
|
|
pull.number
|
|
}: labels missing '${PULL_LABEL_FILTER}', found ${pull.labels.join(',')}`
|
|
);
|
|
return false;
|
|
}
|
|
|
|
return { comment, pull };
|
|
})
|
|
)).filter(Boolean);
|
|
|
|
const records = (await Promise.all(
|
|
pulls.map(async ({ pull, comment }) => {
|
|
const commit = await getCommits(repo, pull.number, true);
|
|
const buildStatus = await getCommitStatus(repo, commit.sha);
|
|
|
|
// do nothing if the build has not started, or is pending or successful
|
|
if (!buildStatus || buildStatus.state === 'pending' || buildStatus.state === 'success') {
|
|
logger.debug(`SKIP PULL #${pull.number}: build status is ${buildStatus.state}`);
|
|
return false;
|
|
}
|
|
|
|
return { comment, pull, commit, buildStatus };
|
|
})
|
|
)).filter(Boolean);
|
|
|
|
logger.log(`${records.length ? `Found ${records.length}` : `≧(´▽`)≦ No`} outstanding failures`);
|
|
|
|
await Promise.all(
|
|
records.map(record =>
|
|
execAndCheck(
|
|
() => createPullComment(repo, record.pull.number, PULL_RETEST_BODY, { delete: true }),
|
|
async () => {
|
|
logger.debug(`CHECK #${record.pull.number}: verify state change`);
|
|
|
|
// wait for the ci to restart
|
|
await sleep(ACTION_RETRY_DELAY);
|
|
|
|
// check that the commit status is now pending
|
|
const buildStatus = await getCommitStatus(repo, record.commit.sha);
|
|
return buildStatus.state === 'pending';
|
|
},
|
|
() => {
|
|
history.remove(record.comment);
|
|
logger.error(`Pull comment failed to trigger action on pull #${record.pull.number}`);
|
|
}
|
|
)
|
|
)
|
|
);
|
|
}
|
|
|
|
export default function() {
|
|
return ghActionBot().catch(err => {
|
|
logger.error(err);
|
|
return process.exit(1);
|
|
});
|
|
}
|