From fc3e1f5330b4c746f477811603b91bc1157adff2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christopher=20All=C3=A8ne?= Date: Wed, 2 Aug 2023 17:15:43 +0000 Subject: [PATCH 1/4] Add support for multiple amplify URIs in monorepos --- lib/actions/amplify.js | 29 +++++++++++++++++------------ lib/actions/pullRequest.js | 38 ++++++++++++++++++++++++-------------- 2 files changed, 41 insertions(+), 26 deletions(-) diff --git a/lib/actions/amplify.js b/lib/actions/amplify.js index d977be77..92df81e6 100644 --- a/lib/actions/amplify.js +++ b/lib/actions/amplify.js @@ -1,20 +1,25 @@ const core = require("@actions/core"); const github = require("@actions/github"); -exports.getAmplifyURI = async function getAmplifyURI() { +exports.getAmplifyURIs = async function getAmplifyURI() { const pullRequest = github.context.payload.pull_request; - const amplifyUri = core.getInput("amplify-uri"); - if (!amplifyUri) { + const labels = pullRequest.labels.map((label) => label.name); + const amplifyUriRaw = core.getInput("amplify-uri"); + if (!amplifyUriRaw) { return; } - return amplifyUri.replace("%", pullRequest.number); -}; - -exports.getStorybookAmplifyUri = async function getStorybookAmplifyUri() { - const pullRequest = github.context.payload.pull_request; - const storybookAmplifyUri = core.getInput("storybook-amplify-uri"); - if (!storybookAmplifyUri) { - return; + const amplifyUri = amplifyUriRaw.replace(/%/g, pullRequest.number); + if (amplifyUri.match(/^{/)) { + const result = []; + for (const label of labels) { + if (amplifyUri[label]) { + result.push(amplifyUri[label]); + } + } + return result; + } else if (!amplifyUri) { + return null; + } else { + return [amplifyUri]; } - return storybookAmplifyUri.replace("%", pullRequest.number); }; diff --git a/lib/actions/pullRequest.js b/lib/actions/pullRequest.js index b4f972e6..c88076d5 100644 --- a/lib/actions/pullRequest.js +++ b/lib/actions/pullRequest.js @@ -3,7 +3,7 @@ const { isBranchNameValid } = require("../branch"); const { isPullRequestTitleValid: isPullRequestTitleValid, } = require("../pullRequest"); -const { getAmplifyURI, getStorybookAmplifyUri } = require("./amplify"); +const { getAmplifyURIs, getStorybookAmplifyUri } = require("./amplify"); exports.validatePR = async function validatePR({ pullRequest }) { const { @@ -119,34 +119,44 @@ exports.validatePR = async function validatePR({ pullRequest }) { // do we have an AWS Amplify URI? If so, make sure that at least one comment // exists with a link to it - const amplifyUri = await getAmplifyURI(); - if (!amplifyUri) { + const amplifyUris = await getAmplifyURIs(); + if (!amplifyUris) { console.log("No AWS Amplify URI for this repository"); } else { - const storybookAmplifyUri = await getStorybookAmplifyUri(); - console.log(`AWS Amplify URI: ${amplifyUri}`); + console.log("AWS Amplify URIs: ", amplifyUris); const comments = await octokit.paginate( "GET /repos/{owner}/{repo}/issues/{issue_number}/comments", { owner, repo, issue_number: pullNumber } ); // add a comment with the PR - if (comments.some(({ body }) => body.match(amplifyUri))) { - console.log("A comment already exists with a link to AWS Amplify"); + const body = "AWS Amplify live test URI:\n" + amplifyUris.join("\n"); + const previousComments = comments.filter(({ body }) => + body.match(/AWS Amplify live/) + ); + if (previousComments.length > 0) { + console.log( + "A comment already exists with a link to AWS Amplify, editing it" + ); + const firstComment = previousComments[0]; + await octokit.request( + "PATCH /repos/{owner}/{repo}/issues/comments/{comment_id}", + { + owner, + repo, + comment_id: firstComment.id, + body, + } + ); } else { - console.log("Comment with link to Amplify URI missing"); - console.log("Adding comment with AWS Amplify URI"); + console.log("Comment with link to Amplify URI missing, creating it"); await octokit.request( "POST /repos/{owner}/{repo}/issues/{issue_number}/comments", { owner, repo, issue_number: pullNumber, - body: `AWS Amplify live test URI: [${amplifyUri}](${amplifyUri})${ - !storybookAmplifyUri - ? "" - : `\n\nAWS Amplify Storybook URI: [${storybookAmplifyUri}](${storybookAmplifyUri})` - }`, + body, } ); } From 46122e5353c979f2f093e9a83da8be759fcbffd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christopher=20All=C3=A8ne?= Date: Wed, 2 Aug 2023 17:17:38 +0000 Subject: [PATCH 2/4] Remove storybook --- lib/actions/pullRequest.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/actions/pullRequest.js b/lib/actions/pullRequest.js index c88076d5..f1501262 100644 --- a/lib/actions/pullRequest.js +++ b/lib/actions/pullRequest.js @@ -3,7 +3,7 @@ const { isBranchNameValid } = require("../branch"); const { isPullRequestTitleValid: isPullRequestTitleValid, } = require("../pullRequest"); -const { getAmplifyURIs, getStorybookAmplifyUri } = require("./amplify"); +const { getAmplifyURIs } = require("./amplify"); exports.validatePR = async function validatePR({ pullRequest }) { const { From d92343be942855668e8efa8ee9d6a8988fcc5335 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christopher=20All=C3=A8ne?= Date: Wed, 2 Aug 2023 17:24:07 +0000 Subject: [PATCH 3/4] Fix multiple amplify --- lib/actions/amplify.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/actions/amplify.js b/lib/actions/amplify.js index 92df81e6..af77e3e1 100644 --- a/lib/actions/amplify.js +++ b/lib/actions/amplify.js @@ -11,9 +11,10 @@ exports.getAmplifyURIs = async function getAmplifyURI() { const amplifyUri = amplifyUriRaw.replace(/%/g, pullRequest.number); if (amplifyUri.match(/^{/)) { const result = []; + const amplifyUriMap = JSON.parse(amplifyUri); for (const label of labels) { - if (amplifyUri[label]) { - result.push(amplifyUri[label]); + if (amplifyUriMap[label]) { + result.push(amplifyUriMap[label]); } } return result; From ee49b5682a77a7f9bca57494c7256100fe7900d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christopher=20All=C3=A8ne?= Date: Wed, 2 Aug 2023 17:25:50 +0000 Subject: [PATCH 4/4] Improve naming --- lib/actions/amplify.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/actions/amplify.js b/lib/actions/amplify.js index af77e3e1..877410ff 100644 --- a/lib/actions/amplify.js +++ b/lib/actions/amplify.js @@ -11,10 +11,10 @@ exports.getAmplifyURIs = async function getAmplifyURI() { const amplifyUri = amplifyUriRaw.replace(/%/g, pullRequest.number); if (amplifyUri.match(/^{/)) { const result = []; - const amplifyUriMap = JSON.parse(amplifyUri); + const amplifyUris = JSON.parse(amplifyUri); for (const label of labels) { - if (amplifyUriMap[label]) { - result.push(amplifyUriMap[label]); + if (amplifyUris[label]) { + result.push(amplifyUris[label]); } } return result;