Skip to content

Commit f939bef

Browse files
committed
Notifications now open a link in browser, and also work even if lastComment is missing
1 parent 6e3b5a6 commit f939bef

File tree

1 file changed

+53
-28
lines changed

1 file changed

+53
-28
lines changed

src/github.ts

+53-28
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,16 @@ abstract class GitHubTreeItem extends UpdatableTreeItem<GitHubTreeItem> {
104104
}
105105

106106
class GitHubLeafTreeItem extends GitHubTreeItem {
107-
constructor(label: string, id: string, icon: string,
107+
constructor(label: string, id: string, icon: string, targetUrl: string | undefined,
108108
onDidChangeTreeDataEmitter: vscode.EventEmitter<vscode.TreeItem | undefined>) {
109109
super(label, id, vscode.TreeItemCollapsibleState.None, false, icon, onDidChangeTreeDataEmitter);
110+
if (targetUrl !== undefined) {
111+
this.command = {
112+
command: 'vscode.open',
113+
title: 'Open in Browser',
114+
arguments: [ vscode.Uri.parse(targetUrl) ]
115+
};
116+
}
110117
}
111118

112119
protected loadChildrenArrayFromWeb(): Promise<GitHubTreeItem[]> {
@@ -133,7 +140,8 @@ class AlertsRootItem extends GitHubTreeItem {
133140
for (const alert of json) {
134141
if (alert.unread && alert.repository.owner.login === 'openjdk') {
135142
const notInfo = new AlertTreeItem(alert.subject.title, 'alert-' + alert.id,
136-
alert.subject.latest_comment_url, alert.subject.url, new Date(alert.updated_at), alert.repository.full_name, this.onDidChangeTreeDataEmitter);
143+
alert.subject.latest_comment_url, alert.subject.url, new Date(alert.updated_at),
144+
alert.repository.full_name, this.onDidChangeTreeDataEmitter);
137145
newAlerts.push(notInfo);
138146
}
139147
}
@@ -147,42 +155,56 @@ class AlertsRootItem extends GitHubTreeItem {
147155
}
148156

149157
class AlertTreeItem extends GitHubTreeItem {
158+
prWebUrl: string;
150159
constructor(label: string, id: string, private commentUrl: string, private prUrl: string, private updatedAt: Date,
151160
private repository: string, onDidChangeTreeDataEmitter: vscode.EventEmitter<vscode.TreeItem | undefined>) {
152161
super(label, id, vscode.TreeItemCollapsibleState.Collapsed, false, 'github-item.svg', onDidChangeTreeDataEmitter);
162+
// Technically we should look this up, but keep it simple and just rewrite URL
163+
this.prWebUrl = this.prUrl.replace(/https:\/\/api\.github\.com\/repos\/(.*\/.*)\/pulls\/(.*)/,
164+
'https://github.com/$1/pull/$2');
153165
}
154166

155-
protected loadChildrenArrayFromWeb(): Promise<GitHubTreeItem[]> {
156-
return GitHubProvider.getGHjson(this.commentUrl, (comment: any, resolveJson: any, rejectJson: any) => {
157-
const newCommentInfo: GitHubTreeItem[] = [];
167+
private fillInTimeStampAndPR(items: GitHubTreeItem[]) {
168+
const prNumber = this.prUrl.split('/').pop();
158169

159-
// Stupid cleaning of html tags; will likely work ok since GitHub does the real work for us
160-
const cleanedComment = comment.body.replace(/<\/?[^>]+(>|$)/g, '').trim();
170+
let localeConf = vscode.workspace.getConfiguration('openjdkDevel').get('locale', '');
171+
let locale;
172+
if (localeConf === '') {
173+
locale = undefined;
174+
} else {
175+
locale = localeConf;
176+
}
161177

162-
const prNumber = this.prUrl.split('/').pop();
178+
items.push(new GitHubLeafTreeItem(this.updatedAt.toLocaleString(locale),
179+
this.commentUrl + '+date', 'github-time.svg', this.prWebUrl, this.onDidChangeTreeDataEmitter));
163180

164-
let localeConf = vscode.workspace.getConfiguration('openjdkDevel').get('locale', '');
165-
let locale;
166-
if (localeConf === '') {
167-
locale = undefined;
168-
} else {
169-
locale = localeConf;
170-
}
181+
items.push(new GitHubLeafTreeItem(`${this.repository}#${prNumber}`,
182+
this.commentUrl + '+pr', 'github-pullrequest.svg', this.prWebUrl, this.onDidChangeTreeDataEmitter));
183+
}
171184

172-
newCommentInfo.push(new GitHubLeafTreeItem(cleanedComment,
173-
this.commentUrl + '+comment', 'github-conversation.svg', this.onDidChangeTreeDataEmitter));
185+
protected loadChildrenArrayFromWeb(): Promise<GitHubTreeItem[]> {
186+
const newCommentInfo: GitHubTreeItem[] = [];
174187

175-
newCommentInfo.push(new GitHubLeafTreeItem(comment.user.login,
176-
this.commentUrl + '+username', 'github-user.svg', this.onDidChangeTreeDataEmitter));
188+
if (this.commentUrl !== null) {
189+
return GitHubProvider.getGHjson(this.commentUrl, (comment: any, resolveJson: any, rejectJson: any) => {
190+
// Stupid cleaning of html tags; will likely work ok since GitHub does the real work for us
191+
const cleanedComment = comment.body.replace(/<\/?[^>]+(>|$)/g, '').trim();
177192

178-
newCommentInfo.push(new GitHubLeafTreeItem(this.updatedAt.toLocaleString(locale),
179-
this.commentUrl + '+date', 'github-time.svg', this.onDidChangeTreeDataEmitter));
193+
newCommentInfo.push(new GitHubLeafTreeItem(cleanedComment,
194+
this.commentUrl + '+comment', 'github-conversation.svg', this.prWebUrl, this.onDidChangeTreeDataEmitter));
180195

181-
newCommentInfo.push(new GitHubLeafTreeItem(`${this.repository}#${prNumber}`,
182-
this.commentUrl + '+pr', 'github-pullrequest.svg', this.onDidChangeTreeDataEmitter));
196+
newCommentInfo.push(new GitHubLeafTreeItem(comment.user.login,
197+
this.commentUrl + '+username', 'github-user.svg', this.prWebUrl, this.onDidChangeTreeDataEmitter));
183198

184-
resolveJson(newCommentInfo);
185-
});
199+
this.fillInTimeStampAndPR(newCommentInfo);
200+
201+
resolveJson(newCommentInfo);
202+
});
203+
} else {
204+
this.fillInTimeStampAndPR(newCommentInfo);
205+
206+
return Promise.resolve(newCommentInfo);
207+
}
186208
}
187209

188210
protected updateSelfAfterWebLoad() {
@@ -239,14 +261,17 @@ class PRTreeItem extends GitHubTreeItem {
239261
this.tooltip = `${label}\n${repo}#${prNumber} by @${author}`;
240262

241263
this.convItem = new GitHubLeafTreeItem(
242-
`${repo}#${prNumber} by @${author}`, 'goto' + id, 'github-conversation.svg', onDidChangeTreeDataEmitter
264+
`${repo}#${prNumber} by @${author}`, 'goto' + id, 'github-conversation.svg', undefined,
265+
onDidChangeTreeDataEmitter
243266
);
244267
this.generated.push(this.convItem);
245268
// Diff description must be complemented from prUrl, which can only be done later
246-
this.diffItem = new GitHubLeafTreeItem('Diff', 'diff' + id, 'github-diff.svg', onDidChangeTreeDataEmitter);
269+
this.diffItem = new GitHubLeafTreeItem('Diff', 'diff' + id, 'github-diff.svg', undefined,
270+
onDidChangeTreeDataEmitter);
247271
this.generated.push(this.diffItem);
248272
if (tags) {
249-
this.generated.push(new GitHubLeafTreeItem(tags, 'tags' + id, 'github-tags.svg', onDidChangeTreeDataEmitter));
273+
this.generated.push(new GitHubLeafTreeItem(tags, 'tags' + id, 'github-tags.svg', undefined,
274+
onDidChangeTreeDataEmitter));
250275

251276
}
252277
}

0 commit comments

Comments
 (0)