Skip to content

Commit 13fd241

Browse files
committed
Show Play button for workflows with repo_dispatch trigger
1 parent f337cbc commit 13fd241

File tree

6 files changed

+83
-6
lines changed

6 files changed

+83
-6
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,11 @@
7474
{
7575
"command": "explorer.triggerRun",
7676
"title": "Trigger workflow",
77-
"when": "viewItem =~ /workflow/ && viewItem =~ /rdispatch/"
77+
"when": "viewItem =~ /workflow/ && viewItem =~ /rdispatch/",
78+
"icon": {
79+
"dark": "resources/icons/dark/run.svg",
80+
"light": "resources/icons/light/run.svg"
81+
}
7882
},
7983
{
8084
"command": "workflow.run.open",
@@ -154,6 +158,13 @@
154158
"when": "view == workflows || view == settings"
155159
}
156160
],
161+
"editor/title": [
162+
{
163+
"command": "explorer.triggerRun",
164+
"when": "githubActions:activeFile =~ /rdispatch/ && resourceExtname =~ /\\.ya?ml/",
165+
"group": "navigation"
166+
}
167+
],
157168
"view/item/context": [
158169
{
159170
"command": "explorer.openWorkflowFile",

resources/icons/dark/run.svg

Lines changed: 3 additions & 0 deletions
Loading

resources/icons/light/run.svg

Lines changed: 3 additions & 0 deletions
Loading

src/extension.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { Octokit } from "@octokit/rest";
22
import * as vscode from "vscode";
33
import { setPAT } from "./auth/pat";
4+
import { getClient } from "./client/client";
45
import { initConfiguration } from "./configuration/configuration";
56
import { Protocol } from "./external/protocol";
6-
import { getGitHubUrl } from "./git/repository";
7+
import { getGitHubProtocol, getGitHubUrl } from "./git/repository";
78
import { LogScheme } from "./logs/constants";
89
import { WorkflowStepLogProvider } from "./logs/fileProvider";
910
import { WorkflowStepLogFoldingProvider } from "./logs/foldingProvider";
@@ -20,6 +21,7 @@ import {
2021
} from "./model";
2122
import { initPinnedWorkflows } from "./pinnedWorkflows/pinnedWorkflows";
2223
import { encodeSecret } from "./secrets";
24+
import { initWorkflowDocumentTracking } from "./tracker/workflowDocumentTracker";
2325
import { initResources } from "./treeViews/icons";
2426
import { SettingsTreeProvider } from "./treeViews/settings";
2527
import { ActionsExplorerProvider as WorkflowsTreeProvider } from "./treeViews/workflows";
@@ -37,6 +39,9 @@ export function activate(context: vscode.ExtensionContext) {
3739
initConfiguration(context);
3840
initPinnedWorkflows(context);
3941

42+
// Track workflow
43+
initWorkflowDocumentTracking(context);
44+
4045
// Actions Explorer
4146
const workflowTreeProvider = new WorkflowsTreeProvider();
4247
context.subscriptions.push(
@@ -83,8 +88,15 @@ export function activate(context: vscode.ExtensionContext) {
8388
vscode.commands.registerCommand("explorer.triggerRun", async (args) => {
8489
let event_type: string | undefined;
8590

91+
let workflowUri: vscode.Uri | null = null;
92+
8693
const wf: Workflow = args.wf;
87-
const workflowUri = getWorkflowUri(wf.path);
94+
if (wf) {
95+
workflowUri = getWorkflowUri(wf.path);
96+
} else if (args.fsPath) {
97+
workflowUri = args;
98+
}
99+
88100
if (!workflowUri) {
89101
return;
90102
}
@@ -114,8 +126,8 @@ export function activate(context: vscode.ExtensionContext) {
114126
}
115127

116128
if (event_type) {
117-
const repo: Protocol = args.repo;
118-
const client: Octokit = args.client;
129+
const repo: Protocol = args.repo || (await getGitHubProtocol());
130+
const client: Octokit = args.client || (await getClient());
119131

120132
await client.repos.createDispatchEvent({
121133
owner: repo.owner,
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { extname } from "path";
2+
import * as vscode from "vscode";
3+
import { usesRepositoryDispatch } from "../workflow/workflow";
4+
5+
export function initWorkflowDocumentTracking(context: vscode.ExtensionContext) {
6+
context.subscriptions.push(
7+
vscode.window.onDidChangeActiveTextEditor(onDidChangeActiveTextEditor)
8+
);
9+
10+
// Check for initial document
11+
onDidChangeActiveTextEditor(vscode.window.activeTextEditor);
12+
}
13+
14+
function onDidChangeActiveTextEditor(editor?: vscode.TextEditor) {
15+
if (!editor || !isTextEditor(editor)) {
16+
return;
17+
}
18+
19+
// Check if the file is saved and could be a workflow
20+
if (
21+
!editor.document.uri?.fsPath ||
22+
editor.document.uri.scheme !== "file" ||
23+
(extname(editor.document.fileName) !== ".yaml" &&
24+
extname(editor.document.fileName) !== ".yml") ||
25+
editor.document.fileName.indexOf(".github/workflows") === -1
26+
) {
27+
return;
28+
}
29+
30+
vscode.commands.executeCommand(
31+
"setContext",
32+
"githubActions:activeFile",
33+
usesRepositoryDispatch(editor.document.uri.fsPath) ? "rdispatch" : ""
34+
);
35+
}
36+
37+
// Adapted from from https://github.com/eamodio/vscode-gitlens/blob/f22a9cd4199ac498c217643282a6a412e1fc01ae/src/constants.ts#L74
38+
enum DocumentSchemes {
39+
DebugConsole = "debug",
40+
Output = "output",
41+
}
42+
43+
function isTextEditor(editor: vscode.TextEditor): boolean {
44+
const scheme = editor.document.uri.scheme;
45+
return (
46+
scheme !== DocumentSchemes.Output && scheme !== DocumentSchemes.DebugConsole
47+
);
48+
}

0 commit comments

Comments
 (0)