This commit is contained in:
Adam Majer 2024-07-10 17:20:23 +02:00
parent fe58462b59
commit 96a07e95c7
3 changed files with 81 additions and 11 deletions

View File

@ -2,9 +2,12 @@ package common
import (
"errors"
"fmt"
"io"
"os"
"path/filepath"
"regexp"
"strings"
transport "github.com/go-openapi/runtime/client"
apiclient "src.opensuse.org/autogits/common/gitea-generated/client"
@ -13,11 +16,12 @@ import (
"src.opensuse.org/autogits/common/gitea-generated/models"
)
func (h *RequestHandler) allocateGiteaTransport() *transport.Runtime {
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)
return r
return r, apiclient.New(r, nil)
}
func (h *RequestHandler) CreateRepositoryIfNotExist(org Organization, repoName string) *models.Repository {
@ -25,8 +29,7 @@ func (h *RequestHandler) CreateRepositoryIfNotExist(org Organization, repoName s
return nil
}
transport := h.allocateGiteaTransport()
client := apiclient.New(transport, nil)
transport, client := h.allocateGiteaTransport()
repo, err := client.Repository.RepoGet(
repository.NewRepoGetParams().WithDefaults().WithOwner(org.Username).WithRepo(repoName),
transport.DefaultAuthentication)
@ -96,8 +99,7 @@ func (h *RequestHandler) CreatePullRequest(repo *models.Repository, srcId, targe
return nil
}
transport := h.allocateGiteaTransport()
client := apiclient.New(transport, nil)
transport, client := h.allocateGiteaTransport()
prOptions := models.CreatePullRequestOption{
Base: repo.DefaultBranch,
@ -136,8 +138,7 @@ func (h *RequestHandler) RequestReviews(pr *models.PullRequest, reviewer string)
return nil
}
transport := h.allocateGiteaTransport()
client := apiclient.New(transport, nil)
transport, client := h.allocateGiteaTransport()
reviewOptions := models.PullReviewRequestOptions{
Reviewers: []string{reviewer},
@ -167,3 +168,50 @@ func (h *RequestHandler) RequestReviews(pr *models.PullRequest, reviewer string)
return review.GetPayload()
}
func (h *RequestHandler) GetAssociatedPrjGitPR(pr *PullRequest) *models.PullRequest {
if h.HasError() {
return nil
}
transport, client := h.allocateGiteaTransport()
var page, maxSize int64;
page = 1
maxSize = 10000
state := "open"
prs, err := client.Repository.RepoListPullRequests(
repository.
NewRepoListPullRequestsParams().
WithDefaults().
WithOwner(pr.Repo.Owner.Username).
WithRepo(DefaultGitPrj).
WithState(&state).
WithLimit(&maxSize).
WithPage(&page),
transport.DefaultAuthentication)
if err != nil {
h.Error = fmt.Errorf("cannot fetch PR list for %s / %s : %v", pr.Repo.Owner.Username, pr.Repo.Name, err)
return nil
}
if !prs.IsSuccess() {
h.Error = fmt.Errorf("cannot fetch PR list for %s / %s : %s", pr.Repo.Owner.Username, pr.Repo.Name, prs.Error())
}
payload_processing:
for _, pr := range prs.Payload {
lines := strings.Split(pr.Body, "\n")
for _, line := range lines {
r := regexp.MustCompile(`^PullRequest: ([^/]+)/([^/]+)#(\d+)$`)
if m := r.FindSubmatch([]byte(line)); m != nil {
h.Log("match: %#v", m)
break payload_processing
}
}
}
return nil
}

View File

@ -61,3 +61,8 @@ func (h *RequestHandler) parsePullRequest(data io.Reader) *PullRequestAction {
return &action
}
func (h *RequestHandler) parsePullRequestSync(data io.Reader) *PullRequestAction {
return h.parsePullRequest(data)
}

View File

@ -14,6 +14,20 @@ const (
PrReview = "pr-review"
)
func processPullRequestSync(h *common.RequestHandler) error {
req := h.Data.(*common.PullRequestAction)
// sync requests for prjgit not handled here
if req.Repository.Name == common.DefaultGitPrj {
return nil
}
// find prjgit pull request associated with this one
h.GetAssociatedPrjGitPR(&req.Pull_Request)
return nil
}
func processPullRequestOpened(h *common.RequestHandler) error {
req := h.Data.(*common.PullRequestAction)
@ -29,7 +43,8 @@ func processPullRequestOpened(h *common.RequestHandler) error {
This commit was autocreated by %s
referencing
PullRequest: %s#%d`, req.Repository.Name, GitAuthor, req.Repository.Name, req.Pull_Request.Number)
PullRequest: %s/%s#%d`, req.Repository.Owner.Username,
req.Repository.Name, GitAuthor, req.Repository.Name, req.Pull_Request.Number)
prjGit := h.CreateRepositoryIfNotExist(*req.Repository.Owner, common.DefaultGitPrj)
if h.HasError() {
@ -49,8 +64,8 @@ PullRequest: %s#%d`, req.Repository.Name, GitAuthor, req.Repository.Name, req.Pu
fmt.Sprintf(`This is a forwarded pull request by %s
referencing the following pull request:
PullRequest: %s#%d
`, GitAuthor, req.Repository.Name, req.Pull_Request.Number),
PR: %s/%s#%d
`, GitAuthor, req.Repository.Owner.Username, req.Repository.Name, req.Pull_Request.Number),
)
if h.HasError() {
@ -69,6 +84,8 @@ func processPullRequest(h *common.RequestHandler) error {
switch req.Action {
case "opened":
return processPullRequestOpened(h)
case "syncronized":
return processPullRequestSync(h)
}
return fmt.Errorf("Unhandled pull request action: %s", req.Action)