-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync-subtask.js
More file actions
162 lines (137 loc) · 5.32 KB
/
sync-subtask.js
File metadata and controls
162 lines (137 loc) · 5.32 KB
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
module.exports = async ({ github, context, core }) => {
const owner = context.repo.owner;
const repo = context.repo.repo;
const STATUS_FIELD_NAME = "Status";
const actualProjectId = "PVT_kwDOA5i8as4BC9Vt";
async function listProjectIssues(projectId = actualProjectId) {
let hasNextPage = true;
let after = null;
while (hasNextPage) {
const q = `
query ($after:String) {
node(id:"PVT_kwDOA5i8as4BC9Vt") {
... on ProjectV2 {
items(first:100, after:$after) {
nodes {
id
content {
... on Issue {
number
title
issueType {
name
}
subIssues(first:100) {
nodes {
id
number
}
}
}
}
fieldValueByName(name:"Status") {
... on ProjectV2ItemFieldSingleSelectValue {
name
}
}
}
}
}
}
}`;
const res = await github.graphql(q, { projectId, after });
const items = res.node?.items?.nodes || [];
for (const it of items) {
content = it.content;
if (!content) continue;
core.info(`issue ${content}`)
if (content.issueType?.name == "Epic") {
parentStatus = it.fieldValueByName?.name
core.info(`is Epic with status ${parentStatus}`)
}
}
const pageInfo = res.node?.items?.pageInfo;
hasNextPage = pageInfo?.hasNextPage || false;
after = pageInfo?.endCursor || null;
}
return results;
}
async function getStatusFieldInfo() {
const q = `
query($projectId: ID!, $fieldName: String!) {
node(id: $projectId) {
... on ProjectV2 {
field(name: $fieldName) {
... on ProjectV2SingleSelectField {
id
options { id name }
}
}
}
}
}`;
const res = await github.graphql(q, { projectId: actualProjectId, fieldName: STATUS_FIELD_NAME });
return res.node.field;
}
async function updateIssueStatus(issueNumber, newStatus) {
const fieldInfo = await getStatusFieldInfo();
const statusOption = fieldInfo.options.find(o => o.name === newStatus);
if (!statusOption) {
core.warning(`Status "${newStatus}" not found in project`);
return;
}
const qItem = `
query($owner:String!, $repo:String!, $number:Int!, $projectId:ID!) {
repository(owner:$owner, name:$repo) {
issue(number:$number) {
projectItems(first:10) {
nodes { id project { id } }
}
}
}
}`;
const rItem = await github.graphql(qItem, { owner, repo, number: issueNumber, projectId: actualProjectId });
const items = rItem.repository.issue.projectItems.nodes.filter(n => n.project.id === actualProjectId);
if (!items.length) {
core.warning(`Issue #${issueNumber} not in project ${actualProjectId}`);
return;
}
const itemId = items[0].id;
const mutation = `
mutation($projectId:ID!, $itemId:ID!, $fieldId:ID!, $value:String!) {
updateProjectV2ItemFieldValue(input: {
projectId: $projectId
itemId: $itemId
fieldId: $fieldId
value: { singleSelectOptionId: $value }
}) {
projectV2Item { id }
}
}`;
await github.graphql(mutation, {
projectId: actualProjectId,
itemId: itemId,
fieldId: fieldInfo.id,
value: statusOption.id
});
core.info(`Updated issue #${issueNumber} to status "${newStatus}"`);
}
async function syncSubIssuesStatus(issueNumber) {
const epicStatus = await getIssueStatus(issueNumber);
const subIssues = await listSubIssues(issueNumber);
core.info(`Epic status: ${epicStatus}, found ${subIssues.length} sub-issues`);
for (const subIssue of subIssues) {
const subIssueStatus = await getIssueStatus(subIssue.number);
if (epicStatus === "Backlog") {
await updateIssueStatus(subIssue.number, "Backlog");
} else if (epicStatus === "Done") {
await updateIssueStatus(subIssue.number, "Done");
} else if (subIssueStatus === "Backlog") {
await updateIssueStatus(subIssue.number, "Todo");
}
}
return;
}
core.info(`Processing`);
await listProjectIssues();
};