Skip to content
This repository was archived by the owner on Mar 28, 2023. It is now read-only.

Commit 8686c09

Browse files
committed
Trigger workflows for workspaces
1 parent ce9533a commit 8686c09

8 files changed

Lines changed: 250 additions & 196 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@
181181
"editor/title": [
182182
{
183183
"command": "github-actions.explorer.triggerRun",
184-
"when": "githubActions:activeFile =~ /rdispatch/ && resourceExtname =~ /\\.ya?ml/",
184+
"when": "(githubActions:activeFile =~ /rdispatch/ || githubActions:activeFile =~ /wdispatch/) && resourceExtname =~ /\\.ya?ml/",
185185
"group": "navigation"
186186
}
187187
],

src/commands/triggerWorkflowRun.ts

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
import * as vscode from "vscode";
2+
import { getGitHead, getGitHubContextForWorkspaceUri } from "../git/repository";
3+
import { Workflow } from "../model";
4+
import { getWorkflowUri, parseWorkflow } from "../workflow/workflow";
5+
6+
interface TriggerRunCommandOptions {
7+
wf?: Workflow;
8+
}
9+
10+
export function registerTriggerWorkflowRun(context: vscode.ExtensionContext) {
11+
context.subscriptions.push(
12+
vscode.commands.registerCommand(
13+
"github-actions.explorer.triggerRun",
14+
async (args: TriggerRunCommandOptions | vscode.Uri) => {
15+
let workflowUri: vscode.Uri | null = null;
16+
if (args instanceof vscode.Uri) {
17+
workflowUri = args;
18+
} else if (args.wf) {
19+
const wf: Workflow = args.wf;
20+
workflowUri = getWorkflowUri(wf.path);
21+
}
22+
23+
if (!workflowUri) {
24+
return;
25+
}
26+
27+
// Parse
28+
const workspaceFolder =
29+
vscode.workspace.getWorkspaceFolder(workflowUri);
30+
if (!workspaceFolder) {
31+
return;
32+
}
33+
34+
const gitHubRepoContext = await getGitHubContextForWorkspaceUri(
35+
workspaceFolder.uri
36+
);
37+
if (!gitHubRepoContext) {
38+
return;
39+
}
40+
41+
const workflow = await parseWorkflow(workflowUri, gitHubRepoContext);
42+
if (!workflow) {
43+
return;
44+
}
45+
46+
let selectedEvent: string | undefined;
47+
if (
48+
workflow.on.workflow_dispatch !== undefined &&
49+
workflow.on.repository_dispatch !== undefined
50+
) {
51+
selectedEvent = await vscode.window.showQuickPick(
52+
["repository_dispatch", "workflow_dispatch"],
53+
{
54+
placeHolder: "Which event to trigger?",
55+
}
56+
);
57+
if (!selectedEvent) {
58+
return;
59+
}
60+
}
61+
62+
if (
63+
(!selectedEvent || selectedEvent === "workflow_dispatch") &&
64+
workflow.on.workflow_dispatch !== undefined
65+
) {
66+
const ref = await vscode.window.showInputBox({
67+
prompt: "Enter ref to trigger workflow on",
68+
value: (await getGitHead()) || gitHubRepoContext.defaultBranch,
69+
});
70+
71+
if (ref) {
72+
// Inputs
73+
let inputs: { [key: string]: string } | undefined;
74+
const definedInputs = workflow.on.workflow_dispatch?.inputs;
75+
if (definedInputs) {
76+
inputs = {};
77+
78+
for (const definedInput of Object.keys(definedInputs)) {
79+
const value = await vscode.window.showInputBox({
80+
prompt: `Value for input ${definedInput} ${
81+
definedInputs[definedInput].required ? "[required]" : ""
82+
}`,
83+
value: definedInputs[definedInput].default,
84+
});
85+
if (!value && definedInputs[definedInput].required) {
86+
vscode.window.showErrorMessage(
87+
`Input ${definedInput} is required`
88+
);
89+
return;
90+
}
91+
92+
if (value) {
93+
inputs[definedInput] = value;
94+
}
95+
}
96+
}
97+
98+
try {
99+
const relativeWorkflowPath = vscode.workspace.asRelativePath(
100+
workflowUri,
101+
false
102+
);
103+
104+
await gitHubRepoContext.client.actions.createWorkflowDispatch({
105+
owner: gitHubRepoContext.owner,
106+
repo: gitHubRepoContext.name,
107+
workflow_id: relativeWorkflowPath,
108+
ref,
109+
inputs,
110+
});
111+
112+
vscode.window.setStatusBarMessage(
113+
`GitHub Actions: Workflow event dispatched`,
114+
2000
115+
);
116+
} catch (error) {
117+
vscode.window.showErrorMessage(
118+
`Could not create workflow dispatch: ${error.message}`
119+
);
120+
}
121+
}
122+
} else if (
123+
(!selectedEvent || selectedEvent === "repository_dispatch") &&
124+
workflow.on.repository_dispatch !== undefined
125+
) {
126+
let event_type: string | undefined;
127+
const event_types = workflow.on.repository_dispatch.types;
128+
if (Array.isArray(event_types) && event_types?.length > 0) {
129+
const custom_type = "✐ Enter custom type";
130+
const selection = await vscode.window.showQuickPick(
131+
[custom_type, ...event_types],
132+
{
133+
placeHolder: "Select an event_type to dispatch",
134+
}
135+
);
136+
137+
if (selection === undefined) {
138+
return;
139+
} else if (selection != custom_type) {
140+
event_type = selection;
141+
}
142+
}
143+
144+
if (event_type === undefined) {
145+
event_type = await vscode.window.showInputBox({
146+
prompt: "Enter `event_type` to dispatch to the repository",
147+
value: "default",
148+
});
149+
}
150+
151+
if (event_type) {
152+
await gitHubRepoContext.client.repos.createDispatchEvent({
153+
owner: gitHubRepoContext.owner,
154+
repo: gitHubRepoContext.name,
155+
event_type,
156+
client_payload: {},
157+
});
158+
159+
vscode.window.setStatusBarMessage(
160+
`GitHub Actions: Repository event '${event_type}' dispatched`,
161+
2000
162+
);
163+
}
164+
}
165+
166+
vscode.commands.executeCommand("github-actions.explorer.refresh");
167+
}
168+
)
169+
);
170+
}

