Skip to content

Add option to add labels #124

@fty4

Description

@fty4

If labels like major, minor or patch should be added to a PR the workflow needs to be extended and use the outputs of this action.

But this should be implemented directly into the action as optional flag.

example

name: commit

on:
  pull_request:

  push:
    branches:
      - main

jobs:
  commit-message:
    name: conventional commits
    runs-on: [self-hosted, Linux]
    steps:
      - uses: actions/checkout@v2

      - uses: taskmedia/action-conventional-commits@v1.0.1
        id: cc
        with:
          types: "fix|feat|revert|chore|docs"

      - name: label PR with version type
        if: |
          github.event_name == 'pull_request' &&
          (github.event.action == 'opened' || github.event.action == 'synchronize') &&
          github.event.pull_request.user.login != 'dependabot[bot]'
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          PR_NUMBER: ${{ github.event.number }}
          REPO: ${{ github.repository }}
        shell: bash
        run: |
          echo $GITHUB_TOKEN | gh auth login --with-token

          LABELS=$(gh pr view $PR_NUMBER --repo $REPO --json "labels" --jq '.labels[].name')
          # add version_type label if not already present
          if [[ "$LABELS" != *"${{ steps.cc.outputs.version_type }}"* ]]; then
            echo "add label ${{ steps.cc.outputs.version_type }} to PR $PR_NUMBER"
            gh pr edit $PR_NUMBER \
              --add-label "${{ steps.cc.outputs.version_type }}" \
              --repo $REPO
          else
            echo "label ${{ steps.cc.outputs.version_type }} already present at PR $PR_NUMBER"
          fi
          # remove old SemVer labels if present
          while read -r label
          do
            if [ "$label" = "patch" ] && [ "$label" != "${{ steps.cc.outputs.version_type }}" ]; then
              echo "removing label patch from PR $PR_NUMBER"
              gh pr edit $PR_NUMBER --repo $REPO --remove-label "patch"
              continue
            fi
            if [ "$label" = "minor" ] && [ "$label" != "${{ steps.cc.outputs.version_type }}" ]; then
              echo "removing label minor from PR $PR_NUMBER"
              gh pr edit $PR_NUMBER --repo $REPO --remove-label "minor"
              continue
            fi
            if [ "$label" = "major" ] && [ "$label" != "${{ steps.cc.outputs.version_type }}" ]; then
              echo "removing label major from PR $PR_NUMBER"
              gh pr edit $PR_NUMBER --repo $REPO --remove-label "major"
              continue
            fi
          done <<< "$LABELS"

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions