This commit is contained in:
Adam Majer 2024-07-11 16:45:49 +02:00
parent 429cc2fe02
commit f40888ea45
3 changed files with 55 additions and 14 deletions

View File

@ -202,3 +202,24 @@ func (e *RequestHandler) GitExec(cwd string, params ...string) ExecStream {
return e
}
type writerFunc func([]byte) (int, error)
// Implement the Write method for the custom type
func (f writerFunc) Write(p []byte) (int, error) {
return f(p)
}
func (e *RequestHandler) GitSubmoduleCommitId(cwd, packageName string) (string, bool) {
if e.Error != nil {
return "", false
}
cmd := exec.Command("/usr/bin/git", "cat-file", "--batch")
cmd.Dir = filepath.Join(e.GitPath, cwd)
cmd.Stdout = writerFunc(func(p []byte) (int, error) {
return len(p), nil
})
return "", false
}

View File

@ -6,7 +6,6 @@ import (
"io"
"os"
"path/filepath"
"regexp"
"strings"
transport "github.com/go-openapi/runtime/client"
@ -16,6 +15,8 @@ import (
"src.opensuse.org/autogits/common/gitea-generated/models"
)
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)
@ -200,17 +201,15 @@ func (h *RequestHandler) GetAssociatedPrjGitPR(pr *PullRequestAction) *models.Pu
h.Error = fmt.Errorf("cannot fetch PR list for %s / %s : %s", pr.Repository.Owner.Username, pr.Repository.Name, prs.Error())
}
payload_processing:
prLine := fmt.Sprintf(PrPattern, pr.Repository.Owner.Username, pr.Repository.Name, pr.Number)
// 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
} else {
h.Log("not matched: '%s'", line)
if strings.TrimSpace(line) == prLine {
return pr
}
}
}

View File

@ -14,11 +14,29 @@ const (
PrReview = "pr-review"
)
func processPullRequestClosed(h *common.RequestHandler) error {
return nil
}
func processPrjGitPullRequestSync(h *common.RequestHandler) error {
// req := h.Data.(*common.PullRequestAction)
return nil
}
func processPullRequestSync(h *common.RequestHandler) error {
req := h.Data.(*common.PullRequestAction)
// find prjgit pull request associated with this one
h.GetAssociatedPrjGitPR(req)
if req.Repository.Name == common.DefaultGitPrj {
return processPrjGitPullRequestSync(h)
}
// need to verify that submodule in the PR for prjgit
// is still pointing to the HEAD of the PR
prjPr := h.GetAssociatedPrjGitPR(req)
h.GitExec("", "clone", "--branch", prjPr.Head.Name, "--depth", "1", prjPr.Head.Repo.SSHURL, common.DefaultGitPrj)
commitId := h.GitSubmoduleCommitId(common.DefaultGitPrj, req.Repository.Name)
return nil
}
@ -59,8 +77,8 @@ PullRequest: %s/%s#%d`, req.Repository.Owner.Username,
fmt.Sprintf(`This is a forwarded pull request by %s
referencing the following pull request:
PR: %s/%s#%d
`, GitAuthor, req.Repository.Owner.Username, req.Repository.Name, req.Pull_Request.Number),
`+common.PrPattern,
GitAuthor, req.Repository.Owner.Username, req.Repository.Name, req.Pull_Request.Number),
)
if h.HasError() {
@ -80,9 +98,12 @@ func processPullRequest(h *common.RequestHandler) error {
case "opened":
return processPullRequestOpened(h)
case "synchronized":
fallthrough
case "edited":
return processPullRequestSync(h)
case "edited":
// not need to be handled??
return nil
case "closed":
return processPullRequestClosed(h)
}
return fmt.Errorf("Unhandled pull request action: %s", req.Action)