Compiles, ship it!
ported pr-review to rabbitmq
This commit is contained in:
parent
7940a8cc86
commit
7e663964ee
@ -26,6 +26,7 @@ require (
|
||||
github.com/mitchellh/mapstructure v1.5.0 // indirect
|
||||
github.com/oklog/ulid v1.3.1 // indirect
|
||||
github.com/opentracing/opentracing-go v1.2.0 // indirect
|
||||
github.com/rabbitmq/amqp091-go v1.10.0 // indirect
|
||||
go.mongodb.org/mongo-driver v1.14.0 // indirect
|
||||
go.opentelemetry.io/otel v1.24.0 // indirect
|
||||
go.opentelemetry.io/otel/metric v1.24.0 // indirect
|
||||
|
@ -48,6 +48,8 @@ github.com/opentracing/opentracing-go v1.2.0 h1:uEJPy/1a5RIPAJ0Ov+OIO8OxWu77jEv+
|
||||
github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/rabbitmq/amqp091-go v1.10.0 h1:STpn5XsHlHGcecLmMFCtg7mqq0RnD+zFr4uzukfVhBw=
|
||||
github.com/rabbitmq/amqp091-go v1.10.0/go.mod h1:Hy4jKW5kQART1u+JkDTF9YYOQUHXqMuhrgxOEeS7G4o=
|
||||
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
|
||||
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
@ -64,6 +66,8 @@ go.opentelemetry.io/otel/sdk v1.24.0 h1:YMPPDNymmQN3ZgczicBY3B6sf9n62Dlj9pWD3ucg
|
||||
go.opentelemetry.io/otel/sdk v1.24.0/go.mod h1:KVrIYw6tEubO9E96HQpcmpTKDVn9gdv35HoYiQWGDFg=
|
||||
go.opentelemetry.io/otel/trace v1.24.0 h1:CsKnnL4dUAr/0llH9FKuc698G04IrpWV0MQA/Y1YELI=
|
||||
go.opentelemetry.io/otel/trace v1.24.0/go.mod h1:HPc3Xr/cOApsBI154IU0OI0HJexz+aw5uPdbs3UCjNU=
|
||||
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
|
||||
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
|
||||
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
|
||||
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
|
||||
|
@ -19,21 +19,26 @@ package main
|
||||
*/
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"log"
|
||||
"path"
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
"src.opensuse.org/autogits/common"
|
||||
"src.opensuse.org/autogits/common/gitea-generated/models"
|
||||
)
|
||||
|
||||
const (
|
||||
ListenAddr = "[::1]:8001"
|
||||
|
||||
GitAuthor = "GiteaBot - AutoStaging"
|
||||
GitAuthor = "AutoGits - pr-review"
|
||||
GitEmail = "adam+autogits-pr@zombino.com"
|
||||
PrReview = "pr-review"
|
||||
)
|
||||
|
||||
var configuredRepos map[string][]*common.AutogitConfig
|
||||
var gitea *common.GiteaTransport
|
||||
|
||||
/*
|
||||
func fetchPrGit(h *common.RequestHandler, pr *models.PullRequest) error {
|
||||
// clone PR head and base and return path
|
||||
if h.HasError() {
|
||||
@ -47,9 +52,9 @@ func fetchPrGit(h *common.RequestHandler, pr *models.PullRequest) error {
|
||||
}
|
||||
|
||||
return h.Error
|
||||
}
|
||||
}*/
|
||||
|
||||
func processPullRequestClosed(h *common.RequestHandler) error {
|
||||
func processPullRequestClosed(req *common.PullRequestWebhookEvent, git *common.GitHandler, config *common.AutogitConfig) error {
|
||||
// this needs to be moved to pull merger
|
||||
return nil
|
||||
/*
|
||||
@ -70,45 +75,42 @@ func processPullRequestClosed(h *common.RequestHandler) error {
|
||||
*/
|
||||
}
|
||||
|
||||
func processPrjGitPullRequestSync(h *common.RequestHandler) error {
|
||||
func processPrjGitPullRequestSync(req *common.PullRequestWebhookEvent) error {
|
||||
// req := h.Data.(*common.PullRequestAction)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func prGitBranchNameForPR(req *common.PullRequestAction) string {
|
||||
func prGitBranchNameForPR(req *common.PullRequestWebhookEvent) string {
|
||||
return fmt.Sprintf("PR_%s#%d", req.Repository.Name, req.Pull_Request.Number)
|
||||
}
|
||||
|
||||
func updateOrCreatePRBranch(h *common.RequestHandler, commitMsg, branchName string) {
|
||||
req := h.Data.(*common.PullRequestAction)
|
||||
|
||||
h.GitExec(common.DefaultGitPrj, "submodule", "update", "--init", "--checkout", "--depth", "1", req.Repository.Name)
|
||||
h.GitExec(path.Join(common.DefaultGitPrj, req.Repository.Name), "fetch", "--depth", "1", "origin", req.Pull_Request.Head.Sha)
|
||||
h.GitExec(path.Join(common.DefaultGitPrj, req.Repository.Name), "checkout", req.Pull_Request.Head.Sha)
|
||||
h.GitExec(common.DefaultGitPrj, "commit", "-a", "-m", commitMsg)
|
||||
h.GitExec(common.DefaultGitPrj, "push", "-f", "origin", branchName)
|
||||
func updateOrCreatePRBranch(req *common.PullRequestWebhookEvent, git *common.GitHandler, commitMsg, branchName string) error {
|
||||
common.PanicOnError(git.GitExec(common.DefaultGitPrj, "submodule", "update", "--init", "--checkout", "--depth", "1", req.Repository.Name))
|
||||
common.PanicOnError(git.GitExec(path.Join(common.DefaultGitPrj, req.Repository.Name), "fetch", "--depth", "1", "origin", req.Pull_Request.Head.Sha))
|
||||
common.PanicOnError(git.GitExec(path.Join(common.DefaultGitPrj, req.Repository.Name), "checkout", req.Pull_Request.Head.Sha))
|
||||
common.PanicOnError(git.GitExec(common.DefaultGitPrj, "commit", "-a", "-m", commitMsg))
|
||||
common.PanicOnError(git.GitExec(common.DefaultGitPrj, "push", "-f", "origin", branchName))
|
||||
return nil
|
||||
}
|
||||
|
||||
func processPullRequestSync(h *common.RequestHandler) error {
|
||||
req := h.Data.(*common.PullRequestAction)
|
||||
|
||||
if req.Repository.Name == common.DefaultGitPrj {
|
||||
return processPrjGitPullRequestSync(h)
|
||||
func processPullRequestSync(req *common.PullRequestWebhookEvent, git *common.GitHandler, config *common.AutogitConfig) error {
|
||||
if req.Repository.Name == config.GitProjectName {
|
||||
return processPrjGitPullRequestSync(req)
|
||||
}
|
||||
|
||||
// need to verify that submodule in the PR for prjgit
|
||||
// is still pointing to the HEAD of the PR
|
||||
prjPr := h.GetAssociatedPrjGitPR(req)
|
||||
|
||||
if h.HasError() {
|
||||
h.LogError("%v", h.Error)
|
||||
return h.Error
|
||||
prjPr, err := gitea.GetAssociatedPrjGitPR(req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Cannot get associated PrjGit PR in processPullRequestSync. Err: %w", err)
|
||||
}
|
||||
h.Log("associated pr: %v", prjPr)
|
||||
|
||||
h.GitExec("", "clone", "--branch", prjPr.Head.Name, "--depth", "1", prjPr.Head.Repo.SSHURL, common.DefaultGitPrj)
|
||||
commitId, ok := h.GitSubmoduleCommitId(common.DefaultGitPrj, req.Repository.Name, prjPr.Head.Sha)
|
||||
log.Println("associated pr:", prjPr)
|
||||
|
||||
prjgit := config.GitProjectName
|
||||
common.PanicOnError(git.GitExec("", "clone", "--branch", prjPr.Head.Name, "--depth", "1", prjPr.Head.Repo.SSHURL, prjgit))
|
||||
commitId, ok := git.GitSubmoduleCommitId(prjgit, req.Repository.Name, prjPr.Head.Sha)
|
||||
|
||||
if !ok {
|
||||
return fmt.Errorf("Cannot fetch submodule commit id in prjgit for '%s'", req.Repository.Name)
|
||||
@ -116,31 +118,26 @@ func processPullRequestSync(h *common.RequestHandler) error {
|
||||
|
||||
// nothing changed, still in sync
|
||||
if commitId == req.Pull_Request.Head.Sha {
|
||||
h.Log("commitID already match - nothing to do")
|
||||
log.Println("commitID already match - nothing to do")
|
||||
return nil
|
||||
}
|
||||
|
||||
h.Log("different ids: '%s' vs. '%s'", req.Pull_Request.Head.Sha, commitId)
|
||||
log.Printf("different ids: '%s' vs. '%s'\n", req.Pull_Request.Head.Sha, commitId)
|
||||
|
||||
commitMsg := fmt.Sprintf(`Sync PR
|
||||
|
||||
Update to %s`, req.Pull_Request.Head.Sha)
|
||||
|
||||
h.Log("will create new commit msg: %s", commitMsg)
|
||||
h.Log("error? %v", h.Error)
|
||||
log.Println("will create new commit msg:", commitMsg)
|
||||
|
||||
// we need to update prjgit PR with the new head hash
|
||||
branchName := prGitBranchNameForPR(req)
|
||||
updateOrCreatePRBranch(h, commitMsg, branchName)
|
||||
|
||||
return h.Error
|
||||
return updateOrCreatePRBranch(req, git, commitMsg, branchName)
|
||||
}
|
||||
|
||||
func processPullRequestOpened(h *common.RequestHandler) error {
|
||||
req := h.Data.(*common.PullRequestAction)
|
||||
|
||||
func processPullRequestOpened(req *common.PullRequestWebhookEvent, git *common.GitHandler, config *common.AutogitConfig) error {
|
||||
// requests against project are not handled here
|
||||
if req.Repository.Name == common.DefaultGitPrj {
|
||||
if req.Repository.Name == config.GitProjectName {
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -154,16 +151,16 @@ referencing
|
||||
PullRequest: %s/%s#%d`, req.Repository.Owner.Username,
|
||||
req.Repository.Name, GitAuthor, req.Repository.Name, req.Pull_Request.Number)
|
||||
|
||||
prjGit := h.CreateRepositoryIfNotExist(*req.Repository.Owner, common.DefaultGitPrj)
|
||||
if h.HasError() {
|
||||
return h.Error
|
||||
prjGit, err := gitea.CreateRepositoryIfNotExist(git, *req.Repository.Owner, config.GitProjectName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
h.GitExec("", "clone", "--depth", "1", prjGit.SSHURL, common.DefaultGitPrj)
|
||||
h.GitExec(common.DefaultGitPrj, "checkout", "-B", branchName, prjGit.DefaultBranch)
|
||||
updateOrCreatePRBranch(h, commitMsg, branchName)
|
||||
common.PanicOnError(git.GitExec("", "clone", "--depth", "1", prjGit.SSHURL, config.GitProjectName))
|
||||
common.PanicOnError(git.GitExec(common.DefaultGitPrj, "checkout", "-B", branchName, config.GitProjectName))
|
||||
common.PanicOnError(updateOrCreatePRBranch(req, git, commitMsg, branchName))
|
||||
|
||||
PR := h.CreatePullRequest(prjGit, branchName, prjGit.DefaultBranch,
|
||||
PR, err := gitea.CreatePullRequest(prjGit, branchName, prjGit.DefaultBranch,
|
||||
fmt.Sprintf("Forwarded PR: %s", req.Repository.Name),
|
||||
fmt.Sprintf(`This is a forwarded pull request by %s
|
||||
referencing the following pull request:
|
||||
@ -172,45 +169,119 @@ referencing the following pull request:
|
||||
GitAuthor, req.Repository.Owner.Username, req.Repository.Name, req.Pull_Request.Number),
|
||||
)
|
||||
|
||||
if h.HasError() {
|
||||
return h.Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// request build review
|
||||
h.RequestReviews(PR, common.Bot_BuildReview)
|
||||
|
||||
return h.Error
|
||||
_, err = gitea.RequestReviews(PR, common.Bot_BuildReview)
|
||||
return err
|
||||
}
|
||||
|
||||
func processPullRequest(h *common.RequestHandler) error {
|
||||
req := h.Data.(*common.PullRequestAction)
|
||||
func processPullRequest(request *common.Request) error {
|
||||
req, ok := request.Data.(*common.PullRequestWebhookEvent)
|
||||
if !ok {
|
||||
return fmt.Errorf("*** Invalid data format for PR processing.")
|
||||
}
|
||||
|
||||
configs := configuredRepos[req.Repository.Owner.Username]
|
||||
if len(configs) < 1 {
|
||||
// ignoring pull request against unconfigured project (could be just regular sources?)
|
||||
return nil
|
||||
}
|
||||
|
||||
var config *common.AutogitConfig
|
||||
for _, c := range configs {
|
||||
if c.Branch == req.Pull_Request.Base.Ref {
|
||||
config = c
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
git, err := common.CreateGitHandler(GitAuthor, GitEmail, PrReview)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error allocating GitHandler. Err: %w", err)
|
||||
}
|
||||
|
||||
switch req.Action {
|
||||
case "opened", "reopened":
|
||||
return processPullRequestOpened(h)
|
||||
return processPullRequestOpened(req, git, config)
|
||||
case "synchronized":
|
||||
return processPullRequestSync(h)
|
||||
return processPullRequestSync(req, git, config)
|
||||
case "edited":
|
||||
// not need to be handled??
|
||||
return nil
|
||||
case "closed":
|
||||
return processPullRequestClosed(h)
|
||||
return processPullRequestClosed(req, git, config)
|
||||
}
|
||||
|
||||
return fmt.Errorf("Unhandled pull request action: %s", req.Action)
|
||||
}
|
||||
|
||||
var DebugMode bool
|
||||
var checkOnStart bool
|
||||
var checkInterval time.Duration
|
||||
|
||||
func consistencyCheckProcess() {
|
||||
// TODO -- add self-healing options
|
||||
}
|
||||
|
||||
func main() {
|
||||
if err := common.RequireGiteaSecretToken(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if err := common.RequireRabbitSecrets(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
workflowConfig := flag.String("config", "", "Repository and workflow definition file")
|
||||
giteaHost := flag.String("gitea", "src.opensuse.org", "Gitea instance")
|
||||
rabbitUrl := flag.String("url", "amqps://rabbit.opensuse.org", "URL for RabbitMQ instance")
|
||||
flag.BoolVar(&DebugMode, "debug", false, "Extra debugging information")
|
||||
flag.BoolVar(&checkOnStart, "check-on-start", false, "Check all repositories for consistency on start, without delays")
|
||||
checkIntervalHours := flag.Float64("check-interval", 5, "Check interval (+-random delay) for repositories for consitency, in hours")
|
||||
flag.Parse()
|
||||
|
||||
checkInterval = time.Duration(*checkIntervalHours) * time.Hour
|
||||
|
||||
if len(*workflowConfig) == 0 {
|
||||
log.Fatalln("No configuratio file specified. Aborting")
|
||||
}
|
||||
|
||||
configs, err := common.ReadWorkflowConfigsFile(*workflowConfig)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
configuredRepos = make(map[string][]*common.AutogitConfig)
|
||||
orgs := make([]string, 0, 1)
|
||||
for _, c := range configs {
|
||||
if slices.Contains(c.Workflows, "pr") {
|
||||
if DebugMode {
|
||||
log.Printf(" + adding org: '%s', branch: '%s', prjgit: '%s'\n", c.Organization, c.Branch, c.GitProjectName)
|
||||
}
|
||||
configs := configuredRepos[c.Organization]
|
||||
if configs == nil {
|
||||
configs = make([]*common.AutogitConfig, 0, 1)
|
||||
}
|
||||
configs = append(configs, c)
|
||||
configuredRepos[c.Organization] = configs
|
||||
|
||||
orgs = append(orgs, c.Organization)
|
||||
}
|
||||
}
|
||||
|
||||
gitea = common.AllocateGiteaTransport(*giteaHost)
|
||||
go consistencyCheckProcess()
|
||||
|
||||
var defs common.ListenDefinitions
|
||||
|
||||
defs.Url = PrReview
|
||||
defs.GitAuthor = GitAuthor
|
||||
defs.RabbitURL = *rabbitUrl
|
||||
|
||||
defs.Handlers = make(map[string]common.RequestProcessor)
|
||||
defs.Handlers[common.RequestType_PR] = processPullRequest
|
||||
defs.Handlers[common.RequestType_PR_sync] = processPullRequest
|
||||
defs.Handlers[common.RequestType_PRSync] = processPullRequest
|
||||
|
||||
common.RequireGiteaSecretToken()
|
||||
common.RequireObsSecretToken()
|
||||
common.StartServerWithAddress(defs, ListenAddr)
|
||||
log.Fatal(common.ProcessRabbitMQEvents(defs, orgs))
|
||||
}
|
||||
|
@ -86,7 +86,7 @@ func processConfiguredRepositoryAction(action *common.RepositoryWebhookEvent, co
|
||||
prjgit := config.GitProjectName
|
||||
git, err := common.CreateGitHandler(GitAuthor, GitEmail, AppName)
|
||||
common.PanicOnError(err)
|
||||
if !debugMode {
|
||||
if !DebugMode {
|
||||
defer git.Close()
|
||||
}
|
||||
|
||||
@ -159,7 +159,7 @@ func processConfiguredPushAction(action *common.PushWebhookEvent, config *common
|
||||
prjgit := config.GitProjectName
|
||||
git, err := common.CreateGitHandler(GitAuthor, GitEmail, AppName)
|
||||
common.PanicOnError(err)
|
||||
if !debugMode {
|
||||
if !DebugMode {
|
||||
defer git.Close()
|
||||
}
|
||||
|
||||
@ -273,7 +273,7 @@ next_package:
|
||||
}
|
||||
|
||||
// find all missing repositories, and add them
|
||||
if debugMode {
|
||||
if DebugMode {
|
||||
log.Println("checking for missing repositories...")
|
||||
}
|
||||
repos, err := gitea.GetOrganizationRepositories(orgName)
|
||||
@ -281,13 +281,13 @@ next_package:
|
||||
return err
|
||||
}
|
||||
|
||||
if debugMode {
|
||||
if DebugMode {
|
||||
log.Println(" nRepos:", len(repos))
|
||||
}
|
||||
|
||||
next_repo:
|
||||
for _, r := range repos {
|
||||
if debugMode {
|
||||
if DebugMode {
|
||||
log.Println(" -- checking", r.Name)
|
||||
}
|
||||
|
||||
@ -305,7 +305,7 @@ next_repo:
|
||||
}
|
||||
}
|
||||
|
||||
if debugMode {
|
||||
if DebugMode {
|
||||
log.Println(" -- checking repository:", r.Name)
|
||||
}
|
||||
|
||||
@ -333,7 +333,7 @@ next_repo:
|
||||
common.PanicOnError(git.GitExec(config.GitProjectName, "push"))
|
||||
}
|
||||
|
||||
if debugMode {
|
||||
if DebugMode {
|
||||
log.Println("Verification finished for ", orgName, ", config", config.GitProjectName)
|
||||
}
|
||||
|
||||
@ -381,7 +381,7 @@ func consistencyCheckProcess() {
|
||||
}
|
||||
}
|
||||
|
||||
var debugMode bool
|
||||
var DebugMode bool
|
||||
|
||||
func main() {
|
||||
if err := common.RequireGiteaSecretToken(); err != nil {
|
||||
@ -394,7 +394,7 @@ func main() {
|
||||
workflowConfig := flag.String("config", "", "Repository and workflow definition file")
|
||||
giteaHost := flag.String("gitea", "src.opensuse.org", "Gitea instance")
|
||||
rabbitUrl := flag.String("url", "amqps://rabbit.opensuse.org", "URL for RabbitMQ instance")
|
||||
flag.BoolVar(&debugMode, "debug", false, "Extra debugging information")
|
||||
flag.BoolVar(&DebugMode, "debug", false, "Extra debugging information")
|
||||
flag.BoolVar(&checkOnStart, "check-on-start", false, "Check all repositories for consistency on start, without delays")
|
||||
checkIntervalHours := flag.Float64("check-interval", 5, "Check interval (+-random delay) for repositories for consitency, in hours")
|
||||
flag.Parse()
|
||||
@ -414,7 +414,7 @@ func main() {
|
||||
orgs := make([]string, 0, 1)
|
||||
for _, c := range configs {
|
||||
if slices.Contains(c.Workflows, "direct") {
|
||||
if debugMode {
|
||||
if DebugMode {
|
||||
log.Printf(" + adding org: '%s', branch: '%s', prjgit: '%s'\n", c.Organization, c.Branch, c.GitProjectName)
|
||||
}
|
||||
configs := configuredRepos[c.Organization]
|
||||
|
Loading…
Reference in New Issue
Block a user