Skip to content

[Enhancement] Allow admin to merge pr with protected file changes #12078

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 35 commits into from
Oct 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
f6dfa21
[Enhancement] Allow admin to merge pr with protected file changes
a1012112796 Jun 28, 2020
9d64570
Merge branch 'master' into protected_files
a1012112796 Jun 28, 2020
cc7b2ea
remove unused ver
a1012112796 Jun 28, 2020
da514ca
Update options/locale/locale_en-US.ini
techknowlogick Jun 29, 2020
df7844d
Add TrN
a1012112796 Jun 29, 2020
cdadd09
Merge branch 'master' into protected_files
a1012112796 Jun 29, 2020
1bd95cb
Merge branch 'master' into protected_files
zeripath Jul 2, 2020
b7094f8
Merge branch 'master' into protected_files
a1012112796 Aug 3, 2020
6d2c017
Merge branch 'master' into protected_files
a1012112796 Aug 20, 2020
ba4631f
Merge branch 'master' into protected_files
a1012112796 Sep 4, 2020
5c1ab25
Apply suggestions from code review
a1012112796 Sep 5, 2020
de85e99
Merge branch 'master' into protected_files
a1012112796 Sep 5, 2020
1e491c7
fix lint
a1012112796 Sep 5, 2020
fcd4ce5
Update options/locale/locale_en-US.ini
a1012112796 Sep 5, 2020
ea67165
Apply suggestions from code review
a1012112796 Sep 6, 2020
3fe9e97
Merge branch 'master' into protected_files
a1012112796 Sep 6, 2020
8ccb646
Merge branch 'master' into protected_files
a1012112796 Sep 12, 2020
ed808be
Apply review suggestion @CirnoT
a1012112796 Sep 15, 2020
9e69e1d
Merge branch 'master' into protected_files
a1012112796 Sep 15, 2020
9396863
Merge branch 'master' into protected_files
a1012112796 Sep 17, 2020
736e3c1
Merge branch 'master' into protected_files
a1012112796 Sep 21, 2020
4c7f370
move to service @lunny
a1012112796 Sep 21, 2020
3e27c68
Merge branch 'master' into protected_files
techknowlogick Sep 27, 2020
4459e0e
Merge branch 'master' into protected_files
zeripath Oct 12, 2020
adeec88
Merge remote-tracking branch 'origin/master' into protected_files
zeripath Oct 12, 2020
2adda2d
slightly restructure routers/private/hook.go
zeripath Oct 13, 2020
c50a2ed
Merge remote-tracking branch 'origin/master' into protected_files
zeripath Oct 13, 2020
9a7c9c5
placate lint
zeripath Oct 13, 2020
2dc3c6d
skip duplicate protected files check
a1012112796 Oct 13, 2020
350b482
fix check logic
a1012112796 Oct 13, 2020
741bf9f
slight refactor of TestPatch
zeripath Oct 13, 2020
0c63499
When checking for protected files changes in TestPatch use the tempor…
zeripath Oct 13, 2020
3e24686
fix introduced issue with hook
zeripath Oct 13, 2020
538317e
Merge branch 'master' into protected_files
zeripath Oct 13, 2020
62e55c5
Remove the check on PR index being greater than 0 as it unnecessary
zeripath Oct 13, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions models/branches.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,38 @@ func (protectBranch *ProtectedBranch) GetProtectedFilePatterns() []glob.Glob {
return extarr
}

// MergeBlockedByProtectedFiles returns true if merge is blocked by protected files change
func (protectBranch *ProtectedBranch) MergeBlockedByProtectedFiles(pr *PullRequest) bool {
glob := protectBranch.GetProtectedFilePatterns()
if len(glob) == 0 {
return false
}

return len(pr.ChangedProtectedFiles) > 0
}

// IsProtectedFile return if path is protected
func (protectBranch *ProtectedBranch) IsProtectedFile(patterns []glob.Glob, path string) bool {
if len(patterns) == 0 {
patterns = protectBranch.GetProtectedFilePatterns()
if len(patterns) == 0 {
return false
}
}

lpath := strings.ToLower(strings.TrimSpace(path))

r := false
for _, pat := range patterns {
if pat.Match(lpath) {
r = true
break
}
}

return r
}

