Skip to content

Commit

Permalink
Add debug on top of 170ba16
Browse files Browse the repository at this point in the history
  • Loading branch information
callms committed Aug 3, 2023
1 parent 170ba16 commit 1756353
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 102 deletions.
11 changes: 8 additions & 3 deletions action.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 });
Expand Down
9 changes: 7 additions & 2 deletions lib/actions/amplify.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
202 changes: 105 additions & 97 deletions lib/actions/pullRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 }
Expand Down
5 changes: 5 additions & 0 deletions sample/workflows/mobsuccess.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ on:
types:
- checks_requested

issue_comment:
types:
- created
- edited

pull_request:
types:
- opened
Expand Down

0 comments on commit 1756353

Please sign in to comment.