forked from keif-gwinn/update-project-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.yml
310 lines (295 loc) · 12.7 KB
/
action.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
name: Update project
description: Updates an item's fields on a GitHub Projects (beta) board based on a workflow dispatch (or other) event's input.
inputs:
organization:
description: The organization that contains the project, defaults to the current repository owner
required: false
default: ${{ github.repository_owner }}
project_number:
description: The project number from the project's URL
required: true
operation:
description: Operation type (update or read)
default: update
required: false
content_id:
description: The global ID of the issue or pull request within the project
required: true
field:
description: The field on the project to set the value of
required: true
value:
description: The value to set the project field to
required: true
github_token:
description: A GitHub Token with access to both the source issue and the destination project (`repo` and `write:org` scopes)
required: true
outputs:
project_id:
description: "The global ID of the project"
value: ${{ steps.parse_project_metadata.outputs.project_id }}
item_id:
description: "The global ID of the issue or pull request"
value: ${{ steps.parse_project_metadata.outputs.item_id }}
item_title:
description: "The title of the issue or pull request"
value: ${{ steps.parse_project_metadata.outputs.item_title }}
field_id:
description: "The global ID of the field"
value: ${{ steps.parse_project_metadata.outputs.field_id }}
field_read_value:
description: "The value of the field before the update"
value: ${{ steps.parse_content_metadata.outputs.item_value }}
field_updated_value:
description: "The value of the field after the update"
value: ${{ steps.output_values.outputs.field_updated_value }}
field_type:
description: "The updated field's ProjectV2FieldType (text, single_select, number, date, or iteration)"
value: ${{ steps.parse_project_metadata.outputs.field_type }}
option_id:
description: "The global ID of the selected option"
value: ${{ steps.parse_project_metadata.outputs.option_id }}
runs:
using: "composite"
steps:
- name: Check that the operation type is valid
env:
OPERATION: ${{ inputs.operation }}
shell: bash
run: |
if [ "$OPERATION" != "read" ] && [ "$OPERATION" != "update" ]; then
echo "Invalid value passed for the 'operation' parameter (passed: $OPERATION, allowed: read, update)";
exit 1;
fi
- name: Fetch content ID, value and title
id: fetch_content_metadata
env:
GITHUB_TOKEN: ${{ inputs.github_token }}
CONTENT_ID: ${{ inputs.content_id }}
FIELD_NAME: ${{ inputs.field }}
FILE_NAME: organization-${{ inputs.organization }}-issue-${{ inputs.content_id }}.json
OPERATION: ${{ inputs.operation }}
QUERY: |
query($issue_id: ID!, $field_name: String!) {
node(id: $issue_id) {
... on Issue {
id
title
projectItems(first: 100) {
nodes {
id
project {
number
owner {
... on Organization {
login
}
... on User {
login
}
}
}
field: fieldValueByName(name: $field_name) {
... on ProjectV2ItemFieldSingleSelectValue {
value: name
}
... on ProjectV2ItemFieldNumberValue {
value: number
}
... on ProjectV2ItemFieldTextValue {
value: text
}
... on ProjectV2ItemFieldDateValue {
value: date
}
}
}
}
}
}
}
shell: bash
run: |
# Fetch project fields metadata
if [ ! -f "$FILE_NAME" ] || [ "$OPERATION" == "read" ]; then
gh api graphql -f query="$QUERY" -F issue_id="$CONTENT_ID" -F field_name="$FIELD_NAME" > "$FILE_NAME"
else
echo "Using cached project fields metadata from '$FILE_NAME'"
fi
echo "file_name=$FILE_NAME" >> $GITHUB_OUTPUT
- name: Parse content ID, value and title
id: parse_content_metadata
shell: bash
env:
PROJECT_NUMBER: ${{ inputs.project_number }}
OWNER: ${{ inputs.organization }}
FILE_NAME: ${{ steps.fetch_content_metadata.outputs.file_name }}
run: |
# Parse content metadata
item_id=$(jq -r '.data.node.projectItems.nodes | .[] | select(.project.number==($PROJECT_NUMBER|fromjson) and .project.owner.login==$OWNER).id' "$FILE_NAME" --arg OWNER "$OWNER" --arg PROJECT_NUMBER "$PROJECT_NUMBER")
item_title=$(jq -r '.data.node.title' "$FILE_NAME")
item_value=$(jq -r '.data.node.projectItems.nodes | .[] | select(.project.number==($PROJECT_NUMBER|fromjson) and .project.owner.login==$OWNER).field.value' "$FILE_NAME" --arg OWNER "$OWNER" --arg PROJECT_NUMBER "$PROJECT_NUMBER")
echo "item_id=$item_id" >> $GITHUB_OUTPUT
echo "item_title=$item_title" >> $GITHUB_OUTPUT
echo "item_value=$item_value" >> $GITHUB_OUTPUT
- name: Ensure content item was found
env:
CONTENT_ID: ${{ inputs.content_id }}
ITEM_ID: ${{ steps.parse_content_metadata.outputs.item_id }}
shell: bash
run: |
# Ensure content item was found
if [ -z "$ITEM_ID" ]; then echo "Item not found with ID '$CONTENT_ID'"; exit 1; fi
- name: Fetch project fields metadata
id: fetch_project_fields_metadata
env:
GITHUB_TOKEN: ${{ inputs.github_token }}
PROJECT_NUMBER: ${{ inputs.project_number }}
ORGANIZATION: ${{ inputs.organization }}
FILE_NAME: project-${{ inputs.organization }}-${{ inputs.project_number }}.json
QUERY: |
query ($organization: String!, $project_number: Int!) {
organization(login: $organization) {
projectV2(number: $project_number) {
id
fields(first: 100) {
nodes {
... on ProjectV2FieldCommon {
id
name
dataType
}
... on ProjectV2SingleSelectField {
options {
id
name
}
}
}
}
}
}
}
shell: bash
run: |
# Fetch project fields metadata
if [ ! -f "$FILE_NAME" ]; then
gh api graphql -f query="$QUERY" -F project_number="$PROJECT_NUMBER" -F organization="$ORGANIZATION" > "$FILE_NAME"
else
echo "Using cached project fields metadata from '$FILE_NAME'"
fi
echo "file_name=$FILE_NAME" >> $GITHUB_OUTPUT
- name: Parse project fields metadata
id: parse_project_fields_metadata
shell: bash
env:
FIELD: "${{ inputs.field }}"
VALUE: ${{ inputs.value }}
FILE_NAME: ${{ steps.fetch_project_fields_metadata.outputs.file_name }}
run: |
# Parse project metadata
project_id=$(jq -r '.data.organization.projectV2.id' "$FILE_NAME")
field_id=$(jq -r '.data.organization.projectV2.fields.nodes[] | select(.name==$FIELD) | .id' "$FILE_NAME" --arg FIELD "$FIELD")
field_type=$(jq -r '.data.organization.projectV2.fields.nodes[] | select(.name==$FIELD) | .dataType | ascii_downcase' "$FILE_NAME" --arg FIELD "$FIELD")
option_id=$(jq -r '.data.organization.projectV2.fields.nodes[] | select(.name==$FIELD) | .options[]? | select(.name == $VALUE) | .id' "$FILE_NAME" --arg VALUE "$VALUE" --arg FIELD "$FIELD")
echo "project_id=$project_id" >> $GITHUB_OUTPUT
echo "field_id=$field_id" >> $GITHUB_OUTPUT
echo "field_type=$field_type" >> $GITHUB_OUTPUT
echo "option_id=$option_id" >> $GITHUB_OUTPUT
- name: Ensure project, field, and option were found
env:
PROJECT_NUMBER: ${{ inputs.project_number }}
ORGANIZATION: ${{ inputs.organization }}
FIELD: ${{ inputs.field }}
VALUE: ${{ inputs.value }}
PROJECT_ID: ${{ steps.parse_project_fields_metadata.outputs.project_id }}
FIELD_ID: ${{ steps.parse_project_fields_metadata.outputs.field_id }}
FIELD_TYPE: ${{ steps.parse_project_fields_metadata.outputs.field_type }}
OPTION_ID: ${{ steps.parse_project_fields_metadata.outputs.option_id }}
shell: bash
run: |
# Ensure project, item, field, and option were found
if [ -z "$PROJECT_ID" ] || [ "$PROJECT_ID" = "null" ]; then echo "Project '$PROJECT_NUMBER' not found for organization '$ORGANIZATION'"; exit 1; fi
if [ -z "$FIELD_ID" ]; then echo "Field '$FIELD' not found"; exit 1; fi
if [ "$OPERATION" == "update" ]; then
if [[ "$FIELD_TYPE" = "single_select" && -z "$OPTION_ID" ]]; then echo "Option not found with value '$VALUE'"; exit 1; fi
fi
- name: Parse value
shell: bash
id: parse_value
env:
FIELD_TYPE: ${{ steps.parse_project_fields_metadata.outputs.field_type }}
OPTION_ID: ${{ steps.parse_project_fields_metadata.outputs.option_id }}
VALUE: ${{ inputs.value }}
run: |
# Parse value
if [ "$FIELD_TYPE" = "single_select" ]; then
echo "value_to_set=$OPTION_ID" >> $GITHUB_OUTPUT
echo 'value_type=singleSelectOptionId' >> $GITHUB_OUTPUT
else
echo "value_to_set=$VALUE" >> $GITHUB_OUTPUT
echo "value_type=$FIELD_TYPE" >> $GITHUB_OUTPUT
fi
# Set GraphQL Field Type
if [ "$FIELD_TYPE" = "date" ]; then
echo 'value_graphql_type=Date' >> $GITHUB_OUTPUT
elif [ "$FIELD_TYPE" = "number" ]; then
echo 'value_graphql_type=Float' >> $GITHUB_OUTPUT
else
echo 'value_graphql_type=String' >> $GITHUB_OUTPUT
fi
- name: Output values
id: output_values
shell: bash
env:
OPERATION: ${{ inputs.operation }}
run: |
if [ "$OPERATION" == "read" ]; then
echo "field_updated_value="${{ steps.parse_content_metadata.outputs.item_value }} >> $GITHUB_OUTPUT
else
echo "field_updated_value="${{ inputs.value }} >> $GITHUB_OUTPUT
fi
- name: Update field
if: ${{ inputs.operation == 'update' }}
env:
GITHUB_TOKEN: ${{ inputs.github_token }}
FIELD: ${{ inputs.field }}
FIELD_TYPE: ${{ steps.parse_project_fields_metadata.outputs.field_type }}
PROJECT_ID: ${{ steps.parse_project_fields_metadata.outputs.project_id }}
ITEM_ID: ${{ steps.parse_content_metadata.outputs.item_id }}
FIELD_ID: ${{ steps.parse_project_fields_metadata.outputs.field_id }}
ITEM_TITLE: ${{ steps.parse_content_metadata.outputs.item_title }}
VALUE_TO_SET: ${{ steps.parse_value.outputs.value_to_set }}
VALUE: ${{ inputs.value }}
QUERY: |
mutation($project: ID!, $item: ID!, $field: ID!, $value: ${{ steps.parse_value.outputs.value_graphql_type }}) {
updateProjectV2ItemFieldValue(
input: {
projectId: $project
itemId: $item
fieldId: $field
value: {
${{ steps.parse_value.outputs.value_type }}: $value
}
}
) {
projectV2Item {
id
}
}
}
shell: bash
run: |
# Update project
if [ "$FIELD_TYPE" = "single_select" ]; then
# From https://cli.github.com/manual/gh_api:
# > The -F/--field flag has magic type conversion based on the format of the value:
# > literal values "true", "false", "null", and integer numbers get converted to appropriate JSON types;
# Single select option ids are hexadecimal values, but if they are all digits, gh cli coerces them to integers
# Reading a value from stdin bypasses the conversion:
echo -n $VALUE_TO_SET | gh api graphql -f query="$QUERY" -F project="$PROJECT_ID" -F item="$ITEM_ID" -F field="$FIELD_ID" -F value="@-"
else
gh api graphql -f query="$QUERY" -F project="$PROJECT_ID" -F item="$ITEM_ID" -F field="$FIELD_ID" -F value="$VALUE_TO_SET"
fi
echo ""
echo "Updated field '$FIELD' on '$ITEM_TITLE' to '$VALUE'. Happy reporting! 📈"