Compare commits
11 Commits
conflicts
...
wip_manual
Author | SHA256 | Date | |
---|---|---|---|
17ec5c5ea2 | |||
0cefb45d8a | |||
ddbb824006 | |||
76aec3aabb | |||
19fb7e277b | |||
51261f1bc1 | |||
949810709d | |||
c012570e89 | |||
44a3b15a7d | |||
e5d07f0ce6
|
|||
df9478a920
|
@@ -610,7 +610,11 @@ func (gitea *GiteaTransport) CreatePullRequestIfNotExist(repo *models.Repository
|
||||
}
|
||||
|
||||
if pr, err := gitea.client.Repository.RepoGetPullRequestByBaseHead(
|
||||
repository.NewRepoGetPullRequestByBaseHeadParams().WithOwner(repo.Owner.UserName).WithRepo(repo.Name).WithBase(targetId).WithHead(srcId),
|
||||
repository.NewRepoGetPullRequestByBaseHeadParams().
|
||||
WithOwner(repo.Owner.UserName).
|
||||
WithRepo(repo.Name).
|
||||
WithBase(targetId).
|
||||
WithHead(srcId),
|
||||
gitea.transport.DefaultAuthentication,
|
||||
); err == nil {
|
||||
return pr.Payload, nil
|
||||
|
58
common/manifest.go
Normal file
58
common/manifest.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type Manifest struct {
|
||||
Subdirectories []string
|
||||
}
|
||||
|
||||
func (m *Manifest) SubdirForPackage(pkg string) string {
|
||||
if m == nil {
|
||||
return pkg
|
||||
}
|
||||
|
||||
idx := -1
|
||||
matchLen := 0
|
||||
lowercasePkg := strings.ToLower(pkg)
|
||||
|
||||
for i, sub := range m.Subdirectories {
|
||||
basename := strings.ToLower(strings.TrimSuffix(sub, "/"))
|
||||
if idx := strings.LastIndex(basename, "/"); idx > 0 {
|
||||
basename = basename[idx+1:]
|
||||
}
|
||||
if strings.HasPrefix(lowercasePkg, basename) && matchLen < len(basename) {
|
||||
idx = i
|
||||
matchLen = len(basename)
|
||||
}
|
||||
}
|
||||
|
||||
if idx > -1 {
|
||||
return path.Join(m.Subdirectories[idx], pkg)
|
||||
}
|
||||
return pkg
|
||||
}
|
||||
|
||||
func ReadManifestFile(filename string) (*Manifest, error) {
|
||||
data, err := os.ReadFile(filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return ParseManifestFile(data)
|
||||
}
|
||||
|
||||
func ParseManifestFile(data []byte) (*Manifest, error) {
|
||||
ret := &Manifest{}
|
||||
err := yaml.Unmarshal(data, ret)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
}
|
56
common/manifest_test.go
Normal file
56
common/manifest_test.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package common_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"src.opensuse.org/autogits/common"
|
||||
)
|
||||
|
||||
func TestManifestSubdirAssignments(t *testing.T) {
|
||||
tests := []struct {
|
||||
Name string
|
||||
ManifestContent string
|
||||
Packages []string
|
||||
ManifestLocations []string
|
||||
}{
|
||||
{
|
||||
Name: "empty manifest",
|
||||
Packages: []string{"atom", "blarg", "Foobar", "X-Ray", "boost", "NodeJS"},
|
||||
ManifestLocations: []string{"atom", "blarg", "Foobar", "X-Ray", "boost", "NodeJS"},
|
||||
},
|
||||
{
|
||||
Name: "only few subdirs manifest",
|
||||
ManifestContent: "subdirectories:\n - a\n - b",
|
||||
Packages: []string{"atom", "blarg", "Foobar", "X-Ray", "Boost", "NodeJS"},
|
||||
ManifestLocations: []string{"a/atom", "b/blarg", "Foobar", "X-Ray", "b/Boost", "NodeJS"},
|
||||
},
|
||||
{
|
||||
Name: "multilayer subdirs manifest",
|
||||
ManifestContent: "subdirectories:\n - a\n - b\n - libs/boo",
|
||||
Packages: []string{"atom", "blarg", "Foobar", "X-Ray", "Boost", "NodeJS"},
|
||||
ManifestLocations: []string{"a/atom", "b/blarg", "Foobar", "X-Ray", "libs/boo/Boost", "NodeJS"},
|
||||
},
|
||||
{
|
||||
Name: "multilayer subdirs manifest with trailing /",
|
||||
ManifestContent: "subdirectories:\n - a\n - b\n - libs/boo/\n - somedir/Node/",
|
||||
Packages: []string{"atom", "blarg", "Foobar", "X-Ray", "Boost", "NodeJS"},
|
||||
ManifestLocations: []string{"a/atom", "b/blarg", "Foobar", "X-Ray", "libs/boo/Boost", "somedir/Node/NodeJS"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.Name, func(t *testing.T) {
|
||||
m, err := common.ParseManifestFile([]byte(test.ManifestContent))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for i, pkg := range test.Packages {
|
||||
expected := test.ManifestLocations[i]
|
||||
if l := m.SubdirForPackage(pkg); l != expected {
|
||||
t.Error("Expected:", expected, "but got:", l)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
@@ -25,6 +25,13 @@ type PRSet struct {
|
||||
BotUser string
|
||||
}
|
||||
|
||||
func (prinfo *PRInfo) PRComponents() (org string, repo string, idx int64) {
|
||||
org = prinfo.PR.Base.Repo.Owner.UserName
|
||||
repo = prinfo.PR.Base.Repo.Name
|
||||
idx = prinfo.PR.Index
|
||||
return
|
||||
}
|
||||
|
||||
func readPRData(gitea GiteaPRFetcher, pr *models.PullRequest, currentSet []*PRInfo, config *AutogitConfig) ([]*PRInfo, error) {
|
||||
for _, p := range currentSet {
|
||||
if pr.Index == p.PR.Index && pr.Base.Repo.Name == p.PR.Base.Repo.Name && pr.Base.Repo.Owner.UserName == p.PR.Base.Repo.Owner.UserName {
|
||||
|
@@ -73,6 +73,10 @@ func runObsCommand(args ...string) ([]string, error) {
|
||||
|
||||
var DebugMode bool
|
||||
|
||||
func giteaPackage(pkg string) string {
|
||||
return strings.ReplaceAll(pkg, "+", "_")
|
||||
}
|
||||
|
||||
func projectMaintainer(obs *common.ObsClient, prj string) ([]string, []string) { // users, groups
|
||||
meta, err := obs.GetProjectMeta(prj)
|
||||
if err != nil {
|
||||
@@ -186,13 +190,16 @@ func cloneDevel(git common.Git, gitDir, outName, urlString string) error {
|
||||
}
|
||||
|
||||
func importRepos(packages []string) {
|
||||
RepoToObsName := make(map[string]string)
|
||||
|
||||
factoryRepos := make([]*models.Repository, 0, len(packages)*2)
|
||||
develProjectPackages := make([]string, 0, len(packages))
|
||||
for _, pkg := range packages {
|
||||
src_pkg_name := strings.Split(pkg, ":")
|
||||
RepoToObsName[giteaPackage(src_pkg_name[0])] = src_pkg_name[0]
|
||||
repo, err := client.Repository.RepoGet(
|
||||
repository.NewRepoGetParams().
|
||||
WithDefaults().WithOwner("pool").WithRepo(src_pkg_name[0]),
|
||||
WithDefaults().WithOwner("pool").WithRepo(giteaPackage(src_pkg_name[0])),
|
||||
r.DefaultAuthentication)
|
||||
|
||||
if err != nil {
|
||||
@@ -219,7 +226,7 @@ func importRepos(packages []string) {
|
||||
|
||||
oldPackageNames := make([]string, 0, len(factoryRepos))
|
||||
for _, repo := range factoryRepos {
|
||||
oldPackageNames = append(oldPackageNames, repo.Name)
|
||||
oldPackageNames = append(oldPackageNames, RepoToObsName[repo.Name])
|
||||
}
|
||||
|
||||
// fork packags from pool
|
||||
@@ -241,44 +248,60 @@ func importRepos(packages []string) {
|
||||
log.Println("adding remotes...")
|
||||
for i := 0; i < len(factoryRepos); i++ {
|
||||
pkg := factoryRepos[i]
|
||||
pkgName := RepoToObsName[pkg.Name]
|
||||
gitName := pkg.Name
|
||||
|
||||
// verify that package was created by `git-importer`, or it's scmsync package and clone it
|
||||
fi, err := os.Stat(filepath.Join(git.GetPath(), pkg.Name))
|
||||
fi, err := os.Stat(filepath.Join(git.GetPath(), gitName))
|
||||
if os.IsNotExist(err) {
|
||||
if slices.Contains(develProjectPackages, pkg.Name) {
|
||||
if slices.Contains(develProjectPackages, pkgName) {
|
||||
// failed import of former factory package
|
||||
log.Println("Failed to import former factory pkg:", pkgName)
|
||||
continue
|
||||
}
|
||||
|
||||
// scmsync?
|
||||
devel_project, err := devel_projects.GetDevelProject(pkg.Name)
|
||||
devel_project, err := devel_projects.GetDevelProject(pkgName)
|
||||
if err != nil {
|
||||
log.Panicln("devel project not found for", pkg.Name, "err:", err)
|
||||
log.Panicln("devel project not found for", RepoToObsName[pkg.Name], "err:", err)
|
||||
}
|
||||
meta, _ := obs.GetPackageMeta(devel_project, pkg.Name)
|
||||
meta, _ := obs.GetPackageMeta(devel_project, pkgName)
|
||||
if len(meta.ScmSync) > 0 {
|
||||
if err2 := cloneDevel(git, "", pkg.Name, meta.ScmSync); err != nil {
|
||||
if err2 := cloneDevel(git, "", gitName, meta.ScmSync); err != nil {
|
||||
log.Panicln(err2)
|
||||
}
|
||||
git.GitExecOrPanic(pkg.Name, "checkout", "-B", "main")
|
||||
if err2 := git.GitExec(gitName, "checkout", "-B", "main"); err2 != nil {
|
||||
git.GitExecOrPanic(gitName, "checkout", "-B", "master")
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
// try again, should now exist
|
||||
if fi, err = os.Stat(filepath.Join(git.GetPath(), pkg.Name)); err != nil {
|
||||
if fi, err = os.Stat(filepath.Join(git.GetPath(), gitName)); err != nil {
|
||||
log.Panicln(err)
|
||||
}
|
||||
} else if err != nil {
|
||||
log.Panicln(err)
|
||||
} else {
|
||||
// verify that we do not have scmsync for imported packages
|
||||
meta, err := obs.GetPackageMeta(prj, pkg.Name)
|
||||
meta, err := obs.GetPackageMeta(prj, pkgName)
|
||||
if err != nil {
|
||||
log.Panicln(err)
|
||||
}
|
||||
|
||||
if len(meta.ScmSync) > 0 {
|
||||
log.Panicln("importing an scmsync package??:", prj, pkg.Name)
|
||||
u, err := url.Parse(meta.ScmSync)
|
||||
if err != nil {
|
||||
log.Println("Invlid scmsync in", pkg, meta.ScmSync, err)
|
||||
}
|
||||
o, err := url.Parse(strings.TrimSpace(git.GitExecWithOutputOrPanic(gitName, "remote", "get-url", "origin")))
|
||||
log.Println("Invlid scmsync in git repo", pkg, meta.ScmSync, err)
|
||||
if u.Host != o.Host || u.Path != u.Path {
|
||||
log.Panicln("importing an scmsync package??:", prj, gitName)
|
||||
} else {
|
||||
log.Println("previous SCMSYNC package. Pull.")
|
||||
git.GitExecOrPanic(gitName, "pull", "origin", "HEAD:main")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -287,11 +310,11 @@ func importRepos(packages []string) {
|
||||
}
|
||||
|
||||
// add remote repos
|
||||
out := git.GitExecWithOutputOrPanic(pkg.Name, "remote", "show", "-n")
|
||||
out := git.GitExecWithOutputOrPanic(gitName, "remote", "show", "-n")
|
||||
switch pkg.Owner.UserName {
|
||||
case "pool":
|
||||
if !slices.Contains(strings.Split(out, "\n"), "pool") {
|
||||
out := git.GitExecWithOutputOrPanic(pkg.Name, "remote", "add", "pool", pkg.CloneURL)
|
||||
out := git.GitExecWithOutputOrPanic(gitName, "remote", "add", "pool", pkg.CloneURL)
|
||||
if len(strings.TrimSpace(out)) > 1 {
|
||||
log.Println(out)
|
||||
}
|
||||
@@ -398,12 +421,22 @@ func importRepos(packages []string) {
|
||||
|
||||
for i := 0; i < len(develProjectPackages); i++ {
|
||||
pkg := develProjectPackages[i]
|
||||
meta, _ := obs.GetPackageMeta(prj, pkg)
|
||||
if len(meta.ScmSync) > 0 {
|
||||
if err2 := cloneDevel(git, "", pkg, meta.ScmSync); err2 != nil {
|
||||
log.Panicln(err2)
|
||||
meta, err := obs.GetPackageMeta(prj, pkg)
|
||||
if err != nil {
|
||||
meta, err = obs.GetPackageMeta(prj, pkg)
|
||||
if err != nil {
|
||||
log.Println("Error fetching pkg meta for:", prj, pkg, err)
|
||||
}
|
||||
}
|
||||
if meta == nil {
|
||||
log.Println(" **** pkg meta is nil? ****")
|
||||
} else if len(meta.ScmSync) > 0 {
|
||||
if _, err := os.Stat(path.Join(git.GetPath(), pkg)); os.IsNotExist(err) {
|
||||
if err2 := cloneDevel(git, "", pkg, meta.ScmSync); err2 != nil {
|
||||
log.Panicln(err2)
|
||||
}
|
||||
git.GitExecOrPanic(pkg, "checkout", "-B", "main")
|
||||
}
|
||||
git.GitExecOrPanic(pkg, "checkout", "-B", "main")
|
||||
continue
|
||||
} else {
|
||||
common.PanicOnError(gitImporter(prj, pkg))
|
||||
@@ -465,7 +498,7 @@ func importRepos(packages []string) {
|
||||
remotes := common.SplitStringNoEmpty(git.GitExecWithOutputOrPanic(pkg.Name, "remote", "show"), "\n")
|
||||
if !slices.Contains(remotes, "develorigin") {
|
||||
git.GitExecOrPanic(pkg.Name, "remote", "add", "develorigin", repo.SSHURL)
|
||||
// git.GitExecOrPanic(pkg.Name, "fetch", "devel")
|
||||
// git.GitExecOrPanic(pkgName, "fetch", "devel")
|
||||
}
|
||||
if slices.Contains(remotes, "origin") {
|
||||
git.GitExecOrPanic(pkg.Name, "lfs", "fetch", "--all")
|
||||
@@ -473,8 +506,8 @@ func importRepos(packages []string) {
|
||||
}
|
||||
git.GitExecOrPanic(pkg.Name, "push", "develorigin", "main", "-f")
|
||||
git.GitExec(pkg.Name, "push", "develorigin", "--delete", "factory", "devel")
|
||||
// git.GitExecOrPanic(pkg.Name, "checkout", "-B", "main", "devel/main")
|
||||
_, err := client.Repository.RepoEdit(repository.NewRepoEditParams().WithOwner(org).WithRepo(repo.Name).WithBody(&models.EditRepoOption{
|
||||
// git.GitExecOrPanic(pkg.ame, "checkout", "-B", "main", "devel/main")
|
||||
_, err := client.Repository.RepoEdit(repository.NewRepoEditParams().WithOwner(org).WithRepo(giteaPackage(repo.Name)).WithBody(&models.EditRepoOption{
|
||||
DefaultBranch: "main",
|
||||
DefaultMergeStyle: "fast-forward-only",
|
||||
HasPullRequests: true,
|
||||
@@ -499,12 +532,13 @@ func importRepos(packages []string) {
|
||||
|
||||
for _, pkg := range develProjectPackages {
|
||||
var repo *models.Repository
|
||||
if repoData, err := client.Repository.RepoGet(repository.NewRepoGetParams().WithOwner(org).WithRepo(pkg), r.DefaultAuthentication); err != nil {
|
||||
if repoData, err := client.Repository.RepoGet(repository.NewRepoGetParams().WithOwner(org).WithRepo(giteaPackage(pkg)), r.DefaultAuthentication); err != nil {
|
||||
giteaPkg := giteaPackage(pkg)
|
||||
_, err := client.Organization.CreateOrgRepo(organization.NewCreateOrgRepoParams().WithOrg(org).WithBody(
|
||||
&models.CreateRepoOption{
|
||||
ObjectFormatName: "sha256",
|
||||
AutoInit: false,
|
||||
Name: &pkg,
|
||||
Name: &giteaPkg,
|
||||
DefaultBranch: "main",
|
||||
}),
|
||||
r.DefaultAuthentication,
|
||||
@@ -514,7 +548,7 @@ func importRepos(packages []string) {
|
||||
log.Panicln("Error creating new package repository:", pkg, err)
|
||||
}
|
||||
|
||||
ret, err := client.Repository.RepoEdit(repository.NewRepoEditParams().WithOwner(org).WithRepo(pkg).WithBody(
|
||||
ret, err := client.Repository.RepoEdit(repository.NewRepoEditParams().WithOwner(org).WithRepo(giteaPkg).WithBody(
|
||||
&models.EditRepoOption{
|
||||
HasPullRequests: true,
|
||||
HasPackages: false,
|
||||
@@ -554,7 +588,7 @@ func importRepos(packages []string) {
|
||||
git.GitExecOrPanic(pkg, "push", "develorigin", "main", "-f")
|
||||
git.GitExec(pkg, "push", "develorigin", "--delete", "factory", "devel")
|
||||
|
||||
_, err := client.Repository.RepoEdit(repository.NewRepoEditParams().WithOwner(org).WithRepo(pkg).WithBody(&models.EditRepoOption{
|
||||
_, err := client.Repository.RepoEdit(repository.NewRepoEditParams().WithOwner(org).WithRepo(giteaPackage(pkg)).WithBody(&models.EditRepoOption{
|
||||
DefaultBranch: "main",
|
||||
DefaultMergeStyle: "fast-forward-only",
|
||||
}), r.DefaultAuthentication)
|
||||
@@ -653,7 +687,7 @@ func syncPackageCollaborators(pkg string, orig_uids []common.PersonRepoMeta) []s
|
||||
missing := []string{}
|
||||
uids := make([]common.PersonRepoMeta, len(orig_uids))
|
||||
copy(uids, orig_uids)
|
||||
collab, err := client.Repository.RepoListCollaborators(repository.NewRepoListCollaboratorsParams().WithOwner(org).WithRepo(pkg), r.DefaultAuthentication)
|
||||
collab, err := client.Repository.RepoListCollaborators(repository.NewRepoListCollaboratorsParams().WithOwner(org).WithRepo(giteaPackage(pkg)), r.DefaultAuthentication)
|
||||
if err != nil {
|
||||
if errors.Is(err, &repository.RepoListCollaboratorsNotFound{}) {
|
||||
return missing
|
||||
@@ -674,7 +708,7 @@ func syncPackageCollaborators(pkg string, orig_uids []common.PersonRepoMeta) []s
|
||||
log.Println("missing collabs for", pkg, ":", uids)
|
||||
}
|
||||
for _, u := range uids {
|
||||
_, err := client.Repository.RepoAddCollaborator(repository.NewRepoAddCollaboratorParams().WithOwner(org).WithRepo(pkg).WithBody(&models.AddCollaboratorOption{
|
||||
_, err := client.Repository.RepoAddCollaborator(repository.NewRepoAddCollaboratorParams().WithOwner(org).WithRepo(giteaPackage(pkg)).WithBody(&models.AddCollaboratorOption{
|
||||
Permission: "write",
|
||||
}).WithCollaborator(u.UserID), r.DefaultAuthentication)
|
||||
|
||||
@@ -809,14 +843,14 @@ func createPrjGit() {
|
||||
if err != nil {
|
||||
log.Panicln(err)
|
||||
}
|
||||
file.WriteString("{\n // Reference build project\n \"ObsProject\": \""+prj+"\",\n}\n")
|
||||
file.WriteString("{\n // Reference build project\n \"ObsProject\": \"" + prj + "\",\n}\n")
|
||||
file.Close()
|
||||
git.GitExecOrPanic(common.DefaultGitPrj, "add", "staging.config")
|
||||
|
||||
if file, err = os.Create(path.Join(git.GetPath(), common.DefaultGitPrj, "workflow.config")); err != nil {
|
||||
log.Panicln(err)
|
||||
}
|
||||
file.WriteString("{\n \"Workflows\": [\"direct\", \"pr\"],\n \"Organization\": \""+org+"\",\n}\n")
|
||||
file.WriteString("{\n \"Workflows\": [\"direct\", \"pr\"],\n \"Organization\": \"" + org + "\",\n}\n")
|
||||
file.Close()
|
||||
git.GitExecOrPanic(common.DefaultGitPrj, "add", "workflow.config")
|
||||
}
|
||||
@@ -857,6 +891,7 @@ func main() {
|
||||
syncMaintainers := flags.Bool("sync-maintainers-only", false, "Sync maintainers to Gitea and exit")
|
||||
flags.BoolVar(&forceBadPool, "bad-pool", false, "Force packages if pool has no branches due to bad import")
|
||||
flags.BoolVar(&forceNonPoolPackages, "non-pool", false, "Allow packages that are not in pool to be created. WARNING: Can't add to factory later!")
|
||||
specificPackage := flags.String("package", "", "Process specific package only, ignoring the others")
|
||||
|
||||
if help := flags.Parse(os.Args[1:]); help == flag.ErrHelp || flags.NArg() != 2 {
|
||||
printHelp(helpString.String())
|
||||
@@ -953,11 +988,15 @@ func main() {
|
||||
if *purgeOnly {
|
||||
log.Println("Purging repositories...")
|
||||
for _, pkg := range packages {
|
||||
client.Repository.RepoDelete(repository.NewRepoDeleteParams().WithOwner(org).WithRepo(pkg), r.DefaultAuthentication)
|
||||
client.Repository.RepoDelete(repository.NewRepoDeleteParams().WithOwner(org).WithRepo(giteaPackage(pkg)), r.DefaultAuthentication)
|
||||
}
|
||||
os.Exit(10)
|
||||
}
|
||||
|
||||
if len(*specificPackage) != 0 {
|
||||
importRepos([]string{*specificPackage})
|
||||
return
|
||||
}
|
||||
importRepos(packages)
|
||||
syncMaintainersToGitea(packages)
|
||||
}
|
||||
|
46
gitea_status_proxy/config.go
Normal file
46
gitea_status_proxy/config.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/tailscale/hujson"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
ForgeEndpoint string `json:"forge_url"`
|
||||
Keys []string `json:"keys"`
|
||||
}
|
||||
|
||||
type contextKey string
|
||||
|
||||
const configKey contextKey = "config"
|
||||
|
||||
func ReadConfig(reader io.Reader) (*Config, error) {
|
||||
data, err := io.ReadAll(reader)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error reading config data: %w", err)
|
||||
}
|
||||
config := Config{}
|
||||
data, err = hujson.Standardize(data)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse json: %w", err)
|
||||
}
|
||||
if err := json.Unmarshal(data, &config); err != nil {
|
||||
return nil, fmt.Errorf("error parsing json to api keys and target url: %w", err)
|
||||
}
|
||||
|
||||
return &config, nil
|
||||
}
|
||||
|
||||
func ReadConfigFile(filename string) (*Config, error) {
|
||||
file, err := os.Open(filename)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot open config file for reading. err: %w", err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
return ReadConfig(file)
|
||||
}
|
15
gitea_status_proxy/handlers.go
Normal file
15
gitea_status_proxy/handlers.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func ConfigMiddleWare(cfg *Config) func(http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := context.WithValue(r.Context(), configKey, cfg)
|
||||
next.ServeHTTP(w, r.WithContext(ctx))
|
||||
})
|
||||
}
|
||||
}
|
169
gitea_status_proxy/main.go
Normal file
169
gitea_status_proxy/main.go
Normal file
@@ -0,0 +1,169 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"src.opensuse.org/autogits/common"
|
||||
)
|
||||
|
||||
type Status struct {
|
||||
Context string `json:"context"`
|
||||
State string `json:"state"`
|
||||
TargetUrl string `json:"target_url"`
|
||||
}
|
||||
|
||||
type StatusInput struct {
|
||||
State string `json:"state"`
|
||||
TargetUrl string `json:"target_url"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
configFile := flag.String("config", "", "status proxy config file")
|
||||
flag.Parse()
|
||||
|
||||
if *configFile == "" {
|
||||
common.LogError("missing required argument config")
|
||||
return
|
||||
}
|
||||
|
||||
config, err := ReadConfigFile(*configFile)
|
||||
|
||||
if err != nil {
|
||||
common.LogError("Failed to read config file", err)
|
||||
return
|
||||
}
|
||||
|
||||
mux := http.NewServeMux()
|
||||
|
||||
mux.Handle("/repos/{owner}/{repo}/statuses/{sha}", ConfigMiddleWare(config)(http.HandlerFunc(StatusProxy)))
|
||||
|
||||
common.LogInfo("server up and listening on :3000")
|
||||
err = http.ListenAndServe(":3000", mux)
|
||||
|
||||
if err != nil {
|
||||
common.LogError("Server failed to start up", err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func StatusProxy(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == http.MethodPost {
|
||||
config, ok := r.Context().Value(configKey).(*Config)
|
||||
|
||||
if !ok {
|
||||
common.LogError("Config missing from context")
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
header := r.Header.Get("Authorization")
|
||||
if header == "" {
|
||||
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
token_arr := strings.Split(header, " ")
|
||||
if len(token_arr) != 2 {
|
||||
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
if !strings.EqualFold(token_arr[0], "Bearer") {
|
||||
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
token := token_arr[1]
|
||||
|
||||
if !slices.Contains(config.Keys, token) {
|
||||
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
owner := r.PathValue("owner")
|
||||
repo := r.PathValue("repo")
|
||||
sha := r.PathValue("sha")
|
||||
|
||||
if !ok {
|
||||
common.LogError("Failed to get config from context, is it set?")
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
posturl := fmt.Sprintf("%s/repos/%s/%s/statuses/%s", config.ForgeEndpoint, owner, repo, sha)
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
var statusinput StatusInput
|
||||
err := decoder.Decode(&statusinput)
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
status := Status{
|
||||
Context: "Build in obs",
|
||||
State: statusinput.State,
|
||||
TargetUrl: statusinput.TargetUrl,
|
||||
}
|
||||
|
||||
status_payload, err := json.Marshal(status)
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
client := &http.Client{}
|
||||
req, err := http.NewRequest("POST", posturl, bytes.NewBuffer(status_payload))
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
ForgeToken := os.Getenv("GITEA_TOKEN")
|
||||
|
||||
if ForgeToken == "" {
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
common.LogError("GITEA_TOKEN was not set, all requests will fail")
|
||||
return
|
||||
}
|
||||
|
||||
req.Header.Add("Content-Type", "Content-Type")
|
||||
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", ForgeToken))
|
||||
|
||||
resp, err := client.Do(req)
|
||||
|
||||
if err != nil {
|
||||
common.LogError(fmt.Sprintf("Request to forge endpoint failed: %v", err))
|
||||
http.Error(w, http.StatusText(http.StatusBadGateway), http.StatusBadGateway)
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(resp.StatusCode)
|
||||
|
||||
/*
|
||||
the commented out section sets every key
|
||||
value from the headers, unsure if this
|
||||
leaks information from gitea
|
||||
|
||||
for k, v := range resp.Header {
|
||||
for _, vv := range v {
|
||||
w.Header().Add(k, vv)
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
_, err = io.Copy(w, resp.Body)
|
||||
if err != nil {
|
||||
common.LogError("Error copying response body: %v", err)
|
||||
}
|
||||
} else {
|
||||
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
}
|
@@ -11,6 +11,7 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode"
|
||||
|
||||
"src.opensuse.org/autogits/common"
|
||||
"src.opensuse.org/autogits/common/gitea-generated/models"
|
||||
@@ -21,9 +22,10 @@ var acceptRx *regexp.Regexp
|
||||
var rejectRx *regexp.Regexp
|
||||
var groupName string
|
||||
|
||||
func InitRegex(groupName string) {
|
||||
acceptRx = regexp.MustCompile("\\s*:\\s*LGTM")
|
||||
rejectRx = regexp.MustCompile("\\s*:\\s*")
|
||||
func InitRegex(newGroupName string) {
|
||||
groupName = newGroupName
|
||||
acceptRx = regexp.MustCompile("^:\\s*(LGTM|approved?)")
|
||||
rejectRx = regexp.MustCompile("^:\\s*")
|
||||
}
|
||||
|
||||
func ParseReviewLine(reviewText string) (bool, string) {
|
||||
@@ -34,7 +36,18 @@ func ParseReviewLine(reviewText string) (bool, string) {
|
||||
return false, line
|
||||
}
|
||||
|
||||
return true, line[glen:]
|
||||
l := line[glen:]
|
||||
for idx, r := range l {
|
||||
if unicode.IsSpace(r) {
|
||||
continue
|
||||
} else if r == ':' {
|
||||
return true, l[idx:]
|
||||
} else {
|
||||
return false, line
|
||||
}
|
||||
}
|
||||
|
||||
return false, line
|
||||
}
|
||||
|
||||
func ReviewAccepted(reviewText string) bool {
|
||||
|
@@ -2,6 +2,76 @@ package main
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestReviews(t *testing.T) {
|
||||
func TestReviewApprovalCheck(t *testing.T) {
|
||||
tests := []struct {
|
||||
Name string
|
||||
GroupName string
|
||||
InString string
|
||||
Approved bool
|
||||
Rejected bool
|
||||
}{
|
||||
{
|
||||
Name: "Empty String",
|
||||
GroupName: "group",
|
||||
InString: "",
|
||||
},
|
||||
{
|
||||
Name: "Random Text",
|
||||
GroupName: "group",
|
||||
InString: "some things LGTM",
|
||||
},
|
||||
{
|
||||
Name: "Group name with Random Text means disapproval",
|
||||
GroupName: "group",
|
||||
InString: "@group: some things LGTM",
|
||||
Rejected: true,
|
||||
},
|
||||
{
|
||||
Name: "Bad name with Approval",
|
||||
GroupName: "group2",
|
||||
InString: "@group: LGTM",
|
||||
},
|
||||
{
|
||||
Name: "Bad name with Approval",
|
||||
GroupName: "group2",
|
||||
InString: "@group: LGTM",
|
||||
},
|
||||
{
|
||||
Name: "LGTM approval",
|
||||
GroupName: "group2",
|
||||
InString: "@group2: LGTM",
|
||||
Approved: true,
|
||||
},
|
||||
{
|
||||
Name: "approval",
|
||||
GroupName: "group2",
|
||||
InString: "@group2: approved",
|
||||
Approved: true,
|
||||
},
|
||||
{
|
||||
Name: "approval",
|
||||
GroupName: "group2",
|
||||
InString: "@group2: approve",
|
||||
Approved: true,
|
||||
},
|
||||
{
|
||||
Name: "disapproval",
|
||||
GroupName: "group2",
|
||||
InString: "@group2: disapprove",
|
||||
Rejected: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.Name, func(t *testing.T) {
|
||||
InitRegex(test.GroupName)
|
||||
|
||||
if r := ReviewAccepted(test.InString); r != test.Approved {
|
||||
t.Error("ReviewAccepted() returned", r, "expecting", test.Approved)
|
||||
}
|
||||
if r := ReviewRejected(test.InString); r != test.Rejected {
|
||||
t.Error("ReviewRejected() returned", r, "expecting", test.Rejected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@@ -18,6 +18,36 @@ func prGitBranchNameForPR(repo string, prNo int) string {
|
||||
return fmt.Sprintf("PR_%s#%d", repo, prNo)
|
||||
}
|
||||
|
||||
func PrjGitDescription(prset *common.PRSet) (title string, desc string) {
|
||||
title_refs := make([]string, 0, len(prset.PRs)-1)
|
||||
refs := make([]string, 0, len(prset.PRs)-1)
|
||||
|
||||
for _, pr := range prset.PRs {
|
||||
if prset.IsPrjGitPR(pr.PR) {
|
||||
continue
|
||||
}
|
||||
org, repo, idx := pr.PRComponents()
|
||||
|
||||
title_refs = append(title_refs, repo)
|
||||
ref := fmt.Sprintf(common.PrPattern, org, repo, idx)
|
||||
refs = append(refs, ref)
|
||||
}
|
||||
|
||||
title = "Forwarded PRs: " + strings.Join(title_refs, ", ")
|
||||
desc = fmt.Sprintf("This is a forwarded pull request by %s\nreferencing the following pull request(s):\n\n", GitAuthor) + strings.Join(refs, "\n") + "\n"
|
||||
|
||||
if prset.Config.ManualMergeOnly {
|
||||
desc = desc + "\n### ManualMergeOnly enabled. To merge, 'merge ok' is required in either the project PR or every package PR."
|
||||
}
|
||||
if prset.Config.ManualMergeProject {
|
||||
desc = desc + "\n### ManualMergeProject enabled. To merge, 'merge ok' is required by project maintainer in the project PR."
|
||||
}
|
||||
if !prset.Config.ManualMergeOnly && !prset.Config.ManualMergeProject {
|
||||
desc = desc + "\n### Automatic merge enabled. This will merge when all review requirements are satisfied."
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func verifyRepositoryConfiguration(repo *models.Repository) error {
|
||||
if repo.AutodetectManualMerge && repo.AllowManualMerge {
|
||||
return nil
|
||||
@@ -90,24 +120,20 @@ func AllocatePRProcessor(req *common.PullRequestWebhookEvent, configs common.Aut
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (pr *PRProcessor) SetSubmodulesToMatchPRSet(prset *common.PRSet) ([]string, []string, error) {
|
||||
func (pr *PRProcessor) SetSubmodulesToMatchPRSet(prset *common.PRSet) error {
|
||||
git := pr.git
|
||||
subList, err := git.GitSubmoduleList(common.DefaultGitPrj, "HEAD")
|
||||
if err != nil {
|
||||
common.LogError("Error fetching submodule list for PrjGit", err)
|
||||
return nil, nil, err
|
||||
return err
|
||||
}
|
||||
|
||||
refs := make([]string, 0, len(prset.PRs))
|
||||
title_refs := make([]string, 0, len(prset.PRs))
|
||||
for _, pr := range prset.PRs {
|
||||
if prset.IsPrjGitPR(pr.PR) {
|
||||
continue
|
||||
}
|
||||
|
||||
org := pr.PR.Base.Repo.Owner.UserName
|
||||
repo := pr.PR.Base.Repo.Name
|
||||
idx := pr.PR.Index
|
||||
org, repo, idx := pr.PRComponents()
|
||||
prHead := pr.PR.Head.Sha
|
||||
revert := false
|
||||
|
||||
@@ -118,7 +144,7 @@ func (pr *PRProcessor) SetSubmodulesToMatchPRSet(prset *common.PRSet) ([]string,
|
||||
var valid bool
|
||||
if prHead, valid = git.GitSubmoduleCommitId(common.DefaultGitPrj, repo, prjGitPR.PR.MergeBase); !valid {
|
||||
common.LogError("Failed fetching original submodule commit id for repo")
|
||||
return nil, nil, err
|
||||
return err
|
||||
}
|
||||
}
|
||||
revert = true
|
||||
@@ -135,9 +161,6 @@ func (pr *PRProcessor) SetSubmodulesToMatchPRSet(prset *common.PRSet) ([]string,
|
||||
|
||||
if revert {
|
||||
commitMsg = fmt.Sprintln("auto-created for", repo, "\n\nThis commit was autocreated by", GitAuthor, "removing\n", ref)
|
||||
} else {
|
||||
refs = append(refs, ref)
|
||||
title_refs = append(title_refs, repo)
|
||||
}
|
||||
|
||||
updateSubmoduleInPR(submodulePath, prHead, git)
|
||||
@@ -156,7 +179,7 @@ func (pr *PRProcessor) SetSubmodulesToMatchPRSet(prset *common.PRSet) ([]string,
|
||||
common.LogError("Failed to find expected repo:", repo)
|
||||
}
|
||||
}
|
||||
return title_refs, refs, nil
|
||||
return nil
|
||||
}
|
||||
|
||||
func (pr *PRProcessor) CreatePRjGitPR(prjGitPRbranch string, prset *common.PRSet) error {
|
||||
@@ -176,8 +199,7 @@ func (pr *PRProcessor) CreatePRjGitPR(prjGitPRbranch string, prset *common.PRSet
|
||||
common.LogError("Failed to fetch PrjGit branch", prjGitPRbranch, err)
|
||||
return err
|
||||
}
|
||||
title_refs, refs, err := pr.SetSubmodulesToMatchPRSet(prset)
|
||||
if err != nil {
|
||||
if err := pr.SetSubmodulesToMatchPRSet(prset); err != nil {
|
||||
return err
|
||||
}
|
||||
newHeadCommit, err := git.GitBranchHead(common.DefaultGitPrj, prjGitPRbranch)
|
||||
@@ -186,12 +208,13 @@ func (pr *PRProcessor) CreatePRjGitPR(prjGitPRbranch string, prset *common.PRSet
|
||||
return err
|
||||
}
|
||||
|
||||
if !common.IsDryRun && headCommit != newHeadCommit {
|
||||
common.PanicOnError(git.GitExec(common.DefaultGitPrj, "push", RemoteName, "+HEAD:"+prjGitPRbranch))
|
||||
pr, err := Gitea.CreatePullRequestIfNotExist(PrjGit, prjGitPRbranch, PrjGitBranch,
|
||||
"Forwarded PRs: "+strings.Join(title_refs, ", "),
|
||||
fmt.Sprintf("This is a forwarded pull request by %s\nreferencing the following pull request(s):\n\n", GitAuthor)+strings.Join(refs, ", "),
|
||||
)
|
||||
if !common.IsDryRun {
|
||||
if headCommit != newHeadCommit {
|
||||
common.PanicOnError(git.GitExec(common.DefaultGitPrj, "push", RemoteName, "+HEAD:"+prjGitPRbranch))
|
||||
}
|
||||
|
||||
title, desc := PrjGitDescription(prset)
|
||||
pr, err := Gitea.CreatePullRequestIfNotExist(PrjGit, prjGitPRbranch, PrjGitBranch, title, desc)
|
||||
if err != nil {
|
||||
common.LogError("Error creating PrjGit PR:", err)
|
||||
return err
|
||||
@@ -247,10 +270,10 @@ func (pr *PRProcessor) UpdatePrjGitPR(prset *common.PRSet) error {
|
||||
PrjGitPR.RemoteName, err = git.GitClone(common.DefaultGitPrj, prjGitPRbranch, PrjGit.SSHURL)
|
||||
common.PanicOnError(err)
|
||||
git.GitExecOrPanic(common.DefaultGitPrj, "fetch", PrjGitPR.RemoteName, PrjGitBranch)
|
||||
ExpectedMergeCommit, err := git.GitRemoteHead(common.DefaultGitPrj, PrjGitPR.RemoteName, PrjGitBranch)
|
||||
|
||||
forcePush := false
|
||||
if ExpectedMergeCommit != PrjGitPR.PR.MergeBase {
|
||||
// trust Gitea here on mergeability
|
||||
if !PrjGitPR.PR.Mergeable {
|
||||
common.PanicOnError(pr.RebaseAndSkipSubmoduleCommits(prset, PrjGitBranch))
|
||||
forcePush = true
|
||||
}
|
||||
@@ -260,8 +283,7 @@ func (pr *PRProcessor) UpdatePrjGitPR(prset *common.PRSet) error {
|
||||
common.LogError("Failed to fetch PrjGit branch", prjGitPRbranch, err)
|
||||
return err
|
||||
}
|
||||
title_refs, refs, err := pr.SetSubmodulesToMatchPRSet(prset)
|
||||
if err != nil {
|
||||
if err := pr.SetSubmodulesToMatchPRSet(prset); err != nil {
|
||||
return err
|
||||
}
|
||||
newHeadCommit, err := git.GitBranchHead(common.DefaultGitPrj, prjGitPRbranch)
|
||||
@@ -270,22 +292,24 @@ func (pr *PRProcessor) UpdatePrjGitPR(prset *common.PRSet) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if !common.IsDryRun && headCommit != newHeadCommit {
|
||||
params := []string{"push", PrjGitPR.RemoteName, "+HEAD:" + prjGitPRbranch}
|
||||
if forcePush {
|
||||
params = slices.Insert(params, 1, "-f")
|
||||
if !common.IsDryRun {
|
||||
if headCommit != newHeadCommit {
|
||||
params := []string{"push", PrjGitPR.RemoteName, "+HEAD:" + prjGitPRbranch}
|
||||
if forcePush {
|
||||
params = slices.Insert(params, 1, "-f")
|
||||
}
|
||||
common.PanicOnError(git.GitExec(common.DefaultGitPrj, params...))
|
||||
}
|
||||
common.PanicOnError(git.GitExec(common.DefaultGitPrj, params...))
|
||||
|
||||
// update PR
|
||||
PrjGitTitle := "Forwarded PRs: " + strings.Join(title_refs, ", ")
|
||||
PrjGitBody := fmt.Sprintf("This is a forwarded pull request by %s\nreferencing the following pull request(s):\n\n", GitAuthor) + strings.Join(refs, ", ")
|
||||
|
||||
Gitea.UpdatePullRequest(PrjGit.Owner.UserName, PrjGit.Name, PrjGitPR.PR.Index, &models.EditPullRequestOption{
|
||||
RemoveDeadline: true,
|
||||
Title: PrjGitTitle,
|
||||
Body: PrjGitBody,
|
||||
})
|
||||
PrjGitTitle, PrjGitBody := PrjGitDescription(prset)
|
||||
if PrjGitPR.PR.Body != PrjGitBody || PrjGitPR.PR.Title != PrjGitTitle {
|
||||
Gitea.UpdatePullRequest(PrjGit.Owner.UserName, PrjGit.Name, PrjGitPR.PR.Index, &models.EditPullRequestOption{
|
||||
RemoveDeadline: true,
|
||||
Title: PrjGitTitle,
|
||||
Body: PrjGitBody,
|
||||
})
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -322,13 +346,42 @@ func (pr *PRProcessor) Process(req *common.PullRequestWebhookEvent) error {
|
||||
prjGitPRbranch = prjGitPR.PR.Head.Name
|
||||
|
||||
if prjGitPR.PR.State != "open" {
|
||||
// close entire prset
|
||||
if prjGitPR.PR.HasMerged {
|
||||
// update branches in project
|
||||
prjGitPR.RemoteName, err = git.GitClone(common.DefaultGitPrj, prjGitPRbranch, prjGitPR.PR.Base.Repo.SSHURL)
|
||||
common.PanicOnError(err)
|
||||
|
||||
old_pkgs, err := git.GitSubmoduleList(common.DefaultGitPrj, prjGitPR.PR.MergeBase)
|
||||
common.PanicOnError(err)
|
||||
new_pkgs, err := git.GitSubmoduleList(common.DefaultGitPrj, prjGitPRbranch)
|
||||
common.PanicOnError(err)
|
||||
|
||||
pkgs := make(map[string]string)
|
||||
for pkg, old_commit := range old_pkgs {
|
||||
if new_commit, found := new_pkgs[pkg]; found {
|
||||
// pkg modified
|
||||
if new_commit != old_commit {
|
||||
pkgs[pkg] = new_commit
|
||||
}
|
||||
} else { // not found, pkg removed
|
||||
pkgs[pkg] = ""
|
||||
}
|
||||
}
|
||||
for pkg, commit := range new_pkgs {
|
||||
if _, found := old_pkgs[pkg]; !found {
|
||||
// pkg added
|
||||
pkgs[pkg] = commit
|
||||
}
|
||||
}
|
||||
|
||||
PrjGitSubmoduleCheck(config, git, common.DefaultGitPrj, pkgs)
|
||||
}
|
||||
|
||||
// close entire prset that is still open
|
||||
common.LogInfo("PR State is closed:", prjGitPR.PR.State)
|
||||
for _, pr := range prset.PRs {
|
||||
if pr.PR.State == "open" {
|
||||
org := pr.PR.Base.Repo.Owner.UserName
|
||||
repo := pr.PR.Base.Repo.Name
|
||||
idx := pr.PR.Index
|
||||
org, repo, idx := pr.PRComponents()
|
||||
Gitea.UpdatePullRequest(org, repo, idx, &models.EditPullRequestOption{
|
||||
State: "closed",
|
||||
})
|
||||
@@ -337,6 +390,14 @@ func (pr *PRProcessor) Process(req *common.PullRequestWebhookEvent) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
if len(prset.PRs) > 1 {
|
||||
for _, pr := range prset.PRs {
|
||||
if prset.IsPrjGitPR(pr.PR) {
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if err = pr.UpdatePrjGitPR(prset); err != nil {
|
||||
return err
|
||||
}
|
||||
|
@@ -55,44 +55,7 @@ func (s *DefaultStateChecker) ProcessPR(pr *models.PullRequest, config *common.A
|
||||
return ProcesPullRequest(&event, common.AutogitConfigs{config})
|
||||
}
|
||||
|
||||
func (s *DefaultStateChecker) VerifyProjectState(config *common.AutogitConfig) ([]*interfaces.PRToProcess, error) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
common.LogError("panic caught")
|
||||
if err, ok := r.(error); !ok {
|
||||
common.LogError(err)
|
||||
}
|
||||
common.LogError(string(debug.Stack()))
|
||||
}
|
||||
}()
|
||||
|
||||
prsToProcess := []*interfaces.PRToProcess{}
|
||||
|
||||
prjGitOrg, prjGitRepo, prjGitBranch := config.GetPrjGit()
|
||||
common.LogInfo(" checking", prjGitOrg+"/"+prjGitRepo+"#"+prjGitBranch)
|
||||
|
||||
git, err := GitHandler.CreateGitHandler(config.Organization)
|
||||
common.LogDebug("Git Path:", git.GetPath())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Cannot create git handler: %w", err)
|
||||
}
|
||||
defer git.Close()
|
||||
|
||||
repo, err := Gitea.CreateRepositoryIfNotExist(git, prjGitOrg, prjGitRepo)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Error fetching or creating '%s/%s#%s' -- aborting verifyProjectState(). Err: %w", prjGitBranch, prjGitRepo, prjGitBranch, err)
|
||||
}
|
||||
|
||||
_, err = git.GitClone(prjGitRepo, prjGitBranch, repo.SSHURL)
|
||||
common.PanicOnError(err)
|
||||
|
||||
prsToProcess = append(prsToProcess, &interfaces.PRToProcess{
|
||||
Org: prjGitOrg,
|
||||
Repo: prjGitRepo,
|
||||
Branch: prjGitBranch,
|
||||
})
|
||||
submodules, err := git.GitSubmoduleList(prjGitRepo, "HEAD")
|
||||
|
||||
func PrjGitSubmoduleCheck(config *common.AutogitConfig, git common.Git, repo string, submodules map[string]string) (prsToProcess []*interfaces.PRToProcess, err error) {
|
||||
nextSubmodule:
|
||||
for sub, commitID := range submodules {
|
||||
common.LogDebug(" + checking", sub, commitID)
|
||||
@@ -135,8 +98,8 @@ nextSubmodule:
|
||||
}
|
||||
|
||||
// not found in past, check if we should advance the branch label ... pull the submodule
|
||||
git.GitExecOrPanic(prjGitRepo, "submodule", "update", "--init", "--filter", "blob:none", "--", sub)
|
||||
subDir := path.Join(prjGitRepo, sub)
|
||||
git.GitExecOrPanic(repo, "submodule", "update", "--init", "--filter", "blob:none", "--", sub)
|
||||
subDir := path.Join(repo, sub)
|
||||
newCommits := common.SplitStringNoEmpty(git.GitExecWithOutputOrPanic(subDir, "rev-list", "^origin/"+branch, commitID), "\n")
|
||||
|
||||
if len(newCommits) >= 1 {
|
||||
@@ -152,10 +115,51 @@ nextSubmodule:
|
||||
}
|
||||
}
|
||||
|
||||
// forward any package-gits referred by the project git, but don't go back
|
||||
return prsToProcess, nil
|
||||
}
|
||||
|
||||
func (s *DefaultStateChecker) VerifyProjectState(config *common.AutogitConfig) ([]*interfaces.PRToProcess, error) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
common.LogError("panic caught")
|
||||
if err, ok := r.(error); !ok {
|
||||
common.LogError(err)
|
||||
}
|
||||
common.LogError(string(debug.Stack()))
|
||||
}
|
||||
}()
|
||||
|
||||
prsToProcess := []*interfaces.PRToProcess{}
|
||||
|
||||
prjGitOrg, prjGitRepo, prjGitBranch := config.GetPrjGit()
|
||||
common.LogInfo(" checking", prjGitOrg+"/"+prjGitRepo+"#"+prjGitBranch)
|
||||
|
||||
git, err := GitHandler.CreateGitHandler(config.Organization)
|
||||
common.LogDebug("Git Path:", git.GetPath())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Cannot create git handler: %w", err)
|
||||
}
|
||||
defer git.Close()
|
||||
|
||||
repo, err := Gitea.CreateRepositoryIfNotExist(git, prjGitOrg, prjGitRepo)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Error fetching or creating '%s/%s#%s' -- aborting verifyProjectState(). Err: %w", prjGitBranch, prjGitRepo, prjGitBranch, err)
|
||||
}
|
||||
|
||||
_, err = git.GitClone(prjGitRepo, prjGitBranch, repo.SSHURL)
|
||||
common.PanicOnError(err)
|
||||
|
||||
prsToProcess = append(prsToProcess, &interfaces.PRToProcess{
|
||||
Org: prjGitOrg,
|
||||
Repo: prjGitRepo,
|
||||
Branch: prjGitBranch,
|
||||
})
|
||||
submodules, err := git.GitSubmoduleList(prjGitRepo, "HEAD")
|
||||
|
||||
// forward any package-gits referred by the project git, but don't go back
|
||||
return PrjGitSubmoduleCheck(config, git, prjGitRepo, submodules)
|
||||
}
|
||||
|
||||
func (s *DefaultStateChecker) CheckRepos() error {
|
||||
errorList := make([]error, 0, 10)
|
||||
|
||||
|
Reference in New Issue
Block a user