From e1313105d1dcc2b7c299ec7513a4972ad53a9c828c6a6594d62c00ee35631d7b Mon Sep 17 00:00:00 2001 From: Adam Majer Date: Wed, 26 Feb 2025 18:10:42 +0100 Subject: [PATCH] remove rpm and re-org user queries --- devel-importer/main.go | 172 ++++++++++++++++++++++++----------------- 1 file changed, 101 insertions(+), 71 deletions(-) diff --git a/devel-importer/main.go b/devel-importer/main.go index 81cfd4e..153dc4b 100644 --- a/devel-importer/main.go +++ b/devel-importer/main.go @@ -66,46 +66,72 @@ func runObsCommand(args ...string) ([]string, error) { var DebugMode bool -func listMaintainers(obs *common.ObsClient, prj string, pkgs []string) { +func projectMaintainer(obs *common.ObsClient, prj string) ([]string, []string) { // users, groups meta, err := obs.GetProjectMeta(prj) if err != nil { log.Panicln(err) } uids := []string{} + gids := []string{} for _, p := range meta.Persons { - log.Println(p.UserID, p.Role) if !slices.Contains(uids, p.UserID) { uids = append(uids, p.UserID) } } for _, g := range meta.Groups { - log.Println(g.GroupID, g.Role) + if !slices.Contains(gids, g.GroupID) { + gids = append(gids, g.GroupID) + } } + return uids, gids +} + +func packageMaintainers(obs *common.ObsClient, prj, pkg string) ([]string, []string) { // users, groups + meta, err := obs.GetPackageMeta(prj, pkg) + if err != nil { + log.Panicln(err, "FOR:", prj, "/", pkg) + } + + uids := []string{} + gids := []string{} + + for _, p := range meta.Persons { + if !slices.Contains(uids, p.UserID) { + uids = append(uids, p.UserID) + } + } + for _, g := range meta.Groups { + if !slices.Contains(gids, g.GroupID) { + gids = append(gids, g.GroupID) + } + } + + return uids, gids +} + +func listMaintainers(obs *common.ObsClient, prj string, pkgs []string) { + users, groups := projectMaintainer(obs, prj) + for _, pkg := range pkgs { - meta, err := obs.GetPackageMeta(prj, pkg) - if err != nil { - log.Panicln(err, "FOR:", prj, "/", pkg) - } - - for _, p := range meta.Persons { - log.Println(" +", pkg, "=>", p.UserID) - if !slices.Contains(uids, p.UserID) { - uids = append(uids, p.UserID) - } - } + u, g := packageMaintainers(obs, prj, pkg) + users = append(users, u...) + groups = append(groups, g...) } - slices.Sort(uids) - log.Println("need to contact following:", strings.Join(uids, ", ")) + slices.Sort(users) + slices.Sort(groups) + users = slices.Compact(users) + groups = slices.Compact(groups) + log.Println("need to contact following:", strings.Join(users, ", ")) contact_email := []string{} - for _, uid := range uids { + for _, uid := range users { user, err := obs.GetUserMeta(uid) if err != nil { - log.Panicln(err); + log.Panicln(err) } contact_email = append(contact_email, fmt.Sprintf("%s <%s>", user.Name, user.Email)) @@ -131,7 +157,7 @@ func main() { //rabbitUrl := flag.String("url", "amqps://rabbit.opensuse.org", "URL for RabbitMQ instance") flags.BoolVar(&DebugMode, "debug", false, "Extra debugging information") // revNew := flag.Int("nrevs", 20, "Number of new revisions in factory branch. Indicator of broken history import") - purgeOnly := flags.Bool("purge", false, "Purges package repositories. Use with caution") + purgeOnly := flags.Bool("purge-only", false, "Purges package repositories on Gitea. Use with caution") debugGitPath := flags.String("git-path", "", "Path for temporary git directory. Only used if DebugMode") getMaintainers := flags.Bool("maintainer-only", false, "Get maintainers only and exit") @@ -150,7 +176,10 @@ func main() { if DebugMode { if len(*debugGitPath) > 0 { git.Close() - gh.ReadExistingPath("Autogits - Devel Importer", "not.exist", *debugGitPath) + git, err = gh.ReadExistingPath("Autogits - Devel Importer", "not.exist", *debugGitPath) + if err != nil { + log.Panicln(err) + } } log.Println(" - working directory:" + git.GetPath()) } else { @@ -200,20 +229,6 @@ func main() { WithDefaults().WithOwner("pool").WithRepo(src_pkg_name[0]), r.DefaultAuthentication) - rpm, rpmErr := client.Repository.RepoGet( - repository.NewRepoGetParams().WithDefaults().WithOwner("rpm").WithRepo(src_pkg_name[0]), - r.DefaultAuthentication) - - if rpmErr == nil { - factoryRepos = append(factoryRepos, rpm.Payload) - } else { - if !errors.Is(rpmErr, &repository.RepoGetNotFound{}) { - log.Panicln(rpmErr) - } - - log.Println("No RPM package?", src_pkg_name) - } - if err != nil { if !errors.Is(err, &repository.RepoGetNotFound{}) { log.Panicln(err) @@ -232,17 +247,19 @@ func main() { for _, repo := range factoryRepos { oldPackageNames = append(oldPackageNames, repo.Name) } - slices.Sort(oldPackageNames) - oldPackageNames = slices.Compact(oldPackageNames) - copy(oldPackageNames[2:], oldPackageNames) - log.Println("Num of old packages:", len(oldPackageNames)) // fork packags from pool - cmd := exec.Command("./git-importer", append([]string{"-r", git.GetPath()}, oldPackageNames...)...) - out, err := cmd.CombinedOutput() - log.Println(string(out)) - if err != nil { - log.Println("Error returned by importer.", err) + for _, pkg := range oldPackageNames { + log.Println(" + package:", pkg) + cmd := exec.Command("./git-importer", "-r", git.GetPath(), pkg) + if idx := slices.IndexFunc(cmd.Env, func(val string) bool { return val[0:12] == "GITEA_TOKEN=" }); idx != -1 { + cmd.Env = slices.Delete(cmd.Env, idx, idx+1) + } + out, err := cmd.CombinedOutput() + log.Println(string(out)) + if err != nil { + log.Println("Error returned by importer.", err) + } } reposOK := true @@ -291,13 +308,8 @@ func main() { log.Println(out) } } - case "rpm": - if !slices.Contains(strings.Split(out, "\n"), "rpm") { - out := git.GitExecWithOutputOrPanic(pkg.Name, "remote", "add", "rpm", pkg.CloneURL) - if len(strings.TrimSpace(out)) > 1 { - log.Println(out) - } - } + default: + log.Panicln(pkg.Owner.UserName) } } @@ -323,13 +335,8 @@ func main() { added_revs := []string{} out = git.GitExecWithOutputOrPanic(pkgName, "rev-list", head_branch, "^factory/factory") added_revs = strings.Split(out, "\n") - added_rpm_revs := old_revs - if slices.Contains(remotes, "rpm") { - out = git.GitExecWithOutputOrPanic(pkgName, "rev-list", head_branch, "^rpm/factory") - added_rpm_revs = strings.Split(out, "\n") - } - if len(added_revs) == len(old_revs) && len(added_rpm_revs) == len(old_revs) { + if len(added_revs) == len(old_revs) { log.Printf("Something is wrong with rev-ist for (len %d): %s\n", len(added_revs), pkgName) // check if we have broken history in OBS and that Tree objects are still matching @@ -369,17 +376,16 @@ func main() { } } - args := make([]string, 4, len(develProjectPackages)+4) - args[0] = "-p" - args[1] = prj - args[2] = "-r" - args[3] = git.GetPath() - args = append(args, develProjectPackages...) - cmd = exec.Command("./git-importer", args...) - out, err = cmd.CombinedOutput() - log.Println(string(out)) - if err != nil { - log.Panicln("Error returned by importer.", err) + for _, pkg := range develProjectPackages { + cmd := exec.Command("./git-importer", "-p", prj, "-r", git.GetPath(), pkg) + if idx := slices.IndexFunc(cmd.Env, func(val string) bool { return val[0:12] == "GITEA_TOKEN=" }); idx != -1 { + cmd.Env = slices.Delete(cmd.Env, idx, idx+1) + } + out, err := cmd.CombinedOutput() + log.Println(string(out)) + if err != nil { + log.Panicln("Error returned by importer.", err) + } } if !reposOK { @@ -392,8 +398,6 @@ func main() { switch org { case "pool": return 1 - case "rpm": - return 2 } return 0 // current devel to clone } @@ -421,7 +425,7 @@ func main() { repo := fork.Payload branchName := repo.DefaultBranch - if pkg.Owner.UserName == "pool" || pkg.Owner.UserName == "rpm" { + if pkg.Owner.UserName == "pool" { // 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), @@ -463,7 +467,7 @@ func main() { } for _, pkg := range develProjectPackages { - ret, err := client.Organization.CreateOrgRepo(organization.NewCreateOrgRepoParams().WithOrg(org).WithBody( + _, err := client.Organization.CreateOrgRepo(organization.NewCreateOrgRepoParams().WithOrg(org).WithBody( &models.CreateRepoOption{ ObjectFormatName: "sha256", AutoInit: false, @@ -477,6 +481,29 @@ func main() { log.Panicln("Error creating new package repository:", pkg, err) } + ret, err := client.Repository.RepoEdit(repository.NewRepoEditParams().WithOwner(org).WithRepo(pkg).WithBody( + &models.EditRepoOption{ + HasPullRequests: true, + HasPackages: false, + HasReleases: false, + HasActions: false, + AllowMerge: true, + AllowRebaseMerge: false, + AllowSquash: false, + AllowFastForwardOnly: true, + AllowRebaseUpdate: false, + AllowManualMerge: false, + DefaultMergeStyle: "fast-forward-only", + AllowRebase: false, + DefaultAllowMaintainerEdit: true, + }), + r.DefaultAuthentication, + ) + + if err != nil { + log.Panicln("Failed to adjust repository:", pkg, err) + } + repo := ret.Payload remotes := git.GitExecWithOutputOrPanic(pkg, "remote", "show") if !slices.Contains(strings.Split(remotes, "\n"), "devel") { @@ -494,4 +521,7 @@ func main() { log.Panicln("Failed to set default branch for package fork:", repo.Owner.UserName, "/", repo.Name, err) } } + + // set maintainership from OBS .... + }