-
Notifications
You must be signed in to change notification settings - Fork 285
Expand file tree
/
Copy pathhelpers.ts
More file actions
214 lines (181 loc) · 5.89 KB
/
helpers.ts
File metadata and controls
214 lines (181 loc) · 5.89 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
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
import {
ChevronDownIcon,
ChevronLeftIcon,
ChevronRightIcon,
} from '@primer/octicons-react';
import { logError, logWarn } from '../../shared/logger';
import type { Chevron, Hostname, Link } from '../types';
import type { Notification } from '../typesGitHub';
import { getHtmlUrl, getLatestDiscussion } from './api/client';
import type { PlatformType } from './auth/types';
import { Constants } from './constants';
import {
getCheckSuiteAttributes,
getClosestDiscussionCommentOrReply,
getWorkflowRunAttributes,
} from './subject';
export function getPlatformFromHostname(hostname: string): PlatformType {
return hostname.endsWith(Constants.DEFAULT_AUTH_OPTIONS.hostname)
? 'GitHub Cloud'
: 'GitHub Enterprise Server';
}
export function isEnterpriseServerHost(hostname: Hostname): boolean {
return !hostname.endsWith(Constants.DEFAULT_AUTH_OPTIONS.hostname);
}
export function generateNotificationReferrerId(
notification: Notification,
): string {
const buffer = Buffer.from(
`018:NotificationThread${notification.id}:${notification.account.user.id}`,
);
return buffer.toString('base64');
}
export function getCheckSuiteUrl(notification: Notification): 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.html_url, filters);
}
export function getWorkflowRunUrl(notification: Notification): Link {
const filters = [];
const workflowRunAttributes = getWorkflowRunAttributes(notification);
if (workflowRunAttributes?.status) {
filters.push(`is:${workflowRunAttributes.status}`);
}
return actionsURL(notification.repository.html_url, filters);
}
/**
* Construct a GitHub Actions URL for a repository with optional filters.
*/
export function actionsURL(repositoryURL: string, filters: string[]): Link {
const url = new URL(repositoryURL);
url.pathname += '/actions';
if (filters.length > 0) {
url.searchParams.append('query', filters.join('+'));
}
// Note: the GitHub Actions UI cannot handle encoded '+' characters.
return url.toString().replace(/%2B/g, '+') as Link;
}
async function getDiscussionUrl(notification: Notification): Promise<Link> {
const url = new URL(notification.repository.html_url);
url.pathname += '/discussions';
const discussion = await getLatestDiscussion(notification);
if (discussion) {
url.href = discussion.url;
const closestComment = getClosestDiscussionCommentOrReply(
notification,
discussion.comments.nodes,
);
if (closestComment) {
url.hash = `#discussioncomment-${closestComment.databaseId}`;
}
}
return url.toString() as Link;
}
export async function generateGitHubWebUrl(
notification: Notification,
): Promise<Link> {
const url = new URL(notification.repository.html_url);
// FIXME see #1583
// Upstream GitHub API has started returning subject urls for Discussion notification types,
// however these URLs are broken. Temporarily downgrading to use discussion lookup process.
if (notification.subject.type === 'Discussion') {
notification.subject.url = null;
notification.subject.latest_comment_url = null;
}
try {
if (notification.subject.latest_comment_url) {
url.href = await getHtmlUrl(
notification.subject.latest_comment_url,
notification.account.token,
);
} else if (notification.subject.url) {
url.href = await getHtmlUrl(
notification.subject.url,
notification.account.token,
);
} else {
// Perform any specific notification type handling (only required for a few special notification scenarios)
switch (notification.subject.type) {
case 'CheckSuite':
url.href = getCheckSuiteUrl(notification);
break;
case 'Discussion':
url.href = await getDiscussionUrl(notification);
break;
case 'RepositoryInvitation':
url.pathname += '/invitations';
break;
case 'RepositoryDependabotAlertsThread':
url.pathname += '/security/dependabot';
break;
case 'WorkflowRun':
url.href = getWorkflowRunUrl(notification);
break;
default:
break;
}
}
} catch (err) {
logError(
'generateGitHubWebUrl',
'Failed to resolve specific notification html url for',
err,
notification,
);
logWarn(
'generateGitHubWebUrl',
`Falling back to repository root url: ${notification.repository.full_name}`,
);
}
url.searchParams.set(
'notification_referrer_id',
generateNotificationReferrerId(notification),
);
return url.toString() as Link;
}
export function formatForDisplay(text: string[]): string {
if (!text) {
return '';
}
return text
.join(' ')
.replace(/([a-z])([A-Z])/g, '$1 $2') // Add space between lowercase character followed by an uppercase character
.replace(/_/g, ' ') // Replace underscores with spaces
.replace(/\w+/g, (word) => {
// Convert to proper case (capitalize first letter of each word)
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
});
}
export function getChevronDetails(
hasNotifications: boolean,
isVisible: boolean,
type: 'account' | 'repository',
): Chevron {
if (!hasNotifications) {
return {
icon: ChevronLeftIcon,
label: `No notifications for ${type}`,
};
}
if (isVisible) {
return {
icon: ChevronDownIcon,
label: `Hide ${type} notifications`,
};
}
return {
icon: ChevronRightIcon,
label: `Show ${type} notifications`,
};
}