Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 1 addition & 32 deletions .github/workflows/triage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -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,
});