Skip to content

Commit

Permalink
666
Browse files Browse the repository at this point in the history
  • Loading branch information
mo3et committed Oct 21, 2024
1 parent d58f541 commit 6a1b5c1
Showing 1 changed file with 35 additions and 24 deletions.
59 changes: 35 additions & 24 deletions .github/workflows/pr-merged-handle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Handle PR Branch Based on Title and Label
on:
pull_request:
types:
- closed
- closed # Trigger when PR is closed (merged or not)

jobs:
handle_pr:
Expand All @@ -13,42 +13,53 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v3

- name: Check for 'milestone-merge' label or Title Pattern
id: check_conditions
- name: Get the PR title and extract PR numbers
id: extract_pr_numbers
run: |
# Get the PR title and labels
# Get the PR title
PR_TITLE="${{ github.event.pull_request.title }}"
PR_LABELS=$(echo "${{ github.event.pull_request.labels[*].name }}" | tr -d '[],"')
echo "PR Title: $PR_TITLE"
echo "PR Labels: $PR_LABELS"
# Check if title matches the dynamic pattern or has the 'milestone-merge' label
if echo "$PR_TITLE" | grep -qE "^deps: Merge #([0-9]+ )+# PRs into .+" || echo "$PR_LABELS" | grep -q "milestone-merge"; then
echo "::set-output name=proceed::true"
# Extract PR numbers from the title
PR_NUMBERS=$(echo "$PR_TITLE" | grep -oE "#[0-9]+" | tr -d '#' | tr '\n' ' ')
echo "Extracted PR Numbers: $PR_NUMBERS"
# Save PR numbers to a file
echo "$PR_NUMBERS" > pr_numbers.txt
echo "Saved PR Numbers to pr_numbers.txt"
# Check if the title matches a specific pattern
if echo "$PR_TITLE" | grep -qE "^deps: Merge( #[0-9]+)+ PRs into .+"; then
echo "proceed=true" >> $GITHUB_OUTPUT
else
echo "::set-output name=proceed::false"
echo "proceed=false" >> $GITHUB_OUTPUT
fi
- name: Delete branch after PR close
if: steps.check_conditions.outputs.proceed == 'true'
if: steps.extract_pr_numbers.outputs.proceed == 'true' || contains(github.event.pull_request.labels.*.name, 'milestone-merge')
run: |
BRANCH_NAME=$(echo "${{ github.event.pull_request.head.ref }}")
BRANCH_NAME="${{ github.event.pull_request.head.ref }}"
echo "Branch to delete: $BRANCH_NAME"
git push origin --delete $BRANCH_NAME
git push origin --delete "$BRANCH_NAME"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Label PR if merged
if: steps.check_conditions.outputs.proceed == 'true' && github.event.pull_request.merged == true
- name: Use extracted PR numbers and label PRs
if: (steps.extract_pr_numbers.outputs.proceed == 'true' || contains(github.event.pull_request.labels.*.name, 'milestone-merge')) && github.event.pull_request.merged == true
run: |
PR_NUMBER=${{ github.event.pull_request.number }}
echo "PR #$PR_NUMBER was merged."
echo "Adding 'cherry-picked' label to merged PR."
curl -X POST \
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
-H "Accept: application/vnd.github+json" \
https://api.github.com/repos/${{ github.repository }}/issues/$PR_NUMBER/labels \
-d '{"labels":["cherry-picked"]}'
# Read the previously saved PR numbers
PR_NUMBERS=$(cat pr_numbers.txt)
echo "Using extracted PR Numbers: $PR_NUMBERS"
# Loop through each PR number and add label
for PR_NUMBER in $PR_NUMBERS; do
echo "Adding 'cherry-picked' label to PR #$PR_NUMBER"
curl -X POST \
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
-H "Accept: application/vnd.github+json" \
https://api.github.com/repos/${{ github.repository }}/issues/$PR_NUMBER/labels \
-d '{"labels":["cherry-picked"]}'
done
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

0 comments on commit 6a1b5c1

Please sign in to comment.