From 1863239c062de3a730adb6dad67208699056f630 Mon Sep 17 00:00:00 2001 From: joe fleming Date: Tue, 19 Feb 2019 16:39:45 -0700 Subject: [PATCH] feat: check for pending status after comment keep re-trying until state changes to pending, up to 3 times. if state does not change, remove from history so another run will retry --- src/index.mjs | 22 +++++++++++++++++++++- src/lib/exec_and_check.mjs | 6 ++++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/index.mjs b/src/index.mjs index 2658b49..5ca79c6 100644 --- a/src/index.mjs +++ b/src/index.mjs @@ -6,11 +6,14 @@ 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]); @@ -22,6 +25,7 @@ async function ghActionBot() { PULL_LABEL_FILTER, PULL_AUTHOR_FILTER, PULL_RETEST_BODY, + ACTION_RETRY_DELAY = 3000, } = process.env; // load the history module @@ -90,7 +94,23 @@ async function ghActionBot() { await Promise.all( records.map(record => - createPullComment(repo, record.pull.number, PULL_RETEST_BODY, { delete: true }) + 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}`); + } + ) ) ); } diff --git a/src/lib/exec_and_check.mjs b/src/lib/exec_and_check.mjs index b89f140..52ea67c 100644 --- a/src/lib/exec_and_check.mjs +++ b/src/lib/exec_and_check.mjs @@ -1,4 +1,4 @@ -export default async function execAndCheck(fn, test, onFailure = () => {}, opts = { tries: 3 }) { +export default async function execAndCheck(fn, test, onFailure = () => {}) { let count = 0; const tryExec = async () => { @@ -9,9 +9,11 @@ export default async function execAndCheck(fn, test, onFailure = () => {}, opts const res = await test(); if (res) return; + const { ACTION_RETRY_COUNT = 3 } = process.env; + // if retry limit is hit, call failure function count += 1; - if (count >= opts.tries) return onFailure(); // eslint-disable-line consistent-return + if (count >= ACTION_RETRY_COUNT) return onFailure(); // eslint-disable-line consistent-return // retry the function and retest the results return tryExec(); // eslint-disable-line consistent-return