Skip to content

Commit 8d807b2

Browse files
authored
Merge branch 'main' into feat/add-searchbar-in-workflow-dropdown
2 parents 8753cb3 + e806566 commit 8d807b2

12 files changed

Lines changed: 54 additions & 51 deletions

File tree

models/repo/repo.go

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -575,32 +575,20 @@ func (repo *Repository) IsOwnedBy(userID int64) bool {
575575
return repo.OwnerID == userID
576576
}
577577

578-
// CanCreateBranch returns true if repository meets the requirements for creating new branches.
579-
func (repo *Repository) CanCreateBranch() bool {
580-
return !repo.IsMirror
581-
}
582-
583578
// CanEnablePulls returns true if repository meets the requirements of accepting pulls.
584579
func (repo *Repository) CanEnablePulls() bool {
585-
return !repo.IsMirror && !repo.IsEmpty
580+
return repo.CanContentChange() && !repo.IsEmpty
581+
}
582+
583+
func (repo *Repository) CanContentChange() bool {
584+
return !repo.IsMirror && !repo.IsArchived
586585
}
587586

588587
// AllowsPulls returns true if repository meets the requirements of accepting pulls and has them enabled.
589588
func (repo *Repository) AllowsPulls(ctx context.Context) bool {
590589
return repo.CanEnablePulls() && repo.UnitEnabled(ctx, unit.TypePullRequests)
591590
}
592591