// GetProtectedBranchByRepoID getting protected branch by repo ID
func GetProtectedBranchByRepoID(repoID int64) ([]*ProtectedBranch, error) {
protectedBranches := make([]*ProtectedBranch, 0)
Expand Down
2 changes: 2 additions & 0 deletions models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ var migrations = []Migration{
NewMigration("add Team review request support", addTeamReviewRequestSupport),
// v154 > v155
NewMigration("add timestamps to Star, Label, Follow, Watch and Collaboration", addTimeStamps),
// v155 -> v156
NewMigration("add changed_protected_files column for pull_request table", addChangedProtectedFilesPullRequestColumn),
}

// GetCurrentDBVersion returns the current db version
Expand Down
22 changes: 22 additions & 0 deletions models/migrations/v155.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2020 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 addChangedProtectedFilesPullRequestColumn(x *xorm.Engine) error {
type PullRequest struct {
ChangedProtectedFiles []string `xorm:"TEXT JSON"`
}

if err := x.Sync2(new(PullRequest)); err != nil {
return fmt.Errorf("Sync2: %v", err)
}
return nil
}
2 changes: 2 additions & 0 deletions models/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ type PullRequest struct {
CommitsAhead int
CommitsBehind int

ChangedProtectedFiles []string `xorm:"TEXT JSON"`

IssueID int64 `xorm:"INDEX"`
Issue *Issue `xorm:"-"`
Index int64
Expand Down
2 changes: 1 addition & 1 deletion modules/repofiles/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func detectEncodingAndBOM(entry *git.TreeEntry, repo *models.Repository) (string

// CreateOrUpdateRepoFile adds or updates a file in the given repository
func CreateOrUpdateRepoFile(repo *models.Repository, doer *models.User, opts *UpdateRepoFileOptions) (*structs.FileResponse, error) {
// If no branch name is set, assume master
// If no branch name is set, assume default branch
if opts.OldBranch == "" {
opts.OldBranch = repo.DefaultBranch
}
Expand Down
3 changes: 3 additions & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1232,6 +1232,8 @@ pulls.required_status_check_administrator = As an administrator, you may still m
pulls.blocked_by_approvals = "This Pull Request doesn't have enough approvals yet. %d of %d approvals granted."
pulls.blocked_by_rejection = "This Pull Request has changes requested by an official reviewer."
pulls.blocked_by_outdated_branch = "This Pull Request is blocked because it's outdated."
pulls.blocked_by_changed_protected_files_1= "This Pull Request is blocked because it changes a protected file:"
pulls.blocked_by_changed_protected_files_n= "This Pull Request is blocked because it changes protected files:"
pulls.can_auto_merge_desc = This pull request can be merged automatically.
pulls.cannot_auto_merge_desc = This pull request cannot be merged automatically due to conflicts.
pulls.cannot_auto_merge_helper = Merge manually to resolve the conflicts.
Expand Down Expand Up @@ -1779,6 +1781,7 @@ diff.review.comment = Comment
diff.review.approve = Approve
diff.review.reject = Request changes
diff.committed_by = committed by
diff.protected = Protected

releases.desc = Track project versions and downloads.
release.releases = Releases
Expand Down
11 changes: 11 additions & 0 deletions routers/api/v1/repo/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"code.gitea.io/gitea/modules/log"
repo_module "code.gitea.io/gitea/modules/repository"
api "code.gitea.io/gitea/modules/structs"
pull_service "code.gitea.io/gitea/services/pull"
repo_service "code.gitea.io/gitea/services/repository"
)

Expand Down Expand Up @@ -545,6 +546,11 @@ func CreateBranchProtection(ctx *context.APIContext, form api.CreateBranchProtec
return
}

if err = pull_service.CheckPrsForBaseBranch(ctx.Repo.Repository, protectBranch.BranchName); err != nil {
ctx.Error(http.StatusInternalServerError, "CheckPrsForBaseBranch", err)
return
}

// Reload from db to get all whitelists
bp, err := models.GetProtectedBranchBy(ctx.Repo.Repository.ID, form.BranchName)
if err != nil {
Expand Down Expand Up @@ -768,6 +774,11 @@ func EditBranchProtection(ctx *context.APIContext, form api.EditBranchProtection
return
}

if err = pull_service.CheckPrsForBaseBranch(ctx.Repo.Repository, protectBranch.BranchName); err != nil {
ctx.Error(http.StatusInternalServerError, "CheckPrsForBaseBranch", err)
return
}

// Reload from db to ensure get all whitelists
bp, err := models.GetProtectedBranchBy(repo.ID, bpName)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion routers/api/v1/repo/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ func MergePullRequest(ctx *context.APIContext, form auth.MergePullRequestForm) {
return
}

if err := pull_service.CheckPRReadyToMerge(pr); err != nil {
if err := pull_service.CheckPRReadyToMerge(pr, false); err != nil {
if !models.IsErrNotAllowedToMerge(err) {
ctx.Error(http.StatusInternalServerError, "CheckPRReadyToMerge", err)
return
Expand Down
Loading