Skip to content

Commit bec96a7

Browse files
lidavidmkou
andauthored
GH-456: Add action to assign milestone to PR/issue (#659)
## What's Changed Derived from the ADBC one. Fixes #456. --------- Co-authored-by: Sutou Kouhei <[email protected]>
1 parent 8d3fe93 commit bec96a7

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

.github/workflows/dev_pr.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,10 @@ jobs:
7575
script: |
7676
const scripts = require(`${process.env.GITHUB_WORKSPACE}/.github/workflows/dev_pr.js`);
7777
await scripts.check_linked_issue({core, github, context, issue: ${{ steps.title-format.outputs.result }}});
78+
79+
- name: Assign milestone
80+
if: '! github.event.pull_request.draft'
81+
env:
82+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
83+
run: |
84+
./.github/workflows/dev_pr_milestone.sh "${GITHUB_REPOSITORY}" ${{ github.event.number }}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/usr/bin/env bash
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+
# Assign a milestone to the given PR based on the open milestones and known
20+
# releases.
21+
22+
set -euo pipefail
23+
24+
main() {
25+
local -r repo="${1}"
26+
local -r pr_number="${2}"
27+
echo "On ${repo} pull ${pr_number}"
28+
29+
local -r existing_milestone=$(gh pr view "${pr_number}" \
30+
--json milestone \
31+
-t '{{if .milestone}}{{.milestone.title}}{{end}}')
32+
33+
if [[ -n "${existing_milestone}" ]]; then
34+
echo "PR has milestone: ${existing_milestone}"
35+
local -r milestone="${existing_milestone}"
36+
else
37+
local -r milestone=$(
38+
gh api "/repos/${repo}/milestones" |
39+
jq --raw-output '.[] | .title' |
40+
grep -E '^[0-9]+\.[0-9]+\.[0-9]+$'
41+
head -n1
42+
)
43+
44+
echo "Assigning milestone: ${milestone}"
45+
gh pr edit "${pr_number}" -m "${milestone}"
46+
fi
47+
48+
local -r repo_owner=$(echo "${repo}" | cut -d'/' -f1)
49+
local -r repo_name=$(echo "${repo}" | cut -d'/' -f2)
50+
local -r graphql_query="{
51+
repository(owner: \"${repo_owner}\", name: \"${repo_name}\") {
52+
pullRequest(number: ${pr_number}) {
53+
closingIssuesReferences(first: 5) {
54+
edges {
55+
node {
56+
number
57+
}
58+
}
59+
}
60+
}
61+
}
62+
}"
63+
local -r linked_issues=$(gh api graphql -f query="${graphql_query}" | jq -r '.data.repository.pullRequest.closingIssuesReferences.edges | .[].node.number')
64+
for issue in ${linked_issues}; do
65+
echo "Linked issue: ${issue}"
66+
gh issue edit "${issue}" --milestone "${milestone}"
67+
done
68+
}
69+
70+
main "$@"

0 commit comments

Comments
 (0)