Skip to content

Commit

Permalink
Add support for multiple amplify URIs in monorepos (#64)
Browse files Browse the repository at this point in the history
* Add support for multiple amplify URIs in monorepos

* Remove storybook

* Fix multiple amplify

* Improve naming
  • Loading branch information
callms committed Aug 3, 2023
1 parent 9a08a0d commit 106d673
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 26 deletions.
30 changes: 18 additions & 12 deletions lib/actions/amplify.js
@@ -1,20 +1,26 @@
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 = [];
const amplifyUris = JSON.parse(amplifyUri);
for (const label of labels) {
if (amplifyUris[label]) {
result.push(amplifyUris[label]);
}
}
return result;
} else if (!amplifyUri) {
return null;
} else {
return [amplifyUri];
}
return storybookAmplifyUri.replace("%", pullRequest.number);
};
38 changes: 24 additions & 14 deletions lib/actions/pullRequest.js
Expand Up @@ -3,7 +3,7 @@ const { isBranchNameValid } = require("../branch");
const {
isPullRequestTitleValid: isPullRequestTitleValid,
} = require("../pullRequest");
const { getAmplifyURI, getStorybookAmplifyUri } = require("./amplify");
const { getAmplifyURIs } = require("./amplify");

exports.validatePR = async function validatePR({ pullRequest }) {
const {
Expand Down Expand Up @@ -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,
}
);
}
Expand Down

0 comments on commit 106d673

Please sign in to comment.