diff --git a/bots-common/utils.go b/bots-common/utils.go index afc281f..c340fb6 100644 --- a/bots-common/utils.go +++ b/bots-common/utils.go @@ -19,6 +19,7 @@ package common */ import ( + "fmt" "slices" "strings" ) @@ -28,3 +29,18 @@ func SplitStringNoEmpty(str, sep string) []string { return len(s) == 0 }) } + +func TranslateHttpsToSshUrl(url string) (string, error) { + const url1 = "https://src.opensuse.org/" + const url2 = "https://src.suse.de/" + + if len(url) > len(url1) && url[0:len(url1)] == url1 { + return "gitea@src.opensuse.org:" + url[len(url1):], nil + } + if len(url) > len(url2) && url[0:len(url2)] == url2 { + return "gitea@src.suse.de:" + url[len(url2):], nil + } + + return "", fmt.Errorf("Unknown input url %s", url) +} + diff --git a/workflow-pr/main.go b/workflow-pr/main.go index 778de2a..af71c18 100644 --- a/workflow-pr/main.go +++ b/workflow-pr/main.go @@ -271,15 +271,20 @@ nextSubmodule: // not found in past, check if we should advance the branch label ... pull the submodule git.GitExecOrPanic(config.GitProjectName, "submodule", "update", "--init", "--filter", "blob:none", "--", sub) subDir := path.Join(config.GitProjectName, sub) - newCommits := git.GitExecWithOutputOrPanic(subDir, "rev-list", "^origin/"+config.Branch, commitID) - if len(common.SplitStringNoEmpty(newCommits, "\n")) >= 1 { - sshUrl, err := git.translateHttpsToSshUrl(git.SubmoduleUrl(config.GitProjectName), sub) + newCommits := common.SplitStringNoEmpty(git.GitExecWithOutputOrPanic(subDir, "rev-list", "^origin/"+config.Branch, commitID), "\n") + + if len(newCommits) >= 1 { + if DebugMode { + log.Println(" - updating branch", config.Branch, "to new head", commitID, " - len:", len(newCommits)) + } + git.GitExecOrPanic(subDir, "checkout", "-B", config.Branch, commitID) + url := git.GitExecWithOutputOrPanic(subDir, "remote", "get-url", "origin", "--push") + sshUrl, err := common.TranslateHttpsToSshUrl(strings.TrimSpace(url)) if err != nil { return fmt.Errorf("Cannot traslate HTTPS git URL to SSH_URL. %w", err) } - git.GitExecOrPanic(subDir, "checkout", "-B", config.Branch, commitID) - git.GitExecOrPanic(subDir, "remote", "add", "update", sshUrl) - git.GitExecOrPanic(subDir, "push", "update", config.Branch) + git.GitExecOrPanic(subDir, "remote", "set-url", "origin", "--push", sshUrl) + git.GitExecOrPanic(subDir, "push", "origin", config.Branch) } }