This commit is contained in:
Adam Majer 2024-08-09 17:53:18 +02:00
parent d0fb7ab761
commit fb465e6178

View File

@ -2,14 +2,17 @@ package main
import (
"errors"
"flag"
"fmt"
"os"
"os/exec"
"slices"
"strings"
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"
)
@ -28,19 +31,33 @@ func runObsCommand(args ...string) ([]byte, error) {
}
func main() {
err := common.RequireGiteaSecretToken()
if err != nil {
fmt.Println(err)
if err := common.RequireGiteaSecretToken(); err != nil {
fmt.Println("Missing GITEA_TOKEN")
os.Exit(100)
}
if len(os.Args) != 3 {
if err := common.RequireObsSecretToken(); err != nil {
fmt.Printf("Missing OBS_PASSWORD and/or OBS_USER\n")
os.Exit(100)
}
webhookBase := os.Getenv("WEBHOOK_BASE")
if webhookBase == "" {
fmt.Printf("Missing WEBHOOK_BASE\n")
os.Exit(100)
}
var purgeOnly bool
flag.BoolVar(&purgeOnly, "purge", false, "Purges package repositories. Use with caution")
flag.Parse()
if flag.NArg() != 2 {
printHelp()
os.Exit(1)
}
prj := os.Args[1]
// org := os.Args[2]
prj := flag.Args()[0]
org := flag.Args()[1]
packageList, err := runObsCommand("ls", prj)
if err != nil {
@ -55,7 +72,16 @@ func main() {
// r.SetDebug(true)
client := apiclient.New(r, nil)
repos := make([]*models.Repository, 0, len(packages))
if purgeOnly {
fmt.Printf("Purging repositories...\n")
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 {
repo, err := client.Repository.RepoGet(
repository.NewRepoGetParams().
@ -69,9 +95,109 @@ func main() {
}
fmt.Printf("Cannot find package: %s\n", pkg)
newPackages = append(newPackages, pkg)
} else {
repos = append(repos, repo.Payload)
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)
}
hookActive := true
hookType := models.CreateHookOptionTypeGitea
client.Organization.OrgCreateHook(
organization.NewOrgCreateHookParams().WithOrg(org).WithBody(&models.CreateHookOption{
Active: &hookActive,
Type: &hookType,
Config: models.CreateHookOptionConfig{
"method": "POST",
"content_type": "application/json",
"url": webhookBase + "/prgit-updater",
},
Events: []string{
"push",
},
}),
r.DefaultAuthentication)
// fork packags from pool
for i := range oldPackageRepos {
pkg := oldPackageRepos[i]
fork, err := client.Repository.CreateFork(
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
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
for _, pkg := range newPackages {
_, err := client.Organization.CreateOrgRepo(
organization.NewCreateOrgRepoParams().
WithOrg(org).
WithBody(
&models.CreateRepoOption{
Name: &pkg,
AutoInit: false,
ObjectFormatName: "sha256",
}),
r.DefaultAuthentication)
if err != nil {
fmt.Printf("Cannot create %s/%s. Err: %v", org, pkg, err)
os.Exit(13)
}
}
fmt.Printf("Num repos found: %d\n", len(repos))
}