unique org_repo remote names

This commit is contained in:
2025-04-15 14:55:19 +02:00
parent 8645063e8d
commit ed815c3ad1
3 changed files with 69 additions and 5 deletions

View File

@@ -86,13 +86,12 @@ type GitHandlerGenerator interface {
}
type gitHandlerGeneratorImpl struct {
// TODO: add mutex to lock paths so only one workflow per path and others wait
path string
git_author string
email string
lock_lock sync.Mutex
lock map[string]sync.Mutex
lock map[string]*sync.Mutex // per org
}
func AllocateGitWorkTree(basePath, gitAuthor, email string) (GitHandlerGenerator, error) {
@@ -117,7 +116,7 @@ func AllocateGitWorkTree(basePath, gitAuthor, email string) (GitHandlerGenerator
git_author: gitAuthor,
email: email,
lock: make(map[string]sync.Mutex),
lock: make(map[string]*sync.Mutex),
}, nil
}
@@ -193,7 +192,11 @@ func (e *GitHandlerImpl) GitClone(repo, branch, remoteUrl string) error {
return fmt.Errorf("Clone location not a directory or Stat error: %w", err)
}
remoteName := ""
remoteUrlComp, err := ParseGitRemoteUrl(remoteUrl)
if err != nil {
return fmt.Errorf("Cannot parse remote URL: %w", err)
}
remoteName := remoteUrlComp.RemoteName()
clonedRemote := strings.TrimSpace(e.GitExecWithOutputOrPanic(repo, "remote", "get-url", remoteName))
if clonedRemote != remoteUrl {
e.GitExecOrPanic(repo, "remote", "set-url", remoteName, remoteUrl)

View File

@@ -80,7 +80,7 @@ type GitUrl struct {
Commit string
}
var valid_schemas []string = []string { "https", "ssh", "http" }
var valid_schemas []string = []string{"https", "ssh", "http"}
func ParseGitRemoteUrl(urlString string) (*GitUrl, error) {
url, err := url.Parse(urlString)
@@ -111,3 +111,11 @@ func ParseGitRemoteUrl(urlString string) (*GitUrl, error) {
return &u, nil
}
func (giturl *GitUrl) RemoteName() string {
if giturl == nil || len(giturl.Org) == 0 || len(giturl.Repo) == 0 {
return "origin"
}
return strings.ToLower(giturl.Org) + "_" + strings.ToLower(giturl.Repo)
}

View File

@@ -112,3 +112,56 @@ func TestGitUrlParse(t *testing.T) {
})
}
}
func TestRemoteName(t *testing.T) {
tests := []struct {
name string
giturl *common.GitUrl
remotename string
}{
{
name: "empty",
remotename: "origin",
},
{
name: "regular repo",
giturl: &common.GitUrl{
Org: "org1",
Repo: "repo2",
Commit: "main",
},
remotename: "org1_repo2",
},
{
name: "regular repo with upper case",
giturl: &common.GitUrl{
Org: "Org1",
Repo: "REPO2",
},
remotename: "org1_repo2",
},
{
name: "no org",
giturl: &common.GitUrl{
Repo: "REPO2",
},
remotename: "origin",
},
{
name: "no repo",
giturl: &common.GitUrl{
Org: "ORG2",
},
remotename: "origin",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
n := test.giturl.RemoteName()
if n != test.remotename {
t.Errorf("Expected '%s' but received '%s'", test.remotename, n)
}
})
}
}