From 572f8da24f359242c94076e84135e37bea84f3b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=99=E8=85=BE=E9=9D=96?= Date: Fri, 23 Aug 2024 15:16:24 +0800 Subject: [PATCH] feat: support git sub directory #345 --- src/commands/triggerWorkflowRun.ts | 22 +++++++++++++++----- src/git/repository.ts | 33 ++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 5 deletions(-) diff --git a/src/commands/triggerWorkflowRun.ts b/src/commands/triggerWorkflowRun.ts index 3deba5b0..101b70e0 100644 --- a/src/commands/triggerWorkflowRun.ts +++ b/src/commands/triggerWorkflowRun.ts @@ -1,6 +1,13 @@ import * as vscode from "vscode"; -import {getGitHead, getGitHubContextForWorkspaceUri, GitHubRepoContext} from "../git/repository"; +import * as path from "path"; + +import { + getGitHead, + getGitHubContextForWorkspaceUri, + getGitRepositoryFolderUri, + GitHubRepoContext +} from "../git/repository"; import {getWorkflowUri, parseWorkflowFile} from "../workflow/workflow"; import {Workflow} from "../model"; @@ -29,11 +36,14 @@ export function registerTriggerWorkflowRun(context: vscode.ExtensionContext) { // Parse const workspaceFolder = vscode.workspace.getWorkspaceFolder(workflowUri); - if (!workspaceFolder) { + + // support git sub directory + const gitRepoFolderUri = workspaceFolder ? workspaceFolder?.uri : await getGitRepositoryFolderUri(workflowUri); + if (!gitRepoFolderUri) { return; } - const gitHubRepoContext = await getGitHubContextForWorkspaceUri(workspaceFolder.uri); + const gitHubRepoContext = await getGitHubContextForWorkspaceUri(gitRepoFolderUri); if (!gitHubRepoContext) { return; } @@ -85,8 +95,10 @@ export function registerTriggerWorkflowRun(context: vscode.ExtensionContext) { } try { - const relativeWorkflowPath = vscode.workspace.asRelativePath(workflowUri, false); - + // support git sub directory + const relativeWorkflowPath = workspaceFolder + ? vscode.workspace.asRelativePath(workflowUri, false) + : path.relative(gitRepoFolderUri.path, workflowUri.path); await gitHubRepoContext.client.actions.createWorkflowDispatch({ owner: gitHubRepoContext.owner, repo: gitHubRepoContext.name, diff --git a/src/git/repository.ts b/src/git/repository.ts index 9db227cb..aba5a5db 100644 --- a/src/git/repository.ts +++ b/src/git/repository.ts @@ -287,3 +287,36 @@ export function getCurrentBranch(state: RepositoryState | undefined): string | u return head.name; } + +/** + * Get the Git repository folder URI + * @param startUri The starting URI to search from + * @returns The URI of the Git repository folder, or undefined if not found + */ +export async function getGitRepositoryFolderUri(startUri: vscode.Uri): Promise { + let currentPath = startUri; + + // eslint-disable-next-line no-constant-condition + while (true) { + try { + const gitPath = vscode.Uri.joinPath(currentPath, ".git"); + + // Check if .git exists + await vscode.workspace.fs.stat(gitPath); + + // If we reach here, .git exists, so return the current path + return currentPath; + } catch (error) { + // .git doesn't exist, move up to the parent directory + const parentPath = vscode.Uri.joinPath(currentPath, ".."); + + // Check if we've reached the root + if (parentPath.toString() === currentPath.toString()) { + // We've reached the root without finding a .git folder + return undefined; + } + + currentPath = parentPath; + } + } +}