593-
// CanEnableEditor returns true if repository meets the requirements of web editor.
594-
// FIXME: most CanEnableEditor calls should be replaced with CanContentChange
595-
// And all other like CanCreateBranch / CanEnablePulls should also be updated
596-
func (repo *Repository) CanEnableEditor() bool {
597-
return repo.CanContentChange()
598-
}
599-
600-
func (repo *Repository) CanContentChange() bool {
601-
return !repo.IsMirror && !repo.IsArchived
602-
}
603-
604592
// DescriptionHTML does special handles to description and return HTML string.
605593
func (repo *Repository) DescriptionHTML(ctx context.Context) template.HTML {
606594
return markup.PostProcessDescriptionHTML(markup.NewRenderContext(ctx), htmlutil.EscapeString(repo.Description))

options/locale/locale_en-US.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1324,7 +1324,9 @@
13241324
"repo.editor.upload_files_to_dir": "Upload files to \"%s\"",
13251325
"repo.editor.cannot_commit_to_protected_branch": "Cannot commit to protected branch \"%s\".",
13261326
"repo.editor.no_commit_to_branch": "Not allowed to commit directly to branch because:",
1327-
"repo.editor.user_no_push_to_branch": "User cannot push to branch",
1327+
"repo.editor.no_write_permission": "No write permission.",
1328+
"repo.editor.repo_not_editable": "Repository is not editable.",
1329+
"repo.editor.branch_is_protected": "Branch is protected",
13281330
"repo.editor.require_signed_commit": "Branch requires a signed commit",
13291331
"repo.editor.cherry_pick": "Cherry-pick %s onto:",
13301332
"repo.editor.revert": "Revert %s onto:",

routers/api/v1/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -874,7 +874,7 @@ func mustNotBeArchived(ctx *context.APIContext) {
874874
}
875875

876876
func mustEnableEditor(ctx *context.APIContext) {
877-
if !ctx.Repo.Repository.CanEnableEditor() {
877+
if !ctx.Repo.Repository.CanContentChange() {
878878
ctx.APIError(http.StatusLocked, "repo is not allowed to edit")
879879
return
880880
}

routers/web/repo/editor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func prepareEditorPageFormOptions(ctx *context.Context, editorAction string) *co
6767
return nil
6868
}
6969

70-
if commitFormOptions.WillSubmitToFork && !commitFormOptions.TargetRepo.CanEnableEditor() {
70+
if commitFormOptions.WillSubmitToFork && !commitFormOptions.TargetRepo.CanContentChange() {
7171
ctx.Data["NotFoundPrompt"] = ctx.Locale.Tr("repo.editor.fork_not_editable")
7272
ctx.NotFound(nil)
7373
}

routers/web/repo/repo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func MustBeNotEmpty(ctx *context.Context) {
4949

5050
// MustBeEditable check that repo can be edited
5151
func MustBeEditable(ctx *context.Context) {
52-
if !ctx.Repo.Repository.CanEnableEditor() {
52+
if !ctx.Repo.Repository.CanContentChange() {
5353
ctx.NotFound(nil)
5454
return
5555
}

routers/web/repo/view_file.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ func prepareFileView(ctx *context.Context, entry *git.TreeEntry) {
255255

256256
func prepareFileViewEditorButtons(ctx *context.Context) bool {
257257
// archived or mirror repository, the buttons should not be shown
258-
if !ctx.Repo.Repository.CanEnableEditor() {
258+
if !ctx.Repo.Repository.CanContentChange() {
259259
return true
260260
}
261261

routers/web/repo/view_readme.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ func prepareToRenderReadmeFile(ctx *context.Context, subfolder string, readmeFil
220220
ctx.Data["EscapeStatus"], ctx.Data["FileContent"] = charset.EscapeControlHTML(template.HTML(contentEscaped), ctx.Locale)
221221
}
222222

223-
if !fInfo.isLFSFile() && ctx.Repo.Repository.CanEnableEditor() {
223+
if !fInfo.isLFSFile() && ctx.Repo.Repository.CanContentChange() {
224224
ctx.Data["CanEditReadmeFile"] = true
225225
}
226226
}

services/context/repo.go

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"errors"
1010
"fmt"
1111
"html"
12+
"html/template"
1213
"net/http"
1314
"net/url"
1415
"path"
@@ -124,7 +125,7 @@ func (r *Repository) CanWriteToBranch(ctx context.Context, user *user_model.User
124125

125126
// CanCreateBranch returns true if repository is editable and user has proper access level.
126127
func (r *Repository) CanCreateBranch() bool {
127-
return r.Permission.CanWrite(unit_model.TypeCode) && r.Repository.CanCreateBranch()
128+
return r.Permission.CanWrite(unit_model.TypeCode) && r.Repository.CanContentChange()
128129
}
129130

130131
func (r *Repository) GetObjectFormat() git.ObjectFormat {
@@ -143,15 +144,18 @@ func RepoMustNotBeArchived() func(ctx *Context) {
143144
type CommitFormOptions struct {
144145
NeedFork bool
145146

146-
TargetRepo *repo_model.Repository
147-
TargetFormAction string
148-
WillSubmitToFork bool
147+
TargetRepo *repo_model.Repository
148+
TargetFormAction string
149+
150+
WillSubmitToFork bool
151+
149152
CanCommitToBranch bool
150-
UserCanPush bool
151-
RequireSigned bool
152-
WillSign bool
153-
SigningKeyFormDisplay string
154-
WontSignReason string
153+
DenyCommitToBranchReason template.HTML
154+
155+
WillSign bool
156+
SigningKeyFormDisplay string
157+
WontSignReason string
158+
155159
CanCreatePullRequest bool
156160
CanCreateBasePullRequest bool
157161
}
@@ -172,7 +176,7 @@ func PrepareCommitFormOptions(ctx *Context, doer *user_model.User, targetRepo *r
172176
}
173177
// now, we get our own forked repo; it must be writable by us.
174178
}
175-
submitToForkedRepo := targetRepo.ID != originRepo.ID
179+
176180
err := targetRepo.GetBaseRepo(ctx)
177181
if err != nil {
178182
return nil, err
@@ -214,33 +218,43 @@ func PrepareCommitFormOptions(ctx *Context, doer *user_model.User, targetRepo *r
214218
return nil, err
215219
}
216220

217-
canCommitToBranch := !submitToForkedRepo /* same repo */ && targetRepo.CanEnableEditor() && canPushWithProtection
218-
if protectionRequireSigned {
219-
canCommitToBranch = canCommitToBranch && willSign
220-
}
221-
222221
canCreateBasePullRequest := targetRepo.BaseRepo != nil && targetRepo.BaseRepo.UnitEnabled(ctx, unit_model.TypePullRequests)
223222
canCreatePullRequest := targetRepo.UnitEnabled(ctx, unit_model.TypePullRequests) || canCreateBasePullRequest
224223

225224
opts := &CommitFormOptions{
226-
TargetRepo: targetRepo,
227-
WillSubmitToFork: submitToForkedRepo,
228-
CanCommitToBranch: canCommitToBranch,
229-
UserCanPush: canPushWithProtection,
230-
RequireSigned: protectionRequireSigned,
225+
TargetRepo: targetRepo,
226+
227+
WillSubmitToFork: targetRepo.ID != originRepo.ID,
228+
231229
WillSign: willSign,
232230
SigningKeyFormDisplay: asymkey_model.GetDisplaySigningKey(signKey),
233231
WontSignReason: wontSignReason,
234232

235233
CanCreatePullRequest: canCreatePullRequest,
236234
CanCreateBasePullRequest: canCreateBasePullRequest,
237235
}
236+
238237
editorAction := ctx.PathParam("editor_action")
239238
editorPathParamRemaining := util.PathEscapeSegments(branchName) + "/" + util.PathEscapeSegments(ctx.Repo.TreePath)
240-
if submitToForkedRepo {
239+
240+
opts.CanCommitToBranch = false
241+
if opts.WillSubmitToFork {
242+
opts.DenyCommitToBranchReason = ctx.Locale.Tr("repo.editor.no_write_permission")
241243
// there is only "default branch" in forked repo, we will use "from_base_branch" to get a new branch from base repo
242244
editorPathParamRemaining = util.PathEscapeSegments(targetRepo.DefaultBranch) + "/" + util.PathEscapeSegments(ctx.Repo.TreePath) + "?from_base_branch=" + url.QueryEscape(branchName)
245+
} else {
246+
// if the user is committing to the same repo, we need to check if the branch is protected and if the user can push to it
247+
if !targetRepo.CanContentChange() {
248+
opts.DenyCommitToBranchReason = ctx.Locale.Tr("repo.editor.repo_not_editable")
249+
} else if !canPushWithProtection {
250+
opts.DenyCommitToBranchReason = ctx.Locale.Tr("repo.editor.branch_is_protected")
251+
} else if protectionRequireSigned && !willSign {
252+
opts.DenyCommitToBranchReason = ctx.Locale.Tr("repo.editor.require_signed_commit")
253+
} else {
254+
opts.CanCommitToBranch = true
255+
}
243256
}
257+
244258
if editorAction == "_cherrypick" {
245259
opts.TargetFormAction = targetRepo.Link() + "/" + editorAction + "/" + ctx.PathParam("sha") + "/" + editorPathParamRemaining
246260
} else {

templates/repo/diff/box.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@
160160
<a class="item" rel="nofollow" href="{{$.BeforeSourcePath}}/{{PathEscapeSegments .Name}}">{{ctx.Locale.Tr "repo.diff.view_file"}}</a>
161161
{{else}}
162162
<a class="item" rel="nofollow" href="{{$.SourcePath}}/{{PathEscapeSegments .Name}}">{{ctx.Locale.Tr "repo.diff.view_file"}}</a>
163-
{{if and $.Repository.CanEnableEditor $.CanEditFile}}
163+
{{if and $.Repository.CanContentChange $.CanEditFile}}
164164
<a class="item" rel="nofollow" href="{{$.HeadRepoLink}}/_edit/{{PathEscapeSegments $.HeadBranchName}}/{{PathEscapeSegments $file.Name}}?return_uri={{print $.BackToLink "#diff-" $file.NameHash | QueryEscape}}">{{ctx.Locale.Tr "repo.editor.edit_this_file"}}</a>
165165
{{end}}
166166
{{end}}

templates/repo/editor/commit_form.tmpl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,9 @@
3232
{{if not .CommitFormOptions.CanCommitToBranch}}
3333
<div class="tw-mt-2">
3434
{{ctx.Locale.Tr "repo.editor.no_commit_to_branch"}}
35-
<ul class="tw-mb-0">
36-
{{if not .CommitFormOptions.UserCanPush}}<li>{{ctx.Locale.Tr "repo.editor.user_no_push_to_branch"}}</li>{{end}}
37-
{{if and .CommitFormOptions.RequireSigned (not .CommitFormOptions.WillSign)}}<li>{{ctx.Locale.Tr "repo.editor.require_signed_commit"}}</li>{{end}}
38-
</ul>
35+
{{if .CommitFormOptions.DenyCommitToBranchReason}}
36+
<ul class="tw-mb-0"><li>{{.CommitFormOptions.DenyCommitToBranchReason}}</li></ul>
37+
{{end}}
3938
</div>
4039
{{end}}
4140
</label>

0 commit comments

Comments
 (0)