78 lines
1.8 KiB
Go
78 lines
1.8 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
"fmt"
|
||
|
"os"
|
||
|
"os/exec"
|
||
|
"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/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() {
|
||
|
err := common.RequireGiteaSecretToken()
|
||
|
if err != nil {
|
||
|
fmt.Println(err)
|
||
|
os.Exit(100)
|
||
|
}
|
||
|
|
||
|
if len(os.Args) != 3 {
|
||
|
printHelp()
|
||
|
os.Exit(1)
|
||
|
}
|
||
|
|
||
|
prj := os.Args[1]
|
||
|
// org := os.Args[2]
|
||
|
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)
|
||
|
|
||
|
repos := make([]*models.Repository, 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)
|
||
|
} else {
|
||
|
repos = append(repos, repo.Payload)
|
||
|
}
|
||
|
}
|
||
|
fmt.Printf("Num repos found: %d\n", len(repos))
|
||
|
}
|