From ced41f4a495823205e5769e4531f923a982d29b4 Mon Sep 17 00:00:00 2001 From: joe fleming Date: Thu, 18 Oct 2018 16:31:19 -0700 Subject: [PATCH] feat: add retest comment, delete it --- src/index.mjs | 33 +++++++++++++++++++++++++-------- src/lib/comments.mjs | 7 +++++++ 2 files changed, 32 insertions(+), 8 deletions(-) create mode 100644 src/lib/comments.mjs diff --git a/src/index.mjs b/src/index.mjs index deca71e..f70b100 100644 --- a/src/index.mjs +++ b/src/index.mjs @@ -4,6 +4,7 @@ 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 { createComment, deleteComment } from './lib/comments.mjs'; import History from './lib/history.mjs'; // load env vars from .env file @@ -12,7 +13,13 @@ dotenv.config(); export default async function() { // 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 } = process.env; + const { + COMMENT_BODY_REGEXP, + COMMENT_BODY_REGEXP_FLAGS, + COMMENT_ACTOR, + PULL_LABEL_FILTER, + PULL_RETEST_BODY, + } = process.env; // load the history module const history = new History(); @@ -23,11 +30,14 @@ export default async function() { actor: COMMENT_ACTOR, })).filter(comment => !history.get(comment.id)); - // read pull data, filter out any closed pulls + // read pull data const pulls = (await Promise.all( comments.map(async comment => { const pull = await getPull(repo, comment.number); - if (pull.state !== 'open') return false; + + if (pull.state !== 'open') return false; // filter out any closed pulls + if (PULL_LABEL_FILTER && !pull.labels.includes(PULL_LABEL_FILTER)) return false; // filter on label + return { comment, pull }; }) )).filter(Boolean); @@ -43,8 +53,15 @@ export default async function() { }) )).filter(Boolean); - console.log(records); - process.exit(); + console.log(`Found ${records.length} outstanding failures`); + + await Promise.all( + records.map(async record => { + console.log(`Re-testing PR #${record.pull.number}`); + const comment = await createComment(repo, records[0].pull.number, PULL_RETEST_BODY); + await deleteComment(repo, comment.id); + }) + ); /* @@ -52,12 +69,12 @@ export default async function() { - [x] keep track of seen comment ids, only process new ones - [x] check the pr's status and only retest if no longer "Pending" - - [ ] add a retest comment + - [x] add a retest comment - POST /repos/:owner/:repo/issues/:number/comments + - [x] delete the retest comment - [ ] delete the build comment - - DELETE /repos/:owner/:repo/issues/comments/:comment_id - [ ] delete ALL build failure comments - - [ ] delete the retest comment + - DELETE /repos/:owner/:repo/issues/comments/:comment_id */ } diff --git a/src/lib/comments.mjs b/src/lib/comments.mjs new file mode 100644 index 0000000..f97010a --- /dev/null +++ b/src/lib/comments.mjs @@ -0,0 +1,7 @@ +export async function createComment(repo, issueId, body) { + return repo.issues(issueId).comments.create({ body }); +} + +export async function deleteComment(repo, commentId) { + return repo.issues.comments(commentId).remove(); +}