From 4948e66838c9546740592928bc134d15203ef331 Mon Sep 17 00:00:00 2001 From: JP Driver Date: Tue, 6 Mar 2018 17:27:19 +0000 Subject: [PATCH 1/2] don't send duplicate comments to GitHub --- scripts/post-deploy.js | 43 ++++++++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/scripts/post-deploy.js b/scripts/post-deploy.js index 86e8a67..31d9171 100644 --- a/scripts/post-deploy.js +++ b/scripts/post-deploy.js @@ -20,21 +20,44 @@ module.exports = function postDeploy() { `; if (config.githubPullRequestId) { - const issueUrl = `https://${config.githubUsername}:${config.githubToken}@api.github.com/repos/${config.githubOrg}/${config.githubRepo}/issues/${config.githubPullRequestId}/comments`; + const issueUrl = `https://${config.githubUsername}:${config.githubToken}@api.github.com/repos/${ + config.githubOrg + }/${config.githubRepo}/issues/${config.githubPullRequestId}/comments`; + log('GitHub Issue URL', issueUrl); - request.post( + request.get( { url: issueUrl, - headers: { 'User-Agent': 'ci' }, - body: JSON.stringify({ body }) + headers: { 'User-Agent': 'ci' } }, - (error, response) => { - if (error) { - console.error('Failed to post comment to GitHub, an error occurred', error); - } else if (response.statusCode >= 400) { - console.error('Failed to post comment to GitHub, request failed with', response); + (getError, getResponse) => { + if (getError) { + console.error('Failed to check comments on GitHub, an error occurred', getError); + } else if (getResponse.statusCode >= 400) { + console.error('Failed to check comments on GitHub, request failed with', getResponse); } else { - console.log(`Posted message to GitHub PR #${config.githubPullRequestId}`); + const comments = JSON.parse(getResponse.body); + if (!comments.filter(comment => comment.body === body).length) { + request.post( + { + url: issueUrl, + headers: { 'User-Agent': 'ci' }, + body: JSON.stringify({ body }) + }, + (postError, postResponse) => { + if (postError) { + console.error('Failed to post comment to GitHub, an error occurred', postError); + } else if (postResponse.statusCode >= 400) { + console.error( + 'Failed to post comment to GitHub, request failed with', + postResponse + ); + } else { + console.log(`Posted message to GitHub PR #${config.githubPullRequestId}`); + } + } + ); + } } } ); From 260aef0f133bf37e6aea9f620c6304cc1fca5ce7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jani=20Ev=C3=A4kallio?= Date: Mon, 26 Mar 2018 11:54:23 +0100 Subject: [PATCH 2/2] Refactor for readability, prettier makes it uglier --- scripts/post-deploy.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/scripts/post-deploy.js b/scripts/post-deploy.js index 31d9171..af93378 100644 --- a/scripts/post-deploy.js +++ b/scripts/post-deploy.js @@ -2,6 +2,17 @@ const request = require('request'); const utils = require('./utils'); const config = require('./config'); const log = require('./log'); + +function getIssueUrl() { + const user = config.githubUsername; + const token = config.githubToken; + const org = config.githubOrg; + const repo = config.githubRepo; + const pr = config.githubPullRequestId; + + return `https://${user}:${token}@api.github.com/repos/${org}/${repo}/issues/${pr}/comments`; +} + module.exports = function postDeploy() { const expUrl = `https://expo.io/@${config.expUsername}/${utils.readPackageJSON().name}`; const expUrlForQRCode = `https://exp.host/@${config.expUsername}/${utils.readPackageJSON().name}`; @@ -20,11 +31,9 @@ module.exports = function postDeploy() { `; if (config.githubPullRequestId) { - const issueUrl = `https://${config.githubUsername}:${config.githubToken}@api.github.com/repos/${ - config.githubOrg - }/${config.githubRepo}/issues/${config.githubPullRequestId}/comments`; - + const issueUrl = getIssueUrl(); log('GitHub Issue URL', issueUrl); + request.get( { url: issueUrl,