From 8cdb1961641f307ba556f201bccb27aa53627e49734d5e3d973bdcc8e519242c Mon Sep 17 00:00:00 2001 From: Adam Majer Date: Sun, 11 Aug 2024 23:27:34 +0200 Subject: [PATCH] . --- devel-importer/main.go | 206 +++++++++++++++++++++++++++-------------- 1 file changed, 136 insertions(+), 70 deletions(-) diff --git a/devel-importer/main.go b/devel-importer/main.go index a636d93..9a3090a 100644 --- a/devel-importer/main.go +++ b/devel-importer/main.go @@ -6,6 +6,7 @@ import ( "fmt" "os" "os/exec" + "path" "slices" "strings" @@ -30,6 +31,19 @@ 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") @@ -47,8 +61,8 @@ func main() { os.Exit(100) } - var purgeOnly bool - flag.BoolVar(&purgeOnly, "purge", false, "Purges package repositories. Use with caution") + purgeOnly := flag.Bool("purge", false, "Purges package repositories. Use with caution") + // revNew := flag.Int("nrevs", 20, "Number of new revisions in factory branch. Indicator of broken history import") flag.Parse() if flag.NArg() != 2 { @@ -72,7 +86,7 @@ func main() { // r.SetDebug(true) client := apiclient.New(r, nil) - if purgeOnly { + if *purgeOnly { fmt.Printf("Purging repositories...\n") for _, pkg := range packages { client.Repository.RepoDelete(repository.NewRepoDeleteParams().WithOwner(org).WithRepo(pkg), r.DefaultAuthentication) @@ -115,89 +129,141 @@ 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) + /* + 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 { + ret := make([]string, len(r)) + for i := range r { + ret[i] = r[i].Name + } + return ret + }(oldPackageRepos)...) + out, err := cmd.CombinedOutput() + fmt.Print(string(out)) + if err != nil { + fmt.Printf("Error returned by importer. Err: %v\n", err) + os.Exit(14) + } for i := range oldPackageRepos { pkg := oldPackageRepos[i] - fork, err := client.Repository.CreateFork( - repository.NewCreateForkParams(). - WithOwner("pool"). - WithRepo(pkg.Name). - WithBody(&models.CreateForkOption{ - Organization: org, - }), r.DefaultAuthentication) - if err != nil { - fmt.Printf("Error while trying to create fork from pool/%s. Err: %v\n", pkg.Name, err) - os.Exit(10) - } + dir := path.Join("repos", pkg.Name) - repo := fork.Payload - repoList, err := client.Repository.RepoListBranches( - repository.NewRepoListBranchesParams().WithOwner(org).WithRepo(pkg.Name), - r.DefaultAuthentication, - ) - if err != nil { - fmt.Printf("Cannot get list of branches for forked repo: %s/%s\n", org, pkg.Name) - os.Exit(11) + // add remote repos + out := gitExec(dir, "remote", "show", "-n") + if !slices.Contains(strings.Split(out, "\n"), "factory") { + out := gitExec(dir, "remote", "add", "factory", pkg.CloneURL) + if len(strings.TrimSpace(out)) > 1 { + fmt.Println(out) + } } - 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 + if !slices.Contains(strings.Split(out, "\n"), "rpm") { + out := gitExec(dir, "remote", "add", "rpm", "https://src.opensuse.org/rpm/" + pkg.Name + ".git") + if len(strings.TrimSpace(out)) > 1 { + fmt.Println(out) } } - if idx < len(priorityBranches) { - _, err := client.Repository.RepoEdit(repository.NewRepoEditParams().WithOwner(repo.Owner.UserName).WithRepo(repo.Name).WithBody(&models.EditRepoOption{ - DefaultBranch: priorityBranches[idx], - DefaultMergeStyle: "fast-forward-only", - }), r.DefaultAuthentication) + out = gitExec(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") + old_revs := strings.Split(out, "\n") + out = gitExec(dir, "rev-list", "factory", "^factory/factory") + added_revs := strings.Split(out, "\n") + out = gitExec(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 revl-ist for (len %d): %s\n", len(added_revs), pkg.Name) + } + + // update package + /* + fork, err := client.Repository.CreateFork(repository.NewCreateForkParams(). + WithOwner("pool"). + WithRepo(pkg.Name). + WithBody(&models.CreateForkOption{ + Organization: org, + }), r.DefaultAuthentication) if err != nil { - fmt.Printf("Failed to set default branch for package fork: %s/%s Err: %v", repo.Owner.UserName, repo.Name, err) - os.Exit(12) + fmt.Printf("Error while trying to create fork from pool/%s. Err: %v\n", pkg.Name, err) + os.Exit(10) } - } + + repo := fork.Payload + repoList, err := client.Repository.RepoListBranches( + repository.NewRepoListBranchesParams().WithOwner(org).WithRepo(pkg.Name), + r.DefaultAuthentication, + ) + if err != nil { + fmt.Printf("Cannot get list of branches for forked repo: %s/%s\n", org, pkg.Name) + os.Exit(11) + } + 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 + } + } + + if idx < len(priorityBranches) { + _, err := client.Repository.RepoEdit(repository.NewRepoEditParams().WithOwner(repo.Owner.UserName).WithRepo(repo.Name).WithBody(&models.EditRepoOption{ + DefaultBranch: priorityBranches[idx], + DefaultMergeStyle: "fast-forward-only", + }), r.DefaultAuthentication) + + if err != nil { + fmt.Printf("Failed to set default branch for package fork: %s/%s Err: %v", repo.Owner.UserName, repo.Name, err) + os.Exit(12) + } + } + */ } // create new repositories for _, pkg := range newPackages { - _, err := client.Organization.CreateOrgRepo( - organization.NewCreateOrgRepoParams(). - WithOrg(org). - WithBody( - &models.CreateRepoOption{ - Name: &pkg, - AutoInit: false, - ObjectFormatName: "sha256", - }), - r.DefaultAuthentication) + fmt.Printf(" %s\n", pkg) + /* + _, err := client.Organization.CreateOrgRepo( + organization.NewCreateOrgRepoParams(). + WithOrg(org). + WithBody( + &models.CreateRepoOption{ + Name: &pkg, + AutoInit: false, + ObjectFormatName: "sha256", + }), + r.DefaultAuthentication) - if err != nil { - fmt.Printf("Cannot create %s/%s. Err: %v", org, pkg, err) - os.Exit(13) - } + if err != nil { + fmt.Printf("Cannot create %s/%s. Err: %v", org, pkg, err) + os.Exit(13) + } + */ } }