-
Notifications
You must be signed in to change notification settings - Fork 736
Expand file tree
/
Copy pathprComment.ts
More file actions
554 lines (464 loc) · 17.3 KB
/
prComment.ts
File metadata and controls
554 lines (464 loc) · 17.3 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as path from 'path';
import * as vscode from 'vscode';
import { GitHubRepository } from './githubRepository';
import { IAccount } from './interface';
import { updateCommentReactions } from './utils';
import { COPILOT_ACCOUNTS, IComment } from '../common/comment';
import { emojify, ensureEmojis } from '../common/emoji';
import Logger from '../common/logger';
import { DataUri } from '../common/uri';
import { ALLOWED_USERS, JSDOC_NON_USERS, PHPDOC_NON_USERS } from '../common/user';
import { escapeRegExp, stringReplaceAsync } from '../common/utils';
export interface GHPRCommentThread extends vscode.CommentThread2 {
gitHubThreadId: string;
/**
* The uri of the document the thread has been created on.
*/
uri: vscode.Uri;
/**
* The range the comment thread is located within the document. The thread icon will be shown
* at the first line of the range.
*/
range: vscode.Range | undefined;
/**
* The ordered comments of the thread.
*/
comments: (GHPRComment | TemporaryComment)[];
/**
* Whether the thread should be collapsed or expanded when opening the document.
* Defaults to Collapsed.
*/
collapsibleState: vscode.CommentThreadCollapsibleState;
/**
* The optional human-readable label describing the [Comment Thread](#CommentThread)
*/
label?: string;
canReply: boolean | vscode.CommentAuthorInformation;
/**
* Whether the thread has been marked as resolved.
*/
state?: { resolved: vscode.CommentThreadState; applicability?: vscode.CommentThreadApplicability };
reveal(comment?: vscode.Comment, options?: vscode.CommentThreadRevealOptions): Promise<void>;
dispose: () => void;
}
export namespace GHPRCommentThread {
export function is(value: any): value is GHPRCommentThread {
return (value && (typeof (value as GHPRCommentThread).gitHubThreadId) === 'string');
}
}
abstract class CommentBase implements vscode.Comment {
public abstract commentId: undefined | string;
/**
* The comment thread the comment is from
*/
public parent: GHPRCommentThread;
/**
* The text of the comment as from GitHub
*/
public abstract get body(): string | vscode.MarkdownString;
public abstract set body(body: string | vscode.MarkdownString);
/**
* Whether the comment is in edit mode or not
*/
public mode: vscode.CommentMode;
/**
* The author of the comment
*/
public abstract get author(): vscode.CommentAuthorInformation;
/**
* The author of the comment, before any modifications we make for display purposes.
*/
public originalAuthor: vscode.CommentAuthorInformation;
/**
* The label to display on the comment, 'Pending' or nothing
*/
public label: string | undefined;
/**
* The list of reactions to the comment
*/
public reactions?: vscode.CommentReaction[] | undefined;
/**
* The context value, used to determine whether the command should be visible/enabled based on clauses in package.json
*/
public contextValue: string;
/**
* The state of the comment (Published or Draft)
*/
public state?: vscode.CommentState;
constructor(
parent: GHPRCommentThread,
) {
this.parent = parent;
}
public abstract commentEditId(): number | string;
startEdit() {
this.parent.comments = this.parent.comments.map(cmt => {
if (cmt instanceof CommentBase && cmt.commentEditId() === this.commentEditId()) {
cmt.mode = vscode.CommentMode.Editing;
}
return cmt;
});
}
protected abstract getCancelEditBody(): string | vscode.MarkdownString;
protected abstract doSetBody(body: string | vscode.MarkdownString, refresh: boolean): Promise<void>;
cancelEdit() {
this.parent.comments = this.parent.comments.map(cmt => {
if (cmt instanceof CommentBase && cmt.commentEditId() === this.commentEditId()) {
cmt.mode = vscode.CommentMode.Preview;
this.doSetBody(this.getCancelEditBody(), true);
}
return cmt;
});
}
}
/**
* Used to optimistically render updates to comment threads. Temporary comments are immediately
* set when a command is run, and then replaced with real data when the operation finishes.
*/
export class TemporaryComment extends CommentBase {
public commentId: undefined;
/**
* The id of the comment
*/
public id: number;
/**
* If the temporary comment is in place for an edit, the original text value of the comment
*/
public originalBody?: string;
static idPool = 0;
constructor(
parent: GHPRCommentThread,
private input: string,
isDraft: boolean,
currentUser: IAccount,
originalComment?: GHPRComment,
) {
super(parent);
this.mode = vscode.CommentMode.Preview;
this.originalAuthor = {
name: currentUser.specialDisplayName ?? currentUser.login,
iconPath: (currentUser.avatarUrl && DataUri.isGitHubDotComAvatar(currentUser.avatarUrl)) ? vscode.Uri.parse(`${currentUser.avatarUrl}&s=64`) : undefined,
};
this.label = isDraft ? vscode.l10n.t('Pending') : undefined;
this.state = isDraft ? vscode.CommentState.Draft : vscode.CommentState.Published;
this.contextValue = 'temporary,canEdit,canDelete';
this.originalBody = originalComment ? originalComment.rawComment.body : undefined;
this.reactions = originalComment ? originalComment.reactions : undefined;
this.id = TemporaryComment.idPool++;
}
protected async doSetBody(input: string | vscode.MarkdownString): Promise<void> {
if (typeof input === 'string') {
this.input = input;
}
}
set body(input: string | vscode.MarkdownString) {
this.doSetBody(input);
}
get body(): string | vscode.MarkdownString {
const s = new vscode.MarkdownString(this.input);
s.supportAlertSyntax = true;
return s;
}
get author(): vscode.CommentAuthorInformation {
return this.originalAuthor;
}
commentEditId() {
return this.id;
}
protected getCancelEditBody() {
return this.originalBody || this.body;
}
}
const SUGGESTION_EXPRESSION = /```suggestion(\u0020*(\r\n|\n))((?<suggestion>[\s\S]*?)(\r\n|\n))?```/;
const IMG_EXPRESSION = /<img .*src=['"](?<src>.+?)['"].*?>/g;
const UUID_EXPRESSION = /[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}/;
export const COMMIT_SHA_EXPRESSION = /(?<![`\/\w])([0-9a-f]{7})([0-9a-f]{33})?(?![`\/\w])/g;
export class GHPRComment extends CommentBase {
private static ID = 'GHPRComment';
public commentId: string;
public timestamp: Date;
/**
* The complete comment data returned from GitHub
*/
public rawComment: IComment;
private _rawBody: string | vscode.MarkdownString;
private replacedBody: string;
private githubRepository: GitHubRepository | undefined;
constructor(private readonly context: vscode.ExtensionContext, comment: IComment, parent: GHPRCommentThread, githubRepositories?: GitHubRepository[]) {
super(parent);
this.rawComment = comment;
this.originalAuthor = {
name: comment.user?.specialDisplayName ?? comment.user!.login,
iconPath: (comment.user && comment.user.avatarUrl && DataUri.isGitHubDotComAvatar(comment.user.avatarUrl)) ? vscode.Uri.parse(comment.user.avatarUrl) : undefined,
};
const url = vscode.Uri.parse(comment.url);
this.githubRepository = githubRepositories?.find(repo => repo.remote.host === url.authority);
const avatarUrisPromise = (comment.user && DataUri.isGitHubDotComAvatar(comment.user.avatarUrl)) ? DataUri.avatarCirclesAsImageDataUris(context, [comment.user], 28, 28) : Promise.resolve([]);
this.doSetBody(comment.body, !comment.user).then(async () => { // only refresh if there's no user. If there's a user, we'll refresh in the then.
const avatarUris = await avatarUrisPromise;
if (avatarUris.length > 0) {
this.author.iconPath = avatarUris[0];
}
this.refresh();
});
this.commentId = comment.id.toString();
updateCommentReactions(this, comment.reactions);
this.label = comment.isDraft ? vscode.l10n.t('Pending') : undefined;
this.state = comment.isDraft ? vscode.CommentState.Draft : vscode.CommentState.Published;
const contextValues: string[] = [];
if (comment.canEdit) {
contextValues.push('canEdit');
}
if (comment.canDelete) {
contextValues.push('canDelete');
}
if (this.suggestion !== undefined) {
contextValues.push('hasSuggestion');
}
this.contextValue = contextValues.join(',');
this.timestamp = new Date(comment.createdAt);
}
get author(): vscode.CommentAuthorInformation {
if (!this.rawComment.user?.specialDisplayName) {
return this.originalAuthor;
}
return {
name: this.rawComment.user.specialDisplayName,
iconPath: this.originalAuthor.iconPath,
};
}
update(comment: IComment) {
const oldRawComment = this.rawComment;
this.rawComment = comment;
let refresh: boolean = false;
if (updateCommentReactions(this, comment.reactions)) {
refresh = true;
}
const oldLabel = this.label;
this.label = comment.isDraft ? vscode.l10n.t('Pending') : undefined;
const newState = comment.isDraft ? vscode.CommentState.Draft : vscode.CommentState.Published;
if (this.label !== oldLabel || this.state !== newState) {
this.state = newState;
refresh = true;
}
const contextValues: string[] = [];
if (comment.canEdit) {
contextValues.push('canEdit');
}
if (comment.canDelete) {
contextValues.push('canDelete');
}
if (this.suggestion !== undefined) {
contextValues.push('hasSuggestion');
}
const oldContextValue = this.contextValue;
this.contextValue = contextValues.join(',');
if (oldContextValue !== this.contextValue) {
refresh = true;
}
// Set the comment body last as it will trigger an update if set.
if (oldRawComment.body !== comment.body) {
this.doSetBody(comment.body, true);
refresh = false;
}
if (refresh) {
this.refresh();
}
}
private refresh() {
// Self assign the comments to trigger an update of the comments in VS Code now that we have replaced the body.
// eslint-disable-next-line no-self-assign
this.parent.comments = this.parent.comments;
}
get suggestion(): string | undefined {
const match = this.rawComment.body.match(SUGGESTION_EXPRESSION);
const suggestionBody = match?.groups?.suggestion;
if (match) {
return suggestionBody ? suggestionBody : '';
}
return undefined;
}
public commentEditId() {
return this.commentId;
}
private replaceImg(body: string) {
return body.replace(IMG_EXPRESSION, (_substring, _1, _2, _3, { src }) => {
return ``;
});
}
private replaceSuggestion(body: string) {
return body.replace(new RegExp(SUGGESTION_EXPRESSION, 'g'), (_substring: string, ...args: any[]) => {
return `***
Suggested change:
\`\`\`
${args[3] ?? ''}
\`\`\`
***`;
});
}
private async createLocalFilePath(rootUri: vscode.Uri, fileSubPath: string, startLine: number, endLine: number): Promise<string | undefined> {
const localFile = vscode.Uri.joinPath(rootUri, fileSubPath);
try {
const stat = await vscode.workspace.fs.stat(localFile);
if (stat.type === vscode.FileType.File) {
return `${localFile.with({ fragment: `${startLine}-${endLine}` }).toString()}`;
}
} catch (e) {
return undefined;
}
}
private async replacePermalink(body: string): Promise<string> {
const githubRepository = this.githubRepository;
if (!githubRepository) {
return body;
}
const repoName = escapeRegExp(githubRepository.remote.repositoryName);
const expression = new RegExp(`https://github.com/(.+)/${repoName}/blob/([0-9a-f]{40})/(.*)#L([0-9]+)(-L([0-9]+))?`, 'g');
return stringReplaceAsync(body, expression, async (match: string, owner: string, sha: string, file: string, start: string, _endGroup?: string, end?: string, index?: number) => {
if (index && (index > 0) && (body.charAt(index - 1) === '(')) {
return match;
}
const startLine = parseInt(start);
const endLine = end ? parseInt(end) : startLine + 1;
const lineContents = await githubRepository.getLines(sha, file, startLine, endLine);
if (!lineContents) {
return match;
}
const localFile = await this.createLocalFilePath(githubRepository.rootUri, file, startLine, endLine);
const lineMessage = end ? `Lines ${startLine} to ${endLine} in \`${sha.substring(0, 7)}\`` : `Line ${startLine} in \`${sha.substring(0, 7)}\``;
return `
***
[${file}](${localFile ?? match})${localFile ? ` ([view on GitHub](${match}))` : ''}
${lineMessage}
\`\`\`
${lineContents}
\`\`\`
***`;
});
}
private replaceImages(body: string): string {
const html = this.rawComment.bodyHTML;
if (!html) {
return body;
}
return replaceImages(body, html, this.githubRepository?.remote.host);
}
private replaceCommitShas(body: string): string {
const githubRepository = this.githubRepository;
if (!githubRepository) {
return body;
}
// Match commit SHAs that are:
// - Either 7 or 40 hex characters
// - Not already part of a URL or markdown link
// - Not inside code blocks (backticks)
return body.replace(COMMIT_SHA_EXPRESSION, (match, shortSha, remaining, offset) => {
// Don't replace if inside code blocks
const beforeMatch = body.substring(0, offset);
const backtickCount = (beforeMatch.match(/`/g)?.length ?? 0);
if (backtickCount % 2 === 1) {
return match;
}
// Don't replace if already part of a markdown link
if (beforeMatch.endsWith('[') || body.substring(offset + match.length).startsWith(']')) {
return match;
}
const owner = githubRepository.remote.owner;
const repo = githubRepository.remote.repositoryName;
const commitUrl = `https://${githubRepository.remote.host}/${owner}/${repo}/commit/${match}`;
return `[${shortSha}](${commitUrl})`;
});
}
private replaceNewlines(body: string) {
return body.replace(/(?<!\s)(\r\n|\n)/g, ' \n');
}
private postpendSpecialAuthorComment(body: string) {
if (!this.rawComment.specialDisplayBodyPostfix) {
return body;
}
return `${body} \n\n_${this.rawComment.specialDisplayBodyPostfix}_`;
}
private async replaceBody(body: string | vscode.MarkdownString): Promise<string> {
const emojiPromise = ensureEmojis(this.context);
Logger.trace('Replace comment body', GHPRComment.ID);
if (body instanceof vscode.MarkdownString) {
const permalinkReplaced = await this.replacePermalink(body.value);
return this.replaceImg(this.replaceSuggestion(permalinkReplaced));
}
const imagesReplaced = this.replaceImages(body);
const newLinesReplaced = this.replaceNewlines(imagesReplaced);
const documentLanguage = (await vscode.workspace.openTextDocument(this.parent.uri)).languageId;
const replacerRegex = new RegExp(`([^/\[\`]|^)@(${ALLOWED_USERS})`, 'g');
// Replace user
const linkified = newLinesReplaced.replace(replacerRegex, (substring, _1, _2, offset) => {
// Do not try to replace user if there's a code block.
if ((newLinesReplaced.substring(0, offset).match(/```/g)?.length ?? 0) % 2 === 1) {
return substring;
}
// Do not try to replace user if it might already be part of a link
if (substring.includes(']') || substring.includes(')')) {
return substring;
}
const username = substring.substring(substring.startsWith('@') ? 1 : 2);
if ((((documentLanguage === 'javascript') || (documentLanguage === 'typescript')) && JSDOC_NON_USERS.includes(username))
|| ((documentLanguage === 'php') && PHPDOC_NON_USERS.includes(username))) {
return substring;
}
const url = COPILOT_ACCOUNTS[username]?.url ?? `${path.dirname(this.rawComment.user!.url)}/${username}`;
return `${substring.startsWith('@') ? '' : substring.charAt(0)}[@${username}](${url})`;
});
const commitShasReplaced = this.replaceCommitShas(linkified);
const permalinkReplaced = await this.replacePermalink(commitShasReplaced);
await emojiPromise;
return this.postpendSpecialAuthorComment(emojify(this.replaceImg(this.replaceSuggestion(permalinkReplaced))));
}
protected async doSetBody(body: string | vscode.MarkdownString, refresh: boolean) {
this._rawBody = body;
const replacedBody = await this.replaceBody(body);
if (replacedBody !== this.replacedBody) {
this.replacedBody = replacedBody;
if (refresh) {
this.refresh();
}
}
}
set body(body: string | vscode.MarkdownString) {
this.doSetBody(body, false);
}
get body(): string | vscode.MarkdownString {
if (this.mode === vscode.CommentMode.Editing) {
return this._rawBody;
}
const s = new vscode.MarkdownString(this.replacedBody);
s.supportAlertSyntax = true;
return s;
}
protected getCancelEditBody() {
const s = new vscode.MarkdownString(this.rawComment.body);
s.supportAlertSyntax = true;
return s;
}
}
export function replaceImages(markdownBody: string, htmlBody: string, host: string = 'github.com') {
const originalExpression = new RegExp(`https:\/\/${host}\/.+\/assets\/([^\/]+\/)?(?<uuid>${UUID_EXPRESSION.source})`);
let originalMatch = markdownBody.match(originalExpression);
const htmlHost = escapeRegExp(host === 'github.com' ? 'githubusercontent.com' : host);
while (originalMatch) {
if (originalMatch.groups?.uuid) {
const uuid = escapeRegExp(originalMatch.groups.uuid);
const htmlExpression = new RegExp(`https:\/\/([^"]*${htmlHost})\/[^?]+${uuid}[^"]+`);
const htmlMatch = htmlBody.match(htmlExpression);
if (htmlMatch && htmlMatch[0]) {
markdownBody = markdownBody.replace(originalMatch[0], htmlMatch[0]);
} else {
return markdownBody;
}
}
originalMatch = markdownBody.match(originalExpression);
}
return markdownBody;
}