Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for multiple amplify URIs in monorepos #64

Merged
merged 4 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions lib/actions/amplify.js
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Loading