Skip to content

Commit 3ec05f7

Browse files
committed
Refactor GitHub Actions workflow to use create-github-app-token action and improve logging for issue processing
1 parent b7272eb commit 3ec05f7

File tree

1 file changed

+82
-21
lines changed

1 file changed

+82
-21
lines changed

.github/workflows/add-to-backlog-project.yml

Lines changed: 82 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@ jobs:
1717
uses: actions/checkout@v2
1818

1919
- name: Authenticate with GitHub App
20+
uses: actions/create-github-app-token@v1
2021
id: auth
21-
uses: tibdex/github-app-token@v1
2222
with:
23-
app_id: ${{ secrets.APP_ID }}
24-
private_key: ${{ secrets.PRIVATE_KEY }}
25-
installation_id: ${{ secrets.INSTALLATION_ID }}
23+
app-id: ${{ secrets.APP_ID }}
24+
private-key: ${{ secrets.PRIVATE_KEY }}
2625

2726
- name: Move Issues from Refinement Board (in Status "Done") to Project Backlog (with Status "ToDo")
2827
uses: actions/github-script@v7
@@ -37,7 +36,7 @@ jobs:
3736
const doneStatus = "6c6113eb"; // "Done" Status Option ID of Refinement Board
3837
const todoStatus = "f75ad846"; // "ToDo Status Option ID of Backlog Project
3938
40-
// Fetch all issues in Refinement Board that are in "Done" status
39+
// Fetch all issues in Refinement Board
4140
const query = `
4241
query {
4342
node(id: "${projectP1}") {
@@ -50,6 +49,7 @@ jobs:
5049
... on Issue {
5150
id
5251
number
52+
title
5353
}
5454
}
5555
fieldValues(first: 10) {
@@ -80,26 +80,41 @@ jobs:
8080
8181
const result = await github.graphql(query);
8282
const items = result.node.items.nodes;
83-
84-
// Filter only issues with "Done" status
85-
const doneItems = items.filter(item =>
86-
item.fieldValues.nodes.some(f => f.__typename === "ProjectV2ItemFieldSingleSelectValue" &&
87-
f.field.id === statusFieldIdP1 &&
88-
f.optionId === doneStatus)
89-
);
83+
const doneItems = [];
84+
85+
// log the items structure for debugging
86+
// console.log("items structure: ", JSON.stringify(items, null, 2));
87+
88+
// log the field values structure for debugging
89+
items.forEach(item => {
90+
// console.log(JSON.stringify(item.fieldValues.nodes, null, 2));
91+
item.fieldValues.nodes.forEach(field => {
92+
if (field.__typename === "ProjectV2ItemFieldSingleSelectValue") {
93+
if (field.optionId === doneStatus) {
94+
console.log(`Issue ${item.content.title} (#${item.content.number}) is in "Done" status -> should be added to Backlog.`);
95+
doneItems.push(item);
96+
}
97+
}
98+
});
99+
});
90100
91101
if (doneItems.length === 0) {
92102
console.log("No issues in 'Done' status, nothing to do.");
93103
return;
94104
}
95105
106+
// Main Loop over all items in "Done" status
107+
// 1. Adds issue to Project Backlog
108+
// 2. Sets status to "To Do" in Project Backlog
109+
// 3. Removes issue from Refinement Board
96110
for (const item of doneItems) {
97111
if (!item.content || !item.content.number) continue;
98112
99-
const issueNumber = item.content.number;
100-
console.log(`Adding issue #${issueNumber} to Project Backlog ...`);
101-
102-
// Add issue to Project Backlog
113+
let issueNumber = item.content.number;
114+
let issueTitle = item.content.title;
115+
console.log(`**** Start Processing Issue ${issueTitle} (#${issueNumber}) ****`);
116+
117+
// 1. Add issue to Project Backlog
103118
const addToProjectMutation = `
104119
mutation {
105120
addProjectV2ItemById(input: {
@@ -113,10 +128,22 @@ jobs:
113128
}
114129
`;
115130
116-
const addToProjectResult = await github.graphql(addToProjectMutation);
117-
const newProjectItemId = addToProjectResult.addProjectV2ItemById.item.id;
131+
let addToProjectResult;
132+
try {
133+
addToProjectResult = await github.graphql(addToProjectMutation);
118134
119-
// Set status to "To Do" in Backlog Project
135+
if (addToProjectResult && addToProjectResult.addProjectV2ItemById.item.id) {
136+
console.log(`Issue ${issueTitle} (#${issueNumber}) successfully added to backlog project.`);
137+
} else {
138+
console.error("Failed to add issue ${issueTitle} (#${issueNumber}) to backlog project.");
139+
}
140+
} catch (error) {
141+
console.error("Error in add to Backlog Project GraphQL mutation:", error);
142+
}
143+
144+
let newProjectItemId = addToProjectResult.addProjectV2ItemById.item.id;
145+
146+
// 2. Set status to "To Do" in Backlog Project
120147
const updateStatusMutation = `
121148
mutation {
122149
updateProjectV2ItemFieldValue(input: {
@@ -132,7 +159,41 @@ jobs:
132159
}
133160
`;
134161
135-
await github.graphql(updateStatusMutation);
162+
let updateStatusResult;
163+
try {
164+
updateStatusResult = await github.graphql(updateStatusMutation);
165+
if (updateStatusResult && updateStatusResult.updateProjectV2ItemFieldValue.projectV2Item.id) {
166+
console.log(`Changed Status to TODO for Issue ${issueTitle} (#${issueNumber}).`);
167+
} else {
168+
console.error("Failed to change status to TODO for Issue ${issueTitle} (#${issueNumber}).");
169+
}
170+
} catch (error) {
171+
console.error("Error in Change Status GraphQL mutation:", error);
172+
}
173+
174+
// 3. Remove issue from Refinement Board
175+
const deleteMutation = `
176+
mutation {
177+
deleteProjectV2Item(input: {
178+
projectId: "${projectP1}",
179+
itemId: "${item.id}"
180+
}) {
181+
deletedItemId
182+
}
183+
}
184+
`;
185+
186+
let deleteResult;
187+
try {
188+
deleteResult = await github.graphql(deleteMutation);
189+
if (deleteResult && deleteResult.deleteProjectV2Item.deletedItemId) {
190+
console.log(`Issue ${issueTitle} (#${issueNumber}) successfully removed from refinement project.`);
191+
} else {
192+
console.error("Failed to remove issue ${issueTitle} (#${issueNumber}) from refinement project.");
193+
}
194+
} catch (error) {
195+
console.error("Error in delete from refinement project GraphQL mutation:", error);
196+
}
136197
137-
console.log(`Issue #${issueNumber} added to Project Backlog with status "To Do".`);
198+
console.log(`**** Finished processing Issue ${issueTitle} (#${issueNumber}) ****`);
138199
}

0 commit comments

Comments
 (0)