forked from 8398a7/action-slack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fields.ts
237 lines (206 loc) · 6.69 KB
/
fields.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import { context } from '@actions/github';
import { Octokit } from './client';
export interface Field {
title: string;
value: string;
short: boolean;
}
export class FieldFactory {
private octokit: Octokit;
private fields: string[];
private jobName: string;
private gitHubBaseUrl: string;
constructor(
fields: string,
jobName: string,
gitHubBaseUrl: string,
octokit: Octokit,
) {
this.fields = fields.replace(/ /g, '').split(',');
this.jobName = jobName;
this.octokit = octokit;
this.gitHubBaseUrl =
gitHubBaseUrl === '' ? 'https://github.com' : gitHubBaseUrl;
}
includes(field: string) {
return this.fields.includes(field) || this.fields.includes('all');
}
filterField<T extends Array<Field | undefined>, D extends undefined>(
array: T,
diff: D,
) {
return array.filter(item => item !== diff) as Exclude<
T extends { [K in keyof T]: infer U } ? U : never,
D
>[];
}
async attachments(): Promise<Field[]> {
return this.filterField(
[
this.includes('repo')
? createAttachment('repo', await this.repo())
: undefined,
this.includes('message')
? createAttachment('message', await this.message())
: undefined,
this.includes('commit')
? createAttachment('commit', await this.commit())
: undefined,
this.includes('author')
? createAttachment('author', await this.author())
: undefined,
this.includes('action')
? createAttachment('action', await this.action())
: undefined,
this.includes('job')
? createAttachment('job', await this.job())
: undefined,
this.includes('took')
? createAttachment('took', await this.took())
: undefined,
this.includes('eventName')
? createAttachment('eventName', await this.eventName())
: undefined,
this.includes('ref')
? createAttachment('ref', await this.ref())
: undefined,
this.includes('workflow')
? createAttachment('workflow', await this.workflow())
: undefined,
this.includes('pullRequest')
? createAttachment('pullRequest', await this.pullRequest())
: undefined,
],
undefined,
);
}
private async message(): Promise<string> {
const resp = await this.getCommit(this.octokit);
const value = `<${resp.data.html_url}|${resp.data.commit.message
.split('\n')[0]
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')}>`;
process.env.AS_MESSAGE = value;
return value;
}
private async author(): Promise<string> {
const resp = await this.getCommit(this.octokit);
const author = resp.data.commit.author;
const value = `${author?.name}<${author?.email}>`;
process.env.AS_AUTHOR = value;
return value;
}
private async took(): Promise<string> {
const resp = await this.octokit?.rest.actions.listJobsForWorkflowRun({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: context.runId,
});
const currentJob = resp?.data.jobs.find(job => job.name === this.jobName);
if (currentJob === undefined) {
process.env.AS_TOOK = this.jobIsNotFound;
return this.jobIsNotFound;
}
let time = new Date().getTime() - new Date(currentJob.started_at).getTime();
const h = Math.floor(time / (1000 * 60 * 60));
time -= h * 1000 * 60 * 60;
const m = Math.floor(time / (1000 * 60));
time -= m * 1000 * 60;
const s = Math.floor(time / 1000);
let value = '';
if (h > 0) {
value += `${h} hour `;
}
if (m > 0) {
value += `${m} min `;
}
if (s > 0) {
value += `${s} sec`;
}
process.env.AS_TOOK = value;
return value;
}
private async job(): Promise<string> {
const { owner } = context.repo;
const resp = await this.octokit?.rest.actions.listJobsForWorkflowRun({
owner,
repo: context.repo.repo,
run_id: context.runId,
});
const currentJob = resp?.data.jobs.find(job => job.name === this.jobName);
if (currentJob === undefined) {
process.env.AS_JOB = this.jobIsNotFound;
return this.jobIsNotFound;
}
const jobId = currentJob.id;
const value = `<${this.gitHubBaseUrl}/${owner}/${context.repo.repo}/runs/${jobId}|${this.jobName}>`;
process.env.AS_JOB = value;
return value;
}
private async commit(): Promise<string> {
const { sha } = context;
const { owner, repo } = context.repo;
const value = `<${
this.gitHubBaseUrl
}/${owner}/${repo}/commit/${sha}|${sha.slice(0, 8)}>`;
process.env.AS_COMMIT = value;
return value;
}
private async repo(): Promise<string> {
const { owner, repo } = context.repo;
const value = `<${this.gitHubBaseUrl}/${owner}/${repo}|${owner}/${repo}>`;
process.env.AS_REPO = value;
return value;
}
private async eventName(): Promise<string> {
const value = context.eventName;
process.env.AS_EVENT_NAME = value;
return value;
}
private async ref(): Promise<string> {
const value = context.ref;
process.env.AS_REF = value;
return value;
}
private async workflow(): Promise<string> {
const sha = context.payload.pull_request?.head.sha ?? context.sha;
const { owner, repo } = context.repo;
const value = `<${this.gitHubBaseUrl}/${owner}/${repo}/commit/${sha}/checks|${context.workflow}>`;
process.env.AS_WORKFLOW = value;
return value;
}
private async pullRequest(): Promise<string> {
let value;
if (context.eventName.startsWith('pull_request')) {
value = `<${context.payload.pull_request?.html_url}|${context.payload.pull_request?.title} #${context.payload.pull_request?.number}>`;
} else {
value = 'n/a';
}
process.env.AS_PULL_REQUEST = value;
return value;
}
private async action(): Promise<string> {
const sha = context.payload.pull_request?.head.sha ?? context.sha;
const { owner, repo } = context.repo;
const value = `<${this.gitHubBaseUrl}/${owner}/${repo}/commit/${sha}/checks|action>`;
process.env.AS_ACTION = value;
return value;
}
private async getCommit(octokit: Octokit) {
const { owner, repo } = context.repo;
const { sha: ref } = context;
return await octokit.rest.repos.getCommit({ owner, repo, ref });
}
private get jobIsNotFound() {
return 'Job is not found.\nCheck <https://action-slack.netlify.app/fields|the matrix> or <https://action-slack.netlify.app/with#job_name|job name>.';
}
}
function createAttachment(
title: string,
value: string,
short?: boolean,
): Field | undefined {
if (short === undefined) short = true;
return { title, value, short };
}