Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add keybinding to run workflow in active editor #141

Merged
merged 1 commit into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@
"when": "githubLocalActions:noSettings && workspaceFolderCount > 0"
}
],
"keybindings": [
{
"command": "githubLocalActions.runWorkflow",
"key": "ctrl+g",
"mac": "cmd+g"
}
],
"commands": [
{
"category": "GitHub Local Actions",
Expand Down
6 changes: 3 additions & 3 deletions src/act.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ export class Act {
path: workspaceFolder.uri.fsPath,
workflow: workflow,
options: [
`${Option.Workflows} ".github/workflows/${path.parse(workflow.uri.fsPath).base}"`
`${Option.Workflows} "${WorkflowsManager.WORKFLOWS_DIRECTORY}/${path.parse(workflow.uri.fsPath).base}"`
],
name: workflow.name,
extraHeader: [
Expand All @@ -281,7 +281,7 @@ export class Act {
path: workspaceFolder.uri.fsPath,
workflow: workflow,
options: [
`${Option.Workflows} ".github/workflows/${path.parse(workflow.uri.fsPath).base}"`,
`${Option.Workflows} "${WorkflowsManager.WORKFLOWS_DIRECTORY}/${path.parse(workflow.uri.fsPath).base}"`,
`${Option.Job} "${job.id}"`
],
name: `${workflow.name}/${job.name}`,
Expand All @@ -304,7 +304,7 @@ export class Act {
path: workspaceFolder.uri.fsPath,
workflow: workflow,
options: [
`${event} ${Option.Workflows} ".github/workflows/${path.parse(workflow.uri.fsPath).base}"`
`${event} ${Option.Workflows} "${WorkflowsManager.WORKFLOWS_DIRECTORY}/${path.parse(workflow.uri.fsPath).base}"`
],
name: `${workflow.name} (${event})`,
extraHeader: [
Expand Down
3 changes: 2 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import HistoryTreeDataProvider from './views/history/historyTreeDataProvider';
import SettingTreeItem from './views/settings/setting';
import SettingsTreeDataProvider from './views/settings/settingsTreeDataProvider';
import WorkflowsTreeDataProvider from './views/workflows/workflowsTreeDataProvider';
import { WorkflowsManager } from './workflowsManager';

export let act: Act;
export let componentsTreeDataProvider: ComponentsTreeDataProvider;
Expand Down Expand Up @@ -36,7 +37,7 @@ export function activate(context: vscode.ExtensionContext) {
});

// Create file watcher
const workflowsFileWatcher = workspace.createFileSystemWatcher('**/.github/workflows/*.{yml,yaml}');
const workflowsFileWatcher = workspace.createFileSystemWatcher(`**/${WorkflowsManager.WORKFLOWS_DIRECTORY}/*.{${WorkflowsManager.YML_EXTENSION},${WorkflowsManager.YAML_EXTENSION}}`);
workflowsFileWatcher.onDidCreate(() => {
workflowsTreeDataProvider.refresh();
settingsTreeDataProvider.refresh();
Expand Down
39 changes: 38 additions & 1 deletion src/views/workflows/workflowsTreeDataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { CancellationToken, commands, EventEmitter, ExtensionContext, TreeDataPr
import { Event } from "../../act";
import { act } from "../../extension";
import { Utils } from "../../utils";
import { WorkflowsManager } from "../../workflowsManager";
import { GithubLocalActionsTreeItem } from "../githubLocalActionsTreeItem";
import JobTreeItem from "./job";
import WorkflowTreeItem from "./workflow";
Expand Down Expand Up @@ -51,7 +52,43 @@ export default class WorkflowsTreeDataProvider implements TreeDataProvider<Githu
}
}),
commands.registerCommand('githubLocalActions.runWorkflow', async (workflowTreeItem: WorkflowTreeItem) => {
await act.runWorkflow(workflowTreeItem.workspaceFolder, workflowTreeItem.workflow);
if (workflowTreeItem) {
await act.runWorkflow(workflowTreeItem.workspaceFolder, workflowTreeItem.workflow);
} else {
let errorMessage: string | undefined;

const activeTextEditor = window.activeTextEditor;
if (activeTextEditor) {
const uri = activeTextEditor.document.uri;
const fileName = path.parse(uri.fsPath).base;
if (uri.path.match(`.*/${WorkflowsManager.WORKFLOWS_DIRECTORY}/.*\\.(${WorkflowsManager.YAML_EXTENSION}|${WorkflowsManager.YML_EXTENSION})`)) {
const workspaceFolder = workspace.getWorkspaceFolder(uri);
if (workspaceFolder) {
const workflows = await act.workflowsManager.getWorkflows(workspaceFolder);
const workflow = workflows.find(workflow => workflow.uri.fsPath === uri.fsPath);
if (workflow) {
await act.runWorkflow(workspaceFolder, workflow);
} else {
errorMessage = `Workflow not found in workflow directory (${WorkflowsManager.WORKFLOWS_DIRECTORY}).`;
}
} else {
errorMessage = `${fileName} must be opened in a workspace folder to be executed locally.`;
}
} else {
errorMessage = `${fileName} is not a workflow that can be executed locally.`;
}
} else {
errorMessage = 'No workflow opened to execute locally.';
}

if (errorMessage) {
window.showErrorMessage(errorMessage, 'View Workflows').then(async value => {
if (value === 'View Workflows') {
await commands.executeCommand('workflows.focus');
}
});
}
}
}),
commands.registerCommand('githubLocalActions.runJob', async (jobTreeItem: JobTreeItem) => {
await act.runJob(jobTreeItem.workspaceFolder, jobTreeItem.workflow, jobTreeItem.job);
Expand Down
6 changes: 5 additions & 1 deletion src/workflowsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ export interface Job {
}

export class WorkflowsManager {
static WORKFLOWS_DIRECTORY: string = '.github/workflows';
static YAML_EXTENSION: string = 'yaml';
static YML_EXTENSION: string = 'yml';

async getWorkflows(workspaceFolder: WorkspaceFolder): Promise<Workflow[]> {
const workflows: Workflow[] = [];

const workflowFileUris = await workspace.findFiles(new RelativePattern(workspaceFolder, `.github/workflows/*.{yml,yaml}`));
const workflowFileUris = await workspace.findFiles(new RelativePattern(workspaceFolder, `${WorkflowsManager.WORKFLOWS_DIRECTORY}/*.{${WorkflowsManager.YAML_EXTENSION},${WorkflowsManager.YML_EXTENSION}}`));
for await (const workflowFileUri of workflowFileUris) {
let yamlContent: any | undefined;

Expand Down
Loading