From 96a07e95c7883d8aab745cd23e345e8665a2192c68bd5f72054c8c904c9a37cf Mon Sep 17 00:00:00 2001 From: Adam Majer Date: Wed, 10 Jul 2024 17:20:23 +0200 Subject: [PATCH] . --- bots-common/gitea_utils.go | 64 +++++++++++++++++++++++++++++++++----- bots-common/request_pr.go | 5 +++ pr-review/main.go | 23 ++++++++++++-- 3 files changed, 81 insertions(+), 11 deletions(-) diff --git a/bots-common/gitea_utils.go b/bots-common/gitea_utils.go index 7166f9c..16e52e9 100644 --- a/bots-common/gitea_utils.go +++ b/bots-common/gitea_utils.go @@ -2,9 +2,12 @@ package common import ( "errors" + "fmt" "io" "os" "path/filepath" + "regexp" + "strings" transport "github.com/go-openapi/runtime/client" apiclient "src.opensuse.org/autogits/common/gitea-generated/client" @@ -13,11 +16,12 @@ import ( "src.opensuse.org/autogits/common/gitea-generated/models" ) -func (h *RequestHandler) allocateGiteaTransport() *transport.Runtime { +func (h *RequestHandler) allocateGiteaTransport() (*transport.Runtime, *apiclient.GiteaAPI) { r := transport.New("src.opensuse.org", apiclient.DefaultBasePath, [](string){"https"}) r.DefaultAuthentication = transport.BearerToken(giteaToken) r.SetDebug(true) - return r + + return r, apiclient.New(r, nil) } func (h *RequestHandler) CreateRepositoryIfNotExist(org Organization, repoName string) *models.Repository { @@ -25,8 +29,7 @@ func (h *RequestHandler) CreateRepositoryIfNotExist(org Organization, repoName s return nil } - transport := h.allocateGiteaTransport() - client := apiclient.New(transport, nil) + transport, client := h.allocateGiteaTransport() repo, err := client.Repository.RepoGet( repository.NewRepoGetParams().WithDefaults().WithOwner(org.Username).WithRepo(repoName), transport.DefaultAuthentication) @@ -96,8 +99,7 @@ func (h *RequestHandler) CreatePullRequest(repo *models.Repository, srcId, targe return nil } - transport := h.allocateGiteaTransport() - client := apiclient.New(transport, nil) + transport, client := h.allocateGiteaTransport() prOptions := models.CreatePullRequestOption{ Base: repo.DefaultBranch, @@ -136,8 +138,7 @@ func (h *RequestHandler) RequestReviews(pr *models.PullRequest, reviewer string) return nil } - transport := h.allocateGiteaTransport() - client := apiclient.New(transport, nil) + transport, client := h.allocateGiteaTransport() reviewOptions := models.PullReviewRequestOptions{ Reviewers: []string{reviewer}, @@ -167,3 +168,50 @@ func (h *RequestHandler) RequestReviews(pr *models.PullRequest, reviewer string) return review.GetPayload() } + +func (h *RequestHandler) GetAssociatedPrjGitPR(pr *PullRequest) *models.PullRequest { + if h.HasError() { + return nil + } + + transport, client := h.allocateGiteaTransport() + + var page, maxSize int64; + page = 1 + maxSize = 10000 + state := "open" + prs, err := client.Repository.RepoListPullRequests( + repository. + NewRepoListPullRequestsParams(). + WithDefaults(). + WithOwner(pr.Repo.Owner.Username). + WithRepo(DefaultGitPrj). + WithState(&state). + WithLimit(&maxSize). + WithPage(&page), + transport.DefaultAuthentication) + + if err != nil { + h.Error = fmt.Errorf("cannot fetch PR list for %s / %s : %v", pr.Repo.Owner.Username, pr.Repo.Name, err) + return nil + } + + if !prs.IsSuccess() { + h.Error = fmt.Errorf("cannot fetch PR list for %s / %s : %s", pr.Repo.Owner.Username, pr.Repo.Name, prs.Error()) + } + + payload_processing: + for _, pr := range prs.Payload { + lines := strings.Split(pr.Body, "\n") + + for _, line := range lines { + r := regexp.MustCompile(`^PullRequest: ([^/]+)/([^/]+)#(\d+)$`) + if m := r.FindSubmatch([]byte(line)); m != nil { + h.Log("match: %#v", m) + break payload_processing + } + } + } + + return nil +} diff --git a/bots-common/request_pr.go b/bots-common/request_pr.go index 3dc9ba4..cfd4348 100644 --- a/bots-common/request_pr.go +++ b/bots-common/request_pr.go @@ -61,3 +61,8 @@ func (h *RequestHandler) parsePullRequest(data io.Reader) *PullRequestAction { return &action } + +func (h *RequestHandler) parsePullRequestSync(data io.Reader) *PullRequestAction { + return h.parsePullRequest(data) +} + diff --git a/pr-review/main.go b/pr-review/main.go index db3c5eb..cd17641 100644 --- a/pr-review/main.go +++ b/pr-review/main.go @@ -14,6 +14,20 @@ const ( PrReview = "pr-review" ) +func processPullRequestSync(h *common.RequestHandler) error { + req := h.Data.(*common.PullRequestAction) + + // sync requests for prjgit not handled here + if req.Repository.Name == common.DefaultGitPrj { + return nil + } + + // find prjgit pull request associated with this one + h.GetAssociatedPrjGitPR(&req.Pull_Request) + + return nil +} + func processPullRequestOpened(h *common.RequestHandler) error { req := h.Data.(*common.PullRequestAction) @@ -29,7 +43,8 @@ func processPullRequestOpened(h *common.RequestHandler) error { This commit was autocreated by %s referencing -PullRequest: %s#%d`, req.Repository.Name, GitAuthor, req.Repository.Name, req.Pull_Request.Number) +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() { @@ -49,8 +64,8 @@ PullRequest: %s#%d`, req.Repository.Name, GitAuthor, req.Repository.Name, req.Pu fmt.Sprintf(`This is a forwarded pull request by %s referencing the following pull request: -PullRequest: %s#%d -`, GitAuthor, req.Repository.Name, req.Pull_Request.Number), +PR: %s/%s#%d +`, GitAuthor, req.Repository.Owner.Username, req.Repository.Name, req.Pull_Request.Number), ) if h.HasError() { @@ -69,6 +84,8 @@ func processPullRequest(h *common.RequestHandler) error { switch req.Action { case "opened": return processPullRequestOpened(h) + case "syncronized": + return processPullRequestSync(h) } return fmt.Errorf("Unhandled pull request action: %s", req.Action)