autogits/devel-importer/main.go
2024-08-09 17:53:18 +02:00

204 lines
5.4 KiB
Go

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"
)
const commandLineHelp = `
SYNTAX
devel-importer <obs-project> <gitea-org>`
func printHelp() {
fmt.Println(commandLineHelp)
}
func runObsCommand(args ...string) ([]byte, error) {
cmd := exec.Command("osc", args...)
return cmd.Output()
}
func main() {
if err := common.RequireGiteaSecretToken(); err != nil {
fmt.Println("Missing GITEA_TOKEN")
os.Exit(100)
}
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 := flag.Args()[0]
org := flag.Args()[1]
packageList, err := runObsCommand("ls", prj)
if err != nil {
fmt.Printf("Cannot list packages for project '%s'. Err: %v\n", prj, err)
os.Exit(2)
}
packages := strings.Split(strings.TrimSpace(string(packageList)), "\n")
fmt.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 {
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().
WithDefaults().WithOwner("pool").WithRepo(pkg),
r.DefaultAuthentication)
if err != nil {
if !errors.Is(err, &repository.RepoGetNotFound{}) {
fmt.Println(err)
os.Exit(3)
}
fmt.Printf("Cannot find package: %s\n", pkg)
newPackages = append(newPackages, pkg)
} 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)
}
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)
}
}
}