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
108 lines (89 loc) · 3.34 KB
/
pullRequestModel.ts
File metadata and controls
108 lines (89 loc) · 3.34 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
/*---------------------------------------------------------------------------------------------
* 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 { PullRequest, GithubItemStateEnum, ISuggestedReviewer } from './interface';
import { IssueModel } from './issueModel';
interface IPullRequestModel {
head: GitHubRef | null;
}
export interface IResolvedPullRequestModel extends IPullRequestModel {
head: GitHubRef;
}
export class PullRequestModel extends IssueModel implements IPullRequestModel {
public isDraft?: boolean;
public item: PullRequest;
public localBranchName?: string;
public mergeBase?: string;
public suggestedReviewers?: ISuggestedReviewer[];
private _inDraftMode: boolean = false;
private _onDidChangeDraftMode: vscode.EventEmitter<boolean> = new vscode.EventEmitter<boolean>();
public onDidChangeDraftMode = this._onDidChangeDraftMode.event;
constructor(githubRepository: GitHubRepository, remote: Remote, item: PullRequest) {
super(githubRepository, remote, item);
}
public get isMerged(): boolean {
return this.state === GithubItemStateEnum.Merged;
}
public get inDraftMode(): boolean {
return this._inDraftMode;
}
public set inDraftMode(inDraftMode: boolean) {
if (this._inDraftMode !== inDraftMode) {
this._inDraftMode = inDraftMode;
this._onDidChangeDraftMode.fire(this._inDraftMode);
}
}
public head: GitHubRef | null;
public base: GitHubRef;
protected updateState(state: string) {
if (state.toLowerCase() === 'open') {
this.state = GithubItemStateEnum.Open;
} else {
this.state = this.item.merged ? GithubItemStateEnum.Merged : GithubItemStateEnum.Closed;
}
}
update(item: PullRequest): void {
super.update(item);
this.isDraft = item.isDraft;
this.suggestedReviewers = item.suggestedReviewers;
if (item.head) {
this.head = new GitHubRef(item.head.ref, item.head.label, item.head.sha, item.head.repo.cloneUrl);
}
if (item.base) {
this.base = new GitHubRef(item.base.ref, item.base!.label, item.base!.sha, item.base!.repo.cloneUrl);
}
}
/**
* Validate if the pull request has a valid HEAD.
* Use only when the method can fail silently, otherwise use `validatePullRequestModel`
*/
isResolved(): this is IResolvedPullRequestModel {
return !!this.head;
}
/**
* Validate 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.number}. 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;
}
}