forked from go-gitea/gitea
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support unprotected file patterns (go-gitea#16395)
Fixes go-gitea#16381 Note that changes to unprotected files via the web editor still cannot be pushed directly to the protected branch. I could easily add such support for edits and deletes if needed. But for adding, uploading or renaming unprotected files, it is not trivial. * Extract & Move GetAffectedFiles to modules/git
- Loading branch information
Showing
17 changed files
with
254 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -365,7 +365,7 @@ func doBranchProtectPRMerge(baseCtx *APITestContext, dstPath string) func(t *tes | |
t.Run("PushProtectedBranch", doGitPushTestRepository(dstPath, "origin", "protected")) | ||
|
||
ctx := NewAPITestContext(t, baseCtx.Username, baseCtx.Reponame) | ||
t.Run("ProtectProtectedBranchNoWhitelist", doProtectBranch(ctx, "protected", "")) | ||
t.Run("ProtectProtectedBranchNoWhitelist", doProtectBranch(ctx, "protected", "", "")) | ||
t.Run("GenerateCommit", func(t *testing.T) { | ||
_, err := generateCommitWithNewData(littleSize, dstPath, "[email protected]", "User Two", "branch-data-file-") | ||
assert.NoError(t, err) | ||
|
@@ -391,7 +391,15 @@ func doBranchProtectPRMerge(baseCtx *APITestContext, dstPath string) func(t *tes | |
t.Run("MergePR2", doAPIMergePullRequest(ctx, baseCtx.Username, baseCtx.Reponame, pr2.Index)) | ||
t.Run("MergePR", doAPIMergePullRequest(ctx, baseCtx.Username, baseCtx.Reponame, pr.Index)) | ||
t.Run("PullProtected", doGitPull(dstPath, "origin", "protected")) | ||
t.Run("ProtectProtectedBranchWhitelist", doProtectBranch(ctx, "protected", baseCtx.Username)) | ||
|
||
t.Run("ProtectProtectedBranchUnprotectedFilePaths", doProtectBranch(ctx, "protected", "", "unprotected-file-*")) | ||
t.Run("GenerateCommit", func(t *testing.T) { | ||
_, err := generateCommitWithNewData(littleSize, dstPath, "[email protected]", "User Two", "unprotected-file-") | ||
assert.NoError(t, err) | ||
}) | ||
t.Run("PushUnprotectedFilesToProtectedBranch", doGitPushTestRepository(dstPath, "origin", "protected")) | ||
|
||
t.Run("ProtectProtectedBranchWhitelist", doProtectBranch(ctx, "protected", baseCtx.Username, "")) | ||
|
||
t.Run("CheckoutMaster", doGitCheckoutBranch(dstPath, "master")) | ||
t.Run("CreateBranchForced", doGitCreateBranch(dstPath, "toforce")) | ||
|
@@ -406,28 +414,30 @@ func doBranchProtectPRMerge(baseCtx *APITestContext, dstPath string) func(t *tes | |
} | ||
} | ||
|
||
func doProtectBranch(ctx APITestContext, branch string, userToWhitelist string) func(t *testing.T) { | ||
func doProtectBranch(ctx APITestContext, branch string, userToWhitelist string, unprotectedFilePatterns string) func(t *testing.T) { | ||
// We are going to just use the owner to set the protection. | ||
return func(t *testing.T) { | ||
csrf := GetCSRF(t, ctx.Session, fmt.Sprintf("/%s/%s/settings/branches", url.PathEscape(ctx.Username), url.PathEscape(ctx.Reponame))) | ||
|
||
if userToWhitelist == "" { | ||
// Change branch to protected | ||
req := NewRequestWithValues(t, "POST", fmt.Sprintf("/%s/%s/settings/branches/%s", url.PathEscape(ctx.Username), url.PathEscape(ctx.Reponame), url.PathEscape(branch)), map[string]string{ | ||
"_csrf": csrf, | ||
"protected": "on", | ||
"_csrf": csrf, | ||
"protected": "on", | ||
"unprotected_file_patterns": unprotectedFilePatterns, | ||
}) | ||
ctx.Session.MakeRequest(t, req, http.StatusFound) | ||
} else { | ||
user, err := models.GetUserByName(userToWhitelist) | ||
assert.NoError(t, err) | ||
// Change branch to protected | ||
req := NewRequestWithValues(t, "POST", fmt.Sprintf("/%s/%s/settings/branches/%s", url.PathEscape(ctx.Username), url.PathEscape(ctx.Reponame), url.PathEscape(branch)), map[string]string{ | ||
"_csrf": csrf, | ||
"protected": "on", | ||
"enable_push": "whitelist", | ||
"enable_whitelist": "on", | ||
"whitelist_users": strconv.FormatInt(user.ID, 10), | ||
"_csrf": csrf, | ||
"protected": "on", | ||
"enable_push": "whitelist", | ||
"enable_whitelist": "on", | ||
"whitelist_users": strconv.FormatInt(user.ID, 10), | ||
"unprotected_file_patterns": unprotectedFilePatterns, | ||
}) | ||
ctx.Session.MakeRequest(t, req, http.StatusFound) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright 2021 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package migrations | ||
|
||
import ( | ||
"fmt" | ||
|
||
"xorm.io/xorm" | ||
) | ||
|
||
func addBranchProtectionUnprotectedFilesColumn(x *xorm.Engine) error { | ||
type ProtectedBranch struct { | ||
UnprotectedFilePatterns string `xorm:"TEXT"` | ||
} | ||
|
||
if err := x.Sync2(new(ProtectedBranch)); err != nil { | ||
return fmt.Errorf("Sync2: %v", err) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.