-
Notifications
You must be signed in to change notification settings - Fork 285
Expand file tree
/
Copy pathcheckSuite.ts
More file actions
149 lines (126 loc) · 3.62 KB
/
checkSuite.ts
File metadata and controls
149 lines (126 loc) · 3.62 KB
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
import type { FC } from 'react';
import type { OcticonProps } from '@primer/octicons-react';
import {
CheckIcon,
RocketIcon,
SkipIcon,
StopIcon,
XIcon,
} from '@primer/octicons-react';
import {
type GitifyCheckSuiteStatus,
type GitifyNotification,
type GitifySubject,
IconColor,
type Link,
type SettingsState,
} from '../../../types';
import { DefaultHandler, defaultHandler } from './default';
import { actionsURL } from './utils';
export interface CheckSuiteAttributes {
workflowName: string;
attemptNumber?: number;
statusDisplayName: string;
status: GitifyCheckSuiteStatus | null;
branchName: string;
}
class CheckSuiteHandler extends DefaultHandler {
override async enrich(
notification: GitifyNotification,
_settings: SettingsState,
): Promise<Partial<GitifySubject>> {
const state = getCheckSuiteAttributes(notification)?.status;
if (state) {
return {
state: state,
user: null,
htmlUrl: getCheckSuiteUrl(notification),
};
}
return {};
}
override iconType(notification: GitifyNotification): FC<OcticonProps> {
switch (notification.subject.state as GitifyCheckSuiteStatus) {
case 'CANCELLED':
return StopIcon;
case 'FAILURE':
return XIcon;
case 'SKIPPED':
return SkipIcon;
case 'SUCCESS':
return CheckIcon;
default:
return RocketIcon;
}
}
override iconColor(notification: GitifyNotification): IconColor {
switch (notification.subject.state as GitifyCheckSuiteStatus) {
case 'SUCCESS':
return IconColor.GREEN;
case 'FAILURE':
return IconColor.RED;
default:
return defaultHandler.iconColor(notification);
}
}
override defaultUrl(notification: GitifyNotification): Link {
return getCheckSuiteUrl(notification);
}
}
export const checkSuiteHandler = new CheckSuiteHandler();
/**
* Ideally we would be using a GitHub API to fetch the CheckSuite / WorkflowRun state,
* but there isn't an obvious/clean way to do this currently.
*/
export function getCheckSuiteAttributes(
notification: GitifyNotification,
): CheckSuiteAttributes | null {
const regex =
/^(?<workflowName>.*?) workflow run(, Attempt #(?<attemptNumber>\d+))? (?<statusDisplayName>.*?) for (?<branchName>.*?) branch$/;
const match = regex.exec(notification.subject.title);
if (!match?.groups) {
return null;
}
const { workflowName, attemptNumber, statusDisplayName, branchName } =
match.groups;
return {
workflowName,
attemptNumber: attemptNumber ? Number.parseInt(attemptNumber, 10) : null,
status: getCheckSuiteStatus(statusDisplayName),
statusDisplayName,
branchName,
};
}
function getCheckSuiteStatus(
statusDisplayName: string,
): GitifyCheckSuiteStatus {
switch (statusDisplayName) {
case 'cancelled':
return 'CANCELLED';
case 'failed':
case 'failed at startup':
return 'FAILURE';
case 'skipped':
return 'SKIPPED';
case 'succeeded':
return 'SUCCESS';
default:
return null;
}
}
export function getCheckSuiteUrl(notification: GitifyNotification): Link {
const filters = [];
const checkSuiteAttributes = getCheckSuiteAttributes(notification);
if (checkSuiteAttributes?.workflowName) {
filters.push(
`workflow:"${checkSuiteAttributes.workflowName.replaceAll(' ', '+')}"`,
);
}
if (checkSuiteAttributes?.status) {
filters.push(`is:${checkSuiteAttributes.status}`);
}
if (checkSuiteAttributes?.branchName) {
filters.push(`branch:${checkSuiteAttributes.branchName}`);
}
return actionsURL(notification.repository.htmlUrl, filters);
}