Repository navigation
Expand file tree
/
Copy pathcommands.ts
More file actions
156 lines (146 loc) · 4.65 KB
/
Copy pathcommands.ts
File metadata and controls
156 lines (146 loc) · 4.65 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
/**
* @file github1s commands
* @author netcon
*/
import * as vscode from 'vscode';
import { getExtensionContext } from '@/helpers/context';
import router from '@/router';
import repository from '@/repository';
import { validateToken } from '@/interfaces/github-api-rest';
export const commandValidateToken = (silent: boolean = false) => {
const context = getExtensionContext();
const oAuthToken =
(context.globalState.get('github-oauth-token') as string) || '';
return validateToken(oAuthToken).then((tokenStatus) => {
if (!silent) {
const remaining = tokenStatus.remaining;
if (!oAuthToken) {
if (remaining > 0) {
vscode.window.showWarningMessage(
`You haven\'t set a GitHub OAuth Token yet, and you can have ${remaining} requests in the current rate limit window.`
);
} else {
vscode.window.showWarningMessage(
"You haven't set a GitHub OAuth Token yet, and the rate limit is exceeded."
);
}
} else if (!tokenStatus.valid) {
vscode.window.showErrorMessage(
'Current GitHub OAuth Token is invalid.'
);
} else if (tokenStatus.valid && tokenStatus.remaining > 0) {
vscode.window.showInformationMessage(
`Current GitHub OAuth Token is OK, and you can have ${remaining} requests in the current rate limit window.`
);
} else if (tokenStatus.valid && tokenStatus.remaining <= 0) {
vscode.window.showWarningMessage(
'Current GitHub OAuth Token is Valid, but the rate limit is exceeded.'
);
}
}
return tokenStatus;
});
};
export const commandUpdateToken = (silent: boolean = false) => {
return vscode.window
.showInputBox({
placeHolder: 'Please input the GitHub OAuth Token',
})
.then((token) => {
if (!token) {
return;
}
return getExtensionContext()!
.globalState.update('github-oauth-token', token || '')
.then(() => {
// we don't need wait validate, so we don't `return`
validateToken(token).then((tokenStatus) => {
if (!silent) {
if (!tokenStatus.valid) {
vscode.window.showErrorMessage(
'GitHub OAuth Token have updated, but it is invalid.'
);
} else if (tokenStatus.remaining <= 0) {
vscode.window.showWarningMessage(
'GitHub OAuth Token have updated, but the rate limit is exceeded.'
);
} else {
vscode.window.showInformationMessage(
'GitHub OAuth Token have updated.'
);
}
}
tokenStatus.valid &&
tokenStatus.remaining > 0 &&
vscode.commands.executeCommand(
'workbench.files.action.refreshFilesExplorer'
);
});
});
});
};
export const commandClearToken = (silent: boolean = false) => {
return vscode.window
.showWarningMessage(
'Would you want to clear the saved GitHub OAuth Token?',
{ modal: true },
'Confirm'
)
.then((choose) => {
if (choose === 'Confirm') {
return getExtensionContext()!
.globalState.update('github-oauth-token', '')
.then(() => {
!silent &&
vscode.window.showInformationMessage(
'You have cleared the saved GitHb OAuth Token.'
);
})
.then(() => true);
}
return false;
});
};
export const commandGetCurrentAuthority = () => router.getAuthority();
export const commandCheckoutRef = async () => {
const [branchRefs, tagRefs] = await Promise.all([
repository.getBranches(),
repository.getTags(),
]);
const branchPickerItems: vscode.QuickPickItem[] = branchRefs.map(
(branchRef) => ({
label: branchRef.name,
description: (branchRef.object?.sha || '').slice(0, 8),
})
);
const tagPickerItems: vscode.QuickPickItem[] = tagRefs.map((tagRef) => ({
label: tagRef.name,
description: `Tag at ${(tagRef.object?.sha || '').slice(0, 8)}`,
}));
const quickPick = vscode.window.createQuickPick();
const routerState = await router.getState();
quickPick.placeholder = routerState.ref;
quickPick.items = [...branchPickerItems, ...tagPickerItems];
quickPick.show();
const choice = await new Promise<vscode.QuickPickItem | undefined>(
(resolve) => quickPick.onDidAccept(() => resolve(quickPick.activeItems[0]))
);
quickPick.hide();
const targetRef = choice?.label || quickPick.value;
if (targetRef.toUpperCase() === 'HEAD') {
router.replace(`/${routerState.owner}/${routerState.repo}`);
return;
}
router.replace(`/${routerState.owner}/${routerState.repo}/tree/${targetRef}`);
};
export const commandOpenGitpod = () => {
return router.getAuthority().then((currentAuthority) => {
const [currentOwner, currentRepo] = currentAuthority.split('+');
vscode.commands.executeCommand(
'vscode.open',
vscode.Uri.parse(
`https://gitpod.io/#https://github.com/${currentOwner}/${currentRepo}`
)
);
});
};