forked from replicatedhq/kots
-
Notifications
You must be signed in to change notification settings - Fork 0
103 lines (84 loc) · 3.86 KB
/
archive-old-cards.yaml
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
---
on:
workflow_dispatch:
schedule:
- cron: "0 5 * * *"
name: Archive old cards
jobs:
archive-old-cards:
name: Move closed issues to a "done" or "closed" column
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@v6
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
const owner = context.repo.owner;
const repo = context.repo.repo;
const maxOperations = 150;
// fetch all projects
const projects = await github.rest.projects.listForRepo({ owner, repo });
console.log(`got ${projects.data.length} projects`);
// projects.map(fetch all columns)
const columns = await Promise.all(
projects.data.map(async project => {
const resp = await github.rest.projects.listColumns({
project_id: project.id,
});
const doneColumnForProject = resp.data.filter(column =>
column.name == "Closed" ||
column.name == "Done" ||
column.name == "Successful"
)[0];
console.log(project.name, doneColumnForProject.name, doneColumnForProject.id);
return resp.data.map(column => {
column.projectName = project.name;
// attach the "done" column for the project to each column in
// the project so we can move cards to it
column.doneColumnForProject = doneColumnForProject
return column;
});
})
);
const allColumns = columns.flat();
console.log(`got ${allColumns.length} columns`);
// columns.map(fetch all cards)
const cards = await Promise.all(
allColumns.map(async column => {
const cards = await github.rest.projects.listCards({
column_id: column.id,
per_page: maxOperations,
});
return cards.data.map(card => {
card.projectName = column.projectName;
card.columnName = column.name;
card.doneColumnForProject = column.doneColumnForProject;
return card;
});
})
);
const allCards = cards.flat();
console.log(`got ${allCards.length} cards`);
console.log(`processing ${Math.min(maxOperations, allCards.length)} cards`);
// for each card, fetch linked issue. If the issue is closed, move the card to the done column.
let moveCounter = 0;
for (const card of allCards.slice(0,maxOperations)) {
const issue = (await github.request(card.content_url)).data;
console.log(card.id, issue.number, issue.state, card.columnName);
if (issue.state === "open") {
console.log(`SKIP open: ${issue.number} in ${card.projectName} | ${card.columnName} | ${issue.title} | ${issue.html_url}`);
continue;
}
if (card.columnName === card.doneColumnForProject.name) {
console.log(`SKIP already in done column: ${issue.number} in ${card.projectName} | ${card.columnName} | ${issue.title} | ${issue.html_url}`);
continue;
}
console.log(`MOVE card for closed issue ${issue.number} in ${card.projectName} | ${card.columnName} --> ${card.doneColumnForProject.name} | ${issue.title} | ${issue.html_url}`);
await github.rest.projects.moveCard({
position: "top",
card_id: card.id,
column_id: card.doneColumnForProject.id
});
moveCounter += 1;
}
console.log(`Moved ${moveCounter}/${allCards.length} cards`);