-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathupdate-project-fields.ts
268 lines (241 loc) · 7.29 KB
/
update-project-fields.ts
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
import * as core from '@actions/core'
import * as github from '@actions/github'
import {Octokit} from '@octokit/core'
// TODO: Ensure this (and the Octokit client) works for non-github.com URLs, as well.
// https://github.com/orgs|users/<ownerName>/projects/<projectNumber>
const urlParse =
/^(?:https:\/\/)?github\.com\/(?<ownerType>orgs|users)\/(?<ownerName>[^/]+)\/projects\/(?<projectNumber>\d+)/
interface ProjectNodeIDResponse {
organization?: {
projectV2: {
id: string
}
}
user?: {
projectV2: {
id: string
}
}
}
interface ProjectFieldIteration {
id: string
startDate: string
}
interface ProjectFieldData {
id: string
name: string
dataType: string
options?: [
{
id: string
name: string
}
]
configuration?: {
iterations: [ProjectFieldIteration]
}
}
interface ProjectFieldsResponse {
node: {
fields: {
nodes: [ProjectFieldData]
}
}
}
interface ProjectUpdateFieldItemResponse {
updateProjectV2ItemFieldValue: {
projectV2Item: {
id: string
}
}
}
interface FieldsToUpdate {
[key: string]: string
}
function getInputItemId(): string {
return core.getInput('item-id', {required: true})
}
export function getFields(): FieldsToUpdate {
const fieldKeys = core.getInput('field-keys', {required: true})
const fieldValues = core.getInput('field-values', {required: true})
const valuesArray = fieldValues.split(',')
return fieldKeys.split(',').reduce((obj: {[key: string]: any}, f, i) => {
if (valuesArray[i]) {
obj[f] = valuesArray[i]
}
return obj
}, {})
}
async function getProject(octokit: Octokit): Promise<string> {
const projectUrl = core.getInput('project-url', {required: true})
const urlMatch = projectUrl.match(urlParse)
if (!urlMatch) {
throw new Error(
`Invalid project URL: ${projectUrl}. Project URL should match the format https://github.com/<orgs-or-users>/<ownerName>/projects/<projectNumber>`
)
}
const projectOwnerName = urlMatch.groups?.ownerName
const projectNumber = parseInt(urlMatch.groups?.projectNumber ?? '', 10)
const ownerType = urlMatch.groups?.ownerType
const ownerTypeQuery = mustGetOwnerTypeQuery(ownerType)
// First, use the GraphQL API to request the project's node ID.
const idResp = await octokit.graphql<ProjectNodeIDResponse>(
`query getProject($projectOwnerName: String!, $projectNumber: Int!) {
${ownerTypeQuery}(login: $projectOwnerName) {
projectV2(number: $projectNumber) {
id
}
}
}`,
{
projectOwnerName,
projectNumber
}
)
const projectId = idResp[ownerTypeQuery]?.projectV2.id
return projectId as string
}
function getOctokitCli(): Octokit {
const ghToken = core.getInput('github-token', {required: true})
return github.getOctokit(ghToken)
}
export async function updateProject(): Promise<void> {
const octokit: Octokit = getOctokitCli()
const itemId: string = getInputItemId()
const fieldsMap: FieldsToUpdate = getFields()
const projectId: string = await getProject(octokit)
if (Object.keys(fieldsMap).length > 0) {
const projectFields: ProjectFieldsResponse = await octokit.graphql<ProjectFieldsResponse>(
`query getProjectFields($projectId: ID!) {
node(id: $projectId) {
... on ProjectV2 {
fields(first: 100) {
nodes {
... on ProjectV2Field {
id
dataType
name
}
... on ProjectV2IterationField {
id
name
dataType
configuration {
iterations {
startDate
id
}
}
}
... on ProjectV2SingleSelectField {
id
name
dataType
options {
id
name
}
}
}
}
}
}
}`,
{
projectId
}
)
const fieldNodes = projectFields.node.fields.nodes
for (const key of Object.keys(fieldsMap)) {
let value: any = fieldsMap[key]
const field = fieldNodes.find(node => key === node.name)
if (!field) {
core.info(`Failed to find field with name '${key}'.`)
continue
}
if (field.options) {
let option
if (value.startsWith('[') && value.endsWith(']')) {
const index = parseInt(value.slice(1, -1))
if (!isNaN(index)) {
option = field.options[index]
}
} else {
option = field.options.find(o => o.name.toLowerCase() === value.toLowerCase())
}
if (option) {
value = option.id
}
} else if (field.configuration?.iterations) {
let iteration
if (value.startsWith('[') && value.endsWith(']')) {
const index = parseInt(value.slice(1, -1))
if (!isNaN(index)) {
iteration = field.configuration.iterations[index]
}
} else {
iteration = field.configuration.iterations.find(i => i.startDate.toLowerCase() === value.toLowerCase())
}
if (iteration) {
value = iteration.id
}
}
const updateFieldKey = getUpdateFieldValueKey(field.dataType)
if (updateFieldKey === 'number') {
value = parseFloat(value)
}
try {
const updatedItem: ProjectUpdateFieldItemResponse = await octokit.graphql<ProjectUpdateFieldItemResponse>(
`mutation updateProjectV2ItemFieldValue($input: UpdateProjectV2ItemFieldValueInput!) {
updateProjectV2ItemFieldValue(input: $input) {
projectV2Item {
id
}
}
}`,
{
input: {
projectId,
itemId,
fieldId: field.id,
value: {
[updateFieldKey]: value
}
}
}
)
core.info(
`Successfully updated field '${key}' with value '${value}' for item: ${updatedItem.updateProjectV2ItemFieldValue.projectV2Item.id}.`
)
} catch (err) {
core.info(`Failed to update field '${key}' with value '${value}'. ${JSON.stringify(err, null, 2)}`)
}
}
}
}
export function mustGetOwnerTypeQuery(ownerType?: string): 'organization' | 'user' {
const ownerTypeQuery = ownerType === 'orgs' ? 'organization' : ownerType === 'users' ? 'user' : null
if (!ownerTypeQuery) {
throw new Error(`Unsupported ownerType: ${ownerType}. Must be one of 'orgs' or 'users'`)
}
return ownerTypeQuery
}
export function getUpdateFieldValueKey(
fieldDataType: string
): 'text' | 'number' | 'date' | 'singleSelectOptionId' | 'iterationId' {
if (fieldDataType === 'TEXT') {
return 'text'
} else if (fieldDataType === 'NUMBER') {
return 'number'
} else if (fieldDataType === 'DATE') {
return 'date'
} else if (fieldDataType === 'ITERATION') {
return 'iterationId'
} else if (fieldDataType === 'SINGLE_SELECT') {
return 'singleSelectOptionId'
} else {
throw new Error(
`Unsupported dataType: ${fieldDataType}. Must be one of 'text', 'number', 'date', 'singleSelectOptionId', 'iterationId'`
)
}
}