diff --git a/action.js b/action.js index b0fb1662..69f0f19c 100644 --- a/action.js +++ b/action.js @@ -5,22 +5,27 @@ const { onPRMerged } = require("./lib/actions/pullRequestMerged"); const { generateChangelog } = require("./lib/actions/changelog"); exports.getActionParameters = function getActionParameters() { + const comment = github.context.payload.comment; + const issue = github.context.payload.issue; const repository = github.context.payload.repository; const ref = github.context.ref; const pullRequest = github.context.payload.pull_request; const mergeGroup = github.context.payload.merge_group; const action = core.getInput("action", { required: true }); - return { repository, ref, pullRequest, action, mergeGroup }; + return { repository, ref, pullRequest, action, mergeGroup, issue, comment }; }; exports.action = async function action() { + const parameters = exports.getActionParameters(); + console.log("Actiona prameters", JSON.stringify(parameters)); const { repository, ref, pullRequest, action, mergeGroup, - } = exports.getActionParameters(); + issue, + } = parameters; console.info(`Calling action ${action}`); switch (action) { @@ -29,7 +34,7 @@ exports.action = async function action() { console.log("Ignoring PR validation when running in merge group"); return; } - await validatePR({ pullRequest }); + await validatePR({ pullRequest, issue }); break; case "after-pr-merged": await onPRMerged({ pullRequest }); diff --git a/lib/actions/amplify.js b/lib/actions/amplify.js index 877410ff..6b26e0f3 100644 --- a/lib/actions/amplify.js +++ b/lib/actions/amplify.js @@ -3,12 +3,17 @@ const github = require("@actions/github"); exports.getAmplifyURIs = async function getAmplifyURI() { const pullRequest = github.context.payload.pull_request; - const labels = pullRequest.labels.map((label) => label.name); + const issue = github.context.payload.issue; + const pullNumber = pullRequest?.number || issue?.number; + const labels = + pullRequest?.labels.map((label) => label.name) || + issue?.labels.map((label) => label.name); const amplifyUriRaw = core.getInput("amplify-uri"); if (!amplifyUriRaw) { + console.log("No input for amplify-uri"); return; } - const amplifyUri = amplifyUriRaw.replace(/%/g, pullRequest.number); + const amplifyUri = amplifyUriRaw.replace(/%/g, pullNumber); if (amplifyUri.match(/^{/)) { const result = []; const amplifyUris = JSON.parse(amplifyUri); diff --git a/lib/actions/pullRequest.js b/lib/actions/pullRequest.js index f1501262..1a6a3931 100644 --- a/lib/actions/pullRequest.js +++ b/lib/actions/pullRequest.js @@ -5,116 +5,118 @@ const { } = require("../pullRequest"); const { getAmplifyURIs } = require("./amplify"); -exports.validatePR = async function validatePR({ pullRequest }) { - const { - number: pullNumber, - commits, - base: { - ref: baseRef, - repo: { - owner: { login: owner }, - name: repo, - }, - user: { login: committer }, - }, - head: { ref: headRef, sha: headSha }, - title, - } = pullRequest; +exports.validatePR = async function validatePR({ pullRequest, issue }) { + const octokit = getOctokit(); - if ( - ["main", "master", "preprod", "prod"].indexOf(baseRef) === -1 && - headRef.slice(0, baseRef.length) !== baseRef && - !headRef.slice(baseRef.length).match(/^--/) && - !baseRef.match(/^hotfix\//) - ) { - throw new Error( - `As this pull request is based on “${baseRef}”, its name should start with “${baseRef}--”. See https://www.notion.so/mobsuccess/Git-Guidelines-41996ef576cb4f29b7737772b74289c5#f9cadc10d949498dbe38c0eed08fd4f8` - ); - } + if (pullRequest) { + const { + number: pullNumber, + commits, + base: { + ref: baseRef, + repo: { + owner: { login: owner }, + name: repo, + }, + user: { login: committer }, + }, + head: { ref: headRef, sha: headSha }, + title, + } = pullRequest; - if (!isBranchNameValid(headRef)) { - throw new Error( - `This pull request is based on a branch with in invalid name: “${headRef}”. See https://www.notion.so/mobsuccess/Git-Guidelines-41996ef576cb4f29b7737772b74289c5#f9cadc10d949498dbe38c0eed08fd4f8` - ); - } + if ( + ["main", "master", "preprod", "prod"].indexOf(baseRef) === -1 && + headRef.slice(0, baseRef.length) !== baseRef && + !headRef.slice(baseRef.length).match(/^--/) && + !baseRef.match(/^hotfix\//) + ) { + throw new Error( + `As this pull request is based on “${baseRef}”, its name should start with “${baseRef}--”. See https://www.notion.so/mobsuccess/Git-Guidelines-41996ef576cb4f29b7737772b74289c5#f9cadc10d949498dbe38c0eed08fd4f8` + ); + } - if (!isPullRequestTitleValid(title)) { - throw new Error( - `The title of this pull request is invalid, please edit: “${title}”. See https://www.notion.so/mobsuccess/Git-Guidelines-41996ef576cb4f29b7737772b74289c5#4ac148fd42a04141a528a87013ea5c57` - ); - } + if (!isBranchNameValid(headRef)) { + throw new Error( + `This pull request is based on a branch with in invalid name: “${headRef}”. See https://www.notion.so/mobsuccess/Git-Guidelines-41996ef576cb4f29b7737772b74289c5#f9cadc10d949498dbe38c0eed08fd4f8` + ); + } - const octokit = getOctokit(); + if (!isPullRequestTitleValid(title)) { + throw new Error( + `The title of this pull request is invalid, please edit: “${title}”. See https://www.notion.so/mobsuccess/Git-Guidelines-41996ef576cb4f29b7737772b74289c5#4ac148fd42a04141a528a87013ea5c57` + ); + } - const branches = await octokit.paginate( - "GET /repos/{owner}/{repo}/branches", - { owner, repo } - ); - let oneBranchIsOk = false; - let nokBranch; - for (const { name: branchName } of branches) { - if (headRef === branchName) { - continue; + const branches = await octokit.paginate( + "GET /repos/{owner}/{repo}/branches", + { owner, repo } + ); + let oneBranchIsOk = false; + let nokBranch; + for (const { name: branchName } of branches) { + if (headRef === branchName) { + continue; + } + const prefix = headRef.substr(0, branchName.length); + const postfix = headRef.substr(branchName.length); + if (prefix === branchName && prefix.length === branchName.length) { + if (postfix.match(/^--[a-z]/)) { + oneBranchIsOk = true; + console.log(`Found OK branch: “${branchName}” matches “${headRef}”`); + } else { + nokBranch = branchName; + console.log( + `Found NOK branch: “${branchName}” does not match “${headRef}”` + ); + } + } } - const prefix = headRef.substr(0, branchName.length); - const postfix = headRef.substr(branchName.length); - if (prefix === branchName && prefix.length === branchName.length) { - if (postfix.match(/^--[a-z]/)) { - oneBranchIsOk = true; - console.log(`Found OK branch: “${branchName}” matches “${headRef}”`); + if (!oneBranchIsOk && nokBranch) { + throw new Error( + `This pull request is based on the branch “${headRef}”, which starts like “${nokBranch}”. Use double dashes (“--”) to separate sub-branches. See https://app.gitbook.com/@mobsuccess/s/mobsuccess/git` + ); + } + + if ( + commits === 1 && + !title.match(/^Revert ".*"/) && + !title.match(/^Bump .*/) && + !committer === "ms-bot" && + !committer === "ms-upgrade-aws" + ) { + // we have only one commit, make sure its name match the name of the PR + const { + data: { message: commitMessage }, + } = await octokit.rest.git.getCommit({ + owner, + repo, + commit_sha: headSha, + }); + if (commitMessage !== title) { + throw new Error( + `This pull request has only one commit, and its message doesn't match the title of the PR. If you'd like to keep the title of the PR as it is, please add an empty commit.` + ); } else { - nokBranch = branchName; console.log( - `Found NOK branch: “${branchName}” does not match “${headRef}”` + "This pull request has only one commit, and its message matches the title of the PR." ); } } - } - if (!oneBranchIsOk && nokBranch) { - throw new Error( - `This pull request is based on the branch “${headRef}”, which starts like “${nokBranch}”. Use double dashes (“--”) to separate sub-branches. See https://app.gitbook.com/@mobsuccess/s/mobsuccess/git` - ); - } - - if ( - commits === 1 && - !title.match(/^Revert ".*"/) && - !title.match(/^Bump .*/) && - !committer === "ms-bot" && - !committer === "ms-upgrade-aws" - ) { - // we have only one commit, make sure its name match the name of the PR - const { - data: { message: commitMessage }, - } = await octokit.rest.git.getCommit({ - owner, - repo, - commit_sha: headSha, - }); - if (commitMessage !== title) { - throw new Error( - `This pull request has only one commit, and its message doesn't match the title of the PR. If you'd like to keep the title of the PR as it is, please add an empty commit.` - ); - } else { - console.log( - "This pull request has only one commit, and its message matches the title of the PR." - ); - } - } - // everything seems valid: add the label if it exists - const branchTag = headRef.split("/")[0]; + // everything seems valid: add the label if it exists + const branchTag = headRef.split("/")[0]; - try { - await octokit.issues.addLabels({ - owner, - repo, - issue_number: pullNumber, - labels: [branchTag], - }); - } catch (e) { - // ignore error - console.log(`Could not apply label: ${e}`); + try { + await octokit.issues.addLabels({ + owner, + repo, + issue_number: pullNumber, + labels: [branchTag], + }); + } catch (e) { + // ignore error + console.log(`Could not apply label: ${e}`); + } } // do we have an AWS Amplify URI? If so, make sure that at least one comment @@ -124,6 +126,12 @@ exports.validatePR = async function validatePR({ pullRequest }) { console.log("No AWS Amplify URI for this repository"); } else { console.log("AWS Amplify URIs: ", amplifyUris); + const pullNumber = pullRequest?.number || issue?.number; + const owner = + pullRequest?.base.repo.owner.login || + issue?.repository_url?.split("/")[4]; + const repo = + pullRequest?.base.repo.name || issue?.repository_url?.split("/")[5]; const comments = await octokit.paginate( "GET /repos/{owner}/{repo}/issues/{issue_number}/comments", { owner, repo, issue_number: pullNumber } diff --git a/sample/workflows/mobsuccess.yml b/sample/workflows/mobsuccess.yml index a8ad03ab..4380a30e 100644 --- a/sample/workflows/mobsuccess.yml +++ b/sample/workflows/mobsuccess.yml @@ -5,6 +5,11 @@ on: types: - checks_requested + issue_comment: + types: + - created + - edited + pull_request: types: - opened