From 6ec271132dbe6c7d4fb58013c1d1004c1b37af1b276cd34ea2202d386e144a55 Mon Sep 17 00:00:00 2001 From: Adam Majer Date: Fri, 23 Aug 2024 15:19:37 +0200 Subject: [PATCH] . --- bots-common/gitea_utils.go | 68 +++++++++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/bots-common/gitea_utils.go b/bots-common/gitea_utils.go index b8ffbaa..b795e2e 100644 --- a/bots-common/gitea_utils.go +++ b/bots-common/gitea_utils.go @@ -6,6 +6,7 @@ import ( "io" "os" "path/filepath" + "slices" "strings" "time" @@ -23,7 +24,7 @@ const PrPattern = "PR: %s/%s#%d" 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) + // r.SetDebug(true) return r, apiclient.New(r, nil) } @@ -290,10 +291,75 @@ func (h *RequestHandler) RequestReviews(pr *models.PullRequest, reviewer string) return review.GetPayload() } +func (h *RequestHandler) IsReviewed(pr *models.PullRequest) (bool, error) { + if h.HasError() { + return false, h.Error + } + + transport, client := h.allocateGiteaTransport() + // TODO: get review from project git + reviewers := pr.RequestedReviewers + var page, limit int64 + var reviews []*models.PullReview + page = 0 + limit = 20 + for { + res, err := client.Repository.RepoListPullReviews( + repository.NewRepoListPullReviewsParams(). + WithOwner(pr.Base.Repo.Owner.UserName). + WithRepo(pr.Base.Repo.Name). + WithPage(&page). + WithLimit(&limit), + transport.DefaultAuthentication) + + if err != nil { + return false, err + } + + if res.IsSuccess() { + r := res.Payload + + if reviews == nil { + reviews = r + } else { + reviews = append(reviews, r...) + } + + if len(r) < int(limit) { + break + } + } + } + + slices.Reverse(reviews) + + for _, review := range reviews { + if review.Stale || review.Dismissed { + continue + } + + next_review: + for i, reviewer := range reviewers { + if review.User.UserName == reviewer.UserName { + switch (review.State) { + case ReviewStateApproved: + reviewers = slices.Delete(reviewers, i, i) + break next_review + case ReviewStateRequestChanges: + return false, nil + } + } + } + } + + return len(reviewers) == 0, nil +} + func (h *RequestHandler) AddReviewComment(pr *models.PullRequest, state models.ReviewStateType, comment string) (*models.PullReview, error) { transport, client := h.allocateGiteaTransport() h.Log("%#v", *pr) + c, err := client.Repository.RepoCreatePullReview( repository.NewRepoCreatePullReviewParams(). WithDefaults().