Skip to content

Commit

Permalink
fix(api): display only usage for workflow templates and worker model …
Browse files Browse the repository at this point in the history
…with good rights (#3779) (#3780)

* fix(api): display only usafe for workflow templates and worker model with good rights (#3779)

Signed-off-by: Benjamin Coenen <[email protected]>

* fix(api): display only usafe for workflow templates and worker model with good rights (#3779)

Signed-off-by: Benjamin Coenen <[email protected]>
  • Loading branch information
bnjjj authored and richardlt committed Dec 27, 2018
1 parent 427a88a commit 50cbfb8
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 13 deletions.
2 changes: 1 addition & 1 deletion engine/api/api_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ func (api *API) InitRouter() {
r.Handle("/worker/model/communication", r.GET(api.getWorkerModelCommunicationsHandler))
r.Handle("/worker/model/{permModelID}", r.PUT(api.updateWorkerModelHandler), r.DELETE(api.deleteWorkerModelHandler))
r.Handle("/worker/model/{permModelID}/export", r.GET(api.getWorkerModelExportHandler))
r.Handle("/worker/model/{permModelID}/usage", r.GET(api.getWorkerModelUsageHandler))
r.Handle("/worker/model/{modelID}/usage", r.GET(api.getWorkerModelUsageHandler))
r.Handle("/worker/model/capability/type", r.GET(api.getRequirementTypesHandler))

// Workflows
Expand Down
28 changes: 26 additions & 2 deletions engine/api/pipeline/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
"github.com/go-gorp/gorp"

"github.com/ovh/cds/engine/api/cache"
"github.com/ovh/cds/engine/api/database/gorpmapping"
"github.com/ovh/cds/engine/api/event"
"github.com/ovh/cds/engine/api/group"
"github.com/ovh/cds/engine/api/observability"
"github.com/ovh/cds/engine/api/trigger"
"github.com/ovh/cds/sdk"
Expand Down Expand Up @@ -95,7 +97,7 @@ func LoadPipelineByID(ctx context.Context, db gorp.SqlExecutor, pipelineID int64
}

// LoadByWorkerModelName loads pipelines from database for a given worker model name
func LoadByWorkerModelName(db gorp.SqlExecutor, workerModelName string) ([]sdk.Pipeline, error) {
func LoadByWorkerModelName(db gorp.SqlExecutor, workerModelName string, u *sdk.User) ([]sdk.Pipeline, error) {
var pips []sdk.Pipeline
query := `
SELECT DISTINCT pipeline.*, project.projectkey AS projectKey
Expand All @@ -105,8 +107,30 @@ func LoadByWorkerModelName(db gorp.SqlExecutor, workerModelName string) ([]sdk.P
JOIN pipeline ON pipeline.id = pipeline_stage.pipeline_id
JOIN project ON project.id = pipeline.project_id
WHERE action_requirement.type = 'model' AND action_requirement.value = $1`
args := []interface{}{workerModelName}

if _, err := db.Select(&pips, query, workerModelName); err != nil {
if !u.Admin {
query = `
SELECT DISTINCT pipeline.*, project.projectkey AS projectKey
FROM action_requirement
JOIN pipeline_action ON action_requirement.action_id = pipeline_action.action_id
JOIN pipeline_stage ON pipeline_action.pipeline_stage_id = pipeline_stage.id
JOIN pipeline ON pipeline.id = pipeline_stage.pipeline_id
JOIN project ON project.id = pipeline.project_id
WHERE action_requirement.type = 'model'
AND action_requirement.value = $1
AND project.id IN (
SELECT project_group.project_id
FROM project_group
WHERE
project_group.group_id = ANY(string_to_array($2, ',')::int[])
OR
$3 = ANY(string_to_array($2, ',')::int[])
)`
args = append(args, gorpmapping.IDsToQueryString(sdk.GroupsToIDs(u.Groups)), group.SharedInfraGroup.ID)
}

if _, err := db.Select(&pips, query, args...); err != nil {
if err == sql.ErrNoRows {
return nil, nil
}
Expand Down
2 changes: 1 addition & 1 deletion engine/api/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ func (api *API) getTemplateUsageHandler() service.Handler {
}
wfTmpl := getWorkflowTemplate(ctx)

wfs, err := workflow.LoadByWorkflowTemplateID(ctx, api.mustDB(), wfTmpl.ID)
wfs, err := workflow.LoadByWorkflowTemplateID(ctx, api.mustDB(), wfTmpl.ID, getUser(ctx))
if err != nil {
return sdk.WrapError(err, "Cannot load templates")
}
Expand Down
6 changes: 3 additions & 3 deletions engine/api/worker_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,9 +418,9 @@ func (api *API) getWorkerModel(w http.ResponseWriter, r *http.Request, name stri

func (api *API) getWorkerModelUsageHandler() service.Handler {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
workerModelID, errr := requestVarInt(r, "permModelID")
workerModelID, errr := requestVarInt(r, "modelID")
if errr != nil {
return sdk.WrapError(errr, "Invalid permModelID")
return sdk.WrapError(errr, "Invalid modelID")
}
db := api.mustDB()

Expand All @@ -429,7 +429,7 @@ func (api *API) getWorkerModelUsageHandler() service.Handler {
return sdk.WrapError(err, "cannot load worker model for id %d", workerModelID)
}

pips, errP := pipeline.LoadByWorkerModelName(db, wm.Name)
pips, errP := pipeline.LoadByWorkerModelName(db, wm.Name, getUser(ctx))
if errP != nil {
return sdk.WrapError(errP, "Cannot load pipelines linked to worker model")
}
Expand Down
8 changes: 4 additions & 4 deletions engine/api/worker_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func Test_WorkerModelUsage(t *testing.T) {
test.NotNil(t, gr)

model := sdk.Model{
Name: "Test1",
Name: "Test1" + grName,
GroupID: gr.ID,
Type: sdk.Docker,
ModelDocker: sdk.ModelDocker{
Expand Down Expand Up @@ -180,9 +180,9 @@ func Test_WorkerModelUsage(t *testing.T) {
Enabled: true,
Requirements: []sdk.Requirement{
sdk.Requirement{
Name: "Test1",
Name: "Test1" + grName,
Type: sdk.ModelRequirement,
Value: "Test1",
Value: "Test1" + grName,
},
},
},
Expand Down Expand Up @@ -214,7 +214,7 @@ func Test_WorkerModelUsage(t *testing.T) {

//Prepare request
vars := map[string]string{
"permModelID": fmt.Sprintf("%d", model.ID),
"modelID": fmt.Sprintf("%d", model.ID),
}
uri := router.GetRoute("GET", api.getWorkerModelUsageHandler, vars)
test.NotEmpty(t, uri)
Expand Down
27 changes: 25 additions & 2 deletions engine/api/workflow/dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/ovh/cds/engine/api/database/gorpmapping"
"github.com/ovh/cds/engine/api/environment"
"github.com/ovh/cds/engine/api/event"
"github.com/ovh/cds/engine/api/group"
"github.com/ovh/cds/engine/api/keys"
"github.com/ovh/cds/engine/api/observability"
"github.com/ovh/cds/engine/api/permission"
Expand Down Expand Up @@ -410,15 +411,37 @@ func LoadByEnvName(db gorp.SqlExecutor, projectKey string, envName string) ([]sd
}

// LoadByWorkflowTemplateID load all workflows linked to a workflow template but without loading workflow details
func LoadByWorkflowTemplateID(ctx context.Context, db gorp.SqlExecutor, templateID int64) ([]sdk.Workflow, error) {
func LoadByWorkflowTemplateID(ctx context.Context, db gorp.SqlExecutor, templateID int64, u *sdk.User) ([]sdk.Workflow, error) {
var dbRes []Workflow
query := `
SELECT workflow.*
FROM workflow
JOIN workflow_template_instance ON workflow_template_instance.workflow_id = workflow.id
WHERE workflow_template_instance.workflow_template_id = $1 AND workflow.to_delete = false
`
if _, err := db.Select(&dbRes, query, templateID); err != nil {
args := []interface{}{templateID}

if !u.Admin {
query = `
SELECT workflow.*
FROM workflow
JOIN workflow_template_instance ON workflow_template_instance.workflow_id = workflow.id
JOIN project ON workflow.project_id = project.id
WHERE workflow_template_instance.workflow_template_id = $1
AND workflow.to_delete = false
AND project.id IN (
SELECT project_group.project_id
FROM project_group
WHERE
project_group.group_id = ANY(string_to_array($2, ',')::int[])
OR
$3 = ANY(string_to_array($2, ',')::int[])
)
`
args = append(args, gorpmapping.IDsToQueryString(sdk.GroupsToIDs(u.Groups)), group.SharedInfraGroup.ID)
}

if _, err := db.Select(&dbRes, query, args...); err != nil {
if err == sql.ErrNoRows {
return nil, nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ <h3>{{ "common_last_modified" | translate }}</h3>
<div class="ui active centered inline loader" *ngIf="loadingUsage"></div>
<ng-container *ngIf="!loadingUsage">
<app-usage [workflows]="workflowsLinked"></app-usage>
<div class="centered" *ngIf="!workflowsLinked || workflowsLinked.length === 0">{{'workflow_template_no_usage' | translate}}</div>
</ng-container>
</div>
</div>
Expand Down
1 change: 1 addition & 0 deletions ui/src/assets/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1059,6 +1059,7 @@
"workflow_template_diff_application": "Application {{number}}",
"workflow_template_diff_environment": "Environment {{number}}",
"workflow_template_change_message": "Add an optional description of your changes...",
"workflow_template_no_usage": "This template isn't used on one of your workflow",

"workflow_repository_help_line_1": "Your workflow was not imported from your code.",
"workflow_repository_help_line_2": "Manage your workflow as code from your repository to automatically update it with changes on your branches."
Expand Down
1 change: 1 addition & 0 deletions ui/src/assets/i18n/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -1059,6 +1059,7 @@
"workflow_template_diff_application": "Application {{number}}",
"workflow_template_diff_environment": "Environnement {{number}}",
"workflow_template_change_message": "Ajoutez une description optionelle de vos changements...",
"workflow_template_no_usage": "Ce modèle de workflow n'est pas utilisé sur l'un de vos workflows",

"workflow_repository_help_line_1": "Votre workflow n'a pas été importé depuis votre code.",
"workflow_repository_help_line_2": "Gérez votre workflow \"as code\" depuis votre gestionnaire de dépôt pour modifier automatiquement celui-ci avec les changements sur vos branches."
Expand Down

0 comments on commit 50cbfb8

Please sign in to comment.