-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
48 lines (42 loc) · 1.71 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
'use strict';
const core = require('@actions/core'),
github = require('@actions/github'),
board = core.getInput('project'),
ignoredColumns = (core.getInput('ignoredColumns') || '').split(',').map((columnName) => columnName.trim()),
token = core.getInput('token'),
octokit = github.getOctokit(token);
if(!github.context.payload.issue) {
core.info('Not running after an issue was changed.');
return;
}
async function findBoardId(name) {
const boards = await octokit.paginate(octokit.rest.projects.listForRepo.endpoint.merge(github.context.repo));
return boards.find((project) => project.name === name)?.id;
}
async function forEachCard(projectId, callback, ignoredColumns = []) {
const columns = await octokit.paginate(octokit.rest.projects.listColumns.endpoint.merge({ project_id: projectId })),
managedColumns = columns.filter((column) => !ignoredColumns.includes(column.name));
for(const column of managedColumns) {
const cards = await octokit.paginate(octokit.rest.projects.listCards.endpoint.merge({ column_id: column.id }));
for(const card of cards) {
await callback(card);
}
}
}
async function doStuff() {
const boardId = await findBoardId(board);
if(!boardId) {
core.info(`No project with name "${board}" found.`);
return;
}
forEachCard(boardId, async (card) => {
const issueId = /\/(?:issue|pull-request)s\/(\d+)$/.exec(card.content_url);
if(issueId?.[1] == github.context.payload.issue.number) {
await octokit.rest.projects.deleteCard({ card_id: card.id });
}
}, ignoredColumns);
}
doStuff().catch((error) => {
console.error(error);
core.setFailed(error.message);
});