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

feat: support git sub directory #345 #382

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
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;
}
}
}