-
Notifications
You must be signed in to change notification settings - Fork 144
207 lines (192 loc) · 7.95 KB
/
project_automation_track_pr_dates.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# SPDX-FileCopyrightText: Copyright (c) 2023-2024, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
name: Project - Set PR Dates
on:
pull_request_target:
# Run this action when a PR is opened, ready for review, or closed
types: [opened, closed, ready_for_review]
env:
ORG: ${{ github.event.repository.owner.login }}
PR_NUMBER: ${{ github.event.pull_request.number }}
REPO: ${{ github.event.repository.name }}
OPENED_DATE: ${{ github.event.pull_request.created_at }}
CLOSED_DATE: ${{ github.event.pull_request.closed_at }}
TRIGGER: ${{ github.event.action }}
# The environment vars below are hard-coded from external queries to save time + complexity here
# Note: PVT means Project V2, not "Private" - although this is a private project
# PVT = Project V2, PVTSSF = Project V2 Single Select Field, PVTIF = Project V2 Iteration Field, PVTF = Project V2 Date or Text Field
PROJECT_ID: "PVT_kwDOBkAsks4ACeio"
START_DATE_FIELD_ID: "PVTF_lADOBkAsks4ACeiozgLiId4"
REVIEW_START_DATE_FIELD_ID: "PVTF_lADOBkAsks4ACeiozgNUjEs"
END_DATE_FIELD_ID: "PVTF_lADOBkAsks4ACeiozgLiIf4"
jobs:
set_pr_date_fields:
runs-on: ubuntu-latest
steps:
- name: Generate Token
uses: actions/create-github-app-token@v1
id: generate-token
with:
app-id: ${{ secrets.ACTIONS_APP_ID }}
private-key: ${{ secrets.ACTIONS_APP_KEY }}
- name: Sleep 1s
id: sleep_1s
run: sleep 1
- name: Get PR Project ID
id: get_pr_id
env:
GITHUB_TOKEN: ${{ steps.generate-token.outputs.token }}
run: |
# Query up to 10 projects for the PR
gh api graphql -f query='
query {
organization(login: "${{ env.ORG }}") {
repository(name: "${{ env.REPO }}") {
issueOrPullRequest(number: ${{ env.PR_NUMBER }}) {
... on PullRequest {
id
projectItems(first: 10) {
edges {
node {
id
project {
id
}
}
}
}
}
}
}
}
}' > project_data.json
# Filter the json result to only the project-specific ID for the PR
# A PR can be in multiple projects so we need to filter by the project ID we want
pr_id=$(jq -r '.data.organization.repository.issueOrPullRequest.projectItems.edges[] |
select(.node.project.id == "${{ env.PROJECT_ID }}") |
.node.id' project_data.json)
echo "PR_PROJECT_ID=$pr_id" >> $GITHUB_ENV
continue-on-error: true
- name: Get Date for Review Start
if: ${{ env.TRIGGER == 'ready_for_review' }} || (${{ env.TRIGGER == 'opened' }} && ${{ github.event.pull_request.draft == false }})
id: get_date
env:
GITHUB_TOKEN: ${{ steps.generate-token.outputs.token }}
run: |
# Get the current date in the format required by ProjectsV2 GraphQL
# Uses EST timezone
# There's no github.action.date so we have to make one for the Review Date
# Otherwise we use the opened date or closed date
# If the PR is opened as a draft we will set both the open date and the review date as the same date
# An improvement could be to use the first commit date as the start date
review_date=$(TZ=America/New_York date +"%Y-%m-%dT%H:%M:%SZ")
echo "REVIEW_DATE=$review_date" >> $GITHUB_ENV
continue-on-error: true
- name: Set PR Opened Date
id: set_open_date
if: ${{ env.TRIGGER == 'opened' }}
env:
GITHUB_TOKEN: ${{ steps.generate-token.outputs.token }}
run: |
# Mutation to update the PRs's Opened Date field
echo "${{ env.OPENED_DATE }}"
gh api graphql -f query='
mutation {
updateProjectV2ItemFieldValue(
input: {
projectId: "${{ env.PROJECT_ID }}"
itemId: "${{ env.PR_PROJECT_ID }}"
fieldId: "${{ env.START_DATE_FIELD_ID }}"
value: {
date: "${{ env.OPENED_DATE }}"
}
}
) {
projectV2Item {
id
}
}
}'
echo "PR Opened Date set to ${{ env.OPENED_DATE }}"
continue-on-error: true
- name: Set PR Review Start Date
id: set_review_date
if: ${{ env.TRIGGER == 'ready_for_review' }} || (${{ env.TRIGGER == 'opened' }} && ${{ github.event.pull_request.draft == false }})
env:
GITHUB_TOKEN: ${{ steps.generate-token.outputs.token }}
run: |
# Check the Review Start Date
gh api graphql -f query='
query {
node(id: "${{ env.PR_PROJECT_ID }}") {
... on ProjectV2Item {
fieldValueByName(name: "Review Start Date") {
... on ProjectV2ItemFieldDateValue {
date
}
}
}
}
}' > pr_date_data.json
date=$(jq -r '.data.node.fieldValueByName' pr_date_data.json)
# If the Review Start Date is not set, set it
if [ "$date" == "null" ]; then
# Mutation to update the PR's Review Start Date field
gh api graphql -f query='
mutation {
updateProjectV2ItemFieldValue(
input: {
projectId: "${{ env.PROJECT_ID }}"
itemId: "${{ env.PR_PROJECT_ID }}"
fieldId: "${{ env.REVIEW_START_DATE_FIELD_ID }}"
value: {
date: "${{ env.REVIEW_DATE }}"
}
}
) {
projectV2Item {
id
}
}
}'
echo "PR Review Start Date set to ${{ env.REVIEW_DATE }}"
fi
continue-on-error: true
- name: Set PR Closed Date
id: set_closed_date
if: ${{ env.TRIGGER == 'closed' }}
env:
GITHUB_TOKEN: ${{ steps.generate-token.outputs.token }}
run: |
# Mutation to update the PR's Closed Date field
gh api graphql -f query='
mutation {
updateProjectV2ItemFieldValue(
input: {
projectId: "${{ env.PROJECT_ID }}"
itemId: "${{ env.PR_PROJECT_ID }}"
fieldId: "${{ env.END_DATE_FIELD_ID }}"
value: {
date: "${{ env.CLOSED_DATE }}"
}
}
) {
projectV2Item {
id
}
}
}'
echo "PR Closed Date set to ${{ env.CLOSED_DATE }}"
continue-on-error: true