diff --git a/.github/workflows/triage.yaml b/.github/workflows/triage.yaml index 0cbdbbc..b5f26ba 100644 --- a/.github/workflows/triage.yaml +++ b/.github/workflows/triage.yaml @@ -15,37 +15,6 @@ jobs: steps: - name: "Add labels based on branch name" - uses: "actions/github-script@v5" + uses: "./.github/actions/github/pull-request/add-label-based-on-branch-name" with: github-token: "${{ secrets.ERGEBNIS_BOT_TOKEN }}" - script: | - const branchPrefixLabels = { - feature: "enhancement", - fix: "bug", - }; - - const pullRequest = context.payload.pull_request; - const repository = context.repo; - - const branchName = pullRequest.head.ref; - - const matches = branchName.match(new RegExp('^([^/]+)\/'));; - - if (!matches instanceof Array) { - return; - } - - if (!branchPrefixLabels.hasOwnProperty(matches[1])) { - return; - } - - const label = branchPrefixLabels[matches[1]]; - - github.rest.issues.addLabels({ - issue_number: pullRequest.number, - labels: [ - label, - ], - owner: repository.owner, - repo: repository.repo, - }); diff --git a/CHANGELOG.md b/CHANGELOG.md index 69c15c8..962a497 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ For a full diff see [`1.2.1...main`][1.2.1...main]. - Added composite action `github/add-assignee-to-pull-request` ([#59]), by [@localheinz] - Added composite action `github/approve-pull-request` ([#60]), by [@localheinz] - Added composite action `github/merge-pull-request` ([#64]), by [@localheinz] +- Added composite action `github/pull-request/add-label-based-on-branch-name` ([#67]), by [@localheinz] ## [`1.2.1`][1.2.1] @@ -64,5 +65,6 @@ For a full diff see [`1.0.0...main`][1.0.0...main]. [#59]: https://github.com/ergebnis/.github/pull/59 [#60]: https://github.com/ergebnis/.github/pull/60 [#64]: https://github.com/ergebnis/.github/pull/64 +[#67]: https://github.com/ergebnis/.github/pull/67 [@localheinz]: https://github.com/localheinz diff --git a/actions/github/pull-request/add-label-based-on-branch-name/action.yaml b/actions/github/pull-request/add-label-based-on-branch-name/action.yaml new file mode 100644 index 0000000..09feac7 --- /dev/null +++ b/actions/github/pull-request/add-label-based-on-branch-name/action.yaml @@ -0,0 +1,79 @@ +# https://docs.github.com/en/actions/creating-actions/creating-a-composite-action +# https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#inputs +# https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#runs-for-composite-run-steps-actions +# https://docs.github.com/en/rest/reference/issues#add-labels-to-an-issue +# https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#pull_request +# https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#workflow_run + +name: "Add label to pull request based on branch name" + +description: "Adds a label to a pull request based on the branch name" + +inputs: + github-token: + description: "GitHub token of a user with permission to add labels to to a pull request" + required: true + +runs: + using: "composite" + + steps: + - name: "Determine pull request number" + uses: "actions/github-script@v5" + with: + github-token: "${{ inputs.github-token }}" + script: | + if ( + context.eventName == 'pull_request' || + context.eventName == 'pull_request_target' + ) { + core.exportVariable("PULL_REQUEST_NUMBER", context.payload.pull_request.number); + core.exportVariable("PULL_REQUEST_BRANCH_NAME", context.payload.pull_request.head.ref); + + return; + } + + core.setFailed(`Unable to determine the pull request number and branch name for event "${context.eventName}"`); + + - name: "Add label to pull request based on branch name" + uses: "actions/github-script@v5" + with: + github-token: "${{ inputs.github-token }}" + script: | + if (!process.env.PULL_REQUEST_NUMBER) { + core.setFailed("The environment variable PULL_REQUEST_NUMBER is not defined."); + + return; + } + + if (!process.env.PULL_REQUEST_BRANCH_NAME) { + core.setFailed("The environment variable PULL_REQUEST_BRANCH_NAME is not defined."); + + return; + } + + const branchPrefixToLabel = { + feature: "enhancement", + fix: "bug", + }; + + const matches = process.env.PULL_REQUEST_BRANCH_NAME.match(new RegExp('^([^/]+)\/')); + + if (!matches instanceof Array) { + return; + } + + if (!branchPrefixToLabel.hasOwnProperty(matches[1])) { + return; + } + + const label = branchPrefixToLabel[matches[1]]; + + github.rest.issues.addLabels({ + issue_number: process.env.PULL_REQUEST_NUMBER, + labels: [ + label, + ], + owner: context.repo.owner, + repo: context.repo.repo, + });