-
Notifications
You must be signed in to change notification settings - Fork 0
184 lines (164 loc) · 6.49 KB
/
set-start-end-date-issue-status.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
name: Set Start and End Dates Based on Status
on:
issues:
types: [edited]
jobs:
update-date-fields:
runs-on: ubuntu-latest
steps:
- name: Update Start and End Date Fields
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GH_TOKEN }}
script: |
const projectId = 'PVT_kwDOAFK5-84Ak8oz';
let issueNodeId;
let statusFieldId;
let startDateFieldId;
let endDateFieldId;
const issueAction = context.payload.action;
if (issueAction === 'edited') {
console.log('The issue was edited!');
} else {
console.log(`The issue was not edited. It was ${issueAction}.`);
}
// Check if the issue was edited (i.e., status field was changed)
if (context.payload.issue) {
issueNodeId = context.payload.issue.node_id; // Issue update
} else {
console.log('This action was triggered without an issue.');
return;
}
// Step 1: Fetch Project Fields dynamically
const { data: projectData } = await github.graphql(`
query($projectId: ID!) {
node(id: $projectId) {
... on ProjectV2 {
fields(first: 20) {
nodes {
__typename
... on ProjectV2SingleSelectField {
id
name
options {
id
name
}
}
... on ProjectV2Field {
id
name
}
}
}
}
}
}
`, {
projectId: projectId
});
// Extract the necessary field IDs dynamically
const statusField = projectData.node.fields.nodes.find(field => field.name === 'Status');
const startDateField = projectData.node.fields.nodes.find(field => field.name === 'Start Date' && field.__typename === 'ProjectV2Field');
const endDateField = projectData.node.fields.nodes.find(field => field.name === 'End Date' && field.__typename === 'ProjectV2Field');
if (!statusField || !startDateField || !endDateField) {
console.log('One or more required fields (Status, Start Date, End Date) not found.');
return;
}
statusFieldId = statusField.id;
startDateFieldId = startDateField.id;
endDateFieldId = endDateField.id;
console.log(`Status Field ID: ${statusFieldId}`);
console.log(`Start Date Field ID: ${startDateFieldId}`);
console.log(`End Date Field ID: ${endDateFieldId}`);
// Step 2: Fetch the issue's current status from the projectV2Item
const { data: issueData } = await github.graphql(`
query($projectId: ID!, $issueId: ID!) {
node(id: $issueId) {
... on Issue {
projectV2Items(first: 1, projectId: $projectId) {
nodes {
fieldValues(first: 10) {
nodes {
projectV2Field {
id
name
}
value
}
}
}
}
}
}
}
`, {
projectId: projectId,
issueId: issueNodeId
});
// Check if data is returned
if (!issueData || !issueData.node || !issueData.node.projectV2Items.nodes[0]) {
console.log('No projectV2Item found for this issue.');
return;
}
const projectItem = issueData.node.projectV2Items.nodes[0];
// Find the Status field value
const statusFieldValue = projectItem.fieldValues.nodes.find(field => field.projectField.id === statusFieldId)?.value;
if (!statusFieldValue) {
console.log('Status field not found or not updated.');
return;
}
const now = new Date().toISOString(); // Current date and time in ISO format
console.log(`Issue status: ${statusFieldValue}`);
// Step 3: Update Start Date for "In Progress" status
if (statusFieldValue === 'In Progress') {
console.log(`Updating Start Date for issue ${issueNodeId}`);
await github.graphql(`
mutation($projectId: ID!, $itemId: ID!, $fieldId: ID!, $value: String!) {
updateProjectV2ItemFieldValue(input: {
projectId: $projectId,
itemId: $itemId,
fieldId: $fieldId,
value: {
date: $value
}
}) {
projectV2Item {
id
}
}
}
`, {
projectId: projectId,
itemId: issueNodeId,
fieldId: startDateFieldId,
value: now
});
console.log(`Start date set to ${now} for issue.`);
}
// Step 4: Update End Date for "Done" status
if (statusFieldValue === 'Done') {
console.log(`Updating End Date for issue ${issueNodeId}`);
await github.graphql(`
mutation($projectId: ID!, $itemId: ID!, $fieldId: ID!, $value: String!) {
updateProjectV2ItemFieldValue(input: {
projectId: $projectId,
itemId: $itemId,
fieldId: $fieldId,
value: {
date: $value
}
}) {
projectV2Item {
id
}
}
}
`, {
projectId: projectId,
itemId: issueNodeId,
fieldId: endDateFieldId,
value: now
});
console.log(`End date set to ${now} for issue.`);
}