This commit is contained in:
2024-07-26 16:53:09 +02:00
parent 18bfc87a1c
commit e47feaf5e8
12 changed files with 373 additions and 52 deletions

View File

@@ -23,7 +23,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)
}
@@ -287,6 +287,56 @@ func (h *RequestHandler) RequestReviews(pr *models.PullRequest, reviewer string)
return review.GetPayload()
}
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().
WithOwner(pr.Base.Repo.Owner.UserName).
WithRepo(pr.Base.Repo.Name).
WithIndex(pr.Index).
WithBody(&models.CreatePullReviewOptions{
Event: state,
Body: comment,
}),
transport.DefaultAuthentication,
)
/*
c, err := client.Repository.RepoSubmitPullReview(
repository.NewRepoSubmitPullReviewParams().
WithDefaults().
WithOwner(pr.Base.Repo.Owner.UserName).
WithRepo(pr.Base.Repo.Name).
WithIndex(pr.Index).
WithID(review.ID).
WithBody(&models.SubmitPullReviewOptions{
Event: state,
Body: comment,
}),
transport.DefaultAuthentication,
)
*/
/* c, err := client.Issue.IssueCreateComment(
issue.NewIssueCreateCommentParams().
WithDefaults().
WithOwner(pr.Base.Repo.Owner.UserName).
WithRepo(pr.Base.Repo.Name).
WithIndex(pr.Index).
WithBody(&models.CreateIssueCommentOption{
Body: &comment,
}),
transport.DefaultAuthentication)
*/
if err != nil {
return nil, err
}
return c.Payload, nil
}
func (h *RequestHandler) GetAssociatedPrjGitPR(pr *PullRequestAction) *models.PullRequest {
if h.HasError() {
return nil
@@ -335,20 +385,30 @@ func (h *RequestHandler) GetAssociatedPrjGitPR(pr *PullRequestAction) *models.Pu
return nil
}
func (h *RequestHandler) GetRepositoryFileContent(pr *models.PullRequest, path string) ([]byte, error) {
func (h *RequestHandler) GetRepositoryFileContent(repo *models.Repository, hash, path string) ([]byte, error) {
if h.HasError() {
return nil, h.Error
}
transport, client := h.allocateGiteaTransport()
repo := pr.Head.Repo
var retData []byte
dataOut := writeFunc(func(data []byte) (int, error) {
if len(data) == 0 {
return 0, nil
}
retData = data
return len(data), nil
})
file, err := client.Repository.RepoGetRawFile(
repository.NewRepoGetRawFileParams().
WithOwner(repo.Owner.UserName).
WithRepo(repo.Name).
WithFilepath(path).
WithRef(&pr.Head.Ref),
WithRef(&hash),
transport.DefaultAuthentication,
dataOut,
repository.WithContentTypeApplicationOctetStream,
)
if err != nil {
@@ -359,6 +419,9 @@ func (h *RequestHandler) GetRepositoryFileContent(pr *models.PullRequest, path s
return nil, fmt.Errorf("Invalid response from server (%d): %s", file.Code(), file.Error())
}
return file.Body(), nil
return retData, nil
}
func (h *RequestHandler) GetPullRequestFileContent(pr *models.PullRequest, path string) ([]byte, error) {
return h.GetRepositoryFileContent(pr.Head.Repo, pr.Head.Sha, path)
}