Skip to content

Commit 81f6687

Browse files
author
Gusted
committed
Merge branch 'main' into fix-review-form
2 parents cf4137c + a51efb4 commit 81f6687

File tree

31 files changed

+370
-64
lines changed

31 files changed

+370
-64
lines changed

models/migrations/migrations.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ type Version struct {
6161
// update minDBVersion accordingly
6262
var migrations = []Migration{
6363
// Gitea 1.5.0 ends at v69
64+
6465
// v70 -> v71
6566
NewMigration("add issue_dependencies", addIssueDependencies),
6667
// v71 -> v72
@@ -380,6 +381,8 @@ var migrations = []Migration{
380381
NewMigration("Create ForeignReference table", createForeignReferenceTable),
381382
// v212 -> v213
382383
NewMigration("Add package tables", addPackageTables),
384+
// v213 -> v214
385+
NewMigration("Add allow edits from maintainers to PullRequest table", addAllowMaintainerEdit),
383386
}
384387

385388
// GetCurrentDBVersion returns the current db version

models/migrations/v213.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2022 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+
"xorm.io/xorm"
9+
)
10+
11+
func addAllowMaintainerEdit(x *xorm.Engine) error {
12+
// PullRequest represents relation between pull request and repositories.
13+
type PullRequest struct {
14+
AllowMaintainerEdit bool `xorm:"NOT NULL DEFAULT false"`
15+
}
16+
17+
return x.Sync2(new(PullRequest))
18+
}

models/pull.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,16 @@ type PullRequest struct {
6969
Issue *Issue `xorm:"-"`
7070
Index int64
7171

72-
HeadRepoID int64 `xorm:"INDEX"`
73-
HeadRepo *repo_model.Repository `xorm:"-"`
74-
BaseRepoID int64 `xorm:"INDEX"`
75-
BaseRepo *repo_model.Repository `xorm:"-"`
76-
HeadBranch string
77-
HeadCommitID string `xorm:"-"`
78-
BaseBranch string
79-
ProtectedBranch *ProtectedBranch `xorm:"-"`
80-
MergeBase string `xorm:"VARCHAR(40)"`
72+
HeadRepoID int64 `xorm:"INDEX"`
73+
HeadRepo *repo_model.Repository `xorm:"-"`
74+
BaseRepoID int64 `xorm:"INDEX"`
75+
BaseRepo *repo_model.Repository `xorm:"-"`
76+
HeadBranch string
77+
HeadCommitID string `xorm:"-"`
78+
BaseBranch string
79+
ProtectedBranch *ProtectedBranch `xorm:"-"`
80+
MergeBase string `xorm:"VARCHAR(40)"`
81+
AllowMaintainerEdit bool `xorm:"NOT NULL DEFAULT false"`
8182

8283
HasMerged bool `xorm:"INDEX"`
8384
MergedCommitID string `xorm:"VARCHAR(40)"`
@@ -711,6 +712,14 @@ func (pr *PullRequest) GetHeadBranchHTMLURL() string {
711712
return pr.HeadRepo.HTMLURL() + "/src/branch/" + util.PathEscapeSegments(pr.HeadBranch)
712713
}
713714

715+
// UpdateAllowEdits update if PR can be edited from maintainers
716+
func UpdateAllowEdits(ctx context.Context, pr *PullRequest) error {
717+
if _, err := db.GetEngine(ctx).ID(pr.ID).Cols("allow_maintainer_edit").Update(pr); err != nil {
718+
return err
719+
}
720+
return nil
721+
}
722+
714723
// Mergeable returns if the pullrequest is mergeable.
715724
func (pr *PullRequest) Mergeable() bool {
716725
// If a pull request isn't mergable if it's:

models/repo_permission.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,39 @@ func (p *Permission) CanWriteIssuesOrPulls(isPull bool) bool {
103103
return p.CanWrite(unit.TypeIssues)
104104
}
105105

106+
// CanWriteToBranch checks if the branch is writable by the user
107+
func (p *Permission) CanWriteToBranch(user *user_model.User, branch string) bool {
108+
if p.CanWrite(unit.TypeCode) {
109+
return true
110+
}
111+
112+
if len(p.Units) < 1 {
113+
return false
114+
}
115+
116+
prs, err := GetUnmergedPullRequestsByHeadInfo(p.Units[0].RepoID, branch)
117+
if err != nil {
118+
return false
119+
}
120+
121+
for _, pr := range prs {
122+
if pr.AllowMaintainerEdit {
123+
err = pr.LoadBaseRepo()
124+
if err != nil {
125+
continue
126+
}
127+
prPerm, err := GetUserRepoPermission(db.DefaultContext, pr.BaseRepo, user)
128+
if err != nil {
129+
continue
130+
}
131+
if prPerm.CanWrite(unit.TypeCode) {
132+
return true
133+
}
134+
}
135+
}
136+
return false
137+
}
138+
106139
// ColorFormat writes a colored string for these Permissions
107140
func (p *Permission) ColorFormat(s fmt.State) {
108141
noColor := log.ColorBytes(log.Reset)
@@ -160,6 +193,7 @@ func GetUserRepoPermission(ctx context.Context, repo *repo_model.Repository, use
160193
perm)
161194
}()
162195
}
196+
163197
// anonymous user visit private repo.
164198
// TODO: anonymous user visit public unit of private repo???
165199
if user == nil && repo.IsPrivate {

modules/context/permission.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ func RequireRepoWriter(unitType unit.Type) func(ctx *Context) {
2929
}
3030
}
3131

32+
// CanEnableEditor checks if the user is allowed to write to the branch of the repo
33+
func CanEnableEditor() func(ctx *Context) {
34+
return func(ctx *Context) {
35+
if !ctx.Repo.Permission.CanWriteToBranch(ctx.Doer, ctx.Repo.BranchName) {
36+
ctx.NotFound("CanWriteToBranch denies permission", nil)
37+
return
38+
}
39+
}
40+
}
41+
3242
// RequireRepoWriterOr returns a middleware for requiring repository write to one of the unit permission
3343
func RequireRepoWriterOr(unitTypes ...unit.Type) func(ctx *Context) {
3444
return func(ctx *Context) {

modules/context/repo.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ type Repository struct {
7878
}
7979

8080
// CanEnableEditor returns true if repository is editable and user has proper access level.
81-
func (r *Repository) CanEnableEditor() bool {
82-
return r.Permission.CanWrite(unit_model.TypeCode) && r.Repository.CanEnableEditor() && r.IsViewBranch && !r.Repository.IsArchived
81+
func (r *Repository) CanEnableEditor(user *user_model.User) bool {
82+
return r.IsViewBranch && r.Permission.CanWriteToBranch(user, r.BranchName) && r.Repository.CanEnableEditor() && !r.Repository.IsArchived
8383
}
8484

8585
// CanCreateBranch returns true if repository is editable and user has proper access level.
@@ -123,7 +123,7 @@ func (r *Repository) CanCommitToBranch(ctx context.Context, doer *user_model.Use
123123

124124
sign, keyID, _, err := asymkey_service.SignCRUDAction(ctx, r.Repository.RepoPath(), doer, r.Repository.RepoPath(), git.BranchPrefix+r.BranchName)
125125

126-
canCommit := r.CanEnableEditor() && userCanPush
126+
canCommit := r.CanEnableEditor(doer) && userCanPush
127127
if requireSigned {
128128
canCommit = canCommit && sign
129129
}
@@ -139,7 +139,7 @@ func (r *Repository) CanCommitToBranch(ctx context.Context, doer *user_model.Use
139139

140140
return CanCommitToBranchResults{
141141
CanCommitToBranch: canCommit,
142-
EditorEnabled: r.CanEnableEditor(),
142+
EditorEnabled: r.CanEnableEditor(doer),
143143
UserCanPush: userCanPush,
144144
RequireSigned: requireSigned,
145145
WillSign: sign,

modules/convert/convert.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,19 @@ func ToEmail(email *user_model.EmailAddress) *api.Email {
4141
func ToBranch(repo *repo_model.Repository, b *git.Branch, c *git.Commit, bp *models.ProtectedBranch, user *user_model.User, isRepoAdmin bool) (*api.Branch, error) {
4242
if bp == nil {
4343
var hasPerm bool
44+
var canPush bool
4445
var err error
4546
if user != nil {
4647
hasPerm, err = models.HasAccessUnit(user, repo, unit.TypeCode, perm.AccessModeWrite)
4748
if err != nil {
4849
return nil, err
4950
}
51+
52+
perms, err := models.GetUserRepoPermission(db.DefaultContext, repo, user)
53+
if err != nil {
54+
return nil, err
55+
}
56+
canPush = perms.CanWriteToBranch(user, b.Name)
5057
}
5158

5259
return &api.Branch{
@@ -56,7 +63,7 @@ func ToBranch(repo *repo_model.Repository, b *git.Branch, c *git.Commit, bp *mod
5663
RequiredApprovals: 0,
5764
EnableStatusCheck: false,
5865
StatusCheckContexts: []string{},
59-
UserCanPush: hasPerm,
66+
UserCanPush: canPush,
6067
UserCanMerge: hasPerm,
6168
}, nil
6269
}

modules/convert/pull.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ func ToAPIPullRequest(ctx context.Context, pr *models.PullRequest, doer *user_mo
7373
Created: pr.Issue.CreatedUnix.AsTimePtr(),
7474
Updated: pr.Issue.UpdatedUnix.AsTimePtr(),
7575

76+
AllowMaintainerEdit: pr.AllowMaintainerEdit,
77+
7678
Base: &api.PRBranchInfo{
7779
Name: pr.BaseBranch,
7880
Ref: pr.BaseBranch,

modules/hostmatcher/hostmatcher.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,18 @@ func (hl *HostMatchList) checkIP(ip net.IP) bool {
125125

126126
// MatchHostName checks if the host matches an allow/deny(block) list
127127
func (hl *HostMatchList) MatchHostName(host string) bool {
128+
hostname, _, err := net.SplitHostPort(host)
129+
if err != nil {
130+
hostname = host
131+
}
132+
128133
if hl == nil {
129134
return false
130135
}
131-
if hl.checkPattern(host) {
136+
if hl.checkPattern(hostname) {
132137
return true
133138
}
134-
if ip := net.ParseIP(host); ip != nil {
139+
if ip := net.ParseIP(hostname); ip != nil {
135140
return hl.checkIP(ip)
136141
}
137142
return false

modules/hostmatcher/hostmatcher_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ func TestHostOrIPMatchesList(t *testing.T) {
3838

3939
{"", net.ParseIP("10.0.1.1"), true},
4040
{"10.0.1.1", nil, true},
41+
{"10.0.1.1:8080", nil, true},
4142
{"", net.ParseIP("192.168.1.1"), true},
4243
{"192.168.1.1", nil, true},
4344
{"", net.ParseIP("fd00::1"), true},
@@ -48,6 +49,7 @@ func TestHostOrIPMatchesList(t *testing.T) {
4849

4950
{"mydomain.com", net.IPv4zero, false},
5051
{"sub.mydomain.com", net.IPv4zero, true},
52+
{"sub.mydomain.com:8080", net.IPv4zero, true},
5153

5254
{"", net.ParseIP("169.254.1.1"), true},
5355
{"169.254.1.1", nil, true},

0 commit comments

Comments
 (0)