package main /* * This file is part of Autogits. * * Copyright © 2024 SUSE LLC * * Autogits is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 2 of the License, or (at your option) any later * version. * * Autogits is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * Foobar. If not, see . */ import ( "errors" "flag" "log" "os" "os/exec" "path" "slices" "strings" "time" transport "github.com/go-openapi/runtime/client" "src.opensuse.org/autogits/common" apiclient "src.opensuse.org/autogits/common/gitea-generated/client" "src.opensuse.org/autogits/common/gitea-generated/client/organization" "src.opensuse.org/autogits/common/gitea-generated/client/repository" "src.opensuse.org/autogits/common/gitea-generated/models" ) const commandLineHelp = ` SYNTAX devel-importer ` func printHelp() { log.Println(commandLineHelp) } func runObsCommand(args ...string) ([]byte, error) { cmd := exec.Command("osc", args...) return cmd.Output() } func main() { if err := common.RequireGiteaSecretToken(); err != nil { log.Panicln("Missing GITEA_TOKEN") } if err := common.RequireObsSecretToken(); err != nil { log.Panicln("Missing OBS_PASSWORD and/or OBS_USER") } 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") // revNew := flag.Int("nrevs", 20, "Number of new revisions in factory branch. Indicator of broken history import") flag.Parse() if flag.NArg() != 2 { printHelp() os.Exit(1) } prj := flag.Arg(0) org := flag.Arg(1) packageList, err := runObsCommand("ls", prj) if err != nil { log.Printf("Cannot list packages for project '%s'. Err: %v\n", prj, err) } packages := strings.Split(strings.TrimSpace(string(packageList)), "\n") slices.Sort(packages) for i := range packages { packages[i] = strings.Split(packages[i], ":")[0] } packages = slices.Compact(packages) log.Printf("%d packages: %s\n\n", len(packages), strings.Join(packages, " ")) r := transport.New("src.opensuse.org", apiclient.DefaultBasePath, [](string){"https"}) r.DefaultAuthentication = transport.BearerToken(common.GetGiteaToken()) // r.SetDebug(true) client := apiclient.New(r, nil) if *purgeOnly { log.Println("Purging repositories...") for _, pkg := range packages { client.Repository.RepoDelete(repository.NewRepoDeleteParams().WithOwner(org).WithRepo(pkg), r.DefaultAuthentication) } os.Exit(10) } oldPackageRepos := make([]*models.Repository, 0, len(packages)) newPackages := make([]string, 0, len(packages)) for _, pkg := range packages { src_pkg_name := strings.Split(pkg, ":") repo, err := client.Repository.RepoGet( repository.NewRepoGetParams(). WithDefaults().WithOwner("pool").WithRepo(src_pkg_name[0]), r.DefaultAuthentication) if err != nil { if !errors.Is(err, &repository.RepoGetNotFound{}) { log.Panicln(err) } log.Println("Cannot find src package:", src_pkg_name) newPackages = append(newPackages, src_pkg_name[0]) } else { oldPackageRepos = append(oldPackageRepos, repo.Payload) } } log.Println("Num repos found:", len(oldPackageRepos)) // 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() log.Println(string(out)) if err != nil { log.Println("Error returned by importer.", err) } reposOK := true for i := range oldPackageRepos { pkg := oldPackageRepos[i] dir := path.Join("repos", pkg.Name) // add remote repos out := git.GitExecWithOutputOrPanic(dir, "remote", "show", "-n") if !slices.Contains(strings.Split(out, "\n"), "factory") { out := git.GitExecWithOutputOrPanic(dir, "remote", "add", "factory", pkg.CloneURL) if len(strings.TrimSpace(out)) > 1 { log.Println(out) } } if !slices.Contains(strings.Split(out, "\n"), "rpm") { out := git.GitExecWithOutputOrPanic(dir, "remote", "add", "rpm", "https://src.opensuse.org/rpm/"+pkg.Name+".git") if len(strings.TrimSpace(out)) > 1 { log.Println(out) } } out = git.GitExecWithOutputOrPanic(dir, "fetch", "--multiple", "factory", "rpm") if len(strings.TrimSpace(out)) > 1 { log.Println(out) } // check that nothing is broken with the update out = git.GitExecWithOutputOrPanic(dir, "rev-list", "factory") old_revs := strings.Split(out, "\n") out = git.GitExecWithOutputOrPanic(dir, "rev-list", "factory", "^factory/factory") added_revs := strings.Split(out, "\n") 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) { log.Printf("Something is wrong with rev-ist for (len %d): %s\n", len(added_revs), pkg.Name) reposOK = false } } args := make([]string, 2, len(newPackages)+2) args[0] = "-p" args[1] = prj args = append(args, newPackages...) cmd = exec.Command("./git-importer", args...) out, err = cmd.CombinedOutput() log.Println(out) if err != nil { log.Panicln("Error returned by importer.", err) } if !reposOK { log.Println("aborting import due to broken repos above...") } for _, pkg := range oldPackageRepos { // update package fork, err := client.Repository.CreateFork(repository.NewCreateForkParams(). WithOwner("pool"). WithRepo(pkg.Name). WithBody(&models.CreateForkOption{ Organization: org, }), r.DefaultAuthentication) if err != nil { log.Panicln("Error while trying to create fork from pool/", pkg.Name, err) } 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 := priorityBranches[idx] dir := path.Join("repos", pkg.Name) remotes := git.GitExecWithOutputOrPanic(dir, "remote", "show") if !slices.Contains(strings.Split(remotes, "\n"), "devel") { git.GitExecOrPanic(dir, "remote", "add", "devel", repo.SSHURL) } git.GitExecOrPanic(dir, "branch", "main", "-f", branchName) time.Sleep(2 * time.Second) 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{ DefaultBranch: "main", DefaultMergeStyle: "fast-forward-only", }), r.DefaultAuthentication) if err != nil { log.Panicln("Failed to set default branch for package fork:", repo.Owner.UserName, "/", repo.Name, err) } } for _, pkg := range newPackages { ret, err := client.Organization.CreateOrgRepo(organization.NewCreateOrgRepoParams().WithOrg(org).WithBody( &models.CreateRepoOption{ ObjectFormatName: "sha256", AutoInit: false, Name: &pkg, DefaultBranch: "main", }), r.DefaultAuthentication, ) if err != nil { log.Panicln("Error creating new package repository:", pkg, err) } repo := ret.Payload dir := path.Join("repos", pkg) remotes := git.GitExecWithOutputOrPanic(dir, "remote", "show") if !slices.Contains(strings.Split(remotes, "\n"), "devel") { git.GitExecOrPanic(dir, "remote", "add", "devel", repo.SSHURL) } git.GitExecOrPanic(dir, "branch", "main", "-f", "factory") time.Sleep(2 * time.Second) git.GitExecOrPanic(dir, "push", "devel", "main") time.Sleep(2 * time.Second) _, err = client.Repository.RepoEdit(repository.NewRepoEditParams().WithOwner(org).WithRepo(pkg).WithBody(&models.EditRepoOption{ DefaultBranch: "main", DefaultMergeStyle: "fast-forward-only", }), r.DefaultAuthentication) if err != nil { log.Panicln("Failed to set default branch for package fork:", repo.Owner.UserName, "/", repo.Name, err) } } }