devel-importer: migrate logging to log module

also remove webhook stuff, since we use RabbitMQ now
This commit is contained in:
Adam Majer 2024-09-16 16:05:46 +02:00
parent d36c0c407f
commit 6b40bf7bbf

View File

@ -21,7 +21,6 @@ package main
import (
"errors"
"flag"
"fmt"
"log"
"os"
"os/exec"
@ -43,7 +42,7 @@ const commandLineHelp = `
devel-importer <obs-project> <gitea-org>`
func printHelp() {
fmt.Println(commandLineHelp)
log.Println(commandLineHelp)
}
func runObsCommand(args ...string) ([]byte, error) {
@ -53,13 +52,11 @@ func runObsCommand(args ...string) ([]byte, error) {
func main() {
if err := common.RequireGiteaSecretToken(); err != nil {
fmt.Println("Missing GITEA_TOKEN")
os.Exit(100)
log.Panicln("Missing GITEA_TOKEN")
}
if err := common.RequireObsSecretToken(); err != nil {
fmt.Printf("Missing OBS_PASSWORD and/or OBS_USER\n")
os.Exit(100)
log.Panicln("Missing OBS_PASSWORD and/or OBS_USER")
}
git, err := common.CreateGitHandler("Autogits - Devel Importer", "not.exist", "devel-importer")
@ -84,7 +81,13 @@ func main() {
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, " "))
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())
@ -92,7 +95,7 @@ func main() {
client := apiclient.New(r, nil)
if *purgeOnly {
fmt.Printf("Purging repositories...\n")
log.Println("Purging repositories...")
for _, pkg := range packages {
client.Repository.RepoDelete(repository.NewRepoDeleteParams().WithOwner(org).WithRepo(pkg), r.DefaultAuthentication)
}
@ -102,38 +105,24 @@ func main() {
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(pkg),
WithDefaults().WithOwner("pool").WithRepo(src_pkg_name[0]),
r.DefaultAuthentication)
if err != nil {
if !errors.Is(err, &repository.RepoGetNotFound{}) {
fmt.Println(err)
os.Exit(3)
log.Panicln(err)
}
fmt.Printf("Cannot find package: %s\n", pkg)
newPackages = append(newPackages, pkg)
log.Println("Cannot find src package:", src_pkg_name)
newPackages = append(newPackages, src_pkg_name[0])
} else {
oldPackageRepos = append(oldPackageRepos, repo.Payload)
}
}
fmt.Printf("Num repos found: %d\n", len(oldPackageRepos))
// add webhooks to prjgit-updater
hooksReponse, err := client.Organization.OrgListHooks(
organization.NewOrgListHooksParams().WithOrg(org),
r.DefaultAuthentication,
)
if err != nil {
fmt.Printf("Error fetching hooks for '%s'. Err: %v\n", org, err)
os.Exit(4)
}
hooks := hooksReponse.Payload
for _, hook := range hooks {
fmt.Printf("hook %d: %#v", hook.ID, hook.Config)
}
log.Println("Num repos found:", len(oldPackageRepos))
// fork packags from pool
cmd := exec.Command("./git-importer", func(r []*models.Repository) []string {
@ -144,10 +133,9 @@ func main() {
return ret
}(oldPackageRepos)...)
out, err := cmd.CombinedOutput()
fmt.Print(string(out))
log.Println(string(out))
if err != nil {
fmt.Printf("Error returned by importer. Err: %v\n", err)
os.Exit(14)
log.Println("Error returned by importer.", err)
}
reposOK := true
@ -160,19 +148,19 @@ func main() {
if !slices.Contains(strings.Split(out, "\n"), "factory") {
out := git.GitExecWithOutputOrPanic(dir, "remote", "add", "factory", pkg.CloneURL)
if len(strings.TrimSpace(out)) > 1 {
fmt.Println(out)
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 {
fmt.Println(out)
log.Println(out)
}
}
out = git.GitExecWithOutputOrPanic(dir, "fetch", "--multiple", "factory", "rpm")
if len(strings.TrimSpace(out)) > 1 {
fmt.Println(out)
log.Println(out)
}
// check that nothing is broken with the update
@ -183,7 +171,7 @@ func main() {
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)
log.Printf("Something is wrong with rev-ist for (len %d): %s\n", len(added_revs), pkg.Name)
reposOK = false
}
}
@ -194,15 +182,13 @@ func main() {
args = append(args, newPackages...)
cmd = exec.Command("./git-importer", args...)
out, err = cmd.CombinedOutput()
fmt.Print(string(out))
log.Println(out)
if err != nil {
fmt.Printf("Error returned by importer. Err: %v\n", err)
os.Exit(15)
log.Panicln("Error returned by importer.", err)
}
if !reposOK {
fmt.Printf("aborting import due to broken repos above...\n")
os.Exit(100)
log.Println("aborting import due to broken repos above...")
}
for _, pkg := range oldPackageRepos {
@ -214,8 +200,7 @@ func main() {
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)
log.Panicln("Error while trying to create fork from pool/", pkg.Name, err)
}
repo := fork.Payload
@ -224,8 +209,7 @@ func main() {
r.DefaultAuthentication,
)
if err != nil {
fmt.Printf("Cannot get list of branches for forked repo: %s/%s\n", org, pkg.Name)
os.Exit(11)
log.Panicln("Cannot get list of branches for forked repo:", org, "/", pkg.Name)
}
priorityBranches := []string{
"devel",
@ -256,8 +240,7 @@ func main() {
}), 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)
log.Panicln("Failed to set default branch for package fork:", repo.Owner.UserName, "/", repo.Name, err)
}
}
@ -273,8 +256,7 @@ func main() {
)
if err != nil {
fmt.Printf("Error creating new package repository: %s Err: %v", pkg, err)
os.Exit(13)
log.Panicln("Error creating new package repository:", pkg, err)
}
repo := ret.Payload
@ -294,8 +276,7 @@ func main() {
}), 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(14)
log.Panicln("Failed to set default branch for package fork:", repo.Owner.UserName, "/", repo.Name, err)
}
}
}