Skip to content

Commit dfa7291

Browse files
a1012112796techknowlogickCirnoTzeripath
authored
[Enhancement] Allow admin to merge pr with protected file changes (#12078)
* [Enhancement] Allow admin to merge pr with protected file changes As tilte, show protected message in diff page and merge box. Signed-off-by: a1012112796 <[email protected]> * remove unused ver * Update options/locale/locale_en-US.ini Co-authored-by: Cirno the Strongest <[email protected]> * Add TrN * Apply suggestions from code review * fix lint * Update options/locale/locale_en-US.ini Co-authored-by: zeripath <[email protected]> * Apply suggestions from code review * move pr proteced files check to TestPatch * Call TestPatch when protected branches settings changed * Apply review suggestion @CirnoT * move to service @lunny * slightly restructure routers/private/hook.go Adds a lot of comments and simplifies the logic Signed-off-by: Andrew Thornton <[email protected]> * placate lint Signed-off-by: Andrew Thornton <[email protected]> * skip duplicate protected files check * fix check logic * slight refactor of TestPatch Signed-off-by: Andrew Thornton <[email protected]> * When checking for protected files changes in TestPatch use the temporary repository Signed-off-by: Andrew Thornton <[email protected]> * fix introduced issue with hook Signed-off-by: Andrew Thornton <[email protected]> * Remove the check on PR index being greater than 0 as it unnecessary Signed-off-by: Andrew Thornton <[email protected]> Co-authored-by: techknowlogick <[email protected]> Co-authored-by: Cirno the Strongest <[email protected]> Co-authored-by: zeripath <[email protected]> Co-authored-by: techknowlogick <[email protected]>
1 parent da32d0e commit dfa7291

File tree

19 files changed

+453
-174
lines changed

19 files changed

+453
-174
lines changed

models/branches.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,38 @@ func (protectBranch *ProtectedBranch) GetProtectedFilePatterns() []glob.Glob {
209209
return extarr
210210
}
211211

212+
// MergeBlockedByProtectedFiles returns true if merge is blocked by protected files change
213+
func (protectBranch *ProtectedBranch) MergeBlockedByProtectedFiles(pr *PullRequest) bool {
214+
glob := protectBranch.GetProtectedFilePatterns()
215+
if len(glob) == 0 {
216+
return false
217+
}
218+
219+
return len(pr.ChangedProtectedFiles) > 0
220+
}
221+
222+
// IsProtectedFile return if path is protected
223+
func (protectBranch *ProtectedBranch) IsProtectedFile(patterns []glob.Glob, path string) bool {
224+
if len(patterns) == 0 {
225+
patterns = protectBranch.GetProtectedFilePatterns()
226+
if len(patterns) == 0 {
227+
return false
228+
}
229+
}
230+
231+
lpath := strings.ToLower(strings.TrimSpace(path))
232+
233+
r := false
234+
for _, pat := range patterns {
235+
if pat.Match(lpath) {
236+
r = true
237+
break
238+
}
239+
}
240+
241+
return r
242+
}
243+
212244
// GetProtectedBranchByRepoID getting protected branch by repo ID
213245
func GetProtectedBranchByRepoID(repoID int64) ([]*ProtectedBranch, error) {
214246
protectedBranches := make([]*ProtectedBranch, 0)

models/migrations/migrations.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,8 @@ var migrations = []Migration{
244244
NewMigration("add Team review request support", addTeamReviewRequestSupport),
245245
// v154 > v155
246246
NewMigration("add timestamps to Star, Label, Follow, Watch and Collaboration", addTimeStamps),
247+
// v155 -> v156
248+
NewMigration("add changed_protected_files column for pull_request table", addChangedProtectedFilesPullRequestColumn),
247249
}
248250

249251
// GetCurrentDBVersion returns the current db version

models/migrations/v155.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2020 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package migrations
6+
7+
import (
8+
"fmt"
9+
10+
"xorm.io/xorm"
11+
)
12+
13+
func addChangedProtectedFilesPullRequestColumn(x *xorm.Engine) error {
14+
type PullRequest struct {
15+
ChangedProtectedFiles []string `xorm:"TEXT JSON"`
16+
}
17+
18+
if err := x.Sync2(new(PullRequest)); err != nil {
19+
return fmt.Errorf("Sync2: %v", err)
20+
}
21+
return nil
22+
}

models/pull.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ type PullRequest struct {
4545
CommitsAhead int
4646
CommitsBehind int
4747

48+
ChangedProtectedFiles []string `xorm:"TEXT JSON"`
49+
4850
IssueID int64 `xorm:"INDEX"`
4951
Issue *Issue `xorm:"-"`
5052
Index int64

modules/repofiles/update.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func detectEncodingAndBOM(entry *git.TreeEntry, repo *models.Repository) (string
123123

124124
// CreateOrUpdateRepoFile adds or updates a file in the given repository
125125
func CreateOrUpdateRepoFile(repo *models.Repository, doer *models.User, opts *UpdateRepoFileOptions) (*structs.FileResponse, error) {
126-
// If no branch name is set, assume master
126+
// If no branch name is set, assume default branch
127127
if opts.OldBranch == "" {
128128
opts.OldBranch = repo.DefaultBranch
129129
}

options/locale/locale_en-US.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,6 +1232,8 @@ pulls.required_status_check_administrator = As an administrator, you may still m
12321232
pulls.blocked_by_approvals = "This Pull Request doesn't have enough approvals yet. %d of %d approvals granted."
12331233
pulls.blocked_by_rejection = "This Pull Request has changes requested by an official reviewer."
12341234
pulls.blocked_by_outdated_branch = "This Pull Request is blocked because it's outdated."
1235+
pulls.blocked_by_changed_protected_files_1= "This Pull Request is blocked because it changes a protected file:"
1236+
pulls.blocked_by_changed_protected_files_n= "This Pull Request is blocked because it changes protected files:"
12351237
pulls.can_auto_merge_desc = This pull request can be merged automatically.
12361238
pulls.cannot_auto_merge_desc = This pull request cannot be merged automatically due to conflicts.
12371239
pulls.cannot_auto_merge_helper = Merge manually to resolve the conflicts.
@@ -1779,6 +1781,7 @@ diff.review.comment = Comment
17791781
diff.review.approve = Approve
17801782
diff.review.reject = Request changes
17811783
diff.committed_by = committed by
1784+
diff.protected = Protected
17821785

17831786
releases.desc = Track project versions and downloads.
17841787
release.releases = Releases

routers/api/v1/repo/branch.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"code.gitea.io/gitea/modules/log"
1717
repo_module "code.gitea.io/gitea/modules/repository"
1818
api "code.gitea.io/gitea/modules/structs"
19+
pull_service "code.gitea.io/gitea/services/pull"
1920
repo_service "code.gitea.io/gitea/services/repository"
2021
)
2122

@@ -545,6 +546,11 @@ func CreateBranchProtection(ctx *context.APIContext, form api.CreateBranchProtec
545546
return
546547
}
547548

549+
if err = pull_service.CheckPrsForBaseBranch(ctx.Repo.Repository, protectBranch.BranchName); err != nil {
550+
ctx.Error(http.StatusInternalServerError, "CheckPrsForBaseBranch", err)
551+
return
552+
}
553+
548554
// Reload from db to get all whitelists
549555
bp, err := models.GetProtectedBranchBy(ctx.Repo.Repository.ID, form.BranchName)
550556
if err != nil {
@@ -768,6 +774,11 @@ func EditBranchProtection(ctx *context.APIContext, form api.EditBranchProtection
768774
return
769775
}
770776

777+
if err = pull_service.CheckPrsForBaseBranch(ctx.Repo.Repository, protectBranch.BranchName); err != nil {
778+
ctx.Error(http.StatusInternalServerError, "CheckPrsForBaseBranch", err)
779+
return
780+
}
781+
771782
// Reload from db to ensure get all whitelists
772783
bp, err := models.GetProtectedBranchBy(repo.ID, bpName)
773784
if err != nil {

routers/api/v1/repo/pull.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ func MergePullRequest(ctx *context.APIContext, form auth.MergePullRequestForm) {
774774
return
775775
}
776776

777-
if err := pull_service.CheckPRReadyToMerge(pr); err != nil {
777+
if err := pull_service.CheckPRReadyToMerge(pr, false); err != nil {
778778
if !models.IsErrNotAllowedToMerge(err) {
779779
ctx.Error(http.StatusInternalServerError, "CheckPRReadyToMerge", err)
780780
return

0 commit comments

Comments
 (0)