mheap / github-action-required-labels

Fail the build if/unless a certain combination of labels are applied to a pull request
MIT License
99 stars 23 forks source link

github-action-required-labels doesn't detect label changes from previous steps #14

Closed dil-dvinnai closed 2 years ago

dil-dvinnai commented 2 years ago

When mheap/github-action-required-labels@v1 is used in a multi-step workflow, it won't detect label changes from previous steps, causing the condition of having at least 1 label fail. Steps to replicate the issue:

1) Setup files in the repo. release-drafter/release-drafter@v5.15.0 is capable of automatically labeling pull request based on certain criteria, e.g. taking it from the branch prefix, like feature/JIRA-123 -> because of feature prefix, labeling it as enhancement

.github/workflows/pr-label-enforcer.yaml:


on:
  pull_request:
    types: [opened, edited, labeled, unlabeled, synchronize]
jobs:
  pr-label-enforcer:
    runs-on: ubuntu-latest
    steps:
        # Only runs for PR 'opened' trigger. Because job steps run in a sequential order, 'check_label' will use the added label
      - name: Release Drafter - add label for opened PR
        if: github.event.action == 'opened'
        uses: release-drafter/release-drafter@v5.15.0
        with:
          disable-autolabeler: false
          disable-releaser: false
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - name: sleep 15
        if: github.event.action == 'opened'
        run: sleep 15
      - name: check_label
        uses: mheap/github-action-required-labels@v1
        with:
          mode: minimum
          count: 1
          labels: "feature, enhancement, fix, bugfix, bug, chore, skip-changelog"

.github/release-drafter.yml:

name-template: $NEXT_PATCH_VERSION
tag-template: $NEXT_PATCH_VERSION
categories:
  - title: 🚀 Features
    labels:
      - 'feature'
      - 'enhancement'
  - title: 🐛 Bug Fixes
    labels:
      - 'fix'
      - 'bugfix'
      - 'bug'
  - title: 🧰 Maintenance
    labels:
      - chore
autolabeler:
  - label: 'enhancement'
    branch:
      - '/feature\/.+/'
  - label: 'bug'
    branch:
      - '/^(bug|fix)+\/.+/'
exclude-labels:
  - 'skip-changelog'
change-template: "- $TITLE @$AUTHOR (#$NUMBER)"
template: |
  ## What’s Changed

  $CHANGES

2) Submit a PR from e.g. feature/JIRA-123 branch. mheap/github-action-required-labels@v1 adds the label enhancement, the second step sleeps for 15 seconds, still, release-drafter/release-drafter@v5.15.0 fails with an error of not finding any labels. Unfortunately, this causes the workflow to fail on every PR open, because the label checker will fail, and then have to be triggered again manually to pass. The use case of this whole automation would be to enforce labels in all PRs, but also automatically tag PRs when possible.

mheap commented 2 years ago

Thanks for the report! I've just release v1.2.0 with a fix for this. The issue is that the event.json file is static once the job starts. I've added a check to see if GITHUB_TOKEN is set, and if so it will query the API for live information rather than using the event.json file.

You can use it like so:

- uses: mheap/github-action-required-labels@v1
  env:
    GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  with:
    mode: exactly
    count: 1
    labels: "semver:patch, semver:minor, semver:major"

You can continue to use mheap/github-action-required-labels@v1 as the tag has been updated to point to v1.2.0 - all you need to do is provide the GITHUB_TOKEN to use

dil-dvinnai commented 2 years ago

Thanks, Michael, it works as expected!