-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathtriggerWorkflowRun.ts
More file actions
145 lines (124 loc) · 5.1 KB
/
triggerWorkflowRun.ts
File metadata and controls
145 lines (124 loc) · 5.1 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
import * as vscode from "vscode";
import {getGitHead, getGitHubContextForWorkspaceUri, GitHubRepoContext} from "../git/repository";
import {getWorkflowUri, parseWorkflowFile} from "../workflow/workflow";
import {Workflow} from "../model";
interface TriggerRunCommandOptions {
wf?: Workflow;
gitHubRepoContext: GitHubRepoContext;
}
export function registerTriggerWorkflowRun(context: vscode.ExtensionContext) {
context.subscriptions.push(
vscode.commands.registerCommand(
"github-actions.explorer.triggerRun",
async (args: TriggerRunCommandOptions | vscode.Uri) => {
let workflowUri: vscode.Uri | null = null;
if (args instanceof vscode.Uri) {
workflowUri = args;
} else if (args.wf) {
const wf: Workflow = args.wf;
workflowUri = getWorkflowUri(args.gitHubRepoContext, wf.path);
}
if (!workflowUri) {
return;
}
// Parse
const workspaceFolder = vscode.workspace.getWorkspaceFolder(workflowUri);
if (!workspaceFolder) {
return;
}
const gitHubRepoContext = await getGitHubContextForWorkspaceUri(workspaceFolder.uri);
if (!gitHubRepoContext) {
return;
}
const workflow = await parseWorkflowFile(workflowUri);
if (!workflow) {
return;
}
let selectedEvent: string | undefined;
if (workflow.events.workflow_dispatch !== undefined && workflow.events.repository_dispatch !== undefined) {
selectedEvent = await vscode.window.showQuickPick(["repository_dispatch", "workflow_dispatch"], {
placeHolder: "Which event to trigger?"
});
if (!selectedEvent) {
return;
}
}
if (
(!selectedEvent || selectedEvent === "workflow_dispatch") &&
workflow.events.workflow_dispatch !== undefined
) {
const ref = await vscode.window.showInputBox({
prompt: "Enter ref to trigger workflow on",
value: (await getGitHead()) || gitHubRepoContext.defaultBranch
});
if (ref) {
// Inputs
let inputs: {[key: string]: string} | undefined;
const definedInputs = workflow.events.workflow_dispatch?.inputs;
if (definedInputs) {
inputs = {};
for (const definedInput of Object.keys(definedInputs)) {
const value = await vscode.window.showInputBox({
prompt: `Value for input ${definedInput} ${definedInputs[definedInput].required ? "[required]" : ""}`,
value: definedInputs[definedInput].default?.toString() || ""
});
if (!value && definedInputs[definedInput].required) {
return vscode.window.showErrorMessage(`Input ${definedInput} is required`);
}
if (value) {
inputs[definedInput] = value;
}
}
}
try {
const relativeWorkflowPath = vscode.workspace.asRelativePath(workflowUri, false);
await gitHubRepoContext.client.actions.createWorkflowDispatch({
owner: gitHubRepoContext.owner,
repo: gitHubRepoContext.name,
workflow_id: relativeWorkflowPath,
ref,
inputs
});
vscode.window.setStatusBarMessage(`GitHub Actions: Workflow event dispatched`, 2000);
} catch (error) {
return vscode.window.showErrorMessage(`Could not create workflow dispatch: ${(error as Error)?.message}`);
}
}
} else if (
(!selectedEvent || selectedEvent === "repository_dispatch") &&
workflow.events.repository_dispatch !== undefined
) {
let event_type: string | undefined;
const event_types = workflow.events.repository_dispatch.types;
if (Array.isArray(event_types) && event_types?.length > 0) {
const custom_type = "✐ Enter custom type";
const selection = await vscode.window.showQuickPick([custom_type, ...event_types], {
placeHolder: "Select an event_type to dispatch"
});
if (selection === undefined) {
return;
} else if (selection != custom_type) {
event_type = selection;
}
}
if (event_type === undefined) {
event_type = await vscode.window.showInputBox({
prompt: "Enter `event_type` to dispatch to the repository",
value: "default"
});
}
if (event_type) {
await gitHubRepoContext.client.repos.createDispatchEvent({
owner: gitHubRepoContext.owner,
repo: gitHubRepoContext.name,
event_type,
client_payload: {}
});
vscode.window.setStatusBarMessage(`GitHub Actions: Repository event '${event_type}' dispatched`, 2000);
}
}
return vscode.commands.executeCommand("github-actions.explorer.refresh");
}
)
);
}