diff --git a/.github/workflows/add_pr_label.yml b/.github/workflows/add_pr_label.yml new file mode 100644 index 0000000000..57f10f3070 --- /dev/null +++ b/.github/workflows/add_pr_label.yml @@ -0,0 +1,54 @@ +name: Add Label to the Pull Request + +on: + pull_request: + types: + - opened + branches: + - 'main' + - 'feature/**' + - 'chore/**' + - 'fix/**' + - 'hotfix/**' + +jobs: + get_label: + if: startsWith(github.event.pull_request.head.ref, 'feature/') || startsWith(github.event.pull_request.head.ref, 'fix/') || startsWith(github.event.pull_request.head.ref, 'chore/') + runs-on: ubuntu-latest + outputs: + label: ${{ steps.get_label.outputs.label }} + + steps: + - name: Checkout to current branch + uses: actions/checkout@v3 + + - name: Get label + id: get_label + run: | + chmod +x scripts/generate_pr_label.sh + branch_name="${{github.event.pull_request.head.ref}}" + LABEL=$(./scripts/generate_pr_label.sh $branch_name) + echo -e "label=$LABEL" >> $GITHUB_OUTPUT + echo -e $LABEL + + add_label_to_pr: + needs: get_label + runs-on: ubuntu-latest + + steps: + - name: Checkout to current branch + uses: actions/checkout@v3 + + - name: Add label + uses: actions/github-script@v6 + env: + LABEL: ${{ needs.get_label.outputs.label }} + with: + github-token: ${{ secrets.ADYEN_AUTOMATION_BOT_ACCESS_TOKEN }} + script: | + github.rest.issues.addLabels({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: context.payload.pull_request.number, + labels: ['${{ env.LABEL }}'] + }) diff --git a/.github/workflows/check_labels.yml b/.github/workflows/check_labels.yml index 34da792675..fb41e12a9d 100644 --- a/.github/workflows/check_labels.yml +++ b/.github/workflows/check_labels.yml @@ -3,7 +3,7 @@ name: Check Labels # Every PR should have a label and some labels should include an update to the release notes on: pull_request: - types: [ synchronize, labeled, unlabeled ] + types: [ synchronize, labeled, unlabeled, edited ] concurrency: group: ${{ github.workflow }}-${{ github.head_ref }} diff --git a/scripts/generate_pr_label.sh b/scripts/generate_pr_label.sh new file mode 100644 index 0000000000..9cd15c4449 --- /dev/null +++ b/scripts/generate_pr_label.sh @@ -0,0 +1,32 @@ +#!/bin/bash + +# +# Copyright (c) 2025 Adyen N.V. +# +# This file is open source and available under the MIT license. See the LICENSE file for more info. +# +# Created by ozgur on 21/1/2025. +# +function generate_pr_label() { + if [ "$#" -ne 1 ]; then + echo "Usage: $0 " >&2 + exit 1 + fi + branch_name=$1 + + # Check if the string starts with a specific prefix + if [[ "$branch_name" == feature/* ]]; then + label="Feature" + elif [[ "$branch_name" == fix/* ]]; then + label="Fix" + elif [[ "$branch_name" == chore/* ]]; then + label="Chore" + else + echo "Branch name is not valid. It should start with feature/, fix/, chore/ or renovate/" >&2 + exit 1 + fi + + echo "$label" +} + +generate_pr_label $1