diff --git a/bots-common/git_utils.go b/bots-common/git_utils.go index 580c439..68137b5 100644 --- a/bots-common/git_utils.go +++ b/bots-common/git_utils.go @@ -98,7 +98,7 @@ func (e *GitHandler) GitBranchHead(gitDir, branchName string) (string, error) { return "", fmt.Errorf("Can't find default remote branch: %s", branchName) } - return strings.TrimSpace(id), nil + return strings.TrimSpace(id), nil } func (e *GitHandler) Close() error { @@ -125,6 +125,20 @@ func (h writeFunc) Close() error { return err } +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) + } + 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) + } +} + func (e *GitHandler) GitExec(cwd string, params ...string) error { _, err := e.GitExecWithOutput(cwd, params...) return err diff --git a/devel-importer/go.mod b/devel-importer/go.mod index 7042c6d..e7f22c2 100644 --- a/devel-importer/go.mod +++ b/devel-importer/go.mod @@ -4,7 +4,10 @@ go 1.22.5 replace src.opensuse.org/autogits/common => ../bots-common -require src.opensuse.org/autogits/common v0.0.0-00010101000000-000000000000 +require ( + github.com/go-openapi/runtime v0.28.0 + src.opensuse.org/autogits/common v0.0.0-00010101000000-000000000000 +) require ( github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect @@ -15,7 +18,6 @@ require ( github.com/go-openapi/jsonpointer v0.21.0 // indirect github.com/go-openapi/jsonreference v0.21.0 // indirect github.com/go-openapi/loads v0.22.0 // indirect - github.com/go-openapi/runtime v0.28.0 // indirect github.com/go-openapi/spec v0.21.0 // indirect github.com/go-openapi/strfmt v0.23.0 // indirect github.com/go-openapi/swag v0.23.0 // indirect @@ -26,6 +28,7 @@ require ( github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/oklog/ulid v1.3.1 // indirect github.com/opentracing/opentracing-go v1.2.0 // indirect + github.com/rabbitmq/amqp091-go v1.10.0 // indirect go.mongodb.org/mongo-driver v1.14.0 // indirect go.opentelemetry.io/otel v1.24.0 // indirect go.opentelemetry.io/otel/metric v1.24.0 // indirect diff --git a/devel-importer/go.sum b/devel-importer/go.sum index b1db529..43e5142 100644 --- a/devel-importer/go.sum +++ b/devel-importer/go.sum @@ -48,6 +48,8 @@ github.com/opentracing/opentracing-go v1.2.0 h1:uEJPy/1a5RIPAJ0Ov+OIO8OxWu77jEv+ github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rabbitmq/amqp091-go v1.10.0 h1:STpn5XsHlHGcecLmMFCtg7mqq0RnD+zFr4uzukfVhBw= +github.com/rabbitmq/amqp091-go v1.10.0/go.mod h1:Hy4jKW5kQART1u+JkDTF9YYOQUHXqMuhrgxOEeS7G4o= github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8= github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= @@ -64,6 +66,8 @@ go.opentelemetry.io/otel/sdk v1.24.0 h1:YMPPDNymmQN3ZgczicBY3B6sf9n62Dlj9pWD3ucg go.opentelemetry.io/otel/sdk v1.24.0/go.mod h1:KVrIYw6tEubO9E96HQpcmpTKDVn9gdv35HoYiQWGDFg= go.opentelemetry.io/otel/trace v1.24.0 h1:CsKnnL4dUAr/0llH9FKuc698G04IrpWV0MQA/Y1YELI= go.opentelemetry.io/otel/trace v1.24.0/go.mod h1:HPc3Xr/cOApsBI154IU0OI0HJexz+aw5uPdbs3UCjNU= +go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= +go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= diff --git a/devel-importer/main.go b/devel-importer/main.go index e6df87b..744f698 100644 --- a/devel-importer/main.go +++ b/devel-importer/main.go @@ -22,6 +22,7 @@ import ( "errors" "flag" "fmt" + "log" "os" "os/exec" "path" @@ -50,19 +51,6 @@ func runObsCommand(args ...string) ([]byte, error) { return cmd.Output() } -func gitExec(pwd string, args ...string) string { - cmd := exec.Command("git", args...) - cmd.Dir = pwd - str, err := cmd.CombinedOutput() - - if err != nil { - fmt.Printf("git command failed: %v\n", args) - fmt.Println(" err: %v", err) - os.Exit(10) - } - return string(str) -} - func main() { if err := common.RequireGiteaSecretToken(); err != nil { fmt.Println("Missing GITEA_TOKEN") @@ -74,10 +62,9 @@ func main() { os.Exit(100) } - webhookBase := os.Getenv("WEBHOOK_BASE") - if webhookBase == "" { - fmt.Printf("Missing WEBHOOK_BASE\n") - os.Exit(100) + git, err := common.CreateGitHandler("Autogits - Devel Importer", "not.exist", "devel-importer") + if err != nil { + log.Panicln("Failed to allocate git handler. Err:", err) } purgeOnly := flag.Bool("purge", false, "Purges package repositories. Use with caution") @@ -94,8 +81,7 @@ func main() { packageList, err := runObsCommand("ls", prj) if err != nil { - fmt.Printf("Cannot list packages for project '%s'. Err: %v\n", prj, err) - os.Exit(2) + log.Printf("Cannot list packages for project '%s'. Err: %v\n", prj, err) } packages := strings.Split(strings.TrimSpace(string(packageList)), "\n") fmt.Printf("%d packages: %s\n\n", len(packages), strings.Join(packages, " ")) @@ -148,24 +134,6 @@ func main() { for _, hook := range hooks { fmt.Printf("hook %d: %#v", hook.ID, hook.Config) } - /* - hookActive := true - hookType := models.CreateHookOptionTypeGitea - client.Organization.OrgCreateHook( - organization.NewOrgCreateHookParams().WithOrg(org).WithBody(&models.CreateHookOption{ - Active: &hookActive, - Type: &hookType, - Config: models.CreateHookOptionConfig{ - "method": "POST", - "content_type": "application/json", - "url": webhookBase + "/prgit-updater", - }, - Events: []string{ - "push", - }, - }), - r.DefaultAuthentication) - */ // fork packags from pool cmd := exec.Command("./git-importer", func(r []*models.Repository) []string { @@ -188,31 +156,31 @@ func main() { dir := path.Join("repos", pkg.Name) // add remote repos - out := gitExec(dir, "remote", "show", "-n") + out := git.GitExecWithOutputOrPanic(dir, "remote", "show", "-n") if !slices.Contains(strings.Split(out, "\n"), "factory") { - out := gitExec(dir, "remote", "add", "factory", pkg.CloneURL) + out := git.GitExecWithOutputOrPanic(dir, "remote", "add", "factory", pkg.CloneURL) if len(strings.TrimSpace(out)) > 1 { fmt.Println(out) } } if !slices.Contains(strings.Split(out, "\n"), "rpm") { - out := gitExec(dir, "remote", "add", "rpm", "https://src.opensuse.org/rpm/"+pkg.Name+".git") + out := git.GitExecWithOutputOrPanic(dir, "remote", "add", "rpm", "https://src.opensuse.org/rpm/"+pkg.Name+".git") if len(strings.TrimSpace(out)) > 1 { fmt.Println(out) } } - out = gitExec(dir, "fetch", "--multiple", "factory", "rpm") + out = git.GitExecWithOutputOrPanic(dir, "fetch", "--multiple", "factory", "rpm") if len(strings.TrimSpace(out)) > 1 { fmt.Println(out) } // check that nothing is broken with the update - out = gitExec(dir, "rev-list", "factory") + out = git.GitExecWithOutputOrPanic(dir, "rev-list", "factory") old_revs := strings.Split(out, "\n") - out = gitExec(dir, "rev-list", "factory", "^factory/factory") + out = git.GitExecWithOutputOrPanic(dir, "rev-list", "factory", "^factory/factory") added_revs := strings.Split(out, "\n") - out = gitExec(dir, "rev-list", "factory", "^rpm/factory") + out = git.GitExecWithOutputOrPanic(dir, "rev-list", "factory", "^rpm/factory") added_rpm_revs := strings.Split(out, "\n") if len(added_revs) == len(old_revs) && len(added_rpm_revs) == len(old_revs) { fmt.Printf("Something is wrong with rev-ist for (len %d): %s\n", len(added_revs), pkg.Name) @@ -273,13 +241,13 @@ func main() { branchName := priorityBranches[idx] dir := path.Join("repos", pkg.Name) - remotes := gitExec(dir, "remote", "show") + remotes := git.GitExecWithOutputOrPanic(dir, "remote", "show") if !slices.Contains(strings.Split(remotes, "\n"), "devel") { - gitExec(dir, "remote", "add", "devel", repo.SSHURL) + git.GitExecOrPanic(dir, "remote", "add", "devel", repo.SSHURL) } - gitExec(dir, "branch", "main", "-f", branchName) + git.GitExecOrPanic(dir, "branch", "main", "-f", branchName) time.Sleep(2 * time.Second) - gitExec(dir, "push", "devel", "main") + git.GitExecOrPanic(dir, "push", "devel", "main") time.Sleep(2 * time.Second) _, err = client.Repository.RepoEdit(repository.NewRepoEditParams().WithOwner(org).WithRepo(repo.Name).WithBody(&models.EditRepoOption{ @@ -311,13 +279,13 @@ func main() { repo := ret.Payload dir := path.Join("repos", pkg) - remotes := gitExec(dir, "remote", "show") + remotes := git.GitExecWithOutputOrPanic(dir, "remote", "show") if !slices.Contains(strings.Split(remotes, "\n"), "devel") { - gitExec(dir, "remote", "add", "devel", repo.SSHURL) + git.GitExecOrPanic(dir, "remote", "add", "devel", repo.SSHURL) } - gitExec(dir, "branch", "main", "-f", "factory") + git.GitExecOrPanic(dir, "branch", "main", "-f", "factory") time.Sleep(2 * time.Second) - gitExec(dir, "push", "devel", "main") + git.GitExecOrPanic(dir, "push", "devel", "main") time.Sleep(2 * time.Second) _, err = client.Repository.RepoEdit(repository.NewRepoEditParams().WithOwner(org).WithRepo(pkg).WithBody(&models.EditRepoOption{