Add multi-workspace support

Signed-off-by: Sanjula Ganepola <sanjulagane@gmail.com>
This commit is contained in:
Sanjula Ganepola
2024-10-19 21:44:36 -04:00
parent 49e934e297
commit 524a724279
8 changed files with 141 additions and 79 deletions

View File

@@ -366,12 +366,12 @@
},
{
"command": "githubLocalActions.runAllWorkflows",
"when": "view == workflows",
"when": "view == workflows && workspaceFolderCount <= 1",
"group": "navigation@0"
},
{
"command": "githubLocalActions.runEvent",
"when": "view == workflows",
"when": "view == workflows && workspaceFolderCount <= 1",
"group": "navigation@1"
},
{
@@ -381,7 +381,7 @@
},
{
"command": "githubLocalActions.clearAll",
"when": "view == history",
"when": "view == history && workspaceFolderCount <= 1",
"group": "navigation@0"
},
{
@@ -411,6 +411,16 @@
"when": "view == components && viewItem =~ /^githubLocalActions.component_Not Running.*/",
"group": "inline@1"
},
{
"command": "githubLocalActions.runAllWorkflows",
"when": "view == workflows && viewItem =~ /^githubLocalActions.workspaceFolderWorkflows.*/ && workspaceFolderCount > 1",
"group": "inline@0"
},
{
"command": "githubLocalActions.runEvent",
"when": "view == workflows && viewItem =~ /^githubLocalActions.workspaceFolderWorkflows.*/ && workspaceFolderCount > 1",
"group": "inline@1"
},
{
"command": "githubLocalActions.openWorkflow",
"when": "view == workflows && viewItem =~ /^githubLocalActions.workflow.*/",
@@ -426,6 +436,11 @@
"when": "view == workflows && viewItem =~ /^githubLocalActions.job.*/",
"group": "inline@0"
},
{
"command": "githubLocalActions.clearAll",
"when": "view == history && viewItem =~ /^githubLocalActions.workspaceFolderHistory.*/ && workspaceFolderCount > 1",
"group": "inline@0"
},
{
"command": "githubLocalActions.viewOutput",
"when": "view == history && viewItem =~ /^githubLocalActions.history.*/",

View File

@@ -1,12 +1,12 @@
import * as child_process from 'child_process';
import * as path from "path";
import { commands, CustomExecution, env, EventEmitter, ExtensionContext, Pseudoterminal, ShellExecution, TaskDefinition, TaskGroup, TaskPanelKind, TaskRevealKind, tasks, TaskScope, TerminalDimensions, window, workspace, WorkspaceFolder } from "vscode";
import { commands, CustomExecution, env, EventEmitter, ExtensionContext, Pseudoterminal, ShellExecution, TaskDefinition, TaskGroup, TaskPanelKind, TaskRevealKind, tasks, TaskScope, TerminalDimensions, window, WorkspaceFolder } from "vscode";
import { ComponentsManager } from "./componentsManager";
import { historyTreeDataProvider } from './extension';
import { HistoryManager, HistoryStatus } from './historyManager';
import { SettingsManager } from './settingsManager';
import { StorageKey, StorageManager } from './storageManager';
import { Workflow, WorkflowsManager } from "./workflowsManager";
import { Job, Workflow, WorkflowsManager } from "./workflowsManager";
export enum Event {
BranchProtectionRule = 'branch_protection_rule',
@@ -127,13 +127,16 @@ export class Act {
}
}
async runAllWorkflows() {
// TODO: Implement
async runAllWorkflows(workspaceFolder: WorkspaceFolder) {
return await this.runCommand({
workspaceFolder: workspaceFolder,
options: ``,
name: workspaceFolder.name,
typeText: []
});
}
async runWorkflow(workflow: Workflow) {
const workspaceFolder = workspace.getWorkspaceFolder(workflow.uri);
if (workspaceFolder) {
async runWorkflow(workspaceFolder: WorkspaceFolder, workflow: Workflow) {
return await this.runCommand({
workspaceFolder: workspaceFolder,
options: `${Option.Workflows} ".github/workflows/${path.parse(workflow.uri.fsPath).base}"`,
@@ -142,20 +145,30 @@ export class Act {
`Workflow: ${workflow.name}`
]
});
} else {
window.showErrorMessage(`Failed to locate workspace folder for ${workflow.uri.fsPath}`);
}
}
// TODO: Implement
// async runJob(workspaceFolder: WorkspaceFolder, workflow: Workflow, job: Job) {
// return await this.runCommand(workspaceFolder, `${Option.Workflows} ".github/workflows/${path.parse(workflow.uri.fsPath).base}" ${Option.Job} "${job.id}"`, `${workflow.name}/${job.name}`, [`Workflow: ${workflow.name}`, `Job: ${job.name}`]);
// }
async runJob(workspaceFolder: WorkspaceFolder, workflow: Workflow, job: Job) {
return await this.runCommand({
workspaceFolder: workspaceFolder,
options: `${Option.Workflows} ".github/workflows/${path.parse(workflow.uri.fsPath).base}" ${Option.Job} "${job.id}"`,
name: `${workflow.name}/${job.name}`,
typeText: [
`Workflow: ${workflow.name}`,
`Job: ${job.name}`
]
});
}
// TODO: Implement
// async runEvent(workspaceFolder: WorkspaceFolder, event: Event) {
// return await this.runCommand(workspaceFolder, `${Option.Workflows} ${event}`, event, [`Event: ${event}`]);
// }
async runEvent(workspaceFolder: WorkspaceFolder, event: Event) {
return await this.runCommand({
workspaceFolder: workspaceFolder,
options: event,
name: event,
typeText: [
`Event: ${event}`
]
});
}
async runCommand(commandArgs: CommandArgs) {
const command = `${Act.base} ${Option.Json} ${commandArgs.options}`;
@@ -289,7 +302,7 @@ export class Act {
writeEmitter.fire(`Environments: OSSBUILD\r\n`);
writeEmitter.fire(`Variables: VARIABLE1=ABC, VARIABLE2=DEF\r\n`);
writeEmitter.fire(`Secrets: SECRET1=ABC, SECRET2=DEF\r\n`);
writeEmitter.fire(`Command: ${command}\r\n`);
writeEmitter.fire(`Command: ${command.replace(` ${Option.Json}`, ``)}\r\n`);
writeEmitter.fire(`\r\n`);
},

View File

@@ -1,4 +1,4 @@
import { TaskExecution, window, workspace } from "vscode";
import { TaskExecution, window, workspace, WorkspaceFolder } from "vscode";
import { CommandArgs } from "./act";
import { act, historyTreeDataProvider } from "./extension";
import { StorageKey, StorageManager } from "./storageManager";
@@ -42,17 +42,11 @@ export class HistoryManager {
this.workspaceHistory = workspaceHistory;
}
async clearAll() {
//TODO: Fix for multi workspace support
const workspaceFolders = workspace.workspaceFolders;
if (workspaceFolders && workspaceFolders.length > 0) {
for (const workspaceFolder of workspaceFolders) {
async clearAll(workspaceFolder: WorkspaceFolder) {
this.workspaceHistory[workspaceFolder.uri.fsPath] = [];
historyTreeDataProvider.refresh();
this.storageManager.update(StorageKey.WorkspaceHistory, this.workspaceHistory);
}
}
}
async viewOutput(history: History) {
await workspace.openTextDocument({ content: history.output }).then(async document => {

View File

@@ -1,4 +1,6 @@
export namespace DateUtils {
import { window, workspace, WorkspaceFolder } from "vscode";
export namespace Utils {
/**
* Get date time string.
*
@@ -37,4 +39,21 @@ export namespace DateUtils {
const seconds = totalSeconds % 60;
return `${minutes}m ${seconds}s`;
}
/**
* Get the provided workspace folder or the first one if there are multiple.
*/
export async function getWorkspaceFolder(workspaceFolder?: WorkspaceFolder) {
if (workspaceFolder) {
return workspaceFolder;
} else {
const workspaceFolders = workspace.workspaceFolders;
if (workspaceFolders && workspaceFolders.length > 0) {
return workspaceFolders[0];
} else {
await window.showErrorMessage('Failed to find a workspace folder');
return;
}
}
}
}

View File

@@ -1,6 +1,6 @@
import { ThemeColor, ThemeIcon, TreeItem, TreeItemCollapsibleState, WorkspaceFolder } from "vscode";
import { DateUtils } from "../../dateUtils";
import { History, HistoryStatus } from "../../historyManager";
import { Utils } from "../../utils";
import { GithubLocalActionsTreeItem } from "../githubLocalActionsTreeItem";
export default class HistoryTreeItem extends TreeItem implements GithubLocalActionsTreeItem {
@@ -13,7 +13,7 @@ export default class HistoryTreeItem extends TreeItem implements GithubLocalActi
let totalDuration: string | undefined;
if (history.date) {
totalDuration = DateUtils.getTimeDuration(history.date.start, history.date.end);
totalDuration = Utils.getTimeDuration(history.date.start, history.date.end);
}
this.description = totalDuration;
@@ -34,8 +34,8 @@ export default class HistoryTreeItem extends TreeItem implements GithubLocalActi
}
this.tooltip = `Name: ${history.name}\n` +
`Status: ${history.status}\n` +
`Started: ${history.date ? DateUtils.getDateString(history.date.start) : 'N/A'}\n` +
`Ended: ${history.date ? DateUtils.getDateString(history.date.end) : 'N/A'}\n` +
`Started: ${history.date ? Utils.getDateString(history.date.start) : 'N/A'}\n` +
`Ended: ${history.date ? Utils.getDateString(history.date.end) : 'N/A'}\n` +
(totalDuration ? `Total Duration: ${totalDuration}\n` : ``);
}

View File

@@ -1,6 +1,7 @@
import { CancellationToken, commands, EventEmitter, ExtensionContext, extensions, TreeDataProvider, TreeItem, workspace } from "vscode";
import { act } from "../../extension";
import { HistoryStatus } from "../../historyManager";
import { Utils } from "../../utils";
import { GithubLocalActionsTreeItem } from "../githubLocalActionsTreeItem";
import HistoryTreeItem from "./history";
import WorkspaceFolderHistoryTreeItem from "./workspaceFolderHistory";
@@ -16,8 +17,11 @@ export default class HistoryTreeDataProvider implements TreeDataProvider<GithubL
});
context.subscriptions.push(
commands.registerCommand('githubLocalActions.clearAll', async () => {
await act.historyManager.clearAll();
commands.registerCommand('githubLocalActions.clearAll', async (workspaceFolderHistoryTreeItem?: WorkspaceFolderHistoryTreeItem) => {
const workspaceFolder = await Utils.getWorkspaceFolder(workspaceFolderHistoryTreeItem?.workspaceFolder);
if (workspaceFolder) {
await act.historyManager.clearAll(workspaceFolder);
}
}),
commands.registerCommand('githubLocalActions.refreshHistory', async () => {
this.refresh();
@@ -65,6 +69,9 @@ export default class HistoryTreeDataProvider implements TreeDataProvider<GithubL
const workspaceFolders = workspace.workspaceFolders;
if (workspaceFolders) {
if (workspaceFolders.length === 1) {
return await new WorkspaceFolderHistoryTreeItem(workspaceFolders[0]).getChildren();
} else if (workspaceFolders.length > 1) {
for (const workspaceFolder of workspaceFolders) {
items.push(new WorkspaceFolderHistoryTreeItem(workspaceFolder));
@@ -75,6 +82,7 @@ export default class HistoryTreeDataProvider implements TreeDataProvider<GithubL
}
}
}
}
await commands.executeCommand('setContext', 'githubLocalActions:isRunning', isRunning);
await commands.executeCommand('setContext', 'githubLocalActions:noHistory', noHistory);

View File

@@ -61,6 +61,9 @@ export default class SettingsTreeDataProvider implements TreeDataProvider<Github
const workspaceFolders = workspace.workspaceFolders;
if (workspaceFolders) {
if (workspaceFolders.length === 1) {
return await new WorkspaceFolderSettingsTreeItem(workspaceFolders[0]).getChildren();
} else if (workspaceFolders.length > 1) {
for (const workspaceFolder of workspaceFolders) {
items.push(new WorkspaceFolderSettingsTreeItem(workspaceFolder));
@@ -70,6 +73,7 @@ export default class SettingsTreeDataProvider implements TreeDataProvider<Github
}
}
}
}
await commands.executeCommand('setContext', 'githubLocalActions:noSettings', noSettings);
return items;

View File

@@ -1,7 +1,9 @@
import { CancellationToken, commands, EventEmitter, ExtensionContext, TreeDataProvider, TreeItem, window, workspace } from "vscode";
import { Event } from "../../act";
import { act } from "../../extension";
import { Utils } from "../../utils";
import { GithubLocalActionsTreeItem } from "../githubLocalActionsTreeItem";
import JobTreeItem from "./job";
import WorkflowTreeItem from "./workflow";
import WorkspaceFolderWorkflowsTreeItem from "./workspaceFolderWorkflows";
@@ -12,18 +14,23 @@ export default class WorkflowsTreeDataProvider implements TreeDataProvider<Githu
constructor(context: ExtensionContext) {
context.subscriptions.push(
commands.registerCommand('githubLocalActions.runAllWorkflows', async () => {
await act.runAllWorkflows();
commands.registerCommand('githubLocalActions.runAllWorkflows', async (workspaceFolderWorkflowsTreeItem?: WorkspaceFolderWorkflowsTreeItem) => {
const workspaceFolder = await Utils.getWorkspaceFolder(workspaceFolderWorkflowsTreeItem?.workspaceFolder);
if (workspaceFolder) {
await act.runAllWorkflows(workspaceFolder);
}
}),
commands.registerCommand('githubLocalActions.runEvent', async () => {
commands.registerCommand('githubLocalActions.runEvent', async (workspaceFolderWorkflowsTreeItem?: WorkspaceFolderWorkflowsTreeItem) => {
const workspaceFolder = await Utils.getWorkspaceFolder(workspaceFolderWorkflowsTreeItem?.workspaceFolder);
if (workspaceFolder) {
const event = await window.showQuickPick(Object.values(Event), {
title: 'Select the event to run',
placeHolder: 'Event'
});
if (event) {
// TODO: Implement running event
// await act.runEvent(event as Event);
await act.runEvent(workspaceFolder, event as Event);
}
}
}),
commands.registerCommand('githubLocalActions.refreshWorkflows', async () => {
@@ -34,11 +41,10 @@ export default class WorkflowsTreeDataProvider implements TreeDataProvider<Githu
await window.showTextDocument(document);
}),
commands.registerCommand('githubLocalActions.runWorkflow', async (workflowTreeItem: WorkflowTreeItem) => {
await act.runWorkflow(workflowTreeItem.workflow);
await act.runWorkflow(workflowTreeItem.workspaceFolder, workflowTreeItem.workflow);
}),
commands.registerCommand('githubLocalActions.runJob', async (workflowTreeItem: WorkflowTreeItem) => {
// TODO: Implement running job
// await act.runJob()
commands.registerCommand('githubLocalActions.runJob', async (jobTreeItem: JobTreeItem) => {
await act.runJob(jobTreeItem.workspaceFolder, jobTreeItem.workflow, jobTreeItem.job);
})
);
}
@@ -68,6 +74,9 @@ export default class WorkflowsTreeDataProvider implements TreeDataProvider<Githu
const workspaceFolders = workspace.workspaceFolders;
if (workspaceFolders) {
if (workspaceFolders.length === 1) {
return await new WorkspaceFolderWorkflowsTreeItem(workspaceFolders[0]).getChildren();
} else if (workspaceFolders.length > 1) {
for (const workspaceFolder of workspaceFolders) {
items.push(new WorkspaceFolderWorkflowsTreeItem(workspaceFolder));
@@ -76,7 +85,7 @@ export default class WorkflowsTreeDataProvider implements TreeDataProvider<Githu
noWorkflows = false;
}
}
}
}
await commands.executeCommand('setContext', 'githubLocalActions:noWorkflows', noWorkflows);