From 1ed6157b0f07b29c004ac90dfdf73b22ce9c32a5 Mon Sep 17 00:00:00 2001 From: kjvjobin Date: Thu, 6 Nov 2025 22:12:36 +0530 Subject: [PATCH] adds(auto-tag): based on labels --- .github/workflows/auto-tags.yml | 69 +++++++++------------------------ 1 file changed, 18 insertions(+), 51 deletions(-) diff --git a/.github/workflows/auto-tags.yml b/.github/workflows/auto-tags.yml index 87b169c..1a52fd6 100644 --- a/.github/workflows/auto-tags.yml +++ b/.github/workflows/auto-tags.yml @@ -1,9 +1,10 @@ name: Auto tag from PR labels on: - pull_request: + pull_request_target: types: [closed] branches: [main] + workflow_dispatch: {} permissions: contents: write @@ -13,9 +14,10 @@ jobs: if: github.event.pull_request.merged == true runs-on: ubuntu-latest steps: - - name: Checkout + - name: Checkout base (main) uses: actions/checkout@v4 with: + ref: main fetch-depth: 0 - name: Fetch tags @@ -27,20 +29,26 @@ jobs: env: PR_LABELS: ${{ toJson(github.event.pull_request.labels) }} run: | - labels=$(echo "$PR_LABELS" | jq -r '.[].name' | tr '[:upper:]' '[:lower:]') + # Normalize labels to lowercase array using jq + labels=$(echo "$PR_LABELS" | jq -r 'map(.name | ascii_downcase)') echo "PR labels: $labels" - # If 'no-tag' present, skip tagging entirely. - if echo "$labels" | grep -q '\bno-tag\b'; then + # Skip if 'no-tag' is present + if echo "$labels" | jq -e 'index("no-tag") != null' >/dev/null; then echo "skip=true" >> "$GITHUB_OUTPUT" echo "Tagging skipped due to 'no-tag' label." exit 0 fi - level="patch" # default - if echo "$labels" | grep -q '\bmajor\b'; then level="major" - elif echo "$labels" | grep -q '\bminor\b'; then level="minor" - elif echo "$labels" | grep -q '\bpatch\b'; then level="patch" + # Precedence: major > minor > patch (default patch) + if echo "$labels" | jq -e 'index("major") != null' >/dev/null; then + level=major + elif echo "$labels" | jq -e 'index("minor") != null' >/dev/null; then + level=minor + elif echo "$labels" | jq -e 'index("patch") != null' >/dev/null; then + level=patch + else + level=patch fi echo "level=$level" >> "$GITHUB_OUTPUT" @@ -58,6 +66,7 @@ jobs: latest="v0.1.0" echo "No previous tag; starting at $latest" fi + ver="${latest#v}" IFS='.' read -r MA MI PA <<<"$ver" @@ -68,45 +77,3 @@ jobs: esac next="v${MA}.${MI}.${PA}" - echo "next=$next" >> "$GITHUB_OUTPUT" - echo "latest=$latest -> next=$next" - - - name: Tag and push - if: steps.bump.outputs.skip != 'true' - env: - NEXT: ${{ steps.next.outputs.next }} - run: | - if git rev-parse -q --verify "refs/tags/${NEXT}" >/dev/null; then - echo "Tag ${NEXT} already exists; nothing to do." - exit 0 - fi - git config user.name "github-actions[bot]" - git config user.email "41898282+github-actions[bot]@users.noreply.github.com" - git tag -a "$NEXT" -m "chore(release): $NEXT" - git push origin "$NEXT" - - - name: Comment with result - if: steps.bump.outputs.skip != 'true' - uses: actions/github-script@v7 - with: - script: | - const next = '${{ steps.next.outputs.next }}'; - const level = '${{ steps.bump.outputs.level }}'; - github.rest.issues.createComment({ - owner: context.repo.owner, - repo: context.repo.repo, - issue_number: context.payload.pull_request.number, - body: `🔖 Released **${next}** (bump: \`${level}\`).` - }); - - - name: Comment: tagging skipped - if: steps.bump.outputs.skip == 'true' - uses: actions/github-script@v7 - with: - script: | - github.rest.issues.createComment({ - owner: context.repo.owner, - repo: context.repo.repo, - issue_number: context.payload.pull_request.number, - body: `⏭️ Tag creation skipped due to \`no-tag\` label.` - });