Add GitHub workflows for automatic PR labeling for needs-review and needs-changes #1
Workflow file for this run
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: PR Review Label Management | |
| on: | |
| pull_request_review: | |
| types: [submitted] | |
| issue_comment: | |
| types: [created] | |
| pull_request: | |
| types: [labeled] | |
| permissions: | |
| issues: write | |
| pull-requests: write | |
| jobs: | |
| manage-review-labels: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Handle review submission | |
| if: github.event_name == 'pull_request_review' | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| const { owner, repo } = context.repo; | |
| const pr_number = context.payload.pull_request.number; | |
| const review_state = context.payload.review.state; | |
| // Remove needs-review label when any review is submitted | |
| try { | |
| await github.rest.issues.removeLabel({ | |
| owner, | |
| repo, | |
| issue_number: pr_number, | |
| name: 'needs-review' | |
| }); | |
| console.log('Removed needs-review label'); | |
| } catch (error) { | |
| console.log('needs-review label not found or already removed'); | |
| } | |
| // Add needs-changes label for non-approved reviews | |
| if (review_state === 'changes_requested' || review_state === 'commented') { | |
| try { | |
| await github.rest.issues.addLabels({ | |
| owner, | |
| repo, | |
| issue_number: pr_number, | |
| labels: ['needs-changes'] | |
| }); | |
| console.log('Added needs-changes label'); | |
| } catch (error) { | |
| console.log('Error adding needs-changes label:', error.message); | |
| } | |
| } | |
| - name: Handle manual needs-changes via comment | |
| if: github.event_name == 'issue_comment' && github.event.issue.pull_request | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| const comment = context.payload.comment.body.toLowerCase(); | |
| if (comment.includes('needs-changes') || comment.includes('/needs-changes')) { | |
| const { owner, repo } = context.repo; | |
| const issue_number = context.payload.issue.number; | |
| try { | |
| await github.rest.issues.addLabels({ | |
| owner, | |
| repo, | |
| issue_number, | |
| labels: ['needs-changes'] | |
| }); | |
| console.log('Added needs-changes label via comment'); | |
| } catch (error) { | |
| console.log('Error adding needs-changes label:', error.message); | |
| } | |
| } | |
| - name: Remove needs-changes when lgtm is added | |
| if: github.event_name == 'pull_request' && github.event.action == 'labeled' && github.event.label.name == 'lgtm' | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| const { owner, repo } = context.repo; | |
| const pr_number = context.payload.pull_request.number; | |
| try { | |
| await github.rest.issues.removeLabel({ | |
| owner, | |
| repo, | |
| issue_number: pr_number, | |
| name: 'needs-changes' | |
| }); | |
| console.log('Removed needs-changes label when lgtm was added'); | |
| } catch (error) { | |
| console.log('needs-changes label not found or already removed'); | |
| } |