Skip to content

Commit 572f8da

Browse files
committed
feat: support git sub directory github#345
1 parent 7d92497 commit 572f8da

File tree

2 files changed

+50
-5
lines changed

2 files changed

+50
-5
lines changed

Diff for: src/commands/triggerWorkflowRun.ts

+17-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import * as vscode from "vscode";
22

3-
import {getGitHead, getGitHubContextForWorkspaceUri, GitHubRepoContext} from "../git/repository";
3+
import * as path from "path";
4+
5+
import {
6+
getGitHead,
7+
getGitHubContextForWorkspaceUri,
8+
getGitRepositoryFolderUri,
9+
GitHubRepoContext
10+
} from "../git/repository";
411
import {getWorkflowUri, parseWorkflowFile} from "../workflow/workflow";
512

613
import {Workflow} from "../model";
@@ -29,11 +36,14 @@ export function registerTriggerWorkflowRun(context: vscode.ExtensionContext) {
2936

3037
// Parse
3138
const workspaceFolder = vscode.workspace.getWorkspaceFolder(workflowUri);
32-
if (!workspaceFolder) {
39+
40+
// support git sub directory
41+
const gitRepoFolderUri = workspaceFolder ? workspaceFolder?.uri : await getGitRepositoryFolderUri(workflowUri);
42+
if (!gitRepoFolderUri) {
3343
return;
3444
}
3545

36-
const gitHubRepoContext = await getGitHubContextForWorkspaceUri(workspaceFolder.uri);
46+
const gitHubRepoContext = await getGitHubContextForWorkspaceUri(gitRepoFolderUri);
3747
if (!gitHubRepoContext) {
3848
return;
3949
}
@@ -85,8 +95,10 @@ export function registerTriggerWorkflowRun(context: vscode.ExtensionContext) {
8595
}
8696

8797
try {
88-
const relativeWorkflowPath = vscode.workspace.asRelativePath(workflowUri, false);
89-
98+
// support git sub directory
99+
const relativeWorkflowPath = workspaceFolder
100+
? vscode.workspace.asRelativePath(workflowUri, false)
101+
: path.relative(gitRepoFolderUri.path, workflowUri.path);
90102
await gitHubRepoContext.client.actions.createWorkflowDispatch({
91103
owner: gitHubRepoContext.owner,
92104
repo: gitHubRepoContext.name,

Diff for: src/git/repository.ts

+33
Original file line numberDiff line numberDiff line change
@@ -287,3 +287,36 @@ export function getCurrentBranch(state: RepositoryState | undefined): string | u
287287

288288
return head.name;
289289
}
290+
291+
/**
292+
* Get the Git repository folder URI
293+
* @param startUri The starting URI to search from
294+
* @returns The URI of the Git repository folder, or undefined if not found
295+
*/
296+
export async function getGitRepositoryFolderUri(startUri: vscode.Uri): Promise<vscode.Uri | undefined> {
297+
let currentPath = startUri;
298+
299+
// eslint-disable-next-line no-constant-condition
300+
while (true) {
301+
try {
302+
const gitPath = vscode.Uri.joinPath(currentPath, ".git");
303+
304+
// Check if .git exists
305+
await vscode.workspace.fs.stat(gitPath);
306+
307+
// If we reach here, .git exists, so return the current path
308+
return currentPath;
309+
} catch (error) {
310+
// .git doesn't exist, move up to the parent directory
311+
const parentPath = vscode.Uri.joinPath(currentPath, "..");
312+
313+
// Check if we've reached the root
314+
if (parentPath.toString() === currentPath.toString()) {
315+
// We've reached the root without finding a .git folder
316+
return undefined;
317+
}
318+
319+
currentPath = parentPath;
320+
}
321+
}
322+
}

0 commit comments

Comments
 (0)