loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request '[BUG] Detect protected branch on branch rename' (#2811) from gusted/forgejo-protectedbranch-rename into forgejo

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/2811
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Reviewed-by: oliverpool <oliverpool@noreply.codeberg.org>

+61 -23
+1
options/locale/locale_en-US.ini
··· 2513 2513 settings.lfs_pointers.exists=Exists in store 2514 2514 settings.lfs_pointers.accessible=Accessible to User 2515 2515 settings.lfs_pointers.associateAccessible=Associate accessible %d OIDs 2516 + settings.rename_branch_failed_protected=Cannot rename branch %s because it is a protected branch. 2516 2517 settings.rename_branch_failed_exist=Cannot rename branch because target branch %s exists. 2517 2518 settings.rename_branch_failed_not_exist=Cannot rename branch %s because it does not exist. 2518 2519 settings.rename_branch_success =Branch %s was successfully renamed to %s.
+7 -1
routers/web/repo/setting/protected_branch.go
··· 4 4 package setting 5 5 6 6 import ( 7 + "errors" 7 8 "fmt" 8 9 "net/http" 9 10 "net/url" ··· 316 317 317 318 msg, err := repository.RenameBranch(ctx, ctx.Repo.Repository, ctx.Doer, ctx.Repo.GitRepo, form.From, form.To) 318 319 if err != nil { 319 - ctx.ServerError("RenameBranch", err) 320 + if errors.Is(err, git_model.ErrBranchIsProtected) { 321 + ctx.Flash.Error(ctx.Tr("repo.settings.rename_branch_failed_protected", form.To)) 322 + ctx.Redirect(fmt.Sprintf("%s/branches", ctx.Repo.RepoLink)) 323 + } else { 324 + ctx.ServerError("RenameBranch", err) 325 + } 320 326 return 321 327 } 322 328
+53 -22
tests/integration/rename_branch_test.go
··· 10 10 git_model "code.gitea.io/gitea/models/git" 11 11 repo_model "code.gitea.io/gitea/models/repo" 12 12 "code.gitea.io/gitea/models/unittest" 13 + gitea_context "code.gitea.io/gitea/services/context" 13 14 "code.gitea.io/gitea/tests" 14 15 15 16 "github.com/stretchr/testify/assert" ··· 18 19 func TestRenameBranch(t *testing.T) { 19 20 defer tests.PrepareTestEnv(t)() 20 21 21 - unittest.AssertExistsAndLoadBean(t, &git_model.Branch{RepoID: 1, Name: "master"}) 22 + repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) 23 + unittest.AssertExistsAndLoadBean(t, &git_model.Branch{RepoID: repo.ID, Name: "master"}) 22 24 23 25 // get branch setting page 24 26 session := loginUser(t, "user2") 25 - req := NewRequest(t, "GET", "/user2/repo1/settings/branches") 26 - resp := session.MakeRequest(t, req, http.StatusOK) 27 - htmlDoc := NewHTMLParser(t, resp.Body) 27 + t.Run("Normal", func(t *testing.T) { 28 + defer tests.PrintCurrentTest(t)() 29 + 30 + req := NewRequestWithValues(t, "POST", "/user2/repo1/settings/rename_branch", map[string]string{ 31 + "_csrf": GetCSRF(t, session, "/user2/repo1/settings/branches"), 32 + "from": "master", 33 + "to": "main", 34 + }) 35 + session.MakeRequest(t, req, http.StatusSeeOther) 36 + 37 + // check new branch link 38 + req = NewRequest(t, "GET", "/user2/repo1/src/branch/main/README.md") 39 + session.MakeRequest(t, req, http.StatusOK) 40 + 41 + // check old branch link 42 + req = NewRequest(t, "GET", "/user2/repo1/src/branch/master/README.md") 43 + resp := session.MakeRequest(t, req, http.StatusSeeOther) 44 + location := resp.Header().Get("Location") 45 + assert.Equal(t, "/user2/repo1/src/branch/main/README.md", location) 46 + 47 + // check db 48 + repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) 49 + assert.Equal(t, "main", repo1.DefaultBranch) 50 + }) 51 + 52 + t.Run("Protected branch", func(t *testing.T) { 53 + defer tests.PrintCurrentTest(t)() 54 + 55 + // Add protected branch 56 + req := NewRequestWithValues(t, "POST", "/user2/repo1/settings/branches/edit", map[string]string{ 57 + "_csrf": GetCSRF(t, session, "/user2/repo1/settings/branches/edit"), 58 + "rule_name": "*", 59 + "enable_push": "true", 60 + }) 61 + session.MakeRequest(t, req, http.StatusSeeOther) 28 62 29 - postData := map[string]string{ 30 - "_csrf": htmlDoc.GetCSRF(), 31 - "from": "master", 32 - "to": "main", 33 - } 34 - req = NewRequestWithValues(t, "POST", "/user2/repo1/settings/rename_branch", postData) 35 - session.MakeRequest(t, req, http.StatusSeeOther) 63 + // Verify it was added. 64 + unittest.AssertExistsIf(t, true, &git_model.ProtectedBranch{RuleName: "*", RepoID: repo.ID}) 36 65 37 - // check new branch link 38 - req = NewRequestWithValues(t, "GET", "/user2/repo1/src/branch/main/README.md", postData) 39 - session.MakeRequest(t, req, http.StatusOK) 66 + req = NewRequestWithValues(t, "POST", "/user2/repo1/settings/rename_branch", map[string]string{ 67 + "_csrf": GetCSRF(t, session, "/user2/repo1/settings/branches"), 68 + "from": "main", 69 + "to": "main2", 70 + }) 71 + session.MakeRequest(t, req, http.StatusSeeOther) 40 72 41 - // check old branch link 42 - req = NewRequestWithValues(t, "GET", "/user2/repo1/src/branch/master/README.md", postData) 43 - resp = session.MakeRequest(t, req, http.StatusSeeOther) 44 - location := resp.Header().Get("Location") 45 - assert.Equal(t, "/user2/repo1/src/branch/main/README.md", location) 73 + flashCookie := session.GetCookie(gitea_context.CookieNameFlash) 74 + assert.NotNil(t, flashCookie) 75 + assert.EqualValues(t, "error%3DCannot%2Brename%2Bbranch%2Bmain2%2Bbecause%2Bit%2Bis%2Ba%2Bprotected%2Bbranch.", flashCookie.Value) 46 76 47 - // check db 48 - repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) 49 - assert.Equal(t, "main", repo1.DefaultBranch) 77 + // Verify it didn't change. 78 + repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) 79 + assert.Equal(t, "main", repo1.DefaultBranch) 80 + }) 50 81 }