diff --git a/.github/actions/assignPullRequestAuthor.js b/.github/actions/assignPullRequestAuthor.js new file mode 100644 index 00000000000..23b495fa82e --- /dev/null +++ b/.github/actions/assignPullRequestAuthor.js @@ -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, + ); + } +}; diff --git a/.github/actions/labelPullRequestWithCommitType.js b/.github/actions/labelPullRequestWithCommitType.js new file mode 100644 index 00000000000..5ff21137c27 --- /dev/null +++ b/.github/actions/labelPullRequestWithCommitType.js @@ -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"; + } +} + diff --git a/.github/workflows/pr-bot.yml b/.github/workflows/pr-bot.yml index e82d854af8b..540ddeb831f 100644 --- a/.github/workflows/pr-bot.yml +++ b/.github/workflows/pr-bot.yml @@ -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})