Skip to content

Commit

Permalink
feat(api,ui): add custom step name (#3259)
Browse files Browse the repository at this point in the history
close #1408
Signed-off-by: Benjamin Coenen <benjamin.coenen@corp.ovh.com>
  • Loading branch information
bnjjj authored and yesnault committed Aug 24, 2018
1 parent 919dc31 commit a209ade
Show file tree
Hide file tree
Showing 17 changed files with 149 additions and 40 deletions.
23 changes: 16 additions & 7 deletions engine/api/action/children.go
Expand Up @@ -10,11 +10,11 @@ import (
"github.com/ovh/cds/sdk/log"
)

func insertEdge(db gorp.SqlExecutor, parentID, childID int64, execOrder int, optional, alwaysExecuted, enabled bool) (int64, error) {
query := `INSERT INTO action_edge (parent_id, child_id, exec_order, optional, always_executed, enabled) VALUES ($1, $2, $3, $4, $5, $6) RETURNING id`
func insertEdge(db gorp.SqlExecutor, parentID, childID int64, execOrder int, stepName string, optional, alwaysExecuted, enabled bool) (int64, error) {
query := `INSERT INTO action_edge (parent_id, child_id, exec_order, step_name, optional, always_executed, enabled) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING id`

var id int64
err := db.QueryRow(query, parentID, childID, execOrder, optional, alwaysExecuted, enabled).Scan(&id)
err := db.QueryRow(query, parentID, childID, execOrder, stepName, optional, alwaysExecuted, enabled).Scan(&id)
if err != nil {
return 0, err
}
Expand All @@ -27,7 +27,12 @@ func insertActionChild(db gorp.SqlExecutor, actionID int64, child sdk.Action, ex
return fmt.Errorf("insertActionChild: child action has no id")
}

id, err := insertEdge(db, actionID, child.ID, execOrder, child.Optional, child.AlwaysExecuted, child.Enabled)
//Useful to not save a step_name if it's the same than the default name (for ascode)
if strings.ToLower(child.Name) == strings.ToLower(child.StepName) {
child.StepName = ""
}

id, err := insertEdge(db, actionID, child.ID, execOrder, child.StepName, child.Optional, child.AlwaysExecuted, child.Enabled)
if err != nil {
return err
}
Expand All @@ -54,7 +59,7 @@ func insertChildActionParameter(db gorp.SqlExecutor, edgeID, parentID, childID i
name,
type,
value,
description,
description,
advanced) VALUES ($1, $2, $3, $4, $5, $6)`

if _, err := db.Exec(query, edgeID, param.Name, string(param.Type), param.Value, param.Description, param.Advanced); err != nil {
Expand All @@ -68,7 +73,7 @@ func loadActionChildren(db gorp.SqlExecutor, actionID int64) ([]sdk.Action, erro
var children []sdk.Action
var edgeIDs []int64
var childrenIDs []int64
query := `SELECT id, child_id, exec_order, optional, always_executed, enabled FROM action_edge WHERE parent_id = $1 ORDER BY exec_order ASC`
query := `SELECT id, child_id, exec_order, step_name, optional, always_executed, enabled FROM action_edge WHERE parent_id = $1 ORDER BY exec_order ASC`

rows, err := db.Query(query, actionID)
if err != nil {
Expand All @@ -78,18 +83,21 @@ func loadActionChildren(db gorp.SqlExecutor, actionID int64) ([]sdk.Action, erro

var edgeID, childID int64
var execOrder int
var stepName string
var optional, alwaysExecuted, enabled bool
var mapStepName = make(map[int64]string)
var mapOptional = make(map[int64]bool)
var mapAlwaysExecuted = make(map[int64]bool)
var mapEnabled = make(map[int64]bool)

for rows.Next() {
err = rows.Scan(&edgeID, &childID, &execOrder, &optional, &alwaysExecuted, &enabled)
err = rows.Scan(&edgeID, &childID, &execOrder, &stepName, &optional, &alwaysExecuted, &enabled)
if err != nil {
return nil, err
}
edgeIDs = append(edgeIDs, edgeID)
childrenIDs = append(childrenIDs, childID)
mapStepName[edgeID] = stepName
mapOptional[edgeID] = optional
mapAlwaysExecuted[edgeID] = alwaysExecuted
mapEnabled[edgeID] = enabled
Expand All @@ -114,6 +122,7 @@ func loadActionChildren(db gorp.SqlExecutor, actionID int64) ([]sdk.Action, erro
// If child action has been modified, new parameters will show
// and delete one won't be there anymore
replaceChildActionParameters(&children[i], params)
children[i].StepName = mapStepName[edgeIDs[i]]
// Get optional & always_executed flags
children[i].Optional = mapOptional[edgeIDs[i]]
children[i].AlwaysExecuted = mapAlwaysExecuted[edgeIDs[i]]
Expand Down
10 changes: 3 additions & 7 deletions engine/api/pipeline/pipeline_action.go
Expand Up @@ -118,9 +118,7 @@ func UpdateJob(db gorp.SqlExecutor, job *sdk.Job, userID int64) error {
return sdk.ErrForbidden
}

query := `UPDATE pipeline_action set action_id=$1, pipeline_stage_id=$2, enabled=$4 WHERE id=$3`
_, err = db.Exec(query, job.Action.ID, job.PipelineStageID, job.PipelineActionID, job.Enabled)
if err != nil {
if err := UpdatePipelineAction(db, *job); err != nil {
return err
}
job.Action.Enabled = job.Enabled
Expand All @@ -134,10 +132,8 @@ func DeleteJob(db gorp.SqlExecutor, job sdk.Job, userID int64) error {

// UpdatePipelineAction Update an action in a pipeline
func UpdatePipelineAction(db gorp.SqlExecutor, job sdk.Job) error {
query := `UPDATE pipeline_action set action_id=$1, pipeline_stage_id=$2, enabled=$4 WHERE id=$3`

_, err := db.Exec(query, job.Action.ID, job.PipelineStageID, job.PipelineActionID, job.Enabled)
if err != nil {
query := `UPDATE pipeline_action set action_id=$1, pipeline_stage_id=$2, enabled=$3 WHERE id=$4`
if _, err := db.Exec(query, job.Action.ID, job.PipelineStageID, job.Enabled, job.PipelineActionID); err != nil {
return err
}

Expand Down
8 changes: 4 additions & 4 deletions engine/api/pipeline/pipeline_stage.go
Expand Up @@ -104,21 +104,21 @@ func LoadPipelineStage(ctx context.Context, db gorp.SqlExecutor, p *sdk.Pipeline
}

query := `
SELECT pipeline_stage_R.id as stage_id, pipeline_stage_R.pipeline_id, pipeline_stage_R.name, pipeline_stage_R.last_modified,
SELECT pipeline_stage_R.id as stage_id, pipeline_stage_R.pipeline_id, pipeline_stage_R.name, pipeline_stage_R.last_modified,
pipeline_stage_R.build_order, pipeline_stage_R.enabled, pipeline_stage_R.parameter,
pipeline_stage_R.expected_value, pipeline_action_R.id as pipeline_action_id, pipeline_action_R.action_id, pipeline_action_R.action_last_modified,
pipeline_action_R.action_args, pipeline_action_R.action_enabled
FROM (
SELECT pipeline_stage.id, pipeline_stage.pipeline_id,
pipeline_stage.name, pipeline_stage.last_modified ,pipeline_stage.build_order,
SELECT pipeline_stage.id, pipeline_stage.pipeline_id,
pipeline_stage.name, pipeline_stage.last_modified, pipeline_stage.build_order,
pipeline_stage.enabled,
pipeline_stage_prerequisite.parameter, pipeline_stage_prerequisite.expected_value
FROM pipeline_stage
LEFT OUTER JOIN pipeline_stage_prerequisite ON pipeline_stage.id = pipeline_stage_prerequisite.pipeline_stage_id
WHERE pipeline_id = $1
) as pipeline_stage_R
LEFT OUTER JOIN (
SELECT pipeline_action.id, action.id as action_id, action.name as action_name, action.last_modified as action_last_modified,
SELECT pipeline_action.id, action.id as action_id, action.name as action_name, action.last_modified as action_last_modified,
pipeline_action.args as action_args, pipeline_action.enabled as action_enabled,
pipeline_action.pipeline_stage_id
FROM action
Expand Down
2 changes: 1 addition & 1 deletion engine/api/workflow/process.go
Expand Up @@ -623,7 +623,7 @@ func processWorkflowNodeRun(ctx context.Context, db gorp.SqlExecutor, store cach
// Tag VCS infos : add in tag only if it does not exist
if !w.TagExists(tagGitRepository) {
w.Tag(tagGitRepository, run.VCSRepository)
if run.VCSBranch != "" {
if run.VCSBranch != "" && run.VCSTag == "" {
w.Tag(tagGitBranch, run.VCSBranch)
}
if run.VCSTag != "" {
Expand Down
2 changes: 1 addition & 1 deletion engine/api/workflow/resync_workflow.go
Expand Up @@ -135,7 +135,7 @@ func ResyncNodeRunsWithCommits(ctx context.Context, db gorp.SqlExecutor, store c
}

tagsUpdated := false
if curVCSInfos.Branch != "" {
if curVCSInfos.Branch != "" && curVCSInfos.Tag == "" {
tagsUpdated = wr.Tag(tagGitBranch, curVCSInfos.Branch)
}
if curVCSInfos.Hash != "" {
Expand Down
5 changes: 5 additions & 0 deletions engine/sql/123_action_custom_name.sql
@@ -0,0 +1,5 @@
-- +migrate Up
ALTER TABLE action_edge ADD COLUMN step_name TEXT DEFAULT '';

-- +migrate Down
ALTER TABLE action_edge DROP COLUMN step_name;
1 change: 1 addition & 0 deletions sdk/action.go
Expand Up @@ -10,6 +10,7 @@ import (
type Action struct {
ID int64 `json:"id" yaml:"-"`
Name string `json:"name" cli:"name"`
StepName string `json:"step_name,omitempty" yaml:"step_name,omitempty" cli:"step_name"`
Type string `json:"type" yaml:"-" cli:"type"`
Description string `json:"description" yaml:"desc,omitempty"`
Requirements []Requirement `json:"requirements"`
Expand Down
51 changes: 51 additions & 0 deletions sdk/exportentities/action.go
Expand Up @@ -62,6 +62,9 @@ func newSteps(a sdk.Action) []Step {
for i := range a.Actions {
act := &a.Actions[i]
s := Step{}
if act.StepName != "" {
s["name"] = act.StepName
}
if !act.Enabled {
s["enabled"] = act.Enabled
}
Expand Down Expand Up @@ -267,6 +270,10 @@ func (s Step) AsScript() (*sdk.Action, bool, error) {
a := sdk.NewStepScript(bS)

var err error
a.StepName, err = s.Name()
if err != nil {
return nil, true, err
}
a.Enabled, err = s.IsFlagged("enabled")
if err != nil {
return nil, true, err
Expand Down Expand Up @@ -311,6 +318,10 @@ func (s Step) AsAction() (*sdk.Action, bool, error) {
}

a.Enabled, err = s.IsFlagged("enabled")
a.StepName, err = s.Name()
if err != nil {
return nil, true, err
}
if err != nil {
return nil, true, err
}
Expand Down Expand Up @@ -344,6 +355,10 @@ func (s Step) AsJUnitReport() (*sdk.Action, bool, error) {
a := sdk.NewStepJUnitReport(bS)

var err error
a.StepName, err = s.Name()
if err != nil {
return nil, true, err
}
a.Enabled, err = s.IsFlagged("enabled")
if err != nil {
return nil, true, err
Expand Down Expand Up @@ -383,6 +398,10 @@ func (s Step) AsGitClone() (*sdk.Action, bool, error) {
a := sdk.NewStepGitClone(argss)

var err error
a.StepName, err = s.Name()
if err != nil {
return nil, true, err
}
a.Enabled, err = s.IsFlagged("enabled")
if err != nil {
return nil, true, err
Expand Down Expand Up @@ -428,6 +447,10 @@ func (s Step) AsArtifactUpload() (*sdk.Action, bool, error) {
}

var err error
a.StepName, err = s.Name()
if err != nil {
return nil, true, err
}
a.Enabled, err = s.IsFlagged("enabled")
if err != nil {
return nil, true, err
Expand Down Expand Up @@ -462,6 +485,10 @@ func (s Step) AsArtifactDownload() (*sdk.Action, bool, error) {
a := sdk.NewStepArtifactDownload(argss)

var err error
a.StepName, err = s.Name()
if err != nil {
return nil, true, err
}
a.Enabled, err = s.IsFlagged("enabled")
if err != nil {
return nil, true, err
Expand Down Expand Up @@ -495,6 +522,10 @@ func (s Step) AsCheckoutApplication() (*sdk.Action, bool, error) {
a := sdk.NewCheckoutApplication(bS)

var err error
a.StepName, err = s.Name()
if err != nil {
return nil, true, err
}
a.Enabled, err = s.IsFlagged("enabled")
if err != nil {
return nil, true, err
Expand Down Expand Up @@ -528,6 +559,10 @@ func (s Step) AsCoverageAction() (*sdk.Action, bool, error) {
a := sdk.NewCoverage(argss)

var err error
a.StepName, err = s.Name()
if err != nil {
return nil, true, err
}
a.Enabled, err = s.IsFlagged("enabled")
if err != nil {
return nil, true, err
Expand Down Expand Up @@ -561,6 +596,10 @@ func (s Step) AsDeployApplication() (*sdk.Action, bool, error) {
a := sdk.NewDeployApplication(bS)

var err error
a.StepName, err = s.Name()
if err != nil {
return nil, true, err
}
a.Enabled, err = s.IsFlagged("enabled")
if err != nil {
return nil, true, err
Expand Down Expand Up @@ -591,6 +630,18 @@ func (s Step) IsFlagged(flag string) (bool, error) {
return bS, nil
}

// Name returns true the step name if exist
func (s Step) Name() (string, error) {
if stepAttr, ok := s["name"]; ok {
if stepName, okName := stepAttr.(string); okName {
return stepName, nil
} else {
return "", fmt.Errorf("Malformatted Step : name must be a string")
}
}
return "", nil
}

// Action returns an sdk.Action
func (act *Action) Action() (*sdk.Action, error) {
a := new(sdk.Action)
Expand Down
4 changes: 2 additions & 2 deletions sdk/exportentities/pipeline.go
Expand Up @@ -68,7 +68,7 @@ type Step map[string]interface{}
func (s Step) IsValid() bool {
keys := []string{}
for k := range s {
if k != "enabled" && k != "optional" && k != "always_executed" {
if k != "enabled" && k != "optional" && k != "always_executed" && k != "name" {
keys = append(keys, k)
}
}
Expand All @@ -78,7 +78,7 @@ func (s Step) IsValid() bool {
func (s Step) key() string {
keys := []string{}
for k := range s {
if k != "enabled" && k != "optional" && k != "always_executed" {
if k != "enabled" && k != "optional" && k != "always_executed" && k != "name" {
keys = append(keys, k)
}
}
Expand Down
24 changes: 24 additions & 0 deletions sdk/workflow_run_easyjson.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion tests/fixtures/action_disabled.yml
Expand Up @@ -4,7 +4,8 @@ enabled: false
requirements:
- binary: bash
steps:
- script:
- name: my empty script
script:
- '#!/bin/bash'
- set -e
- ""
Expand Down
3 changes: 2 additions & 1 deletion tests/fixtures/action_git_clone.yml
Expand Up @@ -6,7 +6,8 @@ parameters:
requirements:
- binary: git
steps:
- gitClone:
- name: 'gitClone'
gitClone:
branch: '{{.git.branch}}'
commit: '{{.git.hash}}'
depth: "10"
Expand Down
1 change: 1 addition & 0 deletions ui/src/app/model/action.model.ts
Expand Up @@ -4,6 +4,7 @@ import {Requirement} from './requirement.model';
export class Action {
id: number;
name: string;
step_name: string;
type: string;
description = '';
requirements: Array<Requirement>;
Expand Down
1 change: 1 addition & 0 deletions ui/src/app/shared/action/step/step.component.ts
Expand Up @@ -18,6 +18,7 @@ export class ActionStepComponent {
this._step = step;
if (step && step.parameters) {
this.withAdvanced = step.parameters.some((parameter) => parameter.advanced);
this._step.step_name = this._step.step_name || this._step.name;
}
}
get step(): Action {
Expand Down

0 comments on commit a209ade

Please sign in to comment.