Skip to content
Open
64 changes: 64 additions & 0 deletions .github/workflows/needs-changes.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
name: PR Review Label Management

on:
pull_request_review:
types: [submitted]
issue_comment:
types: [created]
pull_request_target:
types: [labeled]

jobs:
manage-review-labels:
runs-on: ubuntu-latest
permissions:
pull-requests: write
steps:
- name: Handle review submission
if: github.event_name == 'pull_request_review'
run: |
echo "Event name: ${{ github.event_name }}"
echo "PR number: ${{ github.event.pull_request.number }}"
echo "Review state: ${{ github.event.review.state }}"
echo "Repository: ${{ github.repository }}"

PR_NUMBER=${{ github.event.pull_request.number }}
REVIEW_STATE="${{ github.event.review.state }}"

# Remove needs-review label when any review is submitted
echo "Removing needs-review label"
gh pr edit $PR_NUMBER --remove-label needs-review || echo "needs-review label not found or already removed"

# Add needs-changes label for non-approved reviews
if [[ "$REVIEW_STATE" == "changes_requested" ]] || [[ "$REVIEW_STATE" == "commented" ]]; then
echo "Adding needs-changes label for $REVIEW_STATE review"
gh pr edit $PR_NUMBER --add-label needs-changes || echo "Failed to add needs-changes label or label already exists"
else
echo "Review state $REVIEW_STATE does not trigger needs-changes label"
fi
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GH_REPO: ${{ github.repository }}

- name: Handle manual needs-changes via comment
if: github.event_name == 'issue_comment' && github.event.issue.pull_request
run: |
COMMENT_BODY="${{ github.event.comment.body }}"
COMMENT_LOWER=$(echo "$COMMENT_BODY" | tr '[:upper:]' '[:lower:]')

if [[ "$COMMENT_LOWER" == *"needs-changes"* ]] || [[ "$COMMENT_LOWER" == *"/needs-changes"* ]]; then
echo "Adding needs-changes label via comment"
gh pr edit ${{ github.event.issue.number }} --add-label needs-changes || echo "Failed to add needs-changes label or label already exists"
fi
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GH_REPO: ${{ github.repository }}

- name: Remove needs-changes when lgtm is added
if: github.event_name == 'pull_request_target' && github.event.action == 'labeled' && github.event.label.name == 'lgtm'
run: |
echo "Removing needs-changes label when lgtm was added"
gh pr edit ${{ github.event.pull_request.number }} --remove-label needs-changes || echo "needs-changes label not found or already removed"
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GH_REPO: ${{ github.repository }}
68 changes: 68 additions & 0 deletions .github/workflows/needs-review.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
name: Auto-label PR for review

on:
pull_request_target:
types: [synchronize, reopened, opened, edited, ready_for_review, labeled]
issue_comment:
types: [created]

jobs:
auto-label:
runs-on: ubuntu-latest
permissions:
pull-requests: write
if: >
github.event_name == 'pull_request_target' &&
contains(fromJSON('["opened", "edited", "ready_for_review"]'), github.event.action) &&
github.event.pull_request.draft == false &&
github.event.pull_request.user.login != 'red-hat-konflux' &&
!contains(github.event.pull_request.title, 'WIP')
steps:
- name: Add needs-review label
run: gh pr edit ${{ github.event.pull_request.number }} --add-label needs-review
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GH_REPO: ${{ github.repository }}

manual-needs-review:
runs-on: ubuntu-latest
permissions:
pull-requests: write
if: github.event_name == 'issue_comment' && github.event.issue.pull_request != null
steps:
- name: Add needs-review label via comment
run: |
echo "Event name: ${{ github.event_name }}"
echo "Issue number: ${{ github.event.issue.number }}"
echo "Comment body: ${{ github.event.comment.body }}"
echo "Is PR: ${{ github.event.issue.pull_request != null }}"

COMMENT_BODY="${{ github.event.comment.body }}"
COMMENT_LOWER=$(echo "$COMMENT_BODY" | tr '[:upper:]' '[:lower:]')

if [[ "$COMMENT_LOWER" == *"needs-review"* ]] || [[ "$COMMENT_LOWER" == *"/needs-review"* ]]; then
echo "Adding needs-review label via comment"
gh pr edit ${{ github.event.issue.number }} --add-label needs-review || echo "Failed to add label or label already exists"
else
echo "Comment does not contain needs-review trigger"
fi
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GH_REPO: ${{ github.repository }}

remove-needs-review-on-needs-changes:
runs-on: ubuntu-latest
permissions:
pull-requests: write
if: >
github.event_name == 'pull_request_target' &&
github.event.action == 'labeled' &&
github.event.label.name == 'needs-changes'
steps:
- name: Remove needs-review label when needs-changes is added
run: |
echo "Removing needs-review label when needs-changes was added"
gh pr edit ${{ github.event.pull_request.number }} --remove-label needs-review || echo "needs-review label not found or already removed"
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GH_REPO: ${{ github.repository }}