Feat: support nested folders in namespace (#87)

This MR fixes an issue with nested namespaces. It also adds CI to the project for Go tests.
This commit is contained in:
Louis LIN
2023-11-13 03:11:37 +01:00
committed by GitHub
parent 80b597e56a
commit 59557e464b
6 changed files with 243 additions and 25 deletions

View File

@@ -7,37 +7,58 @@ import (
"strings"
)
type GitProjectInfo struct {
RemoteUrl string
Namespace string
ProjectName string
BranchName string
}
/*
projectPath returns the Gitlab project full path, which isn't necessarily the same as its name.
See https://docs.gitlab.com/ee/api/rest/index.html#namespaced-path-encoding for more information.
*/
func (g GitProjectInfo) projectPath() string {
return g.Namespace + "/" + g.ProjectName
}
/*
Extracts information about the current repository and returns
it to the client for initialization. The current directory must be a valid
Gitlab project and the branch must be a feature branch
*/
func ExtractGitInfo() (string, string, string, string, error) {
url, err := getProjectUrl()
func ExtractGitInfo(getProjectRemoteUrl func() (string, error), getCurrentBranchName func() (string, error)) (GitProjectInfo, error) {
url, err := getProjectRemoteUrl()
if err != nil {
return "", "", "", "", fmt.Errorf("Could not get project Url: %v", err)
return GitProjectInfo{}, fmt.Errorf("Could not get project Url: %v", err)
}
re := regexp.MustCompile(`(?:[:\/])([^\/]+)\/([^\/]+)\.git$`)
// play with regex at: https://regex101.com/r/P2jSGh/1
re := regexp.MustCompile(`(?:^git@.+:|^https?:\/\/.+?[^\/:]\/)(.+)\/([^\/]+)\.git$`)
matches := re.FindStringSubmatch(url)
if len(matches) != 3 {
return "", "", "", "", fmt.Errorf("Invalid Git URL format: %s", url)
return GitProjectInfo{}, fmt.Errorf("Invalid Git URL format: %s", url)
}
namespace := matches[1]
projectName := matches[2]
branch, err := getCurrentBranch()
branchName, err := getCurrentBranchName()
if err != nil {
return "", "", "", "", fmt.Errorf("Failed to get current branch: %v", err)
return GitProjectInfo{}, fmt.Errorf("Failed to get current branch: %v", err)
}
return url, namespace, projectName, branch, nil
return GitProjectInfo{
RemoteUrl: url,
Namespace: namespace,
ProjectName: projectName,
BranchName: branchName,
},
nil
}
/* Gets the current branch */
func getCurrentBranch() (res string, e error) {
/* Gets the current branch name */
func GetCurrentBranchNameFromNativeGitCmd() (res string, e error) {
gitCmd := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD")
output, err := gitCmd.Output()
@@ -55,10 +76,9 @@ func getCurrentBranch() (res string, e error) {
}
/* Gets the project SSH or HTTPS url */
func getProjectUrl() (res string, e error) {
func GetProjectUrlFromNativeGitCmd() (string, error) {
cmd := exec.Command("git", "remote", "get-url", "origin")
url, err := cmd.Output()
if err != nil {
return "", fmt.Errorf("Could not get origin remote")
}