Skip to content

Commit d9e9f88

Browse files
committed
Revert "Add UnitCommitStatus"
This reverts commit 7c5cc63.
1 parent 7c5cc63 commit d9e9f88

File tree

3 files changed

+48
-18
lines changed

3 files changed

+48
-18
lines changed

models/unit/unit.go

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

3837
// FIXME: TEAM-UNIT-PERMISSION: the team unit "admin" permission's design is not right, when a new unit is added in the future,
3938
// admin team won't inherit the correct admin permission for the new unit, need to have a complete fix before adding any new unit.
@@ -66,7 +65,6 @@ var (
6665
TypeProjects,
6766
TypePackages,
6867
TypeActions,
69-
TypeCommitStatus,
7068
}
7169

7270
// DefaultRepoUnits contains the default unit types
@@ -79,10 +77,8 @@ var (
7977
TypeProjects,
8078
TypePackages,
8179
TypeActions,
82-
TypeCommitStatus,
8380
}
8481

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

243239
// Enumerate all the units
244-
// TODO(not7cd): Add TypeCommitStatus
245240
var (
246241
UnitCode = Unit{
247242
TypeCode,
@@ -333,16 +328,6 @@ var (
333328
perm.AccessModeOwner,
334329
}
335330

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-
346331
// Units contains all the units
347332
Units = map[Type]Unit{
348333
TypeCode: UnitCode,
@@ -355,7 +340,6 @@ var (
355340
TypeProjects: UnitProjects,
356341
TypePackages: UnitPackages,
357342
TypeActions: UnitActions,
358-
TypeCommitStatus: UnitCommitStatus,
359343
}
360344
)
361345

routers/api/v1/api.go

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,28 @@ 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+
458480
// reqRepoReader user should have specific read permission or be a repo admin or a site admin
459481
func reqRepoReader(unitType unit.Type) func(ctx *context.APIContext) {
460482
return func(ctx *context.APIContext) {
@@ -465,6 +487,18 @@ func reqRepoReader(unitType unit.Type) func(ctx *context.APIContext) {
465487
}
466488
}
467489

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+
468502
// reqAnyRepoReader user should have any permission to read repository or permissions of site admin
469503
func reqAnyRepoReader() func(ctx *context.APIContext) {
470504
return func(ctx *context.APIContext) {
@@ -1399,8 +1433,8 @@ func Routes() *web.Router {
13991433
}, mustAllowPulls, reqRepoReader(unit.TypeCode), context.ReferencesGitRepo())
14001434
m.Group("/statuses", func() {
14011435
m.Combo("/{sha}").Get(repo.GetCommitStatuses).
1402-
Post(reqToken(), reqRepoWriter(unit.TypeCommitStatus), bind(api.CreateStatusOption{}), repo.NewCommitStatus)
1403-
}, reqRepoWriter(unit.TypeCommitStatus))
1436+
Post(reqToken(), reqRepoCommitStatusWriter(unit.TypeCode), bind(api.CreateStatusOption{}), repo.NewCommitStatus)
1437+
}, reqRepoCommitStatusReader(unit.TypeCode))
14041438
m.Group("/commits", func() {
14051439
m.Get("", context.ReferencesGitRepo(), repo.GetAllCommits)
14061440
m.Group("/{ref}", func() {

services/context/api.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,3 +367,15 @@ 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)