forked from microsoft/vscode-pull-request-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpullRequestOverviewCommon.ts
More file actions
149 lines (133 loc) · 4.95 KB
/
Copy pathpullRequestOverviewCommon.ts
File metadata and controls
149 lines (133 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import * as vscode from 'vscode';
import {
DEFAULT_DELETION_METHOD,
PR_SETTINGS_NAMESPACE,
SELECT_LOCAL_BRANCH,
SELECT_REMOTE,
} from '../common/settingKeys';
import { Schemes } from '../common/uri';
import { FolderRepositoryManager } from './folderRepositoryManager';
import { PullRequestModel } from './pullRequestModel';
export namespace PullRequestView {
export async function deleteBranch(folderRepositoryManager: FolderRepositoryManager, item: PullRequestModel): Promise<{ isReply: boolean, message: any }> {
const branchInfo = await folderRepositoryManager.getBranchNameForPullRequest(item);
const actions: (vscode.QuickPickItem & { type: 'upstream' | 'local' | 'remote' | 'suspend' })[] = [];
const defaultBranch = await folderRepositoryManager.getPullRequestRepositoryDefaultBranch(item);
if (item.isResolved()) {
const branchHeadRef = item.head.ref;
const isDefaultBranch = defaultBranch === item.head.ref;
if (!isDefaultBranch && !item.isRemoteHeadDeleted) {
actions.push({
label: vscode.l10n.t('Delete remote branch {0}', `${item.remote.remoteName}/${branchHeadRef}`),
description: `${item.remote.normalizedHost}/${item.remote.owner}/${item.remote.repositoryName}`,
type: 'upstream',
picked: true,
});
}
}
if (branchInfo) {
const preferredLocalBranchDeletionMethod = vscode.workspace
.getConfiguration(PR_SETTINGS_NAMESPACE)
.get<boolean>(`${DEFAULT_DELETION_METHOD}.${SELECT_LOCAL_BRANCH}`);
actions.push({
label: vscode.l10n.t('Delete local branch {0}', branchInfo.branch),
type: 'local',
picked: !!preferredLocalBranchDeletionMethod,
});
const preferredRemoteDeletionMethod = vscode.workspace
.getConfiguration(PR_SETTINGS_NAMESPACE)
.get<boolean>(`${DEFAULT_DELETION_METHOD}.${SELECT_REMOTE}`);
if (branchInfo.remote && branchInfo.createdForPullRequest && !branchInfo.remoteInUse) {
actions.push({
label: vscode.l10n.t('Delete remote {0}, which is no longer used by any other branch', branchInfo.remote),
type: 'remote',
picked: !!preferredRemoteDeletionMethod,
});
}
}
if (vscode.env.remoteName === 'codespaces') {
actions.push({
label: vscode.l10n.t('Suspend Codespace'),
type: 'suspend'
});
}
if (!actions.length) {
vscode.window.showWarningMessage(
vscode.l10n.t('There is no longer an upstream or local branch for Pull Request #{0}', item.number),
);
return {
isReply: true,
message: {
cancelled: true
}
};
}
const selectedActions = await vscode.window.showQuickPick(actions, {
canPickMany: true,
ignoreFocusOut: true,
});
const deletedBranchTypes: string[] = [];
if (selectedActions) {
const isBranchActive = item.equals(folderRepositoryManager.activePullRequest);
const promises = selectedActions.map(async action => {
switch (action.type) {
case 'upstream':
await folderRepositoryManager.deleteBranch(item);
deletedBranchTypes.push(action.type);
await folderRepositoryManager.repository.fetch({ prune: true });
// If we're in a remote repository, then we should checkout the default branch.
if (folderRepositoryManager.repository.rootUri.scheme === Schemes.VscodeVfs) {
await folderRepositoryManager.repository.checkout(defaultBranch);
}
return;
case 'local':
if (isBranchActive) {
if (folderRepositoryManager.repository.state.workingTreeChanges.length) {
const yes = vscode.l10n.t('Yes');
const response = await vscode.window.showWarningMessage(
vscode.l10n.t('Your local changes will be lost, do you want to continue?'),
{ modal: true },
yes,
);
if (response === yes) {
await vscode.commands.executeCommand('git.cleanAll');
} else {
return;
}
}
await folderRepositoryManager.repository.checkout(defaultBranch);
}
await folderRepositoryManager.repository.deleteBranch(branchInfo!.branch, true);
return deletedBranchTypes.push(action.type);
case 'remote':
deletedBranchTypes.push(action.type);
return folderRepositoryManager.repository.removeRemote(branchInfo!.remote!);
case 'suspend':
deletedBranchTypes.push(action.type);
return vscode.commands.executeCommand('github.codespaces.disconnectSuspend');
}
});
await Promise.all(promises);
vscode.commands.executeCommand('pr.refreshList');
return {
isReply: false,
message: {
command: 'pr.deleteBranch',
branchTypes: deletedBranchTypes
}
};
} else {
return {
isReply: true,
message: {
cancelled: true
}
};
}
}
}