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
124 lines (104 loc) · 3.51 KB
/
Copy pathpullRequestModel.ts
File metadata and controls
124 lines (104 loc) · 3.51 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
/*---------------------------------------------------------------------------------------------
* 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, IPullRequest, IPullRequestModel, PullRequestStateEnum } from './interface';
export class PullRequestModel implements IPullRequestModel {
public prNumber: number;
public title: string;
public html_url: string;
public state: PullRequestStateEnum = PullRequestStateEnum.Open;
public commentCount: number;
public commitCount: number;
public author: IAccount;
public assignee: IAccount;
public createdAt: string;
public updatedAt: string;
public localBranchName?: string;
public labels: string[];
public get isOpen(): boolean {
return this.state === PullRequestStateEnum.Open;
}
public get isMerged(): boolean {
return this.state === PullRequestStateEnum.Merged;
}
public get userAvatar(): string {
if (this.prItem) {
return this.prItem.user.avatar_url;
}
return null;
}
public get userAvatarUri(): vscode.Uri {
if (this.prItem) {
let key = this.userAvatar;
let gravatar = vscode.Uri.parse(`${key}&s=${64}`);
// hack, to ensure queries are not wrongly encoded.
const originalToStringFn = gravatar.toString;
gravatar.toString = function (skipEncoding?: boolean | undefined) {
return originalToStringFn.call(gravatar, true);
};
return gravatar;
}
return null;
}
public get body(): string {
if (this.prItem) {
return this.prItem.body;
}
return null;
}
public head: GitHubRef;
public base: GitHubRef;
constructor(public readonly githubRepository: GitHubRepository, public readonly remote: Remote, public prItem: IPullRequest) {
this.update(prItem);
}
update(prItem: IPullRequest): void {
this.prNumber = prItem.number;
this.title = prItem.title;
this.html_url = prItem.html_url;
this.author = {
login: prItem.user.login,
isUser: prItem.user.type === 'User',
isEnterprise: prItem.user.type === 'Enterprise',
avatarUrl: prItem.user.avatar_url,
htmlUrl: prItem.user.html_url
};
this.labels = prItem.labels.map(label => label.name);
if (prItem.state === 'open') {
this.state = PullRequestStateEnum.Open;
} else {
this.state = prItem.merged ? PullRequestStateEnum.Merged : PullRequestStateEnum.Closed;
}
if (prItem.assignee) {
this.assignee = {
login: prItem.assignee.login,
isUser: prItem.assignee.type === 'User',
isEnterprise: prItem.assignee.type === 'Enterprise',
avatarUrl: prItem.assignee.avatar_url,
htmlUrl: prItem.assignee.html_url
};
}
this.createdAt = prItem.created_at;
this.updatedAt = prItem.updated_at ? prItem.updated_at : this.createdAt;
this.commentCount = prItem.comments;
this.commitCount = prItem.commits;
this.head = new GitHubRef(prItem.head.ref, prItem.head.label, prItem.head.sha, prItem.head.repo.clone_url);
this.base = new GitHubRef(prItem.base.ref, prItem.base.label, prItem.base.sha, prItem.base.repo.clone_url);
}
equals(other: IPullRequestModel): boolean {
if (!other) {
return false;
}
if (this.prNumber !== other.prNumber) {
return false;
}
if (this.html_url !== other.html_url) {
return false;
}
return true;
}
}