Skip to content

Commit b26c356

Browse files
committed
fix test case
1 parent 845c70c commit b26c356

File tree

3 files changed

+73
-84
lines changed

3 files changed

+73
-84
lines changed

services/repository/branch.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -672,17 +672,11 @@ func GetBranchDivergingInfo(ctx reqctx.RequestContext, baseRepo, headRepo *repo_
672672
// so at the moment, we first check the update time, then check whether the fork branch has base's head
673673
diff, err := git.GetDivergingCommits(ctx, baseRepo.RepoPath(), baseGitBranch.CommitID, headGitBranch.CommitID)
674674
if err != nil {
675-
info.BaseIsNewer = baseGitBranch.UpdatedUnix > headGitBranch.UpdatedUnix
676-
if info.BaseIsNewer {
677-
return info, nil
678-
}
679-
680675
// if the base's update time is before the fork, check whether the base's head is in the fork
681676
headGitRepo, err := gitrepo.RepositoryFromRequestContextOrOpen(ctx, headRepo)
682677
if err != nil {
683678
return nil, err
684679
}
685-
686680
baseCommitID, err := headGitRepo.ConvertToGitID(baseGitBranch.CommitID)
687681
if err != nil {
688682
return nil, err
@@ -691,8 +685,12 @@ func GetBranchDivergingInfo(ctx reqctx.RequestContext, baseRepo, headRepo *repo_
691685
if err != nil {
692686
return nil, err
693687
}
694-
hasPreviousCommit, _ := headCommit.HasPreviousCommit(baseCommitID)
688+
hasPreviousCommit, err := headCommit.HasPreviousCommit(baseCommitID)
695689
info.BaseIsNewer = !hasPreviousCommit
690+
// make update time as last resort for divergence comparison
691+
if err != nil {
692+
info.BaseIsNewer = baseGitBranch.UpdatedUnix > headGitBranch.UpdatedUnix
693+
}
696694
return info, nil
697695
}
698696

tests/integration/repo_branch_test.go

Lines changed: 50 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,8 @@ import (
1111
"strings"
1212
"testing"
1313

14-
auth_model "code.gitea.io/gitea/models/auth"
15-
org_model "code.gitea.io/gitea/models/organization"
16-
"code.gitea.io/gitea/models/perm"
1714
repo_model "code.gitea.io/gitea/models/repo"
18-
"code.gitea.io/gitea/models/unit"
1915
"code.gitea.io/gitea/models/unittest"
20-
api "code.gitea.io/gitea/modules/structs"
2116
"code.gitea.io/gitea/modules/test"
2217
"code.gitea.io/gitea/modules/translation"
2318
"code.gitea.io/gitea/tests"
@@ -142,19 +137,51 @@ func TestCreateBranchInvalidCSRF(t *testing.T) {
142137
assert.Contains(t, resp.Body.String(), "Invalid CSRF token")
143138
}
144139

145-
func prepareBranch(t *testing.T, session *TestSession, repo *repo_model.Repository) {
146-
baseRefSubURL := fmt.Sprintf("branch/%s", repo.DefaultBranch)
147-
140+
func prepareRecentlyPushedBranchTest(t *testing.T, headSession *TestSession, baseRepo, headRepo *repo_model.Repository) {
141+
refSubURL := fmt.Sprintf("branch/%s", headRepo.DefaultBranch)
142+
baseRepoPath := baseRepo.OwnerName + "/" + baseRepo.Name
143+
headRepoPath := headRepo.OwnerName + "/" + headRepo.Name
144+
// Case 1: Normal branch changeset to display pushed message
148145
// create branch with no new commit
149-
testCreateBranch(t, session, repo.OwnerName, repo.Name, baseRefSubURL, "no-commit", http.StatusSeeOther)
146+
testCreateBranch(t, headSession, headRepo.OwnerName, headRepo.Name, refSubURL, "no-commit", http.StatusSeeOther)
150147

151148
// create branch with commit
152-
testCreateBranch(t, session, repo.OwnerName, repo.Name, baseRefSubURL, "new-commit", http.StatusSeeOther)
153-
testAPINewFile(t, session, repo.OwnerName, repo.Name, "new-commit", "new-commit.txt", "new-commit")
149+
testAPINewFile(t, headSession, headRepo.OwnerName, headRepo.Name, "new-commit", fmt.Sprintf("new-file-%s.txt", headRepo.Name), "new-commit")
150+
151+
// create a branch then delete it
152+
testCreateBranch(t, headSession, headRepo.OwnerName, headRepo.Name, "branch/new-commit", "deleted-branch", http.StatusSeeOther)
153+
testUIDeleteBranch(t, headSession, headRepo.OwnerName, headRepo.Name, "deleted-branch")
154+
155+
// only `new-commit` branch has commits ahead the base branch
156+
checkRecentlyPushedNewBranches(t, headSession, headRepoPath, []string{"new-commit"})
157+
if baseRepo.RepoPath() != headRepo.RepoPath() {
158+
checkRecentlyPushedNewBranches(t, headSession, baseRepoPath, []string{fmt.Sprintf("%v:new-commit", headRepo.FullName())})
159+
}
160+
161+
// Case 2: Create PR so that `new-commit` branch will not show
162+
testCreatePullToDefaultBranch(t, headSession, baseRepo, headRepo, "new-commit", "merge new-commit to default branch")
163+
// No push message show because of active PR
164+
checkRecentlyPushedNewBranches(t, headSession, headRepoPath, []string{})
165+
if baseRepo.RepoPath() != headRepo.RepoPath() {
166+
checkRecentlyPushedNewBranches(t, headSession, baseRepoPath, []string{})
167+
}
168+
}
169+
170+
func prepareRecentlyPushedBranchSpecialTest(t *testing.T, session *TestSession, baseRepo, headRepo *repo_model.Repository) {
171+
refSubURL := fmt.Sprintf("branch/%s", headRepo.DefaultBranch)
172+
baseRepoPath := baseRepo.OwnerName + "/" + baseRepo.Name
173+
headRepoPath := headRepo.OwnerName + "/" + headRepo.Name
174+
// create branch with no new commit
175+
testCreateBranch(t, session, headRepo.OwnerName, headRepo.Name, refSubURL, "no-commit-special", http.StatusSeeOther)
176+
177+
// update base (default) branch before head branch is updated
178+
testAPINewFile(t, session, baseRepo.OwnerName, baseRepo.Name, baseRepo.DefaultBranch, fmt.Sprintf("new-file-special-%s.txt", headRepo.Name), "new-commit")
154179

155-
// create deleted branch
156-
testCreateBranch(t, session, repo.OwnerName, repo.Name, "branch/new-commit", "deleted-branch", http.StatusSeeOther)
157-
testUIDeleteBranch(t, session, repo.OwnerName, repo.Name, "deleted-branch")
180+
// Though we have new `no-commit` branch, but the headBranch is not newer or commits ahead baseBranch. No message show.
181+
checkRecentlyPushedNewBranches(t, session, headRepoPath, []string{})
182+
if baseRepo.RepoPath() != headRepo.RepoPath() {
183+
checkRecentlyPushedNewBranches(t, session, baseRepoPath, []string{})
184+
}
158185
}
159186

160187
func testCreatePullToDefaultBranch(t *testing.T, session *TestSession, baseRepo, headRepo *repo_model.Repository, headBranch, title string) string {
@@ -169,6 +196,9 @@ func testCreatePullToDefaultBranch(t *testing.T, session *TestSession, baseRepo,
169196
}
170197

171198
func prepareRepoPR(t *testing.T, baseSession, headSession *TestSession, baseRepo, headRepo *repo_model.Repository) {
199+
refSubURL := fmt.Sprintf("branch/%s", headRepo.DefaultBranch)
200+
testCreateBranch(t, headSession, headRepo.OwnerName, headRepo.Name, refSubURL, "new-commit", http.StatusSeeOther)
201+
172202
// create opening PR
173203
testCreateBranch(t, headSession, headRepo.OwnerName, headRepo.Name, "branch/new-commit", "opening-pr", http.StatusSeeOther)
174204
testCreatePullToDefaultBranch(t, baseSession, baseRepo, headRepo, "opening-pr", "opening pr")
@@ -210,65 +240,19 @@ func checkRecentlyPushedNewBranches(t *testing.T, session *TestSession, repoPath
210240

211241
func TestRecentlyPushedNewBranches(t *testing.T) {
212242
onGiteaRun(t, func(t *testing.T, u *url.URL) {
213-
user1Session := loginUser(t, "user1")
214-
user2Session := loginUser(t, "user2")
215243
user12Session := loginUser(t, "user12")
216-
user13Session := loginUser(t, "user13")
217244

218-
// prepare branch and PRs in original repo
245+
// Same reposioty check
219246
repo10 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 10})
220-
prepareBranch(t, user12Session, repo10)
221247
prepareRepoPR(t, user12Session, user12Session, repo10, repo10)
222-
223-
// outdated new branch should not be displayed
224-
checkRecentlyPushedNewBranches(t, user12Session, "user12/repo10", []string{"new-commit"})
248+
prepareRecentlyPushedBranchTest(t, user12Session, repo10, repo10)
249+
prepareRecentlyPushedBranchSpecialTest(t, user12Session, repo10, repo10)
225250

226251
// create a fork repo in public org
227-
testRepoFork(t, user12Session, repo10.OwnerName, repo10.Name, "org25", "org25_fork_repo10", "new-commit")
252+
testRepoFork(t, user12Session, repo10.OwnerName, repo10.Name, "org25", "org25_fork_repo10", repo10.DefaultBranch)
228253
orgPublicForkRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerID: 25, Name: "org25_fork_repo10"})
229254
prepareRepoPR(t, user12Session, user12Session, repo10, orgPublicForkRepo)
230-
231-
// user12 is the owner of the repo10 and the organization org25
232-
// in repo10, user12 has opening/closed/merged pr and closed/merged pr with deleted branch
233-
checkRecentlyPushedNewBranches(t, user12Session, "user12/repo10", []string{"org25/org25_fork_repo10:new-commit", "new-commit"})
234-
235-
userForkRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 11})
236-
testCtx := NewAPITestContext(t, repo10.OwnerName, repo10.Name, auth_model.AccessTokenScopeWriteRepository)
237-
t.Run("AddUser13AsCollaborator", doAPIAddCollaborator(testCtx, "user13", perm.AccessModeWrite))
238-
prepareBranch(t, user13Session, userForkRepo)
239-
prepareRepoPR(t, user13Session, user13Session, repo10, userForkRepo)
240-
241-
// create branch with same name in different repo by user13
242-
testCreateBranch(t, user13Session, repo10.OwnerName, repo10.Name, "branch/new-commit", "same-name-branch", http.StatusSeeOther)
243-
testCreateBranch(t, user13Session, userForkRepo.OwnerName, userForkRepo.Name, "branch/new-commit", "same-name-branch", http.StatusSeeOther)
244-
testCreatePullToDefaultBranch(t, user13Session, repo10, userForkRepo, "same-name-branch", "same name branch pr")
245-
246-
// user13 pushed 2 branches with the same name in repo10 and repo11
247-
// and repo11's branch has a pr, but repo10's branch doesn't
248-
// in this case, we should get repo10's branch but not repo11's branch
249-
checkRecentlyPushedNewBranches(t, user13Session, "user12/repo10", []string{"same-name-branch", "user13/repo11:new-commit"})
250-
251-
// create a fork repo in private org
252-
testRepoFork(t, user1Session, repo10.OwnerName, repo10.Name, "private_org35", "org35_fork_repo10", "new-commit")
253-
orgPrivateForkRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerID: 35, Name: "org35_fork_repo10"})
254-
prepareRepoPR(t, user1Session, user1Session, repo10, orgPrivateForkRepo)
255-
256-
// user1 is the owner of private_org35 and no write permission to repo10
257-
// so user1 can only see the branch in org35_fork_repo10
258-
checkRecentlyPushedNewBranches(t, user1Session, "user12/repo10", []string{"private_org35/org35_fork_repo10:new-commit"})
259-
260-
// user2 push a branch in private_org35
261-
testCreateBranch(t, user2Session, orgPrivateForkRepo.OwnerName, orgPrivateForkRepo.Name, "branch/new-commit", "user-read-permission", http.StatusSeeOther)
262-
// convert write permission to read permission for code unit
263-
token := getTokenForLoggedInUser(t, user1Session, auth_model.AccessTokenScopeWriteOrganization)
264-
req := NewRequestWithJSON(t, "PATCH", fmt.Sprintf("/api/v1/teams/%d", 24), &api.EditTeamOption{
265-
Name: "team24",
266-
UnitsMap: map[string]string{"repo.code": "read"},
267-
}).AddTokenAuth(token)
268-
MakeRequest(t, req, http.StatusOK)
269-
teamUnit := unittest.AssertExistsAndLoadBean(t, &org_model.TeamUnit{TeamID: 24, Type: unit.TypeCode})
270-
assert.Equal(t, perm.AccessModeRead, teamUnit.AccessMode)
271-
// user2 can see the branch as it is created by user2
272-
checkRecentlyPushedNewBranches(t, user2Session, "user12/repo10", []string{"private_org35/org35_fork_repo10:user-read-permission"})
255+
prepareRecentlyPushedBranchTest(t, user12Session, repo10, orgPublicForkRepo)
256+
prepareRecentlyPushedBranchSpecialTest(t, user12Session, repo10, orgPublicForkRepo)
273257
})
274258
}

