From d7bbe5695c25f4474f78ae1f89761e2e272fc4cfc1ee7aed301fa3ec3f935533 Mon Sep 17 00:00:00 2001 From: Adam Majer Date: Thu, 19 Sep 2024 19:00:56 +0200 Subject: [PATCH] devel-importer: more fixes --- bots-common/git_utils.go | 4 +-- devel-importer/main.go | 69 ++++++++++++++++++++++++++-------------- 2 files changed, 47 insertions(+), 26 deletions(-) diff --git a/bots-common/git_utils.go b/bots-common/git_utils.go index fd06bdb..4b8acff 100644 --- a/bots-common/git_utils.go +++ b/bots-common/git_utils.go @@ -150,14 +150,14 @@ func (h writeFunc) Close() error { func (e *GitHandler) GitExecWithOutputOrPanic(cwd string, params ...string) string { out, err := e.GitExecWithOutput(cwd, params...) if err != nil { - log.Panicln("git command failed:", params, "err:", err) + log.Panicln("git command failed:", params, "@", cwd, "err:", err) } return out } func (e *GitHandler) GitExecOrPanic(cwd string, params ...string) { if err := e.GitExec(cwd, params...); err != nil { - log.Panicln("git command failed:", params, "err:", err) + log.Panicln("git command failed:", params, "@", cwd, "err:", err) } } diff --git a/devel-importer/main.go b/devel-importer/main.go index a582bd3..08eea44 100644 --- a/devel-importer/main.go +++ b/devel-importer/main.go @@ -27,7 +27,6 @@ import ( "path/filepath" "slices" "strings" - "time" transport "github.com/go-openapi/runtime/client" "src.opensuse.org/autogits/common" @@ -314,10 +313,30 @@ func main() { log.Panicln("aborting import due to broken repos above...") } + slices.SortFunc(factoryRepos, func(a, b *models.Repository) int { + if a.Name == b.Name { + orgOrderNo := func(org string) int { + switch org { + case "pool": + return 1 + case "rpm": + return 2 + } + return 0 // current devel to clone + } + + return orgOrderNo(a.Owner.UserName) - orgOrderNo(b.Owner.UserName) + } + return strings.Compare(a.Name, b.Name) + }) + factoryRepos = slices.CompactFunc(factoryRepos, func(a, b *models.Repository) bool { + return a.Name == b.Name + }) + for _, pkg := range factoryRepos { // update package fork, err := client.Repository.CreateFork(repository.NewCreateForkParams(). - WithOwner("pool"). + WithOwner(pkg.Owner.UserName). WithRepo(pkg.Name). WithBody(&models.CreateForkOption{ Organization: org, @@ -327,34 +346,38 @@ func main() { } repo := fork.Payload - repoList, err := client.Repository.RepoListBranches( - repository.NewRepoListBranchesParams().WithOwner(org).WithRepo(pkg.Name), - r.DefaultAuthentication, - ) - if err != nil { - log.Panicln("Cannot get list of branches for forked repo:", org, "/", pkg.Name) - } - priorityBranches := []string{ - "devel", - "factory", - } - idx := len(priorityBranches) - for _, branch := range repoList.Payload { - i := slices.Index(priorityBranches, branch.Name) - if i > -1 && i < idx { - idx = i + branchName := repo.DefaultBranch + + if pkg.Owner.UserName == "pool" || pkg.Owner.UserName == "rpm" { + // forked a git-based devel project, so use the default branch name now + repoList, err := client.Repository.RepoListBranches( + repository.NewRepoListBranchesParams().WithOwner(org).WithRepo(pkg.Name), + r.DefaultAuthentication, + ) + if err != nil { + log.Panicln("Cannot get list of branches for forked repo:", org, "/", pkg.Name) } + priorityBranches := []string{ + "devel", + "factory", + } + idx := len(priorityBranches) + for _, branch := range repoList.Payload { + i := slices.Index(priorityBranches, branch.Name) + if i > -1 && i < idx { + idx = i + } + } + branchName = priorityBranches[idx] } - branchName := priorityBranches[idx] remotes := git.GitExecWithOutputOrPanic(pkg.Name, "remote", "show") if !slices.Contains(strings.Split(remotes, "\n"), "devel") { git.GitExecOrPanic(pkg.Name, "remote", "add", "devel", repo.SSHURL) + git.GitExecOrPanic(pkg.Name, "fetch", "devel") } - git.GitExecOrPanic(pkg.Name, "branch", "main", "-f", branchName) - time.Sleep(2 * time.Second) + git.GitExecOrPanic(pkg.Name, "branch", "main", "-f", "devel/"+branchName) git.GitExecOrPanic(pkg.Name, "push", "devel", "main") - time.Sleep(2 * time.Second) _, err = client.Repository.RepoEdit(repository.NewRepoEditParams().WithOwner(org).WithRepo(repo.Name).WithBody(&models.EditRepoOption{ DefaultBranch: "main", @@ -387,9 +410,7 @@ func main() { git.GitExecOrPanic(pkg, "remote", "add", "devel", repo.SSHURL) } git.GitExecOrPanic(pkg, "branch", "main", "-f", "factory") - time.Sleep(2 * time.Second) git.GitExecOrPanic(pkg, "push", "devel", "main") - time.Sleep(2 * time.Second) _, err = client.Repository.RepoEdit(repository.NewRepoEditParams().WithOwner(org).WithRepo(pkg).WithBody(&models.EditRepoOption{ DefaultBranch: "main",