-
Notifications
You must be signed in to change notification settings - Fork 0
209 lines (182 loc) · 7.9 KB
/
sync-status-across-projects.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
name: Sync Status, Labels, and State Across Projects
on:
issues:
types: [edited, labeled, unlabeled, closed, reopened]
jobs:
sync-status-labels-state:
runs-on: ubuntu-latest
steps:
- name: Sync Issue Fields
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GH_TOKEN }}
script: |
const orgName = process.env.GITHUB_REPOSITORY_OWNER;
const sourceProjectNumber = 1314;
const sourceProjectId = 'PVT_kwDOAFK5-84Ak8oz';
const targetProjectNumber = 1514;
const targetProjectId = 'PVT_kwDOAFK5-84Ar1s4';
// Function to fetch fields for a project
const fetchProjectFields = async (projectNumber, orgName) => {
const query = `
query($orgName: String!, $projectNumber: Int!) {
organization(login: $orgName) {
projectV2(number: $projectNumber) {
id
fields(first: 20) {
nodes {
... on ProjectV2SingleSelectField {
id
name
options {
id
name
}
}
}
}
}
}
}
`;
console.log("Running GraphQL Query with orgName:", orgName, "and projectNumber:", projectNumber);
try {
const response = await github.graphql(query, {
orgName: orgName,
projectNumber: projectNumber
});
console.log("GraphQL Response:", response);
if (!response) {
console.log("No response received from GitHub GraphQL API.");
return null;
}
return response;
} catch (error) {
console.error("Error executing GraphQL query:", error);
return null;
}
};
// Fetch fields for the source project
const sourceProjectData = await fetchProjectFields(sourceProjectNumber, orgName);
console.log("Source Project Fields:", sourceProjectData);
// Fetch fields for the target project
const targetProjectData = await fetchProjectFields(targetProjectNumber, orgName);
console.log("Target Project Fields:", targetProjectData);
// Find the 'Status' field ID from the source project
const statusField = sourceProjectData.organization.projectV2.fields.nodes.find(field => field.name === 'Status');
if (!statusField) {
console.log("No 'Status' field found in source project.");
return;
}
const statusFieldId = statusField.id;
console.log(`Found 'Status' field ID in source project: ${statusFieldId}`);
// Fetch the current issue data (including labels, status, and state)
const issueNodeId = context.payload.issue.node_id;
const labels = context.payload.issue.labels.map(label => label.name);
const issueState = context.payload.issue.state;
// Fetch the status field value for the current issue in the source project
const data = await github.graphql(`
query($issueId: ID!) {
node(id: $issueId) {
... on Issue {
projectItems(first: 10) {
nodes {
project {
id
fields(first: 10) {
nodes {
... on ProjectV2SingleSelectField {
id
name
options {
id
name
}
}
}
}
}
fieldValues(first: 10) {
nodes {
... on ProjectV2ItemFieldSingleSelectValue {
id
name
}
}
}
}
}
}
}
}
`, {
issueId: issueNodeId
});
console.log("Issue data", data)
const projectItems = data.node.projectItems.nodes;
// Find the "Status" field and its options
const statusFieldOptions = projectItems
.map(item => item.project.fields.nodes.find(field => field.name === 'Status')) // Find "Status" field in each project
.find(Boolean);
if (statusFieldOptions) {
// Find the corresponding field value for "Status"
const currentStatus = projectItems
.flatMap(item => item.fieldValues.nodes)
.find(fieldValue => statusFieldOptions.options.some(option => option.name === fieldValue.name))?.name; // Find matching option by name
if (currentStatus) {
console.log(`Current status for the issue: ${currentStatus}`);
} else {
console.log("No matching status found for the issue.");
}
} else {
console.log("No 'Status' field found in the project.");
}
// Now sync the status field, labels, and state in the target project
// Sync Status Field
await github.graphql(`
mutation($projectId: ID!, $contentId: ID!, $status: String!, $statusFieldId: ID!) {
updateProjectV2ItemFieldValue(input: {
projectId: $projectId,
itemId: $contentId,
fieldId: $statusFieldId,
value: $status
}) {
projectV2Item {
id
}
}
}
`, {
projectId: targetProjectId,
contentId: issueNodeId,
status: currentStatus,
statusFieldId: statusFieldId
});
console.log(`Status synced successfully to target project for issue ID: ${issueNodeId}`);
// Sync Labels
await github.issues.addLabels({
owner: orgName,
repo: context.payload.repository.name,
issue_number: context.payload.issue.number,
labels: labels
});
console.log(`Labels synced successfully to target project for issue ID: ${issueNodeId}`);
// Sync Issue State (Closed/Reopened)
if (issueState === 'closed') {
await github.issues.update({
owner: orgName,
repo: context.payload.repository.name,
issue_number: context.payload.issue.number,
state: 'closed'
});
console.log(`Issue state updated to 'closed' for issue ID: ${issueNodeId}`);
} else if (issueState === 'reopened') {
await github.issues.update({
owner: orgName,
repo: context.payload.repository.name,
issue_number: context.payload.issue.number,
state: 'open'
});
console.log(`Issue state updated to 'open' for issue ID: ${issueNodeId}`);
}
console.log(`Issue state synced successfully to target project for issue ID: ${issueNodeId}`);