-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
135 lines (114 loc) · 4.79 KB
/
index.js
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
const core = require('@actions/core');
const github = require('@actions/github');
const { graphql } = require("@octokit/graphql");
const isBefore = require('date-fns/isBefore')
const sub = require('date-fns/sub')
const fetchCardsQuery = `query projectCards($owner: String!, $repo: String!, $projectName: String!, $cursor: String!) {
repository(owner: $owner, name: $repo) {
projects(search: $projectName, last: 1) {
edges {
node {
columns(first: 20) {
edges {
node {
name
cards(first: 50, after: $cursor, archivedStates: NOT_ARCHIVED) {
edges {
node {
id
updatedAt
}
cursor
}
pageInfo {
endCursor
hasNextPage
}
}
}
}
}
}
}
}
}
}`
const archiveCardQuery = `mutation archiveCards($cardId: String!, $isArchived: Boolean = true) {
updateProjectCard(input:{projectCardId: $cardId, isArchived: $isArchived}) {
projectCard {
id
}
}
}`
async function fetchCards(repoOwner, repo, projectName, currentCursor, accessToken) {
return graphql(fetchCardsQuery, {
owner: repoOwner,
repo: repo,
projectName: projectName,
cursor: currentCursor,
headers: {
authorization: `bearer ${accessToken}`,
}
})
}
const dateifyCard = (card) => {
const updatedAt = new Date(card.updatedAt)
return { id: card.id, updatedAt: updatedAt }
}
async function fetchCardInfo(repoOwner, repo, projectName, accessToken, columnToArchive) {
try {
const projectCardIdsWithDate = []
let currentCursor = ''
let nextPage = true
while(nextPage) {
let projectCards = await fetchCards(repoOwner, repo, projectName, currentCursor, accessToken)
projectCards = projectCards.repository.projects.edges[0].node.columns.edges.find(edge => edge.node.name.toLowerCase() === columnToArchive.toLowerCase()).node.cards
projectCardIdsWithDate.push(...projectCards.edges.flatMap(card => card.node).map(dateifyCard))
currentCursor = projectCards.pageInfo.endCursor
nextPage = projectCards.pageInfo.hasNextPage
}
return projectCardIdsWithDate
}
catch(e) {
console.log('fetchCardInfo error: ', e)
return []
}
}
const run = async () => {
try {
const accessToken = core.getInput('access-token')
const columnToArchive = core.getInput('column-to-archive')
const repoOwner = core.getInput('repository-owner')
const repo = core.getInput('repository')
const projectName = core.getInput('project-name')
const payload = JSON.stringify(github.context.payload, undefined, 2)
const daysOld = core.getInput('days-old');
const cutoffDate = sub(new Date(), { days: daysOld })
console.log(`Archiving all cards that have been untouched for ${daysOld} days from column ${columnToArchive}!`);
console.log(`The event payload: ${payload}`);
const projectCardIdsWithDate = await fetchCardInfo(repoOwner, repo, projectName, accessToken, columnToArchive)
// Filter by updated at date
const cardIdsToArchive = projectCardIdsWithDate
.filter(card => isBefore(card.updatedAt, cutoffDate))
.map(node => node.id)
// Archive those - https://docs.github.com/en/free-pro-team@latest/rest/reference/projects#update-an-existing-project-card
console.log(`Archiving ${cardIdsToArchive.length} cards`)
cardIdsToArchive.forEach(async (id) => {
try {
await graphql(archiveCardQuery, {
cardId: id,
headers: {
authorization: `bearer ${accessToken}`,
},
})
}
catch(e) {
console.log('archiveCard error: ', e)
return false
}
})
} catch (error) {
core.setFailed(error.message);
}
}
run()