-
Notifications
You must be signed in to change notification settings - Fork 432
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
feat: project v2 handler #7256
Open
sguiheux
wants to merge
2
commits into
master
Choose a base branch
from
projectv2Handler
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat: project v2 handler #7256
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,11 +6,178 @@ import ( | |
|
||
"github.com/gorilla/mux" | ||
"github.com/ovh/cds/engine/api/authentication" | ||
"github.com/ovh/cds/engine/api/event_v2" | ||
"github.com/ovh/cds/engine/api/permission" | ||
"github.com/ovh/cds/engine/api/project" | ||
"github.com/ovh/cds/engine/api/rbac" | ||
"github.com/ovh/cds/engine/service" | ||
"github.com/ovh/cds/sdk" | ||
) | ||
|
||
func (api *API) getProjectsV2Handler() ([]service.RbacChecker, service.Handler) { | ||
return service.RBAC(), | ||
func(ctx context.Context, w http.ResponseWriter, r *http.Request) error { | ||
|
||
u := getUserConsumer(ctx) | ||
if u == nil { | ||
return sdk.WithStack(sdk.ErrForbidden) | ||
} | ||
|
||
// For admin | ||
if isAdmin(ctx) { | ||
projects, err := project.LoadAll(ctx, api.mustDB(), api.Cache) | ||
if err != nil { | ||
return err | ||
} | ||
return service.WriteJSON(w, projects, http.StatusOK) | ||
} | ||
|
||
// Normal user | ||
keys, err := rbac.LoadAllProjectKeysAllowed(ctx, api.mustDB(), sdk.ProjectRoleRead, u.AuthConsumerUser.AuthentifiedUserID) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
projects, err := project.LoadAllByKeys(ctx, api.mustDB(), keys) | ||
if err != nil { | ||
return err | ||
} | ||
return service.WriteJSON(w, projects, http.StatusOK) | ||
} | ||
} | ||
|
||
func (api *API) deleteProjectV2Handler() ([]service.RbacChecker, service.Handler) { | ||
return service.RBAC(api.projectManage), | ||
func(ctx context.Context, w http.ResponseWriter, r *http.Request) error { | ||
// Get project name in URL | ||
vars := mux.Vars(r) | ||
key := vars["projectKey"] | ||
|
||
u := getUserConsumer(ctx) | ||
if u == nil { | ||
return sdk.WithStack(sdk.ErrForbidden) | ||
} | ||
|
||
p, err := project.Load(ctx, api.mustDB(), key, project.LoadOptions.WithPipelines, project.LoadOptions.WithApplications) | ||
if err != nil { | ||
if !sdk.ErrorIs(err, sdk.ErrNoProject) { | ||
return sdk.WrapError(err, "deleteProject> load project '%s' from db", key) | ||
} | ||
return sdk.WrapError(err, "cannot load project %s", key) | ||
} | ||
|
||
// TODO Delete | ||
if len(p.Pipelines) > 0 { | ||
return sdk.WrapError(sdk.ErrProjectHasPipeline, "project '%s' still used by %d pipelines", key, len(p.Pipelines)) | ||
} | ||
|
||
if len(p.Applications) > 0 { | ||
return sdk.WrapError(sdk.ErrProjectHasApplication, "project '%s' still used by %d applications", key, len(p.Applications)) | ||
} | ||
// | ||
|
||
tx, err := api.mustDB().Begin() | ||
if err != nil { | ||
return sdk.WithStack(err) | ||
} | ||
defer tx.Rollback() // nolint | ||
|
||
if err := project.Delete(tx, p.Key); err != nil { | ||
return err | ||
} | ||
if err := tx.Commit(); err != nil { | ||
return sdk.WithStack(err) | ||
} | ||
|
||
event_v2.PublishProjectEvent(ctx, api.Cache, sdk.EventProjectDeleted, *p, *u.AuthConsumerUser.AuthentifiedUser) | ||
return nil | ||
} | ||
} | ||
|
||
func (api *API) updateProjectV2Handler() ([]service.RbacChecker, service.Handler) { | ||
return service.RBAC(api.projectRead), | ||
func(ctx context.Context, w http.ResponseWriter, r *http.Request) error { | ||
// Get project name in URL | ||
vars := mux.Vars(r) | ||
key := vars["projectKey"] | ||
|
||
u := getUserConsumer(ctx) | ||
if u == nil { | ||
return sdk.WithStack(sdk.ErrForbidden) | ||
} | ||
|
||
proj := &sdk.Project{} | ||
if err := service.UnmarshalBody(r, proj); err != nil { | ||
return sdk.WithStack(err) | ||
} | ||
|
||
if proj.Name == "" { | ||
return sdk.WrapError(sdk.ErrInvalidProjectName, "project name must no be empty") | ||
} | ||
|
||
// Check Request | ||
if key != proj.Key { | ||
return sdk.WrapError(sdk.ErrWrongRequest, "bad Project key %s/%s ", key, proj.Key) | ||
} | ||
|
||
if proj.WorkflowRetention <= 0 { | ||
proj.WorkflowRetention = api.Config.WorkflowV2.WorkflowRunRetention | ||
} | ||
|
||
// Check is project exist | ||
p, err := project.Load(ctx, api.mustDB(), key, project.LoadOptions.WithIcon) | ||
if err != nil { | ||
return err | ||
} | ||
// Update in DB is made given the primary key | ||
proj.ID = p.ID | ||
proj.VCSServers = p.VCSServers | ||
if proj.Icon == "" { | ||
p.Icon = proj.Icon | ||
} | ||
if err := project.Update(api.mustDB(), proj); err != nil { | ||
return sdk.WrapError(err, "cannot update project %s", key) | ||
} | ||
event_v2.PublishProjectEvent(ctx, api.Cache, sdk.EventProjectUpdated, *proj, *u.AuthConsumerUser.AuthentifiedUser) | ||
|
||
// TODO REMOVE | ||
proj.Permissions.Readable = true | ||
proj.Permissions.Writable = true | ||
|
||
return service.WriteJSON(w, proj, http.StatusOK) | ||
} | ||
} | ||
|
||
func (api *API) getProjectV2Handler() ([]service.RbacChecker, service.Handler) { | ||
return service.RBAC(api.projectRead), | ||
func(ctx context.Context, w http.ResponseWriter, r *http.Request) error { | ||
// Get project name in URL | ||
vars := mux.Vars(r) | ||
key := vars["projectKey"] | ||
|
||
p, errProj := project.Load(ctx, api.mustDB(), key) | ||
if errProj != nil { | ||
return sdk.WrapError(errProj, "getProjectHandler (%s)", key) | ||
} | ||
|
||
// TODO REMOVE | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same |
||
if isAdmin(ctx) { | ||
p.Permissions = sdk.Permissions{Readable: true, Writable: true, Executable: true} | ||
} else { | ||
permissions, err := permission.LoadProjectMaxLevelPermission(ctx, api.mustDB(), []string{p.Key}, getUserConsumer(ctx).GetGroupIDs()) | ||
if err != nil { | ||
return err | ||
} | ||
p.Permissions = permissions.Permissions(p.Key) | ||
if isMaintainer(ctx) { | ||
p.Permissions.Readable = true | ||
} | ||
} | ||
|
||
return service.WriteJSON(w, p, http.StatusOK) | ||
} | ||
} | ||
|
||
func (api *API) getProjectV2AccessHandler() ([]service.RbacChecker, service.Handler) { | ||
return service.RBAC(api.isCDNService), | ||
func(ctx context.Context, w http.ResponseWriter, req *http.Request) error { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need on v2 routes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That why there is a TODO. I don't know yet how it will be implement on UI side