-
-
Notifications
You must be signed in to change notification settings - Fork 3k
141 lines (124 loc) · 5.81 KB
/
pr-auto-milestone.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
name: 📅 Auto set milestone on PR
on:
pull_request_target:
types:
- opened
env:
QGIS_MAJOR_VERSION: 3
jobs:
pr-without-milestones:
runs-on: ubuntu-latest
if: github.repository == 'qgis/QGIS'
steps:
# list the tags and milestones
- uses: octokit/[email protected]
id: graphql_request
with:
query: |
query {
repository(owner: "qgis", name: "QGIS") {
pullRequests(states: OPEN, last: 100) {
edges {
node {
number
title
milestone {
number
}
baseRef {
name
}
}
}
}
milestones(orderBy: {field: CREATED_AT, direction: DESC}, first: 50) {
edges {
node {
title
number
}
}
}
refs(refPrefix: "refs/tags/", orderBy: {field: TAG_COMMIT_DATE, direction: DESC}, first: 30) {
edges {
node {
name
}
}
}
}
}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# take the first unprocessed PR and determine if some remain
- name: Filter PR to check
id: extract_data
env:
JSON_DATA: ${{ steps.graphql_request.outputs.data }}
run: |
# get PRs without milestones
PRS_TO_PROCESS=$(echo "${JSON_DATA}" | jq '.repository.pullRequests.edges[] | select( .node.milestone.number | not ) | .node.number')
NUMBER_OF_PRS=$(echo "${PRS_TO_PROCESS}" | jq -s '. | length')
echo "NUMBER_OF_PRS: ${NUMBER_OF_PRS}"
# early exit
[[ ${NUMBER_OF_PRS} == 0 ]] && echo "has_milestone_to_set=0" >> $GITHUB_OUTPUT && exit 0
# Take the first
PR_NUMBER=$(echo "${PRS_TO_PROCESS}" | jq -s '. | first')
echo "PR_NUMBER: ${PR_NUMBER}"
# Not used for now
RE_RUN_JOB=$(echo "${JSON_DATA}" | jq -s '. | length > 1')
echo "RE_RUN_JOB: ${RE_RUN_JOB}"
# Get the base branch
BASE_BRANCH=$(echo "${JSON_DATA}" | jq -r ".repository.pullRequests.edges[] | select( .node.number == ${PR_NUMBER} ) | .node.baseRef.name")
echo "BASE_BRANCH: ${BASE_BRANCH}"
# master => NOTHING, release_3-10 => _10
MINOR_VERSION=$(echo ${BASE_BRANCH} | sed -r -e 's/^release-[0-9]_([0-9]+)/_\1/;t;d')
echo "MINOR_VERSION: ${MINOR_VERSION}"
# get the max release from the tags
MAX_RELEASE=$(echo "${JSON_DATA}" | jq ".repository.refs.edges[].node.name | select( . | test(\"^final-${QGIS_MAJOR_VERSION}${MINOR_VERSION}\") ) | sub(\"^final-${QGIS_MAJOR_VERSION}_(?<m>[0-9]+)_(?<p>.)\"; .m+\".\"+.p) | tonumber" | jq -s '. | max')
echo "MAX_RELEASE: ${MAX_RELEASE}"
# increase the number to get milestone: round+2 for master, +0.1 for release_xxx branches
INCREASE_OPERATION=$([[ -z ${MINOR_VERSION} ]] && echo "${MAX_RELEASE%.*} + 2.0" || echo "${MAX_RELEASE} + 0.1" )
echo "INCREASE_OPERATION: ${INCREASE_OPERATION}"
MILESTONE_TITLE="${QGIS_MAJOR_VERSION}."$(echo "${INCREASE_OPERATION}" | bc)
echo "MILESTONE_TITLE: ${MILESTONE_TITLE}"
MILESTONE_NUMBER=$(echo "${JSON_DATA}" | jq ".repository.milestones.edges[] | select( .node.title == \"${MILESTONE_TITLE}\" ) | .node.number")
echo "MILESTONE_NUMBER: ${MILESTONE_NUMBER}"
HAS_MILESTONE_TO_CREATE=$([[ -z ${MILESTONE_NUMBER} ]] && echo "1" || echo "0" )
echo "HAS_MILESTONE_TO_CREATE: ${HAS_MILESTONE_TO_CREATE}"
echo "has_milestone_to_set=1" >> $GITHUB_OUTPUT
echo "pr_number=${PR_NUMBER}" >> $GITHUB_OUTPUT
echo "milestone_title=${MILESTONE_TITLE}" >> $GITHUB_OUTPUT
echo "milestone_number=${MILESTONE_NUMBER}" >> $GITHUB_OUTPUT
echo "has_milestone_to_create=${HAS_MILESTONE_TO_CREATE}" >> $GITHUB_OUTPUT
# create the milestone if needed
- name: Create milestone if needed
id: create_milestone
if: steps.extract_data.outputs.has_milestone_to_set == 1 && steps.extract_data.outputs.has_milestone_to_create == 1
uses: octokit/[email protected]
with:
route: POST /repos/qgis/QGIS/milestones
title: ${{ steps.extract_data.outputs.milestone_title }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Compute the milestone number
- name: Compute milestone number from existing or created
id: compute_milestone
if: always() && steps.extract_data.outputs.has_milestone_to_set == 1
env:
MILESTONE_NUMBER_EXISTING: ${{ steps.extract_data.outputs.milestone_number }}
MILESTONE_NUMBER_CREATED_JSON: ${{ steps.create_milestone.outputs.data }}
run: |
FINAL_MILESTONE_NUMBER=$([[ -n ${MILESTONE_NUMBER_EXISTING} ]] && echo "${MILESTONE_NUMBER_EXISTING}" || echo $(echo "${MILESTONE_NUMBER_CREATED_JSON}" | jq .number ))
echo "FINAL_MILESTONE_NUMBER: ${FINAL_MILESTONE_NUMBER}"
echo "milestone_number=${FINAL_MILESTONE_NUMBER}" >> $GITHUB_OUTPUT
# update PR with milestone
- name: update PR milestone
if: steps.extract_data.outputs.has_milestone_to_set == 1
uses: octokit/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
route: PATCH /repos/qgis/QGIS/issues/:pull_number
pull_number: ${{ steps.extract_data.outputs.pr_number }}
milestone: ${{ steps.compute_milestone.outputs.milestone_number }}