forked from microsoft/vscode-pull-request-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcurrentIssue.ts
More file actions
156 lines (140 loc) · 5.83 KB
/
Copy pathcurrentIssue.ts
File metadata and controls
156 lines (140 loc) · 5.83 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { PullRequestManager, PullRequestDefaults } from '../github/pullRequestManager';
import { IssueModel } from '../github/issueModel';
import * as vscode from 'vscode';
import { ISSUES_CONFIGURATION, variableSubstitution, BRANCH_NAME_CONFIGURATION, getIssueNumberLabel, BRANCH_CONFIGURATION } from './util';
import { Repository } from '../typings/git';
import { StateManager, IssueState } from './stateManager';
export class CurrentIssue {
private statusBarItem: vscode.StatusBarItem | undefined;
private repoChangeDisposable: vscode.Disposable | undefined;
private _branchName: string | undefined;
private user: string | undefined;
private repo: Repository | undefined;
private repoDefaults: PullRequestDefaults | undefined;
constructor(private issueModel: IssueModel, private manager: PullRequestManager, private stateManager: StateManager, private shouldPromptForBranch?: boolean) {
this.setRepo();
}
private setRepo() {
for (let i = 0; i < this.stateManager.gitAPI.repositories.length; i++) {
const repo = this.stateManager.gitAPI.repositories[i];
for (let j = 0; j < repo.state.remotes.length; j++) {
const remote = repo.state.remotes[j];
if (remote.name === this.issueModel.githubRepository.remote.remoteName &&
(remote.fetchUrl?.toLowerCase().search(`${this.issueModel.githubRepository.remote.owner.toLowerCase()}/${this.issueModel.githubRepository.remote.repositoryName.toLowerCase()}`) !== -1)) {
this.repo = repo;
return;
}
}
}
}
get branchName(): string | undefined {
return this._branchName;
}
get issue(): IssueModel {
return this.issueModel;
}
public async startWorking() {
try {
this.repoDefaults = await this.manager.getPullRequestDefaults();
} catch (e) {
// leave repoDefaults undefined
vscode.window.showErrorMessage('There is no remote. Can\'t start working on an issue.');
}
await this.createIssueBranch();
await this.setCommitMessageAndGitEvent();
this.setStatusBar();
}
public dispose() {
this.statusBarItem?.hide();
this.statusBarItem?.dispose();
this.repoChangeDisposable?.dispose();
}
public async stopWorking() {
if (this.repo) {
this.repo.inputBox.value = '';
}
if (this.repoDefaults) {
await this.manager.repository.checkout(this.repoDefaults.base);
}
this.dispose();
}
private getBasicBranchName(user: string): string {
return `${user}/issue${this.issueModel.number}`;
}
private async branchExists(branch: string): Promise<boolean> {
try {
const repoBranch = await this.manager.repository.getBranch(branch);
return !!repoBranch;
} catch (e) {
// branch doesn't exist
}
return false;
}
private async createOrCheckoutBranch(branch: string): Promise<void> {
if (await this.branchExists(branch)) {
await this.manager.repository.checkout(branch);
} else {
await this.manager.repository.createBranch(branch, true);
}
}
private async getUser(): Promise<string> {
if (!this.user) {
this.user = await this.issueModel.githubRepository.getAuthenticatedUser();
}
return this.user;
}
private async createIssueBranch(): Promise<void> {
const createBranchConfig = this.shouldPromptForBranch ? 'prompt' : <string>vscode.workspace.getConfiguration(ISSUES_CONFIGURATION).get(BRANCH_CONFIGURATION);
if (createBranchConfig === 'off') {
return;
}
const state: IssueState = this.stateManager.getSavedIssueState(this.issueModel.number);
this._branchName = this.shouldPromptForBranch ? undefined : state.branch;
if (!this._branchName) {
if (createBranchConfig === 'on') {
const branchNameConfig = <string>vscode.workspace.getConfiguration(ISSUES_CONFIGURATION).get(BRANCH_NAME_CONFIGURATION);
this._branchName = await variableSubstitution(branchNameConfig, this.issue, undefined, await this.getUser());
} else {
this._branchName = await vscode.window.showInputBox({ placeHolder: `issue${this.issueModel.number}`, prompt: 'Enter the label for the new branch.' });
}
}
if (!this._branchName) {
this._branchName = this.getBasicBranchName(await this.getUser());
}
state.branch = this._branchName;
this.stateManager.setSavedIssueState(this.issueModel, state);
try {
await this.createOrCheckoutBranch(this._branchName);
} catch (e) {
const basicBranchName = this.getBasicBranchName(await this.getUser());
if (this._branchName === basicBranchName) {
vscode.window.showErrorMessage(`Unable to checkout branch ${this._branchName}. There may be file conflicts that prevent this branch change.`);
this._branchName = undefined;
return;
}
vscode.window.showErrorMessage(`Unable to create branch with name ${this._branchName}. Using ${basicBranchName} instead.`);
this._branchName = basicBranchName;
state.branch = this._branchName;
this.stateManager.setSavedIssueState(this.issueModel, state);
await this.createOrCheckoutBranch(this._branchName);
}
}
private async setCommitMessageAndGitEvent() {
const configuration = vscode.workspace.getConfiguration(ISSUES_CONFIGURATION).get('workingIssueFormatScm');
if (this.repo && typeof configuration === 'string') {
this.repo.inputBox.value = await variableSubstitution(configuration, this.issueModel, this.repoDefaults);
}
return;
}
private setStatusBar() {
this.statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, 0);
this.statusBarItem.text = `$(issues) Issue ${getIssueNumberLabel(this.issueModel, this.repoDefaults)}`;
this.statusBarItem.tooltip = this.issueModel.title;
this.statusBarItem.command = 'issue.statusBar';
this.statusBarItem.show();
}
}