Skip to content

Commit 219e6e4

Browse files
authored
feat: improve merge in milestone and merged handle logic. (#794)
* feat: improve merge in milestone logic. * update cleanup milestone.
1 parent c2b2f40 commit 219e6e4

File tree

2 files changed

+41
-126
lines changed

2 files changed

+41
-126
lines changed

.github/workflows/cleanup-after-milestone-prs-merged.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ jobs:
2929
echo "$PR_NUMBERS" > pr_numbers.txt
3030
echo "Saved PR Numbers to pr_numbers.txt"
3131
32-
# Check if the title matches a specific pattern
33-
if echo "$PR_TITLE" | grep -qE "^deps: Merge( #[0-9]+)+ PRs into .+"; then
32+
# Check if the title matches the specific pattern
33+
if echo "$PR_TITLE" | grep -qE "\[Created by @.+ from #[0-9]+\]$"; then
3434
echo "proceed=true" >> $GITHUB_OUTPUT
3535
else
3636
echo "proceed=false" >> $GITHUB_OUTPUT
@@ -57,6 +57,7 @@ jobs:
5757

5858
- name: Delete branch after PR close
5959
if: steps.extract_pr_numbers.outputs.proceed == 'true' || contains(github.event.pull_request.labels.*.name, 'milestone-merge')
60+
continue-on-error: true
6061
run: |
6162
BRANCH_NAME="${{ github.event.pull_request.head.ref }}"
6263
echo "Branch to delete: $BRANCH_NAME"

.github/workflows/merge-from-milestone.yml

+38-124
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,24 @@ on:
99
workflow_dispatch:
1010
inputs:
1111
milestone_name:
12-
description: 'Milestone name to collect closed PRs from'
12+
description: "Milestone name to collect closed PRs from"
1313
required: true
14-
default: 'v3.8.2'
14+
default: "v3.8.3"
1515
target_branch:
16-
description: 'Target branch to merge the consolidated PR'
16+
description: "Target branch to merge the consolidated PR"
1717
required: true
18-
default: 'pre-release-v3.8.2'
18+
default: "pre-release-v3.8.3"
1919

2020
env:
21-
MILESTONE_NAME: ${{ github.event.inputs.milestone_name || 'v3.8.2' }}
22-
TARGET_BRANCH: ${{ github.event.inputs.target_branch || 'pre-release-v3.8.2' }}
21+
MILESTONE_NAME: ${{ github.event.inputs.milestone_name || 'v3.8.3' }}
22+
TARGET_BRANCH: ${{ github.event.inputs.target_branch || 'pre-release-v3.8.3' }}
2323
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2424
BOT_TOKEN: ${{ secrets.BOT_TOKEN }}
2525
LABEL_NAME: cherry-picked
26-
TEMP_DIR: /tmp # Using /tmp as the temporary directory
26+
TEMP_DIR: /tmp # Using /tmp as the temporary directory
2727

2828
jobs:
29-
cherry_pick_milestone_prs:
29+
merge_milestone_prs:
3030
runs-on: ubuntu-latest
3131
steps:
3232
- name: Setup temp directory
@@ -47,7 +47,6 @@ jobs:
4747

4848
- name: Setup Git User for OpenIM-Robot
4949
run: |
50-
# Set up Git credentials for the bot
5150
git config --global user.email "[email protected]"
5251
git config --global user.name "OpenIM-Robot"
5352
@@ -83,136 +82,51 @@ jobs:
8382
if ! echo "$labels" | grep -q "${LABEL_NAME}"; then
8483
echo "PR #$pr_number does not have the 'cherry-picked' label. Adding to the list."
8584
echo "$pr_number" >> ${{ env.TEMP_DIR }}/pr_numbers.txt
86-
else
87-
echo "PR #$pr_number already has the 'cherry-picked' label. Skipping."
8885
fi
8986
done
9087
91-
# Sort the filtered PR numbers
9288
sort -n ${{ env.TEMP_DIR }}/pr_numbers.txt -o ${{ env.TEMP_DIR }}/pr_numbers.txt
9389
94-
echo "Filtered and sorted PR numbers:"
95-
cat ${{ env.TEMP_DIR }}/pr_numbers.txt || echo "No closed PR numbers found for milestone."
96-
97-
- name: Fetch Merge Commits for PRs and Generate Title and Body
90+
- name: Create Individual PRs
9891
run: |
99-
# Ensure the files are initialized
100-
> ${{ env.TEMP_DIR }}/commit_hashes.txt
101-
> ${{ env.TEMP_DIR }}/pr_title.txt
102-
> ${{ env.TEMP_DIR }}/pr_body.txt
103-
104-
# Write description to the PR body
105-
echo "### Description:" >> ${{ env.TEMP_DIR }}/pr_body.txt
106-
echo "Merging PRs from milestone \`$MILESTONE_NAME\` into target branch \`$TARGET_BRANCH\`." >> ${{ env.TEMP_DIR }}/pr_body.txt
107-
echo "" >> ${{ env.TEMP_DIR }}/pr_body.txt
108-
echo "### Need Merge PRs:" >> ${{ env.TEMP_DIR }}/pr_body.txt
109-
110-
pr_numbers_in_title=""
111-
112-
# Process sorted PR numbers and generate commit hashes
11392
for pr_number in $(cat ${{ env.TEMP_DIR }}/pr_numbers.txt); do
114-
echo "Processing PR #$pr_number"
11593
pr_details=$(curl -s -H "Authorization: token $BOT_TOKEN" \
11694
-H "Accept: application/vnd.github+json" \
11795
"https://api.github.com/repos/${{ github.repository }}/pulls/$pr_number")
11896
pr_title=$(echo "$pr_details" | jq -r '.title')
97+
pr_body=$(echo "$pr_details" | jq -r '.body')
98+
pr_creator=$(echo "$pr_details" | jq -r '.user.login')
11999
merge_commit=$(echo "$pr_details" | jq -r '.merge_commit_sha')
120100
short_commit_hash=$(echo "$merge_commit" | cut -c 1-7)
121101
122-
# Append PR details to the body
123-
echo "- $pr_title: (#$pr_number) ($short_commit_hash)" >> ${{ env.TEMP_DIR }}/pr_body.txt
124-
125102
if [ "$merge_commit" != "null" ];then
126-
echo "$merge_commit" >> ${{ env.TEMP_DIR }}/commit_hashes.txt
127-
echo "#$pr_number" >> ${{ env.TEMP_DIR }}/pr_title.txt
128-
pr_numbers_in_title="$pr_numbers_in_title #$pr_number"
129-
fi
130-
done
103+
git fetch origin
104+
git checkout $TARGET_BRANCH
105+
git pull origin $TARGET_BRANCH
106+
cherry_pick_branch="cherry-pick-${short_commit_hash}"
107+
git checkout -b $cherry_pick_branch
108+
git cherry-pick "$merge_commit" --strategy=recursive -X theirs || git cherry-pick --continue
109+
git remote set-url origin "https://${BOT_TOKEN}@github.com/${{ github.repository }}.git"
110+
git push origin $cherry_pick_branch --force
111+
112+
new_pr_title="$pr_title [Created by @$pr_creator from #$pr_number]"
113+
new_pr_body="$pr_body \\n >This PR cherry-picks commit from original PR #$pr_number."
114+
115+
response=$(curl -s -X POST -H "Authorization: token $BOT_TOKEN" \
116+
-H "Accept: application/vnd.github+json" \
117+
https://api.github.com/repos/${{ github.repository }}/pulls \
118+
-d "$(jq -n --arg title "$new_pr_title" \
119+
--arg head "$cherry_pick_branch" \
120+
--arg base "$TARGET_BRANCH" \
121+
--arg body "$new_pr_body" \
122+
'{title: $title, head: $head, base: $base, body: $body}')")
131123
132-
commit_hashes=$(cat ${{ env.TEMP_DIR }}/commit_hashes.txt | tr '\n' ' ')
133-
first_commit_hash=$(head -n 1 ${{ env.TEMP_DIR }}/commit_hashes.txt)
134-
cherry_pick_branch="cherry-pick-${first_commit_hash:0:7}"
135-
echo "COMMIT_HASHES=$commit_hashes" >> $GITHUB_ENV
136-
echo "CHERRY_PICK_BRANCH=$cherry_pick_branch" >> $GITHUB_ENV
137-
echo "pr_numbers_in_title=$pr_numbers_in_title" >> $GITHUB_ENV
124+
new_pr_number=$(echo "$response" | jq -r '.number')
125+
echo "Created PR #$new_pr_number"
138126
139-
- name: Pull and Cherry-pick Commits, Then Push
140-
env:
141-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
142-
BOT_TOKEN: ${{ secrets.BOT_TOKEN }}
143-
run: |
144-
# Fetch and pull the latest changes from the target branch
145-
git fetch origin
146-
git checkout $TARGET_BRANCH
147-
git pull origin $TARGET_BRANCH
148-
149-
# Create a new branch for cherry-picking
150-
git checkout -b $CHERRY_PICK_BRANCH
151-
152-
# Cherry-pick the commits and handle conflicts
153-
for commit_hash in $COMMIT_HASHES; do
154-
echo "Attempting to cherry-pick commit $commit_hash"
155-
if ! git cherry-pick "$commit_hash" --strategy=recursive -X theirs; then
156-
echo "Conflict detected for $commit_hash. Resolving with incoming changes."
157-
conflict_files=$(git diff --name-only --diff-filter=U)
158-
echo "Conflicting files:"
159-
echo "$conflict_files"
160-
161-
for file in $conflict_files; do
162-
if [ -f "$file" ]; then
163-
echo "Resolving conflict for $file"
164-
git add "$file"
165-
else
166-
echo "File $file has been deleted. Skipping."
167-
git rm "$file"
168-
fi
169-
done
170-
171-
echo "Conflicts resolved. Continuing cherry-pick."
172-
git cherry-pick --continue
173-
else
174-
echo "Cherry-pick successful for commit $commit_hash."
127+
curl -s -X POST -H "Authorization: token $GITHUB_TOKEN" \
128+
-H "Accept: application/vnd.github+json" \
129+
-d '{"labels": ["milestone-merge"]}' \
130+
"https://api.github.com/repos/${{ github.repository }}/issues/$new_pr_number/labels"
175131
fi
176-
done
177-
178-
# Push the cherry-pick branch to the repository
179-
git remote set-url origin "https://${BOT_TOKEN}@github.com/${{ github.repository }}.git"
180-
git push origin $CHERRY_PICK_BRANCH --force
181-
182-
- name: Create Pull Request
183-
run: |
184-
# Prepare and create the PR
185-
pr_title="deps: Merge ${{ env.pr_numbers_in_title }} PRs into $TARGET_BRANCH"
186-
pr_body=$(cat ${{ env.TEMP_DIR }}/pr_body.txt)
187-
188-
echo "Prepared PR title:"
189-
echo "$pr_title"
190-
echo "Prepared PR body:"
191-
echo "$pr_body"
192-
193-
# Create the PR using the GitHub API
194-
response=$(curl -s -X POST -H "Authorization: token $BOT_TOKEN" \
195-
-H "Accept: application/vnd.github+json" \
196-
https://api.github.com/repos/${{ github.repository }}/pulls \
197-
-d "$(jq -n --arg title "$pr_title" \
198-
--arg head "$CHERRY_PICK_BRANCH" \
199-
--arg base "$TARGET_BRANCH" \
200-
--arg body "$pr_body" \
201-
'{title: $title, head: $head, base: $base, body: $body}')")
202-
203-
pr_number=$(echo "$response" | jq -r '.number')
204-
echo "$pr_number" > ${{ env.TEMP_DIR }}/created_pr_number.txt
205-
echo "Created PR #$pr_number"
206-
207-
- name: Add Label to Created Pull Request
208-
run: |
209-
# Add 'milestone-merge' label to the created PR
210-
pr_number=$(cat ${{ env.TEMP_DIR }}/created_pr_number.txt)
211-
echo "Adding label to PR #$pr_number"
212-
213-
curl -s -X POST -H "Authorization: token $GITHUB_TOKEN" \
214-
-H "Accept: application/vnd.github+json" \
215-
-d '{"labels": ["milestone-merge"]}' \
216-
"https://api.github.com/repos/${{ github.repository }}/issues/$pr_number/labels"
217-
218-
echo "Added 'milestone-merge' label to PR #$pr_number."
132+
done

0 commit comments

Comments
 (0)