From 6a1b5c10daecddd7b82081fc3c1dab7fdc712f1e Mon Sep 17 00:00:00 2001 From: Monet Lee Date: Mon, 21 Oct 2024 11:32:13 +0800 Subject: [PATCH] 666 --- .github/workflows/pr-merged-handle.yml | 59 +++++++++++++++----------- 1 file changed, 35 insertions(+), 24 deletions(-) diff --git a/.github/workflows/pr-merged-handle.yml b/.github/workflows/pr-merged-handle.yml index 324c086..6e738e0 100644 --- a/.github/workflows/pr-merged-handle.yml +++ b/.github/workflows/pr-merged-handle.yml @@ -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: @@ -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 }} \ No newline at end of file + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}