From 219f0c5203a75c7e7bbac5a886a88b8f463e5745 Mon Sep 17 00:00:00 2001 From: a1012112796 <1012112796@qq.com> Date: Fri, 27 Dec 2024 15:06:15 +0800 Subject: [PATCH 1/3] Fix Agit pull request permission check user with read permission should also can create agit flow pull request. looks this logic was broken in https://github.com/go-gitea/gitea/pull/31033 this pull request try fix it and add test code. Signed-off-by: a1012112796 <1012112796@qq.com> --- services/pull/pull.go | 2 +- tests/integration/pull_create_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/services/pull/pull.go b/services/pull/pull.go index ac91fd6409b05..d59075c59bdec 100644 --- a/services/pull/pull.go +++ b/services/pull/pull.go @@ -64,7 +64,7 @@ func NewPullRequest(ctx context.Context, opts *NewPullRequestOptions) error { } // user should be a collaborator or a member of the organization for base repo - if !issue.Poster.IsAdmin { + if (!issue.Poster.IsAdmin) && pr.Flow == issues_model.PullRequestFlowGithub { canCreate, err := repo_model.IsOwnerMemberCollaborator(ctx, repo, issue.Poster.ID) if err != nil { return err diff --git a/tests/integration/pull_create_test.go b/tests/integration/pull_create_test.go index 9812d2073d1e9..6f92685f6dcab 100644 --- a/tests/integration/pull_create_test.go +++ b/tests/integration/pull_create_test.go @@ -4,14 +4,17 @@ package integration import ( + "bytes" "fmt" "net/http" "net/http/httptest" "net/url" "path" + "regexp" "strings" "testing" + "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/test" "code.gitea.io/gitea/tests" @@ -226,3 +229,26 @@ func TestPullCreatePrFromBaseToFork(t *testing.T) { assert.Regexp(t, "^/user1/repo1/pulls/[0-9]*$", url) }) } + +func TestCreateAgitPullWithReadPermission(t *testing.T) { + onGiteaRun(t, func(t *testing.T, u *url.URL) { + dstPath := t.TempDir() + + u.Path = "user2/repo1.git" + u.User = url.UserPassword("user4", userPassword) + + t.Run("Clone", doGitClone(dstPath, u)) + + t.Run("add commit", doGitAddSomeCommits(dstPath, "master")) + + t.Run("do agit pull create", func(t *testing.T) { + stderr := new(bytes.Buffer) + err := git.NewCommand(git.DefaultContext, "push", "origin", "HEAD:refs/for/master", "-o").AddDynamicArguments("topic=" + "test-topic").Run(&git.RunOpts{Dir: dstPath, Stderr: stderr}) + assert.NoError(t, err) + + findPullRe := regexp.MustCompile("http://localhost:3003/user2/repo1/pulls/([1-9])") + pullLink := findPullRe.FindString(stderr.String()) + assert.True(t, len(pullLink) > 0) + }) + }) +} From a73248d2ab939e238f3457aeb5471bdb557aa8b4 Mon Sep 17 00:00:00 2001 From: a1012112796 <1012112796@qq.com> Date: Fri, 27 Dec 2024 15:54:38 +0800 Subject: [PATCH 2/3] fix lint Signed-off-by: a1012112796 <1012112796@qq.com> --- tests/integration/pull_create_test.go | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/tests/integration/pull_create_test.go b/tests/integration/pull_create_test.go index 6f92685f6dcab..4bc2a1da9ab5f 100644 --- a/tests/integration/pull_create_test.go +++ b/tests/integration/pull_create_test.go @@ -4,13 +4,11 @@ package integration import ( - "bytes" "fmt" "net/http" "net/http/httptest" "net/url" "path" - "regexp" "strings" "testing" @@ -242,13 +240,8 @@ func TestCreateAgitPullWithReadPermission(t *testing.T) { t.Run("add commit", doGitAddSomeCommits(dstPath, "master")) t.Run("do agit pull create", func(t *testing.T) { - stderr := new(bytes.Buffer) - err := git.NewCommand(git.DefaultContext, "push", "origin", "HEAD:refs/for/master", "-o").AddDynamicArguments("topic=" + "test-topic").Run(&git.RunOpts{Dir: dstPath, Stderr: stderr}) + err := git.NewCommand(git.DefaultContext, "push", "origin", "HEAD:refs/for/master", "-o").AddDynamicArguments("topic=" + "test-topic").Run(&git.RunOpts{Dir: dstPath}) assert.NoError(t, err) - - findPullRe := regexp.MustCompile("http://localhost:3003/user2/repo1/pulls/([1-9])") - pullLink := findPullRe.FindString(stderr.String()) - assert.True(t, len(pullLink) > 0) }) }) } From 7a10bac34a95c1217fbd75d96bf11c34f321eadb Mon Sep 17 00:00:00 2001 From: a1012112796 <1012112796@qq.com> Date: Fri, 27 Dec 2024 17:37:31 +0800 Subject: [PATCH 3/3] Update services/pull/pull.go Co-authored-by: wxiaoguang --- services/pull/pull.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/services/pull/pull.go b/services/pull/pull.go index d59075c59bdec..85c36bb16aff8 100644 --- a/services/pull/pull.go +++ b/services/pull/pull.go @@ -64,7 +64,8 @@ func NewPullRequest(ctx context.Context, opts *NewPullRequestOptions) error { } // user should be a collaborator or a member of the organization for base repo - if (!issue.Poster.IsAdmin) && pr.Flow == issues_model.PullRequestFlowGithub { + canCreate := issue.Poster.IsAdmin || pr.Flow == issues_model.PullRequestFlowAGit + if !canCreate { canCreate, err := repo_model.IsOwnerMemberCollaborator(ctx, repo, issue.Poster.ID) if err != nil { return err