forked from github/vscode-github-actions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfiguration.ts
More file actions
89 lines (73 loc) · 3.05 KB
/
configuration.ts
File metadata and controls
89 lines (73 loc) · 3.05 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
import * as vscode from "vscode";
import {deactivateLanguageServer, initLanguageServer} from "../workflow/languageServer";
import {resetGitHubContext} from "../git/repository";
const settingsKey = "github-actions";
const DEFAULT_GITHUB_API = "https://api.github.com";
export function initConfiguration(context: vscode.ExtensionContext) {
context.subscriptions.push(
vscode.workspace.onDidChangeConfiguration(async e => {
if (e.affectsConfiguration(getSettingsKey("workflows.pinned"))) {
pinnedWorkflowsChangeHandlers.forEach(h => h());
} else if (
e.affectsConfiguration(getSettingsKey("use-enterprise")) ||
(useEnterprise() &&
(e.affectsConfiguration("github-enterprise.uri") || e.affectsConfiguration(getSettingsKey("remote-name"))))
) {
await updateLanguageServerApiUrl(context);
resetGitHubContext();
await vscode.commands.executeCommand("github-actions.explorer.refresh");
}
})
);
}
function getConfiguration() {
return vscode.workspace.getConfiguration();
}
function getSettingsKey(settingsPath: string): string {
return `${settingsKey}.${settingsPath}`;
}
const pinnedWorkflowsChangeHandlers: (() => void)[] = [];
export function onPinnedWorkflowsChange(handler: () => void) {
pinnedWorkflowsChangeHandlers.push(handler);
}
export function getPinnedWorkflows(): string[] {
return getConfiguration().get<string[]>(getSettingsKey("workflows.pinned.workflows"), []);
}
export async function pinWorkflow(workflow: string) {
const pinedWorkflows = Array.from(new Set(getPinnedWorkflows()).add(workflow));
await getConfiguration().update(getSettingsKey("workflows.pinned.workflows"), pinedWorkflows);
}
export async function unpinWorkflow(workflow: string) {
const x = new Set(getPinnedWorkflows());
x.delete(workflow);
const pinnedWorkflows = Array.from(x);
await getConfiguration().update(getSettingsKey("workflows.pinned.workflows"), pinnedWorkflows);
}
export function isPinnedWorkflowsRefreshEnabled(): boolean {
return getConfiguration().get<boolean>(getSettingsKey("workflows.pinned.refresh.enabled"), false);
}
export function pinnedWorkflowsRefreshInterval(): number {
return getConfiguration().get<number>(getSettingsKey("workflows.pinned.refresh.interval"), 60);
}
export function getRemoteName(): string {
return getConfiguration().get<string>(getSettingsKey("remote-name"), "origin");
}
export function useEnterprise(): boolean {
return getConfiguration().get<boolean>(getSettingsKey("use-enterprise"), false);
}
export function getGitHubApiUri(): string {
if (!useEnterprise()) return DEFAULT_GITHUB_API;
const base = getConfiguration().get<string>("github-enterprise.uri", DEFAULT_GITHUB_API).replace(/\/$/, "");
if (base === DEFAULT_GITHUB_API) {
return base;
}
if (base.endsWith(".ghe.com")) {
return base.replace(/^(https?):\/\//, "$1://api.");
} else {
return `${base}/api/v3`;
}
}
async function updateLanguageServerApiUrl(context: vscode.ExtensionContext) {
await deactivateLanguageServer();
await initLanguageServer(context);
}