[devel-importer] configurable import location

This commit is contained in:
Adam Majer 2024-09-17 10:56:31 +02:00
parent 6b40bf7bbf
commit a47d217ab3

View File

@ -24,7 +24,6 @@ import (
"log" "log"
"os" "os"
"os/exec" "os/exec"
"path"
"slices" "slices"
"strings" "strings"
"time" "time"
@ -50,6 +49,8 @@ func runObsCommand(args ...string) ([]byte, error) {
return cmd.Output() return cmd.Output()
} }
var DebugMode bool
func main() { func main() {
if err := common.RequireGiteaSecretToken(); err != nil { if err := common.RequireGiteaSecretToken(); err != nil {
log.Panicln("Missing GITEA_TOKEN") log.Panicln("Missing GITEA_TOKEN")
@ -59,14 +60,27 @@ func main() {
log.Panicln("Missing OBS_PASSWORD and/or OBS_USER") log.Panicln("Missing OBS_PASSWORD and/or OBS_USER")
} }
//workflowConfig := flag.String("config", "", "Repository and workflow definition file")
giteaHost := flag.String("gitea", "src.opensuse.org", "Gitea instance")
//rabbitUrl := flag.String("url", "amqps://rabbit.opensuse.org", "URL for RabbitMQ instance")
flag.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 := flag.Bool("purge", false, "Purges package repositories. Use with caution")
debugGitPath := flag.String("git-path", "", "Path for temporary git directory. Only used if DebugMode")
flag.Parse()
git, err := common.CreateGitHandler("Autogits - Devel Importer", "not.exist", "devel-importer") git, err := common.CreateGitHandler("Autogits - Devel Importer", "not.exist", "devel-importer")
if err != nil { if err != nil {
log.Panicln("Failed to allocate git handler. Err:", err) log.Panicln("Failed to allocate git handler. Err:", err)
} }
if DebugMode {
purgeOnly := flag.Bool("purge", false, "Purges package repositories. Use with caution") log.Println(" - working directory:" + git.GitPath)
// revNew := flag.Int("nrevs", 20, "Number of new revisions in factory branch. Indicator of broken history import") if len(*debugGitPath) > 0 {
flag.Parse() git.GitPath = *debugGitPath
}
} else {
defer git.Close()
}
if flag.NArg() != 2 { if flag.NArg() != 2 {
printHelp() printHelp()
@ -89,7 +103,7 @@ func main() {
log.Printf("%d packages: %s\n\n", len(packages), strings.Join(packages, " ")) log.Printf("%d packages: %s\n\n", len(packages), strings.Join(packages, " "))
r := transport.New("src.opensuse.org", apiclient.DefaultBasePath, [](string){"https"}) r := transport.New(*giteaHost, apiclient.DefaultBasePath, [](string){"https"})
r.DefaultAuthentication = transport.BearerToken(common.GetGiteaToken()) r.DefaultAuthentication = transport.BearerToken(common.GetGiteaToken())
// r.SetDebug(true) // r.SetDebug(true)
client := apiclient.New(r, nil) client := apiclient.New(r, nil)
@ -126,9 +140,11 @@ func main() {
// fork packags from pool // fork packags from pool
cmd := exec.Command("./git-importer", func(r []*models.Repository) []string { cmd := exec.Command("./git-importer", func(r []*models.Repository) []string {
ret := make([]string, len(r)) ret := make([]string, len(r)+2)
ret[0] = "-r"
ret[1] = git.GitPath
for i := range r { for i := range r {
ret[i] = r[i].Name ret[i+2] = r[i].Name
} }
return ret return ret
}(oldPackageRepos)...) }(oldPackageRepos)...)
@ -141,34 +157,33 @@ func main() {
reposOK := true reposOK := true
for i := range oldPackageRepos { for i := range oldPackageRepos {
pkg := oldPackageRepos[i] pkg := oldPackageRepos[i]
dir := path.Join("repos", pkg.Name)
// add remote repos // add remote repos
out := git.GitExecWithOutputOrPanic(dir, "remote", "show", "-n") out := git.GitExecWithOutputOrPanic(pkg.Name, "remote", "show", "-n")
if !slices.Contains(strings.Split(out, "\n"), "factory") { if !slices.Contains(strings.Split(out, "\n"), "factory") {
out := git.GitExecWithOutputOrPanic(dir, "remote", "add", "factory", pkg.CloneURL) out := git.GitExecWithOutputOrPanic(pkg.Name, "remote", "add", "factory", pkg.CloneURL)
if len(strings.TrimSpace(out)) > 1 { if len(strings.TrimSpace(out)) > 1 {
log.Println(out) log.Println(out)
} }
} }
if !slices.Contains(strings.Split(out, "\n"), "rpm") { if !slices.Contains(strings.Split(out, "\n"), "rpm") {
out := git.GitExecWithOutputOrPanic(dir, "remote", "add", "rpm", "https://src.opensuse.org/rpm/"+pkg.Name+".git") out := git.GitExecWithOutputOrPanic(pkg.Name, "remote", "add", "rpm", "https://src.opensuse.org/rpm/"+pkg.Name+".git")
if len(strings.TrimSpace(out)) > 1 { if len(strings.TrimSpace(out)) > 1 {
log.Println(out) log.Println(out)
} }
} }
out = git.GitExecWithOutputOrPanic(dir, "fetch", "--multiple", "factory", "rpm") out = git.GitExecWithOutputOrPanic(pkg.Name, "fetch", "--multiple", "factory", "rpm")
if len(strings.TrimSpace(out)) > 1 { if len(strings.TrimSpace(out)) > 1 {
log.Println(out) log.Println(out)
} }
// check that nothing is broken with the update // check that nothing is broken with the update
out = git.GitExecWithOutputOrPanic(dir, "rev-list", "factory") out = git.GitExecWithOutputOrPanic(pkg.Name, "rev-list", "factory")
old_revs := strings.Split(out, "\n") old_revs := strings.Split(out, "\n")
out = git.GitExecWithOutputOrPanic(dir, "rev-list", "factory", "^factory/factory") out = git.GitExecWithOutputOrPanic(pkg.Name, "rev-list", "factory", "^factory/factory")
added_revs := strings.Split(out, "\n") added_revs := strings.Split(out, "\n")
out = git.GitExecWithOutputOrPanic(dir, "rev-list", "factory", "^rpm/factory") out = git.GitExecWithOutputOrPanic(pkg.Name, "rev-list", "factory", "^rpm/factory")
added_rpm_revs := strings.Split(out, "\n") 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) && 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) log.Printf("Something is wrong with rev-ist for (len %d): %s\n", len(added_revs), pkg.Name)
@ -176,9 +191,11 @@ func main() {
} }
} }
args := make([]string, 2, len(newPackages)+2) args := make([]string, 4, len(newPackages)+4)
args[0] = "-p" args[0] = "-p"
args[1] = prj args[1] = prj
args[2] = "-r"
args[4] = git.GitPath
args = append(args, newPackages...) args = append(args, newPackages...)
cmd = exec.Command("./git-importer", args...) cmd = exec.Command("./git-importer", args...)
out, err = cmd.CombinedOutput() out, err = cmd.CombinedOutput()
@ -224,14 +241,13 @@ func main() {
} }
branchName := priorityBranches[idx] branchName := priorityBranches[idx]
dir := path.Join("repos", pkg.Name) remotes := git.GitExecWithOutputOrPanic(pkg.Name, "remote", "show")
remotes := git.GitExecWithOutputOrPanic(dir, "remote", "show")
if !slices.Contains(strings.Split(remotes, "\n"), "devel") { if !slices.Contains(strings.Split(remotes, "\n"), "devel") {
git.GitExecOrPanic(dir, "remote", "add", "devel", repo.SSHURL) git.GitExecOrPanic(pkg.Name, "remote", "add", "devel", repo.SSHURL)
} }
git.GitExecOrPanic(dir, "branch", "main", "-f", branchName) git.GitExecOrPanic(pkg.Name, "branch", "main", "-f", branchName)
time.Sleep(2 * time.Second) time.Sleep(2 * time.Second)
git.GitExecOrPanic(dir, "push", "devel", "main") git.GitExecOrPanic(pkg.Name, "push", "devel", "main")
time.Sleep(2 * time.Second) time.Sleep(2 * time.Second)
_, err = client.Repository.RepoEdit(repository.NewRepoEditParams().WithOwner(org).WithRepo(repo.Name).WithBody(&models.EditRepoOption{ _, err = client.Repository.RepoEdit(repository.NewRepoEditParams().WithOwner(org).WithRepo(repo.Name).WithBody(&models.EditRepoOption{
@ -260,14 +276,13 @@ func main() {
} }
repo := ret.Payload repo := ret.Payload
dir := path.Join("repos", pkg) remotes := git.GitExecWithOutputOrPanic(pkg, "remote", "show")
remotes := git.GitExecWithOutputOrPanic(dir, "remote", "show")
if !slices.Contains(strings.Split(remotes, "\n"), "devel") { if !slices.Contains(strings.Split(remotes, "\n"), "devel") {
git.GitExecOrPanic(dir, "remote", "add", "devel", repo.SSHURL) git.GitExecOrPanic(pkg, "remote", "add", "devel", repo.SSHURL)
} }
git.GitExecOrPanic(dir, "branch", "main", "-f", "factory") git.GitExecOrPanic(pkg, "branch", "main", "-f", "factory")
time.Sleep(2 * time.Second) time.Sleep(2 * time.Second)
git.GitExecOrPanic(dir, "push", "devel", "main") git.GitExecOrPanic(pkg, "push", "devel", "main")
time.Sleep(2 * time.Second) time.Sleep(2 * time.Second)
_, err = client.Repository.RepoEdit(repository.NewRepoEditParams().WithOwner(org).WithRepo(pkg).WithBody(&models.EditRepoOption{ _, err = client.Repository.RepoEdit(repository.NewRepoEditParams().WithOwner(org).WithRepo(pkg).WithBody(&models.EditRepoOption{