From f40888ea4515fe2e8eea617a16f5f50a45f652d894de3ad181d58de3aafb8f99 Mon Sep 17 00:00:00 2001 From: Adam Majer Date: Thu, 11 Jul 2024 16:45:49 +0200 Subject: [PATCH] . --- bots-common/git_utils.go | 21 +++++++++++++++++++++ bots-common/gitea_utils.go | 15 +++++++-------- pr-review/main.go | 33 +++++++++++++++++++++++++++------ 3 files changed, 55 insertions(+), 14 deletions(-) diff --git a/bots-common/git_utils.go b/bots-common/git_utils.go index 7087960..d7fa687 100644 --- a/bots-common/git_utils.go +++ b/bots-common/git_utils.go @@ -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 +} diff --git a/bots-common/gitea_utils.go b/bots-common/gitea_utils.go index a52f27c..cc6f7a2 100644 --- a/bots-common/gitea_utils.go +++ b/bots-common/gitea_utils.go @@ -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 } } } diff --git a/pr-review/main.go b/pr-review/main.go index d69208e..1e523bb 100644 --- a/pr-review/main.go +++ b/pr-review/main.go @@ -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)