autogits/bots-common/gitea_utils.go

424 lines
11 KiB
Go
Raw Normal View History

2024-07-07 21:08:41 +02:00
package common
import (
2024-07-10 17:20:23 +02:00
"fmt"
2024-07-07 21:08:41 +02:00
"io"
2024-08-28 17:20:09 +02:00
"log"
2024-07-07 21:08:41 +02:00
"os"
"path/filepath"
2024-08-23 15:19:37 +02:00
"slices"
2024-07-10 17:20:23 +02:00
"strings"
2024-07-18 16:43:27 +02:00
"time"
2024-07-07 21:08:41 +02:00
transport "github.com/go-openapi/runtime/client"
2024-07-18 16:43:27 +02:00
"github.com/go-openapi/strfmt"
2024-07-07 21:08:41 +02:00
apiclient "src.opensuse.org/autogits/common/gitea-generated/client"
2024-07-18 16:43:27 +02:00
"src.opensuse.org/autogits/common/gitea-generated/client/notification"
2024-07-07 21:08:41 +02:00
"src.opensuse.org/autogits/common/gitea-generated/client/organization"
"src.opensuse.org/autogits/common/gitea-generated/client/repository"
"src.opensuse.org/autogits/common/gitea-generated/models"
)
2024-07-11 16:45:49 +02:00
const PrPattern = "PR: %s/%s#%d"
2024-07-10 17:20:23 +02:00
func (h *RequestHandler) allocateGiteaTransport() (*transport.Runtime, *apiclient.GiteaAPI) {
2024-07-09 23:22:42 +02:00
r := transport.New("src.opensuse.org", apiclient.DefaultBasePath, [](string){"https"})
2024-07-10 11:25:00 +02:00
r.DefaultAuthentication = transport.BearerToken(giteaToken)
2024-08-23 15:19:37 +02:00
// r.SetDebug(true)
2024-07-10 17:20:23 +02:00
return r, apiclient.New(r, nil)
2024-07-09 23:22:42 +02:00
}
2024-07-18 23:36:41 +02:00
const (
// from Gitea
2024-07-22 17:35:48 +02:00
// ReviewStateApproved pr is approved
ReviewStateApproved models.ReviewStateType = "APPROVED"
// ReviewStatePending pr state is pending
ReviewStatePending models.ReviewStateType = "PENDING"
// ReviewStateComment is a comment review
ReviewStateComment models.ReviewStateType = "COMMENT"
// ReviewStateRequestChanges changes for pr are requested
ReviewStateRequestChanges models.ReviewStateType = "REQUEST_CHANGES"
// ReviewStateRequestReview review is requested from user
ReviewStateRequestReview models.ReviewStateType = "REQUEST_REVIEW"
// ReviewStateUnknown state of pr is unknown
ReviewStateUnknown models.ReviewStateType = ""
2024-07-18 23:36:41 +02:00
)
func (h *RequestHandler) GetPullRequestAndReviews(org, project string, num int64) (*models.PullRequest, []*models.PullReview, error) {
transport, client := h.allocateGiteaTransport()
pr, err := client.Repository.RepoGetPullRequest(
repository.NewRepoGetPullRequestParams().
WithDefaults().
WithOwner(org).
WithRepo(project).
2024-07-22 17:35:48 +02:00
WithIndex(num),
2024-07-18 23:36:41 +02:00
transport.DefaultAuthentication,
)
if err != nil {
2024-08-28 17:20:09 +02:00
log.Println(err.Error())
2024-07-18 23:36:41 +02:00
return nil, nil, err
}
limit := int64(1000)
reviews, err := client.Repository.RepoListPullReviews(
repository.NewRepoListPullReviewsParams().
WithDefaults().
WithOwner(org).
WithRepo(project).
WithIndex(num).
WithLimit(&limit),
transport.DefaultAuthentication,
)
if err != nil {
2024-08-28 17:20:09 +02:00
log.Println(err.Error())
2024-07-18 23:36:41 +02:00
return nil, nil, err
}
return pr.Payload, reviews.Payload, nil
}
2024-08-05 17:00:39 +02:00
func (h *RequestHandler) GetPullNotifications(since *time.Time) ([]*models.NotificationThread, error) {
2024-07-18 16:43:27 +02:00
bigLimit := int64(100000)
transport, client := h.allocateGiteaTransport()
params := notification.NewNotifyGetListParams().
2024-07-22 17:35:48 +02:00
WithDefaults().
2024-08-05 17:00:39 +02:00
WithSubjectType([]string{"Pull"}).
WithStatusTypes([]string{"unread"}).
2024-07-22 17:35:48 +02:00
WithLimit(&bigLimit)
2024-07-18 16:43:27 +02:00
if since != nil {
s := strfmt.DateTime(*since)
params.SetSince(&s)
}
2024-07-22 17:35:48 +02:00
2024-07-18 16:43:27 +02:00
list, err := client.Notification.NotifyGetList(params, transport.DefaultAuthentication)
if err != nil {
return nil, err
}
if !list.IsSuccess() {
2024-08-28 17:20:09 +02:00
return nil, fmt.Errorf("Cannot fetch notifications: %s", list.Error())
2024-07-18 16:43:27 +02:00
}
return list.Payload, nil
}
2024-07-18 23:36:41 +02:00
func (h *RequestHandler) SetNotificationRead(notificationId int64) error {
2024-07-18 16:43:27 +02:00
transport, client := h.allocateGiteaTransport()
list, err := client.Notification.NotifyReadThread(
notification.NewNotifyReadThreadParams().
WithDefaults().
2024-08-05 17:00:39 +02:00
WithID(fmt.Sprint(notificationId)),
2024-07-18 16:43:27 +02:00
transport.DefaultAuthentication,
)
if err != nil {
2024-08-28 17:20:09 +02:00
log.Printf("Error setting notification: %d: %v\n", notificationId, err)
2024-07-18 16:43:27 +02:00
return err
}
if !list.IsSuccess() {
2024-08-28 17:20:09 +02:00
return fmt.Errorf("Cannot update notifications: %d", notificationId)
2024-07-18 16:43:27 +02:00
}
return nil
}
2024-08-28 17:20:09 +02:00
func (h *RequestHandler) CreateRepositoryIfNotExist(org Organization, repoName string) (*models.Repository, error) {
2024-07-10 17:20:23 +02:00
transport, client := h.allocateGiteaTransport()
2024-07-07 21:08:41 +02:00
repo, err := client.Repository.RepoGet(
repository.NewRepoGetParams().WithDefaults().WithOwner(org.Username).WithRepo(repoName),
2024-07-09 23:22:42 +02:00
transport.DefaultAuthentication)
2024-07-07 21:08:41 +02:00
if err != nil {
switch err.(type) {
case *repository.RepoGetNotFound:
2024-08-24 13:32:39 +02:00
h.StdLogger.Printf("repo '%s' does not exist. Trying to create it ....\n", repoName)
2024-07-07 21:08:41 +02:00
repo, err := client.Organization.CreateOrgRepo(
organization.NewCreateOrgRepoParams().WithDefaults().WithBody(
&models.CreateRepoOption{
AutoInit: false,
Name: &repoName,
ObjectFormatName: models.CreateRepoOptionObjectFormatNameSha256,
},
).WithOrg(org.Username),
nil,
)
if err != nil {
switch err.(type) {
case *organization.CreateOrgRepoCreated:
2024-08-24 13:32:39 +02:00
h.StdLogger.Printf("repo '%s' created, with notification error?\n", repoName)
2024-07-07 21:08:41 +02:00
default:
2024-08-28 17:20:09 +02:00
log.Printf("error creating repo '%s' under '%s': %s\n", repoName, org.Username, err.Error())
return nil, err
2024-07-07 21:08:41 +02:00
}
} else {
2024-08-24 13:32:39 +02:00
h.StdLogger.Printf("repo '%s' created\n", repoName)
2024-07-07 21:08:41 +02:00
}
// initialize repository
2024-08-28 17:20:09 +02:00
if err = os.Mkdir(filepath.Join(h.Git.GitPath, DefaultGitPrj), 0700); err != nil {
return nil, err
2024-07-07 21:08:41 +02:00
}
2024-08-28 17:20:09 +02:00
h.Git.GitExec(DefaultGitPrj, "init", "--object-format="+repo.Payload.ObjectFormatName)
h.Git.GitExec(DefaultGitPrj, "checkout", "-b", repo.Payload.DefaultBranch)
readmeFilename := filepath.Join(h.Git.GitPath, DefaultGitPrj, "README.md")
2024-07-07 21:08:41 +02:00
{
file, _ := os.Create(readmeFilename)
defer file.Close()
io.WriteString(file, ReadmeBoilerplate)
}
2024-08-28 17:20:09 +02:00
h.Git.GitExec(DefaultGitPrj, "add", "README.md")
h.Git.GitExec(DefaultGitPrj, "commit", "-m", "Automatic devel project creation")
h.Git.GitExec(DefaultGitPrj, "remote", "add", "origin", repo.Payload.SSHURL)
2024-07-07 21:08:41 +02:00
2024-08-28 17:20:09 +02:00
return repo.Payload, nil
2024-07-07 21:08:41 +02:00
default:
2024-08-28 17:20:09 +02:00
return nil, fmt.Errorf("cannot fetch repo data for '%s' / '%s' : %w", org.Username, repoName, err)
2024-07-07 21:08:41 +02:00
}
}
2024-08-28 17:20:09 +02:00
return repo.Payload, nil
2024-07-07 21:08:41 +02:00
}
2024-08-28 17:20:09 +02:00
func (h *RequestHandler) CreatePullRequest(repo *models.Repository, srcId, targetId, title, body string) (*models.PullRequest, error) {
2024-07-10 17:20:23 +02:00
transport, client := h.allocateGiteaTransport()
2024-07-09 23:22:42 +02:00
2024-07-10 11:41:34 +02:00
prOptions := models.CreatePullRequestOption{
Base: repo.DefaultBranch,
Head: srcId,
Title: title,
Body: body,
}
2024-07-09 23:22:42 +02:00
pr, err := client.Repository.RepoCreatePullRequest(
repository.
NewRepoCreatePullRequestParams().
WithDefaults().
WithOwner(repo.Owner.UserName).
WithRepo(repo.Name).
WithBody(&prOptions),
transport.DefaultAuthentication,
)
if err != nil {
2024-08-28 17:20:09 +02:00
return nil, fmt.Errorf("Cannot create pull request. %w", err)
2024-07-09 23:22:42 +02:00
}
2024-08-28 17:20:09 +02:00
return pr.GetPayload(), nil
2024-07-09 23:22:42 +02:00
}
2024-08-28 17:20:09 +02:00
func (h *RequestHandler) RequestReviews(pr *models.PullRequest, reviewer string) ([]*models.PullReview, error) {
2024-07-10 17:20:23 +02:00
transport, client := h.allocateGiteaTransport()
2024-07-09 23:22:42 +02:00
2024-07-10 11:41:34 +02:00
reviewOptions := models.PullReviewRequestOptions{
Reviewers: []string{reviewer},
}
2024-07-09 23:22:42 +02:00
review, err := client.Repository.RepoCreatePullReviewRequests(
repository.
NewRepoCreatePullReviewRequestsParams().
WithOwner(pr.Base.Repo.Owner.UserName).
WithRepo(pr.Base.Repo.Name).
WithIndex(pr.Index).
WithBody(&reviewOptions),
transport.DefaultAuthentication,
)
if err != nil {
2024-08-28 17:20:09 +02:00
return nil, fmt.Errorf("Cannot create pull request: %w", err)
2024-07-09 23:22:42 +02:00
}
2024-08-28 17:20:09 +02:00
return review.GetPayload(), nil
2024-07-09 23:22:42 +02:00
}
2024-07-10 17:20:23 +02:00
2024-08-23 15:19:37 +02:00
func (h *RequestHandler) IsReviewed(pr *models.PullRequest) (bool, 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
}
2024-07-26 16:53:09 +02:00
func (h *RequestHandler) AddReviewComment(pr *models.PullRequest, state models.ReviewStateType, comment string) (*models.PullReview, error) {
transport, client := h.allocateGiteaTransport()
2024-08-24 13:32:39 +02:00
h.StdLogger.Printf("%#v", *pr)
2024-08-23 15:19:37 +02:00
2024-07-26 16:53:09 +02:00
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
}
2024-08-28 17:20:09 +02:00
func (h *RequestHandler) GetAssociatedPrjGitPR(pr *PullRequestWebhookEvent) (*models.PullRequest, error) {
2024-07-10 17:20:23 +02:00
transport, client := h.allocateGiteaTransport()
2024-07-18 16:43:27 +02:00
var page, maxSize int64
2024-07-10 17:20:23 +02:00
page = 1
maxSize = 10000
state := "open"
prs, err := client.Repository.RepoListPullRequests(
repository.
NewRepoListPullRequestsParams().
WithDefaults().
2024-07-10 20:41:24 +02:00
WithOwner(pr.Repository.Owner.Username).
2024-07-10 17:20:23 +02:00
WithRepo(DefaultGitPrj).
WithState(&state).
WithLimit(&maxSize).
WithPage(&page),
transport.DefaultAuthentication)
if err != nil {
2024-08-28 17:20:09 +02:00
return nil, fmt.Errorf("cannot fetch PR list for %s / %s : %w", pr.Repository.Owner.Username, pr.Repository.Name, err)
2024-07-10 17:20:23 +02:00
}
2024-07-11 16:45:49 +02:00
prLine := fmt.Sprintf(PrPattern, pr.Repository.Owner.Username, pr.Repository.Name, pr.Number)
2024-08-24 13:32:39 +02:00
h.StdLogger.Printf("attemping to match line: '%s'\n", prLine)
2024-07-11 16:45:49 +02:00
2024-07-18 16:43:27 +02:00
// payload_processing:
2024-07-10 17:20:23 +02:00
for _, pr := range prs.Payload {
lines := strings.Split(pr.Body, "\n")
for _, line := range lines {
2024-07-11 16:45:49 +02:00
if strings.TrimSpace(line) == prLine {
2024-08-28 17:20:09 +02:00
return pr, nil
2024-07-10 17:20:23 +02:00
}
}
}
2024-08-28 17:20:09 +02:00
return nil, nil
2024-07-10 17:20:23 +02:00
}
2024-07-22 17:35:48 +02:00
2024-07-26 16:53:09 +02:00
func (h *RequestHandler) GetRepositoryFileContent(repo *models.Repository, hash, path string) ([]byte, error) {
2024-07-22 17:35:48 +02:00
transport, client := h.allocateGiteaTransport()
2024-07-26 16:53:09 +02:00
var retData []byte
dataOut := writeFunc(func(data []byte) (int, error) {
if len(data) == 0 {
return 0, nil
}
retData = data
return len(data), nil
})
2024-08-28 17:20:09 +02:00
_, err := client.Repository.RepoGetRawFile(
2024-07-22 17:35:48 +02:00
repository.NewRepoGetRawFileParams().
WithOwner(repo.Owner.UserName).
WithRepo(repo.Name).
WithFilepath(path).
2024-07-26 16:53:09 +02:00
WithRef(&hash),
2024-07-22 17:35:48 +02:00
transport.DefaultAuthentication,
2024-07-26 16:53:09 +02:00
dataOut,
repository.WithContentTypeApplicationOctetStream,
2024-07-22 17:35:48 +02:00
)
if err != nil {
return nil, err
}
2024-07-26 16:53:09 +02:00
return retData, nil
2024-07-22 17:35:48 +02:00
}
2024-07-26 16:53:09 +02:00
func (h *RequestHandler) GetPullRequestFileContent(pr *models.PullRequest, path string) ([]byte, error) {
return h.GetRepositoryFileContent(pr.Head.Repo, pr.Head.Sha, path)
}