forked from microsoft/vscode-pull-request-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpullRequestModel.ts
More file actions
183 lines (149 loc) · 4.95 KB
/
pullRequestModel.ts
File metadata and controls
183 lines (149 loc) · 4.95 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as vscode from 'vscode';
import { GitHubRef } from '../common/githubRef';
import { Remote } from '../common/remote';
import { GitHubRepository } from './githubRepository';
import { IAccount, PullRequest, PullRequestStateEnum, ISuggestedReviewer } from './interface';
interface IPullRequestModel {
head: GitHubRef | null;
}
export interface IResolvedPullRequestModel extends IPullRequestModel {
head: GitHubRef;
}
export class PullRequestModel implements IPullRequestModel {
public id: number;
public graphNodeId: string;
public prNumber: number;
public title: string;
public html_url: string;
public state: PullRequestStateEnum = PullRequestStateEnum.Open;
public author: IAccount;
public assignee: IAccount;
public createdAt: string;
public updatedAt: string;
public localBranchName?: string;
public mergeBase?: string;
public isDraft?: boolean;
public suggestedReviewers: ISuggestedReviewer[];
public get isOpen(): boolean {
return this.state === PullRequestStateEnum.Open;
}
public get isMerged(): boolean {
return this.state === PullRequestStateEnum.Merged;
}
public get userAvatar(): string | undefined {
if (this.prItem) {
return this.prItem.user.avatarUrl;
}
return undefined;
}
public get userAvatarUri(): vscode.Uri | undefined {
if (this.prItem) {
const key = this.userAvatar;
if (key) {
const uri = vscode.Uri.parse(`${key}&s=${64}`);
// hack, to ensure queries are not wrongly encoded.
const originalToStringFn = uri.toString;
uri.toString = function (skipEncoding?: boolean | undefined) {
return originalToStringFn.call(uri, true);
};
return uri;
}
}
return undefined;
}
private _inDraftMode: boolean = false;
public get inDraftMode(): boolean {
return this._inDraftMode;
}
public set inDraftMode(inDraftMode: boolean) {
if (this._inDraftMode !== inDraftMode) {
this._inDraftMode = inDraftMode;
this._onDidChangeDraftMode.fire(this._inDraftMode);
}
}
private _onDidChangeDraftMode: vscode.EventEmitter<boolean> = new vscode.EventEmitter<boolean>();
public onDidChangeDraftMode = this._onDidChangeDraftMode.event;
public get body(): string {
if (this.prItem) {
return this.prItem.body;
}
return '';
}
public bodyHTML?: string;
public head: GitHubRef | null;
public base: GitHubRef;
constructor(public readonly githubRepository: GitHubRepository, public readonly remote: Remote, public prItem: PullRequest) {
this.update(prItem);
}
update(prItem: PullRequest): void {
this.id = prItem.id;
this.graphNodeId = prItem.graphNodeId;
this.prNumber = prItem.number;
this.title = prItem.title;
this.bodyHTML = prItem.bodyHTML;
this.html_url = prItem.url;
this.author = prItem.user;
this.isDraft = prItem.isDraft;
this.suggestedReviewers = prItem.suggestedReviewers;
if (prItem.state.toLowerCase() === 'open') {
this.state = PullRequestStateEnum.Open;
} else {
this.state = prItem.merged ? PullRequestStateEnum.Merged : PullRequestStateEnum.Closed;
}
if (prItem.assignee) {
this.assignee = prItem.assignee;
}
this.createdAt = prItem.createdAt;
this.updatedAt = prItem.updatedAt ? prItem.updatedAt : this.createdAt;
if (prItem.head) {
this.head = new GitHubRef(prItem.head.ref, prItem.head.label, prItem.head.sha, prItem.head.repo.cloneUrl);
}
if (prItem.base) {
this.base = new GitHubRef(prItem.base.ref, prItem.base!.label, prItem.base!.sha, prItem.base!.repo.cloneUrl);
}
}
/**
* Valiate if the pull request has a valid HEAD.
* Use only when the method can fail sliently, otherwise use `validatePullRequestModel`
*/
isResolved(): this is IResolvedPullRequestModel {
return !!this.head;
}
/**
* Valiate if the pull request has a valid HEAD. Show a warning message to users when the pull request is invalid.
* @param message Human readable action execution failure message.
*/
validatePullRequestModel(message?: string): this is IResolvedPullRequestModel {
if (!!this.head) {
return true;
}
const reason = `There is no upstream branch for Pull Request #${this.prNumber}. View it on GitHub for more details`;
if (message) {
message += `: ${reason}`;
} else {
message = reason;
}
vscode.window.showWarningMessage(message, 'Open in GitHub').then(action => {
if (action && action === 'Open in GitHub') {
vscode.commands.executeCommand('vscode.open', vscode.Uri.parse(this.html_url));
}
});
return false;
}
equals(other: PullRequestModel | undefined): boolean {
if (!other) {
return false;
}
if (this.prNumber !== other.prNumber) {
return false;
}
if (this.html_url !== other.html_url) {
return false;
}
return true;
}
}