Skip to content

Commit 7c5cc63

Browse files
committed
Add UnitCommitStatus
1 parent 35f68af commit 7c5cc63

File tree

3 files changed

+18
-48
lines changed

3 files changed

+18
-48
lines changed

models/unit/unit.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const (
3333
TypeProjects // 8 Projects
3434
TypePackages // 9 Packages
3535
TypeActions // 10 Actions
36+
TypeCommitStatus // 11 Commit Status
3637

3738
// FIXME: TEAM-UNIT-PERMISSION: the team unit "admin" permission's design is not right, when a new unit is added in the future,
3839
// admin team won't inherit the correct admin permission for the new unit, need to have a complete fix before adding any new unit.
@@ -65,6 +66,7 @@ var (
6566
TypeProjects,
6667
TypePackages,
6768
TypeActions,
69+
TypeCommitStatus,
6870
}
6971

7072
// DefaultRepoUnits contains the default unit types
@@ -77,8 +79,10 @@ var (
7779
TypeProjects,
7880
TypePackages,
7981
TypeActions,
82+
TypeCommitStatus,
8083
}
8184

85+
// TODO(not7cd): Defaults that need TypeCommitStatus
8286
// ForkRepoUnits contains the default unit types for forks
8387
DefaultForkRepoUnits = []Type{
8488
TypeCode,
@@ -237,6 +241,7 @@ func (u Unit) MaxPerm() perm.AccessMode {
237241
}
238242

239243
// Enumerate all the units
244+
// TODO(not7cd): Add TypeCommitStatus
240245
var (
241246
UnitCode = Unit{
242247
TypeCode,
@@ -328,6 +333,16 @@ var (
328333
perm.AccessModeOwner,
329334
}
330335

336+
// TODO(not7cd): Just copied this
337+
UnitCommitStatus = Unit{
338+
TypeCommitStatus,
339+
"repo.commitstatus",
340+
"/statuses",
341+
"commitstatus.unit.desc",
342+
8,
343+
perm.AccessModeOwner,
344+
}
345+
331346
// Units contains all the units
332347
Units = map[Type]Unit{
333348
TypeCode: UnitCode,
@@ -340,6 +355,7 @@ var (
340355
TypeProjects: UnitProjects,
341356
TypePackages: UnitPackages,
342357
TypeActions: UnitActions,
358+
TypeCommitStatus: UnitCommitStatus,
343359
}
344360
)
345361

routers/api/v1/api.go

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -455,28 +455,6 @@ func reqRepoWriter(unitTypes ...unit.Type) func(ctx *context.APIContext) {
455455
}
456456
}
457457

458-
// reqRepoCommitStatusWriter user should have a permission to write to commit
459-
// statuses, or write to a repo, or be a site admin
460-
func reqRepoCommitStatusWriter(unitTypes ...unit.Type) func(ctx *context.APIContext) {
461-
return func(ctx *context.APIContext) {
462-
// TODO(not7cd)
463-
if !ctx.IsUserRepoWriter(unitTypes) && !ctx.IsUserRepoAdmin() && !ctx.IsUserSiteAdmin() {
464-
ctx.APIError(http.StatusForbidden, "user should have a permission to write to a repo")
465-
return
466-
}
467-
}
468-
}
469-
470-
// TODO(not7cd): do I need this?
471-
// // reqRepoBranchWriter user should have a permission to write to a branch, or be a site admin
472-
// func reqRepoBranchWriter(ctx *context.APIContext) {
473-
// options, ok := web.GetForm(ctx).(api.FileOptionInterface)
474-
// if !ok || (!ctx.Repo.CanWriteToBranch(ctx, ctx.Doer, options.Branch()) && !ctx.IsUserSiteAdmin()) {
475-
// ctx.APIError(http.StatusForbidden, "user should have a permission to write to this branch")
476-
// return
477-
// }
478-
// }
479-
480458
// reqRepoReader user should have specific read permission or be a repo admin or a site admin
481459
func reqRepoReader(unitType unit.Type) func(ctx *context.APIContext) {
482460
return func(ctx *context.APIContext) {
@@ -487,18 +465,6 @@ func reqRepoReader(unitType unit.Type) func(ctx *context.APIContext) {
487465
}
488466
}
489467

490-
// reqRepoReader user should have specific commit status read permission, or
491-
// repo read permission, or be a repo admin or a site admin
492-
func reqRepoCommitStatusReader(unitType unit.Type) func(ctx *context.APIContext) {
493-
return func(ctx *context.APIContext) {
494-
// TODO(not7cd)
495-
if !ctx.Repo.CanRead(unitType) && !ctx.IsUserRepoAdmin() && !ctx.IsUserSiteAdmin() {
496-
ctx.APIError(http.StatusForbidden, "user should have specific read permission or be a repo admin or a site admin")
497-
return
498-
}
499-
}
500-
}
501-
502468
// reqAnyRepoReader user should have any permission to read repository or permissions of site admin
503469
func reqAnyRepoReader() func(ctx *context.APIContext) {
504470
return func(ctx *context.APIContext) {
@@ -1433,8 +1399,8 @@ func Routes() *web.Router {
14331399
}, mustAllowPulls, reqRepoReader(unit.TypeCode), context.ReferencesGitRepo())
14341400
m.Group("/statuses", func() {
14351401
m.Combo("/{sha}").Get(repo.GetCommitStatuses).
1436-
Post(reqToken(), reqRepoCommitStatusWriter(unit.TypeCode), bind(api.CreateStatusOption{}), repo.NewCommitStatus)
1437-
}, reqRepoCommitStatusReader(unit.TypeCode))
1402+
Post(reqToken(), reqRepoWriter(unit.TypeCommitStatus), bind(api.CreateStatusOption{}), repo.NewCommitStatus)
1403+
}, reqRepoWriter(unit.TypeCommitStatus))
14381404
m.Group("/commits", func() {
14391405
m.Get("", context.ReferencesGitRepo(), repo.GetAllCommits)
14401406
m.Group("/{ref}", func() {

services/context/api.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -367,15 +367,3 @@ func (ctx *APIContext) IsUserRepoAdmin() bool {
367367
func (ctx *APIContext) IsUserRepoWriter(unitTypes []unit.Type) bool {
368368
return slices.ContainsFunc(unitTypes, ctx.Repo.CanWrite)
369369
}
370-
371-
// IsUserRepoWriter returns true if current user has write commit status privilege in current repo
372-
func (ctx *APIContext) IsUserCommitStatusWriter(unitTypes []unit.Type) bool {
373-
for _, unitType := range unitTypes {
374-
// TODO
375-
if ctx.Repo.CanWrite(unitType) {
376-
return true
377-
}
378-
}
379-
380-
return false
381-
}

0 commit comments

Comments
 (0)