This commit is contained in:
Adam Majer 2024-08-11 23:27:34 +02:00
parent 4c1617f3d4
commit 8cdb196164

View File

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"os" "os"
"os/exec" "os/exec"
"path"
"slices" "slices"
"strings" "strings"
@ -30,6 +31,19 @@ func runObsCommand(args ...string) ([]byte, error) {
return cmd.Output() 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() { func main() {
if err := common.RequireGiteaSecretToken(); err != nil { if err := common.RequireGiteaSecretToken(); err != nil {
fmt.Println("Missing GITEA_TOKEN") fmt.Println("Missing GITEA_TOKEN")
@ -47,8 +61,8 @@ func main() {
os.Exit(100) os.Exit(100)
} }
var purgeOnly bool purgeOnly := flag.Bool("purge", false, "Purges package repositories. Use with caution")
flag.BoolVar(&purgeOnly, "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() flag.Parse()
if flag.NArg() != 2 { if flag.NArg() != 2 {
@ -72,7 +86,7 @@ func main() {
// r.SetDebug(true) // r.SetDebug(true)
client := apiclient.New(r, nil) client := apiclient.New(r, nil)
if purgeOnly { if *purgeOnly {
fmt.Printf("Purging repositories...\n") fmt.Printf("Purging repositories...\n")
for _, pkg := range packages { for _, pkg := range packages {
client.Repository.RepoDelete(repository.NewRepoDeleteParams().WithOwner(org).WithRepo(pkg), r.DefaultAuthentication) client.Repository.RepoDelete(repository.NewRepoDeleteParams().WithOwner(org).WithRepo(pkg), r.DefaultAuthentication)
@ -115,89 +129,141 @@ func main() {
for _, hook := range hooks { for _, hook := range hooks {
fmt.Printf("hook %d: %#v", hook.ID, hook.Config) fmt.Printf("hook %d: %#v", hook.ID, hook.Config)
} }
/*
hookActive := true hookActive := true
hookType := models.CreateHookOptionTypeGitea hookType := models.CreateHookOptionTypeGitea
client.Organization.OrgCreateHook( client.Organization.OrgCreateHook(
organization.NewOrgCreateHookParams().WithOrg(org).WithBody(&models.CreateHookOption{ organization.NewOrgCreateHookParams().WithOrg(org).WithBody(&models.CreateHookOption{
Active: &hookActive, Active: &hookActive,
Type: &hookType, Type: &hookType,
Config: models.CreateHookOptionConfig{ Config: models.CreateHookOptionConfig{
"method": "POST", "method": "POST",
"content_type": "application/json", "content_type": "application/json",
"url": webhookBase + "/prgit-updater", "url": webhookBase + "/prgit-updater",
}, },
Events: []string{ Events: []string{
"push", "push",
}, },
}), }),
r.DefaultAuthentication) r.DefaultAuthentication)
*/
// fork packags from pool // 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 { for i := range oldPackageRepos {
pkg := oldPackageRepos[i] pkg := oldPackageRepos[i]
fork, err := client.Repository.CreateFork( dir := path.Join("repos", pkg.Name)
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)
}
repo := fork.Payload // add remote repos
repoList, err := client.Repository.RepoListBranches( out := gitExec(dir, "remote", "show", "-n")
repository.NewRepoListBranchesParams().WithOwner(org).WithRepo(pkg.Name), if !slices.Contains(strings.Split(out, "\n"), "factory") {
r.DefaultAuthentication, out := gitExec(dir, "remote", "add", "factory", pkg.CloneURL)
) if len(strings.TrimSpace(out)) > 1 {
if err != nil { fmt.Println(out)
fmt.Printf("Cannot get list of branches for forked repo: %s/%s\n", org, pkg.Name) }
os.Exit(11)
} }
priorityBranches := []string{ if !slices.Contains(strings.Split(out, "\n"), "rpm") {
"devel", out := gitExec(dir, "remote", "add", "rpm", "https://src.opensuse.org/rpm/" + pkg.Name + ".git")
"factory", if len(strings.TrimSpace(out)) > 1 {
} fmt.Println(out)
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) { out = gitExec(dir, "fetch", "--multiple", "factory", "rpm")
_, err := client.Repository.RepoEdit(repository.NewRepoEditParams().WithOwner(repo.Owner.UserName).WithRepo(repo.Name).WithBody(&models.EditRepoOption{ if len(strings.TrimSpace(out)) > 1 {
DefaultBranch: priorityBranches[idx], fmt.Println(out)
DefaultMergeStyle: "fast-forward-only", }
}), r.DefaultAuthentication)
// 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 { if err != nil {
fmt.Printf("Failed to set default branch for package fork: %s/%s Err: %v", repo.Owner.UserName, repo.Name, err) fmt.Printf("Error while trying to create fork from pool/%s. Err: %v\n", pkg.Name, err)
os.Exit(12) 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 // create new repositories
for _, pkg := range newPackages { for _, pkg := range newPackages {
_, err := client.Organization.CreateOrgRepo( fmt.Printf(" %s\n", pkg)
organization.NewCreateOrgRepoParams(). /*
WithOrg(org). _, err := client.Organization.CreateOrgRepo(
WithBody( organization.NewCreateOrgRepoParams().
&models.CreateRepoOption{ WithOrg(org).
Name: &pkg, WithBody(
AutoInit: false, &models.CreateRepoOption{
ObjectFormatName: "sha256", Name: &pkg,
}), AutoInit: false,
r.DefaultAuthentication) ObjectFormatName: "sha256",
}),
r.DefaultAuthentication)
if err != nil { if err != nil {
fmt.Printf("Cannot create %s/%s. Err: %v", org, pkg, err) fmt.Printf("Cannot create %s/%s. Err: %v", org, pkg, err)
os.Exit(13) os.Exit(13)
} }
*/
} }
} }