Skip to content

Commit

Permalink
ci(pr-bot): add write permissions for pull requests (#7517)
Browse files Browse the repository at this point in the history
**Related Issue:** #7510

## Summary

Adds pull request write permission to the pr-bot so it doesn't fail for
people who don't have write permissions

---------

Co-authored-by: Calcite Admin <calcite-admin@esri.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: JC Franco <jfranco@esri.com>
Co-authored-by: Matt Driscoll <mdriscoll@esri.com>
  • Loading branch information
6 people authored Aug 17, 2023
1 parent 7e40a34 commit 07baeef
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 58 deletions.
26 changes: 26 additions & 0 deletions .github/actions/assignPullRequestAuthor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module.exports = async ({ github, context }) => {
const {
assignees,
number,
user: { login: author },
} = context.payload.pull_request;

const updatedAssignees =
assignees && assignees.length
? [...assignees.map((a) => a.login).filter((a) => a !== author), author]
: [author];

try {
await github.rest.issues.addAssignees({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: number,
assignees: updatedAssignees,
});
} catch (e) {
console.error(
"Unable to assign the PR author, they likely do not have write permissions\n",
e,
);
}
};
52 changes: 52 additions & 0 deletions .github/actions/labelPullRequestWithCommitType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
module.exports = async ({ github, context }) => {
const { title, number } = context.payload.pull_request;

const conventionalCommitRegex =
/^(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(\([\w ,-]+\))?(!?:\s+)([\w ]+[\s\S]*)/i;

if (!title) {
console.log("No title found, ending run.");
return;
}

const match = title.match(conventionalCommitRegex);

if (!match || match.length < 2) {
console.log("Title does not match conventional commit regex, ending run.");
return;
}

// commit type is in the first match group
const typeLabel = getLabelName(match[1]);

try {
await github.rest.issues.addLabels({
issue_number: number,
owner: context.repo.owner,
repo: context.repo.repo,
labels: [typeLabel],
});
} catch (e) {
console.error("Unable to label pull request, the author likely does not have write permissions\n", e);
}
};

function getLabelName(type) {
switch (type) {
case "feat":
return "enhancement";
case "fix":
return "bug";
case "docs":
return "docs";
case "test":
return "testing";
case "refactor":
return "refactor";
case "tooling":
return "tooling";
default:
return "chore";
}
}

67 changes: 9 additions & 58 deletions .github/workflows/pr-bot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,76 +2,27 @@ name: PR Bot
on:
pull_request:
branches: [main]
permissions:
pull-requests: write
issues: write
jobs:
assign-author:
if: github.event.pull_request.user.login != 'dependabot[bot]' && github.event.pull_request.user.login != 'renovate[bot]'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/github-script@v6
with:
script: |
const {
assignees,
number,
user: { login: author },
} = context.payload.pull_request;
const updatedAssignees =
assignees && assignees.length
? [...assignees.map((a) => a.login).filter(a => a !== author), author]
: [author];
await github.rest.issues.addAssignees({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: number,
assignees: updatedAssignees,
});
const action = require('${{ github.workspace }}/.github/actions/assignPullRequestAuthor.js')
await action({github, context})
label-type:
if: github.event.pull_request.user.login != 'dependabot[bot]' && github.event.pull_request.user.login != 'renovate[bot]'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/github-script@v6
with:
script: |
const { title, number } = context.payload.pull_request;
const conventionalCommitRegex =
/^(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(\([\w ,-]+\))?(!?:\s+)([\w ]+[\s\S]*)/i;
if (!title) {
console.log("No title found, ending run.");
return;
}
const match = title.match(conventionalCommitRegex);
if (match && match.length > 1) {
// commit type is in the first match group
const typeLabel = getLabelName(match[1]);
await github.rest.issues.addLabels({
issue_number: number,
owner: context.repo.owner,
repo: context.repo.repo,
labels: [typeLabel],
});
function getLabelName(type) {
switch (type) {
case "feat":
return "enhancement";
case "fix":
return "bug";
case "docs":
return "docs";
case "test":
return "testing";
case "refactor":
return "refactor";
case "tooling":
return "tooling";
default:
return "chore";
}
}
}
const action = require('${{ github.workspace }}/.github/actions/labelPullRequestWithCommitType.js')
await action({github, context})

0 comments on commit 07baeef

Please sign in to comment.