tests/integration/repo_merge_upstream_test.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package integration
55

66
import (
7+
"encoding/base64"
78
"fmt"
89
"net/http"
910
"net/url"
@@ -12,8 +13,6 @@ import (
1213
"time"
1314

1415
auth_model "code.gitea.io/gitea/models/auth"
15-
"code.gitea.io/gitea/models/db"
16-
git_model "code.gitea.io/gitea/models/git"
1716
repo_model "code.gitea.io/gitea/models/repo"
1817
"code.gitea.io/gitea/models/unittest"
1918
user_model "code.gitea.io/gitea/models/user"
@@ -37,6 +36,7 @@ func TestRepoMergeUpstream(t *testing.T) {
3736
require.Equal(t, exp, resp.Body.String())
3837
}
3938

39+
baseSession := loginUser(t, baseUser.Name)
4040
session := loginUser(t, forkUser.Name)
4141
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
4242

@@ -60,7 +60,7 @@ func TestRepoMergeUpstream(t *testing.T) {
6060

6161
t.Run("HeadBeforeBase", func(t *testing.T) {
6262
// add a file in base repo
63-
require.NoError(t, createOrReplaceFileInBranch(baseUser, baseRepo, "new-file.txt", "master", "test-content-1"))
63+
testAPINewFile(t, baseSession, baseRepo.OwnerName, baseRepo.Name, "master", "new-file.txt", "test-content-1")
6464

6565
// the repo shows a prompt to "sync fork"
6666
var mergeUpstreamLink string
@@ -83,14 +83,21 @@ func TestRepoMergeUpstream(t *testing.T) {
8383

8484
t.Run("BaseChangeAfterHeadChange", func(t *testing.T) {
8585
// update the files: base first, head later, and check the prompt
86-
require.NoError(t, createOrReplaceFileInBranch(baseUser, baseRepo, "new-file.txt", "master", "test-content-2"))
87-
require.NoError(t, createOrReplaceFileInBranch(forkUser, forkRepo, "new-file-other.txt", "fork-branch", "test-content-other"))
88-
89-
// make sure the base branch's update time is before the fork, to make it test the complete logic
90-
baseBranch := unittest.AssertExistsAndLoadBean(t, &git_model.Branch{RepoID: baseRepo.ID, Name: "master"})
91-
forkBranch := unittest.AssertExistsAndLoadBean(t, &git_model.Branch{RepoID: forkRepo.ID, Name: "fork-branch"})
92-
_, err := db.GetEngine(db.DefaultContext).ID(forkBranch.ID).Update(&git_model.Branch{UpdatedUnix: baseBranch.UpdatedUnix + 1})
93-
require.NoError(t, err)
86+
testAPINewFile(t, session, forkRepo.OwnerName, forkRepo.Name, "fork-branch", "new-file-other.txt", "test-content-other")
87+
baseUserToken := getTokenForLoggedInUser(t, baseSession, auth_model.AccessTokenScopeWriteRepository)
88+
req := NewRequestWithJSON(t, "PUT", fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s", baseRepo.OwnerName, baseRepo.Name, "new-file.txt"), &api.UpdateFileOptions{
89+
DeleteFileOptions: api.DeleteFileOptions{
90+
FileOptions: api.FileOptions{
91+
BranchName: "master",
92+
NewBranchName: "master",
93+
Message: "Update new-file.txt",
94+
},
95+
SHA: "a4007b6679563f949751ed31bb371fdfb3194446",
96+
},
97+
ContentBase64: base64.StdEncoding.EncodeToString([]byte("test-content-2")),
98+
}).
99+
AddTokenAuth(baseUserToken)
100+
MakeRequest(t, req, http.StatusOK)
94101

95102
// the repo shows a prompt to "sync fork"
96103
require.Eventually(t, func() bool {

0 commit comments

Comments
 (0)