|
| 1 | +// Copyright 2025 The Gitea Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package integration |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "net/http" |
| 9 | + "net/url" |
| 10 | + "os" |
| 11 | + "testing" |
| 12 | + "time" |
| 13 | + |
| 14 | + issues_model "code.gitea.io/gitea/models/issues" |
| 15 | + "code.gitea.io/gitea/models/unittest" |
| 16 | + issues_service "code.gitea.io/gitea/services/issue" |
| 17 | + |
| 18 | + "github.com/stretchr/testify/assert" |
| 19 | + "github.com/stretchr/testify/require" |
| 20 | +) |
| 21 | + |
| 22 | +func testWaitForPullRequestStatus(t *testing.T, prIssue *issues_model.Issue, expectedStatus issues_model.PullRequestStatus) (retIssue *issues_model.Issue) { |
| 23 | + require.Eventually(t, func() bool { |
| 24 | + prIssueCond := *prIssue |
| 25 | + retIssue = unittest.AssertExistsAndLoadBean(t, &prIssueCond) |
| 26 | + require.NoError(t, retIssue.LoadPullRequest(t.Context())) |
| 27 | + return retIssue.PullRequest.Status == expectedStatus |
| 28 | + }, 5*time.Second, 20*time.Millisecond) |
| 29 | + return retIssue |
| 30 | +} |
| 31 | + |
| 32 | +func testPullCommentRebase(t *testing.T, u *url.URL, session *TestSession) { |
| 33 | + testPRTitle := "Test PR for rebase comment" |
| 34 | + // make a change on forked branch |
| 35 | + testEditFile(t, session, "user1", "repo1", "test-branch/rebase", "README.md", "Hello, World (Edited)\n") |
| 36 | + testPullCreate(t, session, "user1", "repo1", false, "test-branch/rebase", "test-branch/rebase", testPRTitle) |
| 37 | + // create a conflict on base repo branch |
| 38 | + testEditFile(t, session, "user2", "repo1", "test-branch/rebase", "README.md", "Hello, World (Edited Conflicted)\n") |
| 39 | + |
| 40 | + // Now the pull request status should be conflicted |
| 41 | + testWaitForPullRequestStatus(t, &issues_model.Issue{Title: testPRTitle}, issues_model.PullRequestStatusConflict) |
| 42 | + |
| 43 | + dstPath := t.TempDir() |
| 44 | + u.Path = "/user2/repo1.git" |
| 45 | + doGitClone(dstPath, u)(t) |
| 46 | + doGitCheckoutBranch(dstPath, "test-branch/rebase")(t) |
| 47 | + doGitCreateBranch(dstPath, "local-branch/rebase")(t) |
| 48 | + content, _ := os.ReadFile(dstPath + "/README.md") |
| 49 | + require.Equal(t, "Hello, World (Edited Conflicted)\n", string(content)) |
| 50 | + |
| 51 | + doGitCheckoutWriteFileCommit(localGitAddCommitOptions{ |
| 52 | + LocalRepoPath: dstPath, |
| 53 | + CheckoutBranch: "local-branch/rebase", |
| 54 | + TreeFilePath: "README.md", |
| 55 | + TreeFileContent: "Hello, World (Edited Conflict Resolved)\n", |
| 56 | + })(t) |
| 57 | + |
| 58 | + // do force push |
| 59 | + u.Path = "/user1/repo1.git" |
| 60 | + u.User = url.UserPassword("user1", userPassword) |
| 61 | + doGitAddRemote(dstPath, "base-repo", u)(t) |
| 62 | + doGitPushTestRepositoryFail(dstPath, "base-repo", "local-branch/rebase:test-branch/rebase")(t) |
| 63 | + doGitPushTestRepository(dstPath, "--force", "base-repo", "local-branch/rebase:test-branch/rebase")(t) |
| 64 | + |
| 65 | + // reload the pr |
| 66 | + prIssue := testWaitForPullRequestStatus(t, &issues_model.Issue{Title: testPRTitle}, issues_model.PullRequestStatusMergeable) |
| 67 | + comments, err := issues_model.FindComments(t.Context(), &issues_model.FindCommentsOptions{ |
| 68 | + IssueID: prIssue.ID, |
| 69 | + Type: issues_model.CommentTypeUndefined, // get all comments type |
| 70 | + }) |
| 71 | + require.NoError(t, err) |
| 72 | + lastComment := comments[len(comments)-1] |
| 73 | + assert.NoError(t, issues_service.LoadCommentPushCommits(t.Context(), lastComment)) |
| 74 | + assert.True(t, lastComment.IsForcePush) |
| 75 | +} |
| 76 | + |
| 77 | +func testPullCommentRetarget(t *testing.T, u *url.URL, session *TestSession) { |
| 78 | + testPRTitle := "Test PR for retarget comment" |
| 79 | + // keep a non-conflict branch |
| 80 | + testCreateBranch(t, session, "user2", "repo1", "branch/test-branch/retarget", "test-branch/retarget-no-conflict", http.StatusSeeOther) |
| 81 | + // make a change on forked branch |
| 82 | + testEditFile(t, session, "user1", "repo1", "test-branch/retarget", "README.md", "Hello, World (Edited)\n") |
| 83 | + testPullCreate(t, session, "user1", "repo1", false, "test-branch/retarget", "test-branch/retarget", testPRTitle) |
| 84 | + // create a conflict line on user2/repo1 README.md |
| 85 | + testEditFile(t, session, "user2", "repo1", "test-branch/retarget", "README.md", "Hello, World (Edited Conflicted)\n") |
| 86 | + |
| 87 | + // Now the pull request status should be conflicted |
| 88 | + prIssue := testWaitForPullRequestStatus(t, &issues_model.Issue{Title: testPRTitle}, issues_model.PullRequestStatusConflict) |
| 89 | + |
| 90 | + // do retarget |
| 91 | + req := NewRequestWithValues(t, "POST", fmt.Sprintf("/user2/repo1/pull/%d/target_branch", prIssue.PullRequest.Index), map[string]string{ |
| 92 | + "_csrf": GetUserCSRFToken(t, session), |
| 93 | + "target_branch": "test-branch/retarget-no-conflict", |
| 94 | + }) |
| 95 | + session.MakeRequest(t, req, http.StatusOK) |
| 96 | + testWaitForPullRequestStatus(t, &issues_model.Issue{Title: testPRTitle}, issues_model.PullRequestStatusMergeable) |
| 97 | +} |
| 98 | + |
| 99 | +func TestPullComment(t *testing.T) { |
| 100 | + onGiteaRun(t, func(t *testing.T, u *url.URL) { |
| 101 | + session := loginUser(t, "user1") |
| 102 | + testCreateBranch(t, session, "user2", "repo1", "branch/master", "test-branch/rebase", http.StatusSeeOther) |
| 103 | + testCreateBranch(t, session, "user2", "repo1", "branch/master", "test-branch/retarget", http.StatusSeeOther) |
| 104 | + testRepoFork(t, session, "user2", "repo1", "user1", "repo1", "") |
| 105 | + |
| 106 | + t.Run("RebaseComment", func(t *testing.T) { testPullCommentRebase(t, u, session) }) |
| 107 | + t.Run("RetargetComment", func(t *testing.T) { testPullCommentRetarget(t, u, session) }) |
| 108 | + }) |
| 109 | +} |
0 commit comments