@@ -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
160187func 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
171198func 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
211241func 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}
0 commit comments