workflow-pr: maintainership API change

This commit is contained in:
2024-11-29 17:33:01 +01:00
parent 7342dc42e9
commit f4462190c9
3 changed files with 134 additions and 106 deletions

View File

@@ -23,7 +23,6 @@ import (
"io"
"os"
"path/filepath"
"slices"
"strings"
"time"
@@ -40,6 +39,9 @@ import (
const PrPattern = "PR: %s/%s#%d"
// maintainer list file in ProjectGit
const MaintainershipFile = "_maitnainership.json"
const (
// from Gitea
// ReviewStateApproved pr is approved
@@ -65,10 +67,9 @@ type Gitea interface {
CreateRepositoryIfNotExist(git *GitHandler, org Organization, repoName string) (*models.Repository, error)
CreatePullRequestIfNotExist(repo *models.Repository, srcId, targetId, title, body string) (*models.PullRequest, error)
RequestReviews(pr *models.PullRequest, reviewer string) ([]*models.PullReview, error)
IsReviewed(pr *models.PullRequest) (bool, error)
AddReviewComment(pr *models.PullRequest, state models.ReviewStateType, comment string) (*models.PullReview, error)
GetAssociatedPrjGitPR(pr *PullRequestWebhookEvent) (*models.PullRequest, error)
GetRepositoryFileContent(repo *models.Repository, hash, path string) ([]byte, error)
GetRepositoryFileContent(org, repo, hash, path string) ([]byte, error)
GetPullRequestFileContent(pr *models.PullRequest, path string) ([]byte, error)
GetRecentPullRequests(org, repo string) ([]*models.PullRequest, error)
GetRecentCommits(org, repo, branch string, commitNo int64) ([]*models.Commit, error)
@@ -90,6 +91,10 @@ func AllocateGiteaTransport(host string) Gitea {
return &r
}
func (gitea *GiteaTransport) FetchMaintainershipFile(org, repo, branch string) ([]byte, error) {
return gitea.GetRepositoryFileContent(org, repo, branch, MaintainershipFile)
}
func (gitea *GiteaTransport) GetPullRequestAndReviews(org, project string, num int64) (*models.PullRequest, []*models.PullReview, error) {
pr, err := gitea.client.Repository.RepoGetPullRequest(
repository.NewRepoGetPullRequestParams().
@@ -314,56 +319,6 @@ func (gitea *GiteaTransport) RequestReviews(pr *models.PullRequest, reviewer str
return review.GetPayload(), nil
}
func (gitea *GiteaTransport) IsReviewed(pr *models.PullRequest) (bool, error) {
// TODO: get review from project git
reviewers := pr.RequestedReviewers
var page int64
reviews := make([]*models.PullReview, 0, 10)
for {
page++
res, err := gitea.client.Repository.RepoListPullReviews(
repository.NewRepoListPullReviewsParams().
WithOwner(pr.Base.Repo.Owner.UserName).
WithRepo(pr.Base.Repo.Name).
WithPage(&page),
gitea.transport.DefaultAuthentication)
if err != nil {
return false, err
}
if res.IsSuccess() {
reviews = append(reviews, res.Payload...)
if len(res.Payload) < 10 {
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 (gitea *GiteaTransport) AddReviewComment(pr *models.PullRequest, state models.ReviewStateType, comment string) (*models.PullReview, error) {
c, err := gitea.client.Repository.RepoCreatePullReview(
repository.NewRepoCreatePullReviewParams().
@@ -452,7 +407,7 @@ func (gitea *GiteaTransport) GetAssociatedPrjGitPR(pr *PullRequestWebhookEvent)
return nil, nil
}
func (gitea *GiteaTransport) GetRepositoryFileContent(repo *models.Repository, hash, path string) ([]byte, error) {
func (gitea *GiteaTransport) GetRepositoryFileContent(org, repo, hash, path string) ([]byte, error) {
var retData []byte
dataOut := writeFunc(func(data []byte) (int, error) {
@@ -464,8 +419,8 @@ func (gitea *GiteaTransport) GetRepositoryFileContent(repo *models.Repository, h
})
_, err := gitea.client.Repository.RepoGetRawFile(
repository.NewRepoGetRawFileParams().
WithOwner(repo.Owner.UserName).
WithRepo(repo.Name).
WithOwner(org).
WithRepo(repo).
WithFilepath(path).
WithRef(&hash),
gitea.transport.DefaultAuthentication,
@@ -481,7 +436,7 @@ func (gitea *GiteaTransport) GetRepositoryFileContent(repo *models.Repository, h
}
func (gitea *GiteaTransport) GetPullRequestFileContent(pr *models.PullRequest, path string) ([]byte, error) {
return gitea.GetRepositoryFileContent(pr.Head.Repo, pr.Head.Sha, path)
return gitea.GetRepositoryFileContent(pr.Head.Repo.Owner.UserName, pr.Head.Repo.Name, pr.Head.Sha, path)
}
func (gitea *GiteaTransport) GetRecentPullRequests(org, repo string) ([]*models.PullRequest, error) {