This commit is contained in:
Adam Majer 2024-08-23 15:19:37 +02:00
parent 89eff4164d
commit 6ec271132d

View File

@ -6,6 +6,7 @@ import (
"io" "io"
"os" "os"
"path/filepath" "path/filepath"
"slices"
"strings" "strings"
"time" "time"
@ -23,7 +24,7 @@ const PrPattern = "PR: %s/%s#%d"
func (h *RequestHandler) allocateGiteaTransport() (*transport.Runtime, *apiclient.GiteaAPI) { func (h *RequestHandler) allocateGiteaTransport() (*transport.Runtime, *apiclient.GiteaAPI) {
r := transport.New("src.opensuse.org", apiclient.DefaultBasePath, [](string){"https"}) r := transport.New("src.opensuse.org", apiclient.DefaultBasePath, [](string){"https"})
r.DefaultAuthentication = transport.BearerToken(giteaToken) r.DefaultAuthentication = transport.BearerToken(giteaToken)
// r.SetDebug(true) // r.SetDebug(true)
return r, apiclient.New(r, nil) return r, apiclient.New(r, nil)
} }
@ -290,10 +291,75 @@ func (h *RequestHandler) RequestReviews(pr *models.PullRequest, reviewer string)
return review.GetPayload() 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) { func (h *RequestHandler) AddReviewComment(pr *models.PullRequest, state models.ReviewStateType, comment string) (*models.PullReview, error) {
transport, client := h.allocateGiteaTransport() transport, client := h.allocateGiteaTransport()
h.Log("%#v", *pr) h.Log("%#v", *pr)
c, err := client.Repository.RepoCreatePullReview( c, err := client.Repository.RepoCreatePullReview(
repository.NewRepoCreatePullReviewParams(). repository.NewRepoCreatePullReviewParams().
WithDefaults(). WithDefaults().