src/extension.ts

Lines changed: 3 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as vscode from "vscode";
22
import { registerOpenWorkflowFile } from "./commands/openWorkflowFile";
33
import { registerOpenWorkflowRun } from "./commands/openWorkflowRun";
44
import { registerOpenWorkflowRunLogs } from "./commands/openWorkflowRunLogs";
5+
import { registerTriggerWorkflowRun } from "./commands/triggerWorkflowRun";
56
import { getGitHubContext } from "./git/repository";
67
import { LogScheme } from "./logs/constants";
78
import { WorkflowStepLogProvider } from "./logs/fileProvider";
@@ -59,150 +60,10 @@ export function activate(context: vscode.ExtensionContext) {
5960
registerOpenWorkflowFile(context);
6061
registerOpenWorkflowRunLogs(context);
6162

62-
/*
63-
context.subscriptions.push(
64-
vscode.commands.registerCommand(
65-
"github-actions.explorer.triggerRun",
66-
async (args) => {
67-
let workflowUri: vscode.Uri | null = null;
68-
const wf: Workflow = args.wf;
69-
if (wf) {
70-
workflowUri = getWorkflowUri(wf.path);
71-
} else if (args.fsPath) {
72-
workflowUri = args;
73-
}
74-
75-
if (!workflowUri) {
76-
return;
77-
}
78-
79-
// Parse
80-
const gitHubContext: GitHubContext =
81-
args.gitHubContext || (await getGitHubContext());
82-
const workflow = await parseWorkflow(workflowUri, gitHubContext);
83-
if (!workflow) {
84-
return;
85-
}
63+
registerTriggerWorkflowRun(context);
8664

87-
let selectedEvent: string | undefined;
88-
if (
89-
workflow.on.workflow_dispatch !== undefined &&
90-
workflow.on.repository_dispatch !== undefined
91-
) {
92-
selectedEvent = await vscode.window.showQuickPick(
93-
["repository_dispatch", "workflow_dispatch"],
94-
{
95-
placeHolder: "Which event to trigger?",
96-
}
97-
);
98-
if (!selectedEvent) {
99-
return;
100-
}
101-
}
102-
103-
if (
104-
(!selectedEvent || selectedEvent === "workflow_dispatch") &&
105-
workflow.on.workflow_dispatch !== undefined
106-
) {
107-
const ref = await vscode.window.showInputBox({
108-
prompt: "Enter ref to trigger workflow on",
109-
value: (await getGitHead()) || gitHubContext.defaultBranch,
110-
});
111-
112-
if (ref) {
113-
// Inputs
114-
let inputs: { [key: string]: string } | undefined;
115-
const definedInputs = workflow.on.workflow_dispatch.inputs;
116-
if (definedInputs) {
117-
inputs = {};
118-
119-
for (const definedInput of Object.keys(definedInputs)) {
120-
const value = await vscode.window.showInputBox({
121-
prompt: `Value for input ${definedInput} ${
122-
definedInputs[definedInput].required ? "[required]" : ""
123-
}`,
124-
value: definedInputs[definedInput].default,
125-
});
126-
if (!value && definedInputs[definedInput].required) {
127-
vscode.window.showErrorMessage(
128-
`Input ${definedInput} is required`
129-
);
130-
return;
131-
}
132-
133-
if (value) {
134-
inputs[definedInput] = value;
135-
}
136-
}
137-
}
138-
139-
try {
140-
await gitHubContext.client.actions.createWorkflowDispatch({
141-
owner: gitHubContext.owner,
142-
repo: gitHubContext.name,
143-
workflow_id: wf.id,
144-
ref,
145-
inputs,
146-
});
147-
148-
vscode.window.setStatusBarMessage(
149-
`GitHub Actions: Workflow event dispatched`,
150-
2000
151-
);
152-
} catch (error) {
153-
vscode.window.showErrorMessage(
154-
`Could not create workflow dispatch: ${error.message}`
155-
);
156-
}
157-
}
158-
} else if (
159-
(!selectedEvent || selectedEvent === "repository_dispatch") &&
160-
workflow.on.repository_dispatch !== undefined
161-
) {
162-
let event_type: string | undefined;
163-
const event_types = workflow.on.repository_dispatch.types;
164-
if (Array.isArray(event_types) && event_types?.length > 0) {
165-
const custom_type = "✐ Enter custom type";
166-
const selection = await vscode.window.showQuickPick(
167-
[custom_type, ...event_types],
168-
{
169-
placeHolder: "Select an event_type to dispatch",
170-
}
171-
);
172-
173-
if (selection === undefined) {
174-
return;
175-
} else if (selection != custom_type) {
176-
event_type = selection;
177-
}
178-
}
179-
180-
if (event_type === undefined) {
181-
event_type = await vscode.window.showInputBox({
182-
prompt: "Enter `event_type` to dispatch to the repository",
183-
value: "default",
184-
});
185-
}
186-
187-
if (event_type) {
188-
await gitHubContext.client.repos.createDispatchEvent({
189-
owner: gitHubContext.owner,
190-
repo: gitHubContext.name,
191-
event_type,
192-
client_payload: {},
193-
});
194-
195-
vscode.window.setStatusBarMessage(
196-
`GitHub Actions: Repository event '${event_type}' dispatched`,
197-
2000
198-
);
199-
}
200-
}
65+
/*
20166
202-
workflowTreeProvider.refresh();
203-
}
204-
)
205-
);
20667
20768
context.subscriptions.push(
20869
vscode.commands.registerCommand(

0 commit comments

Comments
 (0)