diff --git a/scripts/post-deploy.js b/scripts/post-deploy.js index 86e8a67..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,21 +31,42 @@ 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.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}`); + } + } + ); + } } } );