Skip to content

Commit

Permalink
feat: support git sub directory #345
Browse files Browse the repository at this point in the history
  • Loading branch information
tjx666 committed Aug 23, 2024
1 parent 7d92497 commit 572f8da
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/commands/triggerWorkflowRun.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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,
Expand Down
33 changes: 33 additions & 0 deletions src/git/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<vscode.Uri | undefined> {
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;
}
}
}

0 comments on commit 572f8da

Please sign in to comment.