-
Notifications
You must be signed in to change notification settings - Fork 285
Expand file tree
/
Copy pathclient.ts
More file actions
283 lines (261 loc) · 7.84 KB
/
client.ts
File metadata and controls
283 lines (261 loc) · 7.84 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import type { AxiosPromise } from 'axios';
import { print } from 'graphql/language/printer';
import type {
Account,
Hostname,
Link,
SettingsState,
Token,
} from '../../types';
import type {
Commit,
CommitComment,
Discussion,
GraphQLSearch,
Issue,
IssueOrPullRequestComment,
Notification,
NotificationThreadSubscription,
PullRequest,
PullRequestReview,
Release,
UserDetails,
} from '../../typesGitHub';
import { isAnsweredDiscussionFeatureSupported } from '../features';
import { rendererLogError } from '../logger';
import { QUERY_SEARCH_DISCUSSIONS } from './graphql/discussions';
import { formatAsGitHubSearchSyntax } from './graphql/utils';
import { apiRequestAuth } from './request';
import { getGitHubAPIBaseUrl, getGitHubGraphQLUrl } from './utils';
/**
* Get the authenticated user
*
* Endpoint documentation: https://docs.github.com/en/rest/users/users#get-the-authenticated-user
*/
export function getAuthenticatedUser(
hostname: Hostname,
token: Token,
): AxiosPromise<UserDetails> {
const url = getGitHubAPIBaseUrl(hostname);
url.pathname += 'user';
return apiRequestAuth(url.toString() as Link, 'GET', token);
}
/**
* Perform a HEAD operation, used to validate that connectivity is established.
*
* Endpoint documentation: https://docs.github.com/en/rest/activity/notifications
*/
export function headNotifications(
hostname: Hostname,
token: Token,
): AxiosPromise<void> {
const url = getGitHubAPIBaseUrl(hostname);
url.pathname += 'notifications';
return apiRequestAuth(url.toString() as Link, 'HEAD', token);
}
/**
* List all notifications for the current user, sorted by most recently updated.
*
* Endpoint documentation: https://docs.github.com/en/rest/activity/notifications#list-notifications-for-the-authenticated-user
*/
export function listNotificationsForAuthenticatedUser(
account: Account,
settings: SettingsState,
): AxiosPromise<Notification[]> {
const url = getGitHubAPIBaseUrl(account.hostname);
url.pathname += 'notifications';
url.searchParams.append('participating', String(settings.participating));
return apiRequestAuth(
url.toString() as Link,
'GET',
account.token,
{},
settings.fetchAllNotifications,
);
}
/**
* Marks a thread as "read." Marking a thread as "read" is equivalent to
* clicking a notification in your notification inbox on GitHub.
*
* Endpoint documentation: https://docs.github.com/en/rest/activity/notifications#mark-a-thread-as-read
*/
export function markNotificationThreadAsRead(
threadId: string,
hostname: Hostname,
token: Token,
): AxiosPromise<void> {
const url = getGitHubAPIBaseUrl(hostname);
url.pathname += `notifications/threads/${threadId}`;
return apiRequestAuth(url.toString() as Link, 'PATCH', token, {});
}
/**
* Marks a thread as "done." Marking a thread as "done" is equivalent to marking a
* notification in your notification inbox on GitHub as done.
*
* NOTE: This was added to GitHub Enterprise Server in version 3.13 or later.
*
* Endpoint documentation: https://docs.github.com/en/rest/activity/notifications#mark-a-thread-as-done
*/
export function markNotificationThreadAsDone(
threadId: string,
hostname: Hostname,
token: Token,
): AxiosPromise<void> {
const url = getGitHubAPIBaseUrl(hostname);
url.pathname += `notifications/threads/${threadId}`;
return apiRequestAuth(url.toString() as Link, 'DELETE', token, {});
}
/**
* Ignore future notifications for threads until you comment on the thread or get an`@mention`.
*
* Endpoint documentation: https://docs.github.com/en/rest/activity/notifications#delete-a-thread-subscription
*/
export function ignoreNotificationThreadSubscription(
threadId: string,
hostname: Hostname,
token: Token,
): AxiosPromise<NotificationThreadSubscription> {
const url = getGitHubAPIBaseUrl(hostname);
url.pathname += `notifications/threads/${threadId}/subscription`;
return apiRequestAuth(url.toString() as Link, 'PUT', token, {
ignored: true,
});
}
/**
* Returns the contents of a single commit reference.
*
* Endpoint documentation: https://docs.github.com/en/rest/commits/commits#get-a-commit
*/
export function getCommit(url: Link, token: Token): AxiosPromise<Commit> {
return apiRequestAuth(url, 'GET', token);
}
/**
* Gets a specified commit comment.
*
* Endpoint documentation: https://docs.github.com/en/rest/commits/comments#get-a-commit-comment
*/
export function getCommitComment(
url: Link,
token: Token,
): AxiosPromise<CommitComment> {
return apiRequestAuth(url, 'GET', token);
}
/**
* Get details of an issue.
*
* Endpoint documentation: https://docs.github.com/en/rest/issues/issues#get-an-issue
*/
export function getIssue(url: Link, token: Token): AxiosPromise<Issue> {
return apiRequestAuth(url, 'GET', token);
}
/**
* Get comments on issues and pull requests.
* Every pull request is an issue, but not every issue is a pull request.
*
* Endpoint documentation: https://docs.github.com/en/rest/issues/comments#get-an-issue-comment
*/
export function getIssueOrPullRequestComment(
url: Link,
token: Token,
): AxiosPromise<IssueOrPullRequestComment> {
return apiRequestAuth(url, 'GET', token);
}
/**
* Get details of a pull request.
*
* Endpoint documentation: https://docs.github.com/en/rest/pulls/pulls#get-a-pull-request
*/
export function getPullRequest(
url: Link,
token: Token,
): AxiosPromise<PullRequest> {
return apiRequestAuth(url, 'GET', token);
}
/**
* Lists all reviews for a specified pull request. The list of reviews returns in chronological order.
*
* Endpoint documentation: https://docs.github.com/en/rest/pulls/reviews#list-reviews-for-a-pull-request
*/
export function getPullRequestReviews(
url: Link,
token: Token,
): AxiosPromise<PullRequestReview[]> {
return apiRequestAuth(url, 'GET', token);
}
/**
* Gets a public release with the specified release ID.
*
* Endpoint documentation: https://docs.github.com/en/rest/releases/releases#get-a-release
*/
export function getRelease(url: Link, token: Token): AxiosPromise<Release> {
return apiRequestAuth(url, 'GET', token);
}
/**
* Get the `html_url` from the GitHub response
*/
export async function getHtmlUrl(url: Link, token: Token): Promise<string> {
try {
const response = (await apiRequestAuth(url, 'GET', token)).data;
return response.html_url;
} catch (err) {
rendererLogError(
'getHtmlUrl',
`error occurred while fetching html url for ${url}`,
err,
);
}
}
/**
* Search for Discussions that match notification title and repository.
*
* Returns the latest discussion and their latest comments / replies
*
*/
export async function searchDiscussions(
notification: Notification,
): AxiosPromise<GraphQLSearch<Discussion>> {
const url = getGitHubGraphQLUrl(notification.account.hostname);
return apiRequestAuth(
url.toString() as Link,
'POST',
notification.account.token,
{
query: print(QUERY_SEARCH_DISCUSSIONS),
variables: {
queryStatement: formatAsGitHubSearchSyntax(
notification.repository.full_name,
notification.subject.title,
),
firstDiscussions: 1,
lastComments: 100,
lastReplies: 100,
firstLabels: 100,
includeIsAnswered: isAnsweredDiscussionFeatureSupported(
notification.account,
),
},
},
);
}
/**
* Return the latest discussion that matches the notification title and repository.
*/
export async function getLatestDiscussion(
notification: Notification,
): Promise<Discussion | null> {
try {
const response = await searchDiscussions(notification);
return (
response.data?.data.search.nodes.filter(
(discussion) => discussion.title === notification.subject.title,
)[0] ?? null
);
} catch (err) {
rendererLogError(
'getLatestDiscussion',
'failed to fetch latest discussion for notification',
err,
notification,
);
}
}