-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
84 lines (67 loc) · 2.56 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
import * as core from '@actions/core';
import * as github from '@actions/github';
async function getReferencedEpics({ octokit }) {
const epicLabelName = core.getInput('epic-label-name', { required: true });
if (github.context.payload.action !== 'deleted') {
const events = await octokit.rest.issues.listEventsForTimeline({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
issue_number: github.context.payload.issue.number,
});
return events.data
.filter((item) => (item.event === 'cross-referenced' && item.source))
.filter((item) => item.source.issue.labels
.filter((label) => label.name.toLowerCase() === epicLabelName.toLowerCase()).length > 0);
}
return [];
}
async function updateEpic({ octokit, epic }) {
const autoCloseEpic = core.getInput('auto-close-epic', { required: false });
const issueNumber = github.context.payload.issue.number;
const issueState = github.context.payload.issue.state;
const convertedIssueState = issueState === 'closed' ? 'x' : ' ';
const epicNumber = epic.source.issue.number;
let epicState = epic.source.issue.state;
let epicBody = epic.source.issue.body;
const pattern = new RegExp(`- \\[[ |x]\\] .*#${issueNumber}.*`, 'gm');
const matches = Array.from(epicBody.matchAll(pattern));
const matchCount = matches.length;
matches.forEach((match) => {
epicBody = epicBody.replace(match[0], match[0].replace(/- \[[ |x]\]/, `- [${convertedIssueState}]`));
});
const allTasksPattern = /- \[[ |x]\] .*/gm;
const doneTasksPattern = /- \[[x]\] .*/gm;
const allTasks = Array.from(epicBody.matchAll(allTasksPattern));
const doneTasks = Array.from(epicBody.matchAll(doneTasksPattern));
const allTasksCount = allTasks.length;
const doneTasksCount = doneTasks.length;
if (!!autoCloseEpic
&& matchCount
&& allTasksCount === doneTasksCount
) {
epicState = 'closed';
}
const result = await octokit.rest.issues.update({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
issue_number: epicNumber,
body: epicBody,
state: epicState,
});
return result;
}
async function updateEpics({ octokit, epics }) {
return Promise.all(epics.map((epic) => updateEpic({ octokit, epic })));
}
async function run() {
try {
const token = core.getInput('github-token', { required: true });
const octokit = github.getOctokit(token);
const epics = await getReferencedEpics({ octokit });
await updateEpics({ octokit, epics });
} catch (error) {
core.error(error);
core.setFailed(error.message);
}
}
run();