Skip to content

Commit 35caa62

Browse files
authored
Update milestone on PR close and ensure they are visible for the originally desired milestone (#18935)
1 parent 711e632 commit 35caa62

File tree

4 files changed

+247
-0
lines changed

4 files changed

+247
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#!/bin/bash
2+
3+
# Licensed to the Apache Software Foundation (ASF) under one or more
4+
# contributor license agreements. See the NOTICE file distributed with
5+
# this work for additional information regarding copyright ownership.
6+
# The ASF licenses this file to You under the Apache License, Version 2.0
7+
# (the "License"); you may not use this file except in compliance with
8+
# the License. You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
18+
set -e
19+
20+
get_milestone_number() {
21+
local milestone_title="$1"
22+
local number=$(gh api "repos/$REPOSITORY/milestones" --jq ".[] | select(.title==\"$milestone_title\") | .number")
23+
24+
if [ -z "$number" ]; then
25+
echo "Creating milestone: $milestone_title"
26+
number=$(gh api "repos/$REPOSITORY/milestones" -f title="$milestone_title" --jq '.number')
27+
fi
28+
29+
echo "$number"
30+
}
31+
32+
create_backport_issue() {
33+
local pr_number="$1"
34+
local pr_title="$2"
35+
local target_milestone="$3"
36+
37+
local milestone_number=$(get_milestone_number "$target_milestone")
38+
local message="Backport #$pr_number to release branch for version $target_milestone"
39+
local note="Add the \`approved\` label to start the automatic backport process."
40+
local body=$(jq -n --arg source_pr "$pr_number" --arg target_version "$target_milestone" --arg message "$message" --arg note "$note" '$ARGS.named')
41+
42+
gh api "repos/$REPOSITORY/issues" \
43+
-f title="[$target_milestone] $pr_title" \
44+
-f body="$body" \
45+
-f milestone="$milestone_number" \
46+
-f labels[]="backport" \
47+
--jq '.number'
48+
}
49+
50+
if [ "$#" -ne 2 ]; then
51+
echo "Usage: $0 <pr_number> <repository>"
52+
echo "Example: $0 12345 apache/druid"
53+
exit 1
54+
fi
55+
56+
PR_NUMBER="$1"
57+
REPOSITORY="$2"
58+
59+
VERSION=$(xmllint --xpath "/*[local-name()='project']/*[local-name()='version']/text()" pom.xml | sed 's/-SNAPSHOT//')
60+
61+
if [ -z "$VERSION" ]; then
62+
echo "Error: Could not extract version from pom.xml"
63+
exit 1
64+
fi
65+
66+
echo "Extracted version: $VERSION"
67+
68+
EXISTING_MILESTONE=$(gh api "repos/$REPOSITORY/issues/$PR_NUMBER" --jq '.milestone.title // empty')
69+
70+
if [ -n "$EXISTING_MILESTONE" ] && [ "$EXISTING_MILESTONE" != "$VERSION" ]; then
71+
echo "PR #$PR_NUMBER has milestone $EXISTING_MILESTONE, but should be $VERSION"
72+
73+
PR_TITLE=$(gh api "repos/$REPOSITORY/issues/$PR_NUMBER" --jq '.title')
74+
BACKPORT_ISSUE=$(create_backport_issue "$PR_NUMBER" "$PR_TITLE" "$EXISTING_MILESTONE")
75+
76+
echo "Created backport issue #$BACKPORT_ISSUE for milestone $EXISTING_MILESTONE"
77+
elif [ -n "$EXISTING_MILESTONE" ]; then
78+
echo "PR #$PR_NUMBER already has correct milestone: $EXISTING_MILESTONE"
79+
exit 0
80+
fi
81+
82+
MILESTONE_NUMBER=$(get_milestone_number "$VERSION")
83+
84+
echo "Adding PR #$PR_NUMBER to milestone $VERSION"
85+
gh api "repos/$REPOSITORY/issues/$PR_NUMBER" -f milestone="$MILESTONE_NUMBER" -X PATCH
86+
echo "Successfully added PR #$PR_NUMBER to milestone $VERSION"

.github/scripts/backport-pr.sh

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/bin/bash
2+
3+
# Licensed to the Apache Software Foundation (ASF) under one or more
4+
# contributor license agreements. See the NOTICE file distributed with
5+
# this work for additional information regarding copyright ownership.
6+
# The ASF licenses this file to You under the Apache License, Version 2.0
7+
# (the "License"); you may not use this file except in compliance with
8+
# the License. You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
18+
set -e
19+
20+
if [ "$#" -lt 3 ] || [ "$#" -gt 4 ]; then
21+
echo "Usage: $0 <commit_hash> <target_branch> <pr_title> [body]"
22+
echo "Example: $0 abc123def456 37.0.0 'Fix bug in query engine' '{\"backport_issue\": \"12345\"}'"
23+
exit 1
24+
fi
25+
26+
COMMIT_HASH="$1"
27+
TARGET_BRANCH="$2"
28+
PR_TITLE="$3"
29+
BODY="${4:-Automatic backport to $TARGET_BRANCH}"
30+
31+
BACKPORT_BRANCH="backport-$COMMIT_HASH-to-$TARGET_BRANCH"
32+
33+
git fetch origin "$TARGET_BRANCH"
34+
git checkout -b "$BACKPORT_BRANCH" "origin/$TARGET_BRANCH"
35+
git cherry-pick -x "$COMMIT_HASH"
36+
git push origin "$BACKPORT_BRANCH"
37+
38+
gh pr create \
39+
--base "$TARGET_BRANCH" \
40+
--head "$BACKPORT_BRANCH" \
41+
--title "$PR_TITLE" \
42+
--body "$BODY" \
43+
--label "backport"

.github/workflows/backport.yml

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one
3+
# or more contributor license agreements. See the NOTICE file
4+
# distributed with this work for additional information
5+
# regarding copyright ownership. The ASF licenses this file
6+
# to you under the Apache License, Version 2.0 (the
7+
# "License"); you may not use this file except in compliance
8+
# with the License. You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing,
13+
# software distributed under the License is distributed on an
14+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
# KIND, either express or implied. See the License for the
16+
# specific language governing permissions and limitations
17+
# under the License.
18+
#
19+
20+
name: "Backport PR"
21+
22+
on:
23+
issues:
24+
types: [labeled]
25+
26+
env:
27+
ISSUE_NUMBER: ${{ github.event.issue.number }}
28+
ISSUE_TITLE: ${{ github.event.issue.title }}
29+
30+
jobs:
31+
backport:
32+
if: github.repository == 'apache/druid' && github.event.label.name == 'approved' && contains(github.event.issue.labels.*.name, 'backport')
33+
permissions:
34+
contents: write
35+
pull-requests: write
36+
issues: write
37+
runs-on: ubuntu-latest
38+
steps:
39+
- name: Checkout code
40+
uses: actions/checkout@v4
41+
with:
42+
fetch-depth: 0
43+
44+
- name: Parse issue and create backport PR
45+
env:
46+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
47+
SOURCE_PR: ${{ fromJson(github.event.issue.body).source_pr }}
48+
TARGET_VERSION: ${{ fromJson(github.event.issue.body).target_version }}
49+
run: |
50+
MERGE_COMMIT=$(gh api "repos/${{ github.repository }}/pulls/$SOURCE_PR" --jq '.merge_commit_sha')
51+
52+
if [ -z "$MERGE_COMMIT" ]; then
53+
echo "Error: Could not find merge commit for PR #$SOURCE_PR"
54+
exit 1
55+
fi
56+
57+
git config user.name "github-actions[bot]"
58+
git config user.email "github-actions[bot]@users.noreply.github.com"
59+
60+
MESSAGE="Backport for #$ISSUE_NUMBER"
61+
BODY=$(jq -n --arg backport_issue "$ISSUE_NUMBER" --arg message "$MESSAGE" '$ARGS.named')
62+
63+
.github/scripts/backport-pr.sh "$MERGE_COMMIT" "$TARGET_VERSION" "[Backport] $ISSUE_TITLE" "$BODY"
64+
65+
- name: Comment on failure
66+
if: failure()
67+
env:
68+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
69+
run: |
70+
gh api "repos/${{ github.repository }}/issues/$ISSUE_NUMBER/comments" \
71+
-f body="❌ Automatic backport failed. See workflow run: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"

.github/workflows/pr-merged.yml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one
3+
# or more contributor license agreements. See the NOTICE file
4+
# distributed with this work for additional information
5+
# regarding copyright ownership. The ASF licenses this file
6+
# to you under the Apache License, Version 2.0 (the
7+
# "License"); you may not use this file except in compliance
8+
# with the License. You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing,
13+
# software distributed under the License is distributed on an
14+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
# KIND, either express or implied. See the License for the
16+
# specific language governing permissions and limitations
17+
# under the License.
18+
#
19+
20+
name: "PR Merged"
21+
22+
on:
23+
pull_request:
24+
types: [closed]
25+
26+
jobs:
27+
add-milestone:
28+
if: github.repository == 'apache/druid' && github.event.pull_request.merged == true && github.event.pull_request.base.ref == 'master'
29+
permissions:
30+
contents: read
31+
pull-requests: write
32+
issues: write
33+
runs-on: ubuntu-latest
34+
steps:
35+
- name: Checkout code
36+
uses: actions/checkout@v4
37+
38+
- name: Install xmllint
39+
run: |
40+
sudo apt-get update
41+
sudo apt-get install -y libxml2-utils
42+
43+
- name: Add milestone to PR
44+
env:
45+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
46+
run: |
47+
.github/scripts/auto-add-milestone.sh "${{ github.event.pull_request.number }}" "${{ github.repository }}"

0 commit comments

Comments
 (0)