-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathworkflowHandler.ts
171 lines (140 loc) · 4.88 KB
/
workflowHandler.ts
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import * as core from '@actions/core';
import * as github from '@actions/github';
type OctokitProp = ReturnType<typeof github.getOctokit>;
async function wait(milliseconds: number): Promise<string> {
return new Promise((resolve) => setTimeout(resolve, milliseconds));
}
function getIssueCommentBody(releaseTag: string) {
const {
repo: { repo, owner },
} = github.context;
const url = `https://github.com/${owner}/${repo}/releases/tag/${releaseTag}`;
return `<!-- disable_global_notification -->✅ <a href="${url}" target="_blank">${releaseTag}</a> 🎉`;
}
const COMMENT_WAIT_INTERVAL_MS = 1500;
// Исключаем задачи, которые были закрыты со статусом "not_planned (won't fix)"
const IGNORED_STATE = 'not_planned';
export class WorkflowHandler {
private readonly gh: OctokitProp;
private readonly releaseTag: string;
private error = false;
public constructor(token: string, releaseTag: string) {
this.gh = github.getOctokit(token);
this.releaseTag = releaseTag.startsWith('v') ? releaseTag : `v${releaseTag}`;
}
public async processMilestone() {
try {
const milestoneNumber = await this.findMilestoneNumberByReleaseTag();
const issueNumbers = await this.getIssueNumbersByMilestone(milestoneNumber);
await this.commentOnIssues(issueNumbers);
await this.closeMilestone(milestoneNumber);
} catch (error) {
if (error instanceof Error) {
core.error(error.message);
}
this.error = true;
}
}
public async processIssuesByTagLabel() {
try {
const issueNumbers = await this.getIssueNumbersByTagLabel();
await this.commentOnIssues(issueNumbers);
} catch (error) {
if (error instanceof Error) {
core.error(error.message);
}
this.error = true;
}
}
public async processReleaseNotes(latest: boolean) {
try {
const releaseNotes = await this.findReleaseNotes();
if (!releaseNotes) {
throw new Error(`There are no release notes for ${this.releaseTag}`);
}
await this.publishReleaseNotes(releaseNotes.id, latest);
} catch (error) {
if (error instanceof Error) {
core.error(error.message);
}
this.error = true;
}
}
public isProcessWithError() {
return this.error;
}
private async findReleaseNotes() {
const { data: releases } = await this.gh.rest.repos.listReleases({
...github.context.repo,
});
return releases.find(({ draft, name }) => draft && name === this.releaseTag);
}
private async publishReleaseNotes(release_id: number, latest: boolean) {
await this.gh.rest.repos.updateRelease({
...github.context.repo,
tag_name: this.releaseTag,
release_id,
draft: false,
prerelease: this.releaseTag.includes('-'),
make_latest: latest ? 'true' : 'false',
});
}
private async findMilestoneNumberByReleaseTag() {
const { data: milestones } = await this.gh.rest.issues.listMilestones({
...github.context.repo,
state: 'open',
});
const milestone = milestones.find(({ title }) => title === this.releaseTag);
if (milestone) {
return milestone.number;
}
throw new Error(`There is no milestone for tag ${this.releaseTag}`);
}
private async getIssueNumbersByMilestone(milestoneNumber: number) {
const issues = await this.gh.paginate(this.gh.rest.issues.listForRepo, {
...github.context.repo,
milestone: `${milestoneNumber}`,
state: 'all',
});
return issues.reduce<number[]>((issueNumbers, issue) => {
if (issue.state_reason !== IGNORED_STATE && !issue.locked) {
issueNumbers.push(issue.number);
}
return issueNumbers;
}, []);
}
private async getIssueNumbersByTagLabel() {
const issues = await this.gh.paginate(this.gh.rest.issues.listForRepo, {
...github.context.repo,
state: 'all',
labels: this.releaseTag,
});
return issues.reduce<number[]>((issueNumbers, issue) => {
if (issue.state_reason !== IGNORED_STATE && !issue.locked) {
issueNumbers.push(issue.number);
}
return issueNumbers;
}, []);
}
private async commentOnIssues(issueNumbers: number[]) {
core.debug(`Processing the following linked issues: [${issueNumbers}]`);
const body = getIssueCommentBody(this.releaseTag);
for (let issue_number of issueNumbers) {
await this.gh.rest.issues.createComment({
...github.context.repo,
issue_number,
body,
});
// https://docs.github.com/en/rest/guides/best-practices-for-integrators#dealing-with-secondary-rate-limits
await wait(COMMENT_WAIT_INTERVAL_MS);
}
}
private async closeMilestone(milestone_number: number) {
core.debug(`Closing ${milestone_number} milestone`);
await this.gh.rest.issues.updateMilestone({
...github.context.repo,
milestone_number,
state: 'closed',
});
}
}