This commit is contained in:
Adam Majer 2024-12-16 08:15:49 +01:00
parent 8c6180a8cf
commit 50aab4c662
8 changed files with 136 additions and 54 deletions

View File

@ -74,7 +74,6 @@ type Gitea interface {
CreatePullRequestIfNotExist(repo *models.Repository, srcId, targetId, title, body string) (*models.PullRequest, error) CreatePullRequestIfNotExist(repo *models.Repository, srcId, targetId, title, body string) (*models.PullRequest, error)
RequestReviews(pr *models.PullRequest, reviewer string) ([]*models.PullReview, error) RequestReviews(pr *models.PullRequest, reviewer string) ([]*models.PullReview, error)
AddReviewComment(pr *models.PullRequest, state models.ReviewStateType, comment string) (*models.PullReview, error) AddReviewComment(pr *models.PullRequest, state models.ReviewStateType, comment string) (*models.PullReview, error)
GetAssociatedPrjGitPR(pr *PullRequestWebhookEvent) (*models.PullRequest, error)
GetRepositoryFileContent(org, repo, hash, path string) ([]byte, error) GetRepositoryFileContent(org, repo, hash, path string) ([]byte, error)
GetPullRequestFileContent(pr *models.PullRequest, path string) ([]byte, error) GetPullRequestFileContent(pr *models.PullRequest, path string) ([]byte, error)
GetRecentPullRequests(org, repo string) ([]*models.PullRequest, error) GetRecentPullRequests(org, repo string) ([]*models.PullRequest, error)
@ -375,47 +374,6 @@ func (gitea *GiteaTransport) AddReviewComment(pr *models.PullRequest, state mode
return c.Payload, nil return c.Payload, nil
} }
func (gitea *GiteaTransport) GetAssociatedPrjGitPR(pr *PullRequestWebhookEvent) (*models.PullRequest, error) {
var page int64
state := "open"
for {
page++
prs, err := gitea.client.Repository.RepoListPullRequests(
repository.
NewRepoListPullRequestsParams().
WithDefaults().
WithOwner(pr.Repository.Owner.Username).
WithRepo(DefaultGitPrj).
WithState(&state).
WithPage(&page),
gitea.transport.DefaultAuthentication)
if err != nil {
return nil, fmt.Errorf("cannot fetch PR list for %s / %s : %w", pr.Repository.Owner.Username, pr.Repository.Name, err)
}
prLine := fmt.Sprintf(PrPattern, pr.Repository.Owner.Username, pr.Repository.Name, pr.Number)
// h.StdLogger.Printf("attemping to match line: '%s'\n", prLine)
// payload_processing:
for _, pr := range prs.Payload {
lines := strings.Split(pr.Body, "\n")
for _, line := range lines {
if strings.TrimSpace(line) == prLine {
return pr, nil
}
}
}
if len(prs.Payload) < 10 {
break
}
}
return nil, nil
}
func (gitea *GiteaTransport) GetAssociatedPRs(org, repo string, prNo int64) ([]*models.PullRequest, error) { func (gitea *GiteaTransport) GetAssociatedPRs(org, repo string, prNo int64) ([]*models.PullRequest, error) {
prData, err := gitea.client.Repository.RepoGetPullRequest( prData, err := gitea.client.Repository.RepoGetPullRequest(
repository.NewRepoGetPullRequestParams(). repository.NewRepoGetPullRequestParams().

View File

@ -45,7 +45,7 @@ type IssueLabelDetail struct {
type PullRequest struct { type PullRequest struct {
Id int Id int
Url string Url string
Number int Number int64
State string State string
Base Head Base Head
@ -58,7 +58,7 @@ type PullRequest struct {
type PullRequestWebhookEvent struct { type PullRequestWebhookEvent struct {
Action string Action string
Number int Number int64
Pull_Request *PullRequest Pull_Request *PullRequest
Repository *Repository Repository *Repository
@ -89,7 +89,7 @@ func PullRequestFromModel(pr *models.PullRequest) *PullRequest {
return &PullRequest{ return &PullRequest{
Id: int(pr.ID), Id: int(pr.ID),
Url: pr.URL, Url: pr.URL,
Number: int(pr.Index), Number: pr.Index,
State: string(pr.State), State: string(pr.State),
Base: Head{ Base: Head{

View File

@ -1,9 +1,11 @@
package main package main
import ( import (
"bufio"
"fmt" "fmt"
"log" "log"
"path" "path"
"strings"
"src.opensuse.org/autogits/common" "src.opensuse.org/autogits/common"
) )
@ -37,9 +39,21 @@ func (o *PullRequestSynced) Process(req *common.PullRequestWebhookEvent, git com
// need to verify that submodule in the PR for prjgit // need to verify that submodule in the PR for prjgit
// is still pointing to the HEAD of the PR // is still pointing to the HEAD of the PR
prjPr, err := o.gitea.GetAssociatedPrjGitPR(req) pr, _, err := o.gitea.GetPullRequestAndReviews(req.Repository.Owner.Username, req.Repository.Name, req.Number)
log.Print(pr)
if err != nil { if err != nil {
return fmt.Errorf("Cannot get associated PrjGit PR in processPullRequestSync. Err: %w", err) return fmt.Errorf("Cannot fetch PR data from gitea: %w", err)
}
_, prs := common.ExtractDescriptionAndPRs(bufio.NewScanner(strings.NewReader(pr.Body)))
if len(prs) != 1 {
return fmt.Errorf("Package update associated with invalid number of projects. Expected 1. Got %d", len(prs))
}
prjPr, _, err := o.gitea.GetPullRequestAndReviews(prs[0].Org, prs[0].Repo, prs[0].Num)
if err != nil {
return fmt.Errorf("Cannot get PrjGit PR in processPullRequestSync. Err: %w", err)
} }
common.PanicOnError(git.GitExec("", "clone", "--branch", prjPr.Head.Name, "--depth", "1", prjPr.Head.Repo.SSHURL, common.DefaultGitPrj)) common.PanicOnError(git.GitExec("", "clone", "--branch", prjPr.Head.Name, "--depth", "1", prjPr.Head.Repo.SSHURL, common.DefaultGitPrj))

View File

@ -3,7 +3,6 @@ package main
import ( import (
"bytes" "bytes"
"errors" "errors"
"log"
"os" "os"
"path" "path"
"strings" "strings"
@ -34,7 +33,10 @@ func TestSyncPR(t *testing.T) {
Ref: "branch", Ref: "branch",
Sha: "8a6a69a4232cabda04a4d9563030aa888ff5482f75aa4c6519da32a951a072e2", Sha: "8a6a69a4232cabda04a4d9563030aa888ff5482f75aa4c6519da32a951a072e2",
Repo: &common.Repository{ Repo: &common.Repository{
Name: "testRepo", Name: "testRepo",
Owner: &common.Organization{
Username: config.Organization,
},
Default_Branch: "main1", Default_Branch: "main1",
}, },
}, },
@ -49,13 +51,42 @@ func TestSyncPR(t *testing.T) {
}, },
Repository: &common.Repository{ Repository: &common.Repository{
Owner: &common.Organization{ Owner: &common.Organization{
Username: "test", Username: config.Organization,
},
},
}
modelPR := &models.PullRequest{
Index: 42,
Body: "PR: test/prj#24",
Base: &models.PRBranchInfo{
Ref: "branch",
Sha: "8a6a69a4232cabda04a4d9563030aa888ff5482f75aa4c6519da32a951a072e2",
Repo: &models.Repository{
Name: "testRepo",
Owner: &models.User{
UserName: "test",
},
DefaultBranch: "main1",
},
},
Head: &models.PRBranchInfo{
Ref: "branch",
Sha: "11eb36d5a58d7bb376cac59ac729a1986c6a7bfc63e7818e14382f545ccda985",
Repo: &models.Repository{
Name: "testRepo",
Owner: &models.User {
UserName: "test",
},
DefaultBranch: "main1",
}, },
}, },
} }
PrjGitPR := &models.PullRequest{ PrjGitPR := &models.PullRequest{
Title: "some pull request", Title: "some pull request",
Body: "PR: test/testRepo#42",
Index: 24,
Head: &models.PRBranchInfo{ Head: &models.PRBranchInfo{
Name: "testing", Name: "testing",
Sha: "db8adab91edb476b9762097d10c6379aa71efd6b60933a1c0e355ddacf419a95", Sha: "db8adab91edb476b9762097d10c6379aa71efd6b60933a1c0e355ddacf419a95",
@ -92,11 +123,13 @@ func TestSyncPR(t *testing.T) {
git.GitPath = t.TempDir() git.GitPath = t.TempDir()
config.GitProjectName = "prjGit" config.GitProjectName = "prjGit"
event.Repository.Name = "tester" event.Repository.Name = "testRepo"
setupGitForTests(t, git) setupGitForTests(t, git)
mock.EXPECT().GetAssociatedPrjGitPR(event).Return(PrjGitPR, nil) // mock.EXPECT().GetAssociatedPrjGitPR(event).Return(PrjGitPR, nil)
mock.EXPECT().GetPullRequestAndReviews(config.Organization, "testRepo", event.Pull_Request.Number).Return(modelPR, nil, nil)
mock.EXPECT().GetPullRequestAndReviews(config.Organization, "prj", int64(24)).Return(PrjGitPR, nil, nil)
err := pr.Process(event, git, config) err := pr.Process(event, git, config)
@ -129,9 +162,11 @@ func TestSyncPR(t *testing.T) {
t.Run("PR sync", func(t *testing.T) { t.Run("PR sync", func(t *testing.T) {
var b bytes.Buffer var b bytes.Buffer
/*
w := log.Writer() w := log.Writer()
log.SetOutput(&b) log.SetOutput(&b)
defer log.SetOutput(w) defer log.SetOutput(w)
*/
ctl := gomock.NewController(t) ctl := gomock.NewController(t)
mock := mock_common.NewMockGitea(ctl) mock := mock_common.NewMockGitea(ctl)
@ -146,7 +181,9 @@ func TestSyncPR(t *testing.T) {
git.DebugLogger = true git.DebugLogger = true
DebugMode = true DebugMode = true
mock.EXPECT().GetAssociatedPrjGitPR(event).Return(PrjGitPR, nil) // mock.EXPECT().GetAssociatedPrjGitPR(event).Return(PrjGitPR, nil)
mock.EXPECT().GetPullRequestAndReviews(config.Organization, "testRepo", event.Pull_Request.Number).Return(modelPR, nil, nil).AnyTimes()
mock.EXPECT().GetPullRequestAndReviews(config.Organization, "prj", int64(24)).Return(PrjGitPR, nil, nil)
err := pr.Process(event, git, config) err := pr.Process(event, git, config)

View File

@ -86,7 +86,7 @@ nextSubmodule:
event.Pull_Request = common.PullRequestFromModel(pr) event.Pull_Request = common.PullRequestFromModel(pr)
event.Action = string(pr.State) event.Action = string(pr.State)
event.Number = int(pr.Index) event.Number = pr.Index
event.Repository = common.RepositoryFromModel(pr.Base.Repo) event.Repository = common.RepositoryFromModel(pr.Base.Repo)
event.Sender = *common.UserFromModel(pr.User) event.Sender = *common.UserFromModel(pr.User)
event.Requested_reviewer = nil event.Requested_reviewer = nil

3
workflow-pr/review.go Normal file
View File

@ -0,0 +1,3 @@
package main

View File

@ -0,0 +1,19 @@
package main
import (
"os"
"os/exec"
"path"
"testing"
)
func TestReview(t *testing.T) {
cwd, _ := os.Getwd()
cmd := exec.Command("/usr/bin/bash", path.Join(cwd, "test_repo_setup.sh"))
cmd.Dir = t.TempDir()
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatal(string(out))
}
}

51
workflow-pr/test_repo_setup.sh Executable file
View File

@ -0,0 +1,51 @@
#!/usr/bin/bash
set -x
export GIT_CONFIG_COUNT=2
export GIT_CONFIG_KEY_0=protocol.file.allow
export GIT_CONFIG_VALUE_0=always
export GIT_CONFIG_KEY_1=init.defaultBranch
export GIT_CONFIG_VALUE_1=main
export GIT_AUTHOR_NAME=testname
export GIT_AUTHOR_EMAIL=test@suse.com
export GIT_AUTHOR_DATE='2005-04-07T22:13:13'
export GIT_COMMITTER_NAME=testname
export GIT_COMMITTER_EMAIL=test@suse.com
export GIT_COMMITTER_DATE='2005-04-07T22:13:13'
create_prjgit_sample() {
mkdir prjgit
pushd prjgit
git init -q --object-format=sha256
echo Project git is here > README.md
git add README.md
git submodule init
git submodule -q add ../pkgA pkgA
git commit -q -m 'first commit'
popd
}
create_pkgA() {
mkdir pkgA
pushd pkgA
git init -q --object-format=sha256
echo "Package A" > README.md
git add README.md
git commit -q -m 'Something base here'
popd
}
create_pkgA
create_prjgit_sample