forked from microsoft/vscode-pull-request-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarkdownUtils.ts
More file actions
357 lines (337 loc) · 11.9 KB
/
markdownUtils.ts
File metadata and controls
357 lines (337 loc) · 11.9 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as marked from 'marked';
import 'url-search-params-polyfill';
import * as vscode from 'vscode';
import { PullRequestDefaults } from './folderRepositoryManager';
import { GithubItemStateEnum, User } from './interface';
import { IssueModel } from './issueModel';
import { PullRequestModel } from './pullRequestModel';
import { RepositoriesManager } from './repositoriesManager';
import { getIssueNumberLabelFromParsed, ISSUE_OR_URL_EXPRESSION, makeLabel, parseIssueExpressionOutput, UnsatisfiedChecks } from './utils';
import { ensureEmojis } from '../common/emoji';
import Logger from '../common/logger';
import { CODE_PERMALINK, findCodeLinkLocally } from '../issues/issueLinkLookup';
function getIconString(issue: IssueModel) {
switch (issue.state) {
case GithubItemStateEnum.Open: {
return issue instanceof PullRequestModel ? '$(git-pull-request)' : '$(issues)';
}
case GithubItemStateEnum.Closed: {
return issue instanceof PullRequestModel ? '$(git-pull-request)' : '$(issue-closed)';
}
case GithubItemStateEnum.Merged:
return '$(git-merge)';
}
}
function getIconMarkdown(issue: IssueModel) {
if (issue instanceof PullRequestModel) {
return getIconString(issue);
}
switch (issue.state) {
case GithubItemStateEnum.Open: {
return `<span style="color:#22863a;">$(issues)</span>`;
}
case GithubItemStateEnum.Closed: {
// Use grey for issues closed as "not planned", purple for "completed"
const color = issue.stateReason !== 'COMPLETED' ? '#6a737d' : '#8957e5';
return `<span style="color:${color};">$(issue-closed)</span>`;
}
}
}
function repoCommitDate(user: User, repoNameWithOwner: string): string | undefined {
let date: string | undefined = undefined;
user.commitContributions.forEach(element => {
if (repoNameWithOwner.toLowerCase() === element.repoNameWithOwner.toLowerCase()) {
date = element.createdAt.toLocaleString('default', { day: 'numeric', month: 'short', year: 'numeric' });
}
});
return date;
}
export function userMarkdown(origin: PullRequestDefaults, user: User): vscode.MarkdownString {
const markdown: vscode.MarkdownString = new vscode.MarkdownString(undefined, true);
markdown.appendMarkdown(
` ${user.name ? `**${user.name}** ` : ''}[${user.login}](${user.url})`,
);
if (user.bio) {
markdown.appendText(' \r\n' + user.bio.replace(/\r\n/g, ' '));
}
const date = repoCommitDate(user, origin.owner + '/' + origin.repo);
if (user.location || date) {
markdown.appendMarkdown(' \r\n\r\n---');
}
if (user.location) {
markdown.appendMarkdown(` \r\n${vscode.l10n.t('{0} {1}', '$(location)', user.location)}`);
}
if (date) {
markdown.appendMarkdown(` \r\n${vscode.l10n.t('{0} Committed to this repository on {1}', '$(git-commit)', date)}`);
}
if (user.company) {
markdown.appendMarkdown(` \r\n${vscode.l10n.t({ message: '{0} Member of {1}', args: ['$(jersey)', user.company], comment: ['An organization that the user is a member of.', 'The first placeholder is an icon and shouldn\'t be localized.', 'The second placeholder is the name of the organization.'] })}`);
}
return markdown;
}
async function findAndModifyString(
text: string,
find: RegExp,
transformer: (match: RegExpMatchArray) => Promise<string | undefined>,
): Promise<string> {
let searchResult = text.search(find);
let position = 0;
while (searchResult >= 0 && searchResult < text.length) {
let newBodyFirstPart: string | undefined;
if (searchResult === 0 || text.charAt(searchResult - 1) !== '&') {
const match = text.substring(searchResult).match(find)!;
if (match) {
const transformed = await transformer(match);
if (transformed) {
newBodyFirstPart = text.slice(0, searchResult) + transformed;
text = newBodyFirstPart + text.slice(searchResult + match[0].length);
}
}
}
position = newBodyFirstPart ? newBodyFirstPart.length : searchResult + 1;
const newSearchResult = text.substring(position).search(find);
searchResult = newSearchResult > 0 ? position + newSearchResult : newSearchResult;
}
return text;
}
function findLinksInIssue(body: string, issue: IssueModel): Promise<string> {
return findAndModifyString(body, ISSUE_OR_URL_EXPRESSION, async (match: RegExpMatchArray) => {
const tryParse = parseIssueExpressionOutput(match);
if (tryParse) {
const issueNumberLabel = getIssueNumberLabelFromParsed(tryParse); // get label before setting owner and name.
if (!tryParse.owner || !tryParse.name) {
tryParse.owner = issue.remote.owner;
tryParse.name = issue.remote.repositoryName;
}
return `[${issueNumberLabel}](https://github.com/${tryParse.owner}/${tryParse.name}/issues/${tryParse.issueNumber})`;
}
return undefined;
});
}
async function findCodeLinksInIssue(body: string, repositoriesManager: RepositoriesManager) {
return findAndModifyString(body, CODE_PERMALINK, async (match: RegExpMatchArray) => {
const codeLink = await findCodeLinkLocally(match, repositoriesManager);
if (codeLink) {
Logger.trace('finding code links in issue', 'Issues');
const textDocument = await vscode.workspace.openTextDocument(codeLink?.file);
const endingTextDocumentLine = textDocument.lineAt(
codeLink.end < textDocument.lineCount ? codeLink.end : textDocument.lineCount - 1,
);
const query = [
codeLink.file,
{
selection: {
start: {
line: codeLink.start,
character: 0,
},
end: {
line: codeLink.end,
character: endingTextDocumentLine.text.length,
},
},
},
];
const openCommand = vscode.Uri.parse(`command:vscode.open?${encodeURIComponent(JSON.stringify(query))}`);
return `[${match[0]}](${openCommand} "Open ${codeLink.file.fsPath}")`;
}
return undefined;
});
}
export const ISSUE_BODY_LENGTH: number = 200;
export async function issueMarkdown(
issue: IssueModel,
context: vscode.ExtensionContext,
repositoriesManager: RepositoriesManager,
commentNumber?: number,
prChecks?: UnsatisfiedChecks
): Promise<vscode.MarkdownString> {
const markdown: vscode.MarkdownString = new vscode.MarkdownString(undefined, true);
markdown.supportHtml = true;
const date = new Date(issue.createdAt);
const ownerName = `${issue.remote.owner}/${issue.remote.repositoryName}`;
markdown.appendMarkdown(
`[${ownerName}](https://github.com/${ownerName}) on ${date.toLocaleString('default', {
day: 'numeric',
month: 'short',
year: 'numeric',
})} \n`,
);
const titleWithDraft = (issue instanceof PullRequestModel && issue.isDraft) ? `\[DRAFT\] ${issue.title}` : issue.title;
const title = marked
.parse(titleWithDraft, {
renderer: new PlainTextRenderer(),
smartypants: true,
})
.trim();
markdown.appendMarkdown(
`${getIconMarkdown(issue)} **${title}** [#${issue.number}](${issue.html_url}) \n`,
);
let body = marked.parse(issue.body, {
renderer: new PlainTextRenderer(),
smartypants: true,
});
markdown.appendMarkdown(' \n');
body = body.length > ISSUE_BODY_LENGTH ? body.substr(0, ISSUE_BODY_LENGTH) + '...' : body;
body = await findLinksInIssue(body, issue);
body = await findCodeLinksInIssue(body, repositoriesManager);
markdown.appendMarkdown(body + ' \n');
if (issue.item.labels.length > 0) {
await ensureEmojis(context);
markdown.appendMarkdown(' \n');
issue.item.labels.forEach(label => {
markdown.appendMarkdown(
`[${makeLabel(label)}](https://github.com/${ownerName}/labels/${encodeURIComponent(
label.name,
)}) `,
);
});
}
if (issue.item.comments && commentNumber) {
for (const comment of issue.item.comments) {
if (comment.databaseId === commentNumber) {
markdown.appendMarkdown(' \r\n\r\n---\r\n');
markdown.appendMarkdown(' \n');
markdown.appendMarkdown(
` **${comment.author.login}** commented`,
);
markdown.appendMarkdown(' \n');
let commentText = marked.parse(
comment.body.length > ISSUE_BODY_LENGTH
? comment.body.substr(0, ISSUE_BODY_LENGTH) + '...'
: comment.body,
{ renderer: new PlainTextRenderer(), smartypants: true },
);
commentText = await findLinksInIssue(commentText, issue);
markdown.appendMarkdown(commentText);
}
}
}
if (prChecks) {
const statusMessage = getStatusDecoration(prChecks)?.tooltip;
if (statusMessage) {
markdown.appendMarkdown(' \r\n\r\n');
markdown.appendMarkdown(`_${statusMessage}_`);
}
}
return markdown;
}
export class PlainTextRenderer extends marked.Renderer {
private allowSimpleMarkdown: boolean;
constructor(allowSimpleMarkdown: boolean = false) {
super();
this.allowSimpleMarkdown = allowSimpleMarkdown;
}
override code(code: string, _infostring: string | undefined): string {
return code;
}
override blockquote(quote: string): string {
return quote;
}
override html(_html: string): string {
return '';
}
override heading(text: string, _level: 1 | 2 | 3 | 4 | 5 | 6, _raw: string, _slugger: marked.Slugger): string {
return text + ' ';
}
override hr(): string {
return '';
}
override list(body: string, _ordered: boolean, _start: number): string {
return body;
}
override listitem(text: string): string {
return ' ' + text;
}
override checkbox(_checked: boolean): string {
return '';
}
override paragraph(text: string): string {
return text.replace(/\</g, '\\\<').replace(/\>/g, '\\\>') + ' ';
}
override table(header: string, body: string): string {
return header + ' ' + body;
}
override tablerow(content: string): string {
return content;
}
override tablecell(
content: string,
_flags: {
header: boolean;
align: 'center' | 'left' | 'right' | null;
},
): string {
return content;
}
override strong(text: string): string {
return text;
}
override em(text: string): string {
return text;
}
override codespan(code: string): string {
if (this.allowSimpleMarkdown) {
return `\`${code}\``;
}
return `\\\`${code}\\\``;
}
override br(): string {
return ' ';
}
override del(text: string): string {
return text;
}
override image(_href: string, _title: string, _text: string): string {
return '';
}
override text(text: string): string {
return text;
}
override link(href: string, title: string, text: string): string {
return text + ' ';
}
}
export function getStatusDecoration(status: UnsatisfiedChecks): vscode.FileDecoration2 | undefined {
if ((status & UnsatisfiedChecks.CIFailed) && (status & UnsatisfiedChecks.ReviewRequired)) {
return {
propagate: false,
badge: new vscode.ThemeIcon('close', new vscode.ThemeColor('list.errorForeground')),
tooltip: 'Review required and some checks have failed'
};
} else if (status & UnsatisfiedChecks.CIFailed) {
return {
propagate: false,
badge: new vscode.ThemeIcon('close', new vscode.ThemeColor('list.errorForeground')),
tooltip: 'Some checks have failed'
};
} else if (status & UnsatisfiedChecks.ChangesRequested) {
return {
propagate: false,
badge: new vscode.ThemeIcon('request-changes', new vscode.ThemeColor('list.errorForeground')),
tooltip: 'Changes requested'
};
} else if (status & UnsatisfiedChecks.CIPending) {
return {
propagate: false,
badge: new vscode.ThemeIcon('sync', new vscode.ThemeColor('list.warningForeground')),
tooltip: 'Checks pending'
};
} else if (status & UnsatisfiedChecks.ReviewRequired) {
return {
propagate: false,
badge: new vscode.ThemeIcon('circle-filled', new vscode.ThemeColor('list.warningForeground')),
tooltip: 'Review required'
};
} else if (status === UnsatisfiedChecks.None) {
return {
propagate: false,
badge: new vscode.ThemeIcon('check-all', new vscode.ThemeColor('issues.open')),
tooltip: 'All checks passed'
};
}
}