Skip to content

Commit

Permalink
feat(cicd): generate deployment in cicd plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
leric committed Feb 20, 2024
1 parent 496d567 commit cbb42c5
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 42 deletions.
47 changes: 31 additions & 16 deletions backend/plugins/dora/tasks/deployment_commits_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func GenerateDeploymentCommits(taskCtx plugin.SubTaskContext) errors.Error {
// select all cicd_pipeline_commits from all "Deployments" in the project
// Note that failed records shall be included as well
noneSkippedResult := []string{devops.RESULT_FAILURE, devops.RESULT_SUCCESS}
cursor, err := db.Cursor(
var clauses = []dal.Clause{
dal.Select(
`
pc.*,
Expand All @@ -77,35 +77,50 @@ func GenerateDeploymentCommits(taskCtx plugin.SubTaskContext) errors.Error {
p.cicd_scope_id,
p.original_status,
p.original_result,
EXISTS(SELECT 1 FROM cicd_tasks t WHERE t.pipeline_id = p.id AND t.environment = ? AND t.result IN ?)
as has_testing_tasks,
EXISTS(SELECT 1 FROM cicd_tasks t WHERE t.pipeline_id = p.id AND t.environment = ? AND t.result IN ?)
as has_staging_tasks,
EXISTS( SELECT 1 FROM cicd_tasks t WHERE t.pipeline_id = p.id AND t.environment = ? AND t.result IN ?)
as has_production_tasks
EXISTS(SELECT 1 FROM cicd_tasks t WHERE t.pipeline_id = p.id AND t.environment = ? AND t.result IN ?) as has_testing_tasks,
EXISTS(SELECT 1 FROM cicd_tasks t WHERE t.pipeline_id = p.id AND t.environment = ? AND t.result IN ?) as has_staging_tasks,
EXISTS( SELECT 1 FROM cicd_tasks t WHERE t.pipeline_id = p.id AND t.environment = ? AND t.result IN ?) as has_production_tasks
`,
devops.TESTING, noneSkippedResult,
devops.STAGING, noneSkippedResult,
devops.PRODUCTION, noneSkippedResult,
),
dal.From("cicd_pipeline_commits pc"),
dal.Join("LEFT JOIN cicd_pipelines p ON (p.id = pc.pipeline_id)"),
dal.Join("LEFT JOIN project_mapping pm ON (pm.table = 'cicd_scopes' AND pm.row_id = p.cicd_scope_id)"),
dal.Where(
}
if data.Options.ScopeId != nil {
clauses = append(clauses, dal.Where(
`
pm.project_name = ? AND (
p.type = ? OR EXISTS(
SELECT 1 FROM cicd_tasks t WHERE t.pipeline_id = p.id AND t.type = ? AND t.result IN ?
)
p.cicd_scope_id = ? AND (
p.type = ? OR EXISTS(SELECT 1 FROM cicd_tasks t WHERE t.pipeline_id = p.id AND t.type = ? AND t.result IN ?)
) AND p.result IN ?
`,
data.Options.ProjectName,
data.Options.ScopeId,
devops.DEPLOYMENT,
devops.DEPLOYMENT,
noneSkippedResult,
noneSkippedResult,
),
)
))
} else {
clauses = append(clauses,
dal.Join("LEFT JOIN project_mapping pm ON (pm.table = 'cicd_scopes' AND pm.row_id = p.cicd_scope_id)"),
dal.Where(
`
pm.project_name = ? AND (
p.type = ? OR EXISTS(
SELECT 1 FROM cicd_tasks t WHERE t.pipeline_id = p.id AND t.type = ? AND t.result IN ?
)
) AND p.result IN ?
`,
data.Options.ProjectName,
devops.DEPLOYMENT,
devops.DEPLOYMENT,
noneSkippedResult,
noneSkippedResult,
),
)
}
cursor, err := db.Cursor(clauses...)
if err != nil {
return err
}
Expand Down
44 changes: 32 additions & 12 deletions backend/plugins/dora/tasks/deployment_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,38 +49,58 @@ func GenerateDeployment(taskCtx plugin.SubTaskContext) errors.Error {
data := taskCtx.GetData().(*DoraTaskData)
// Note that failed records shall be included as well
noneSkippedResult := []string{devops.RESULT_FAILURE, devops.RESULT_SUCCESS}
cursor, err := db.Cursor(
var clauses = []dal.Clause{
dal.Select(
`
p.*,
EXISTS(SELECT 1 FROM cicd_tasks t WHERE t.pipeline_id = p.id AND t.environment = ? AND t.result IN ?)
as has_testing_tasks,
EXISTS(SELECT 1 FROM cicd_tasks t WHERE t.pipeline_id = p.id AND t.environment = ? AND t.result IN ?)
as has_staging_tasks,
EXISTS( SELECT 1 FROM cicd_tasks t WHERE t.pipeline_id = p.id AND t.environment = ? AND t.result IN ?)
EXISTS(SELECT 1 FROM cicd_tasks t WHERE t.pipeline_id = p.id AND t.environment = ? AND t.result IN ?)
as has_production_tasks
`,
devops.TESTING, noneSkippedResult,
devops.STAGING, noneSkippedResult,
devops.PRODUCTION, noneSkippedResult,
),
dal.From("cicd_pipelines p"),
dal.Join("LEFT JOIN project_mapping pm ON (pm.table = 'cicd_scopes' AND pm.row_id = p.cicd_scope_id)"),
dal.Where(
`
}
if data.Options.ScopeId != nil {
clauses = append(clauses,
dal.Where(
`
p.cicd_scope_id = ? AND (
p.type = ? OR EXISTS(SELECT 1 FROM cicd_tasks t WHERE t.pipeline_id = p.id AND t.type = ? AND t.result IN ?)
) AND p.result IN ?
`,
data.Options.ScopeId,
devops.DEPLOYMENT,
devops.DEPLOYMENT,
noneSkippedResult,
noneSkippedResult,
),
)
} else {
clauses = append(clauses,
dal.Join("LEFT JOIN project_mapping pm ON (pm.table = 'cicd_scopes' AND pm.row_id = p.cicd_scope_id)"),
dal.Where(
`
pm.project_name = ? AND (
p.type = ? OR EXISTS(
SELECT 1 FROM cicd_tasks t WHERE t.pipeline_id = p.id AND t.type = ? AND t.result IN ?
)
) AND p.result IN ?
`,
data.Options.ProjectName,
devops.DEPLOYMENT,
devops.DEPLOYMENT,
noneSkippedResult,
noneSkippedResult,
),
)
data.Options.ProjectName,
devops.DEPLOYMENT,
devops.DEPLOYMENT,
noneSkippedResult,
noneSkippedResult,
),
)
}
cursor, err := db.Cursor(clauses...)
if err != nil {
return err
}
Expand Down
48 changes: 35 additions & 13 deletions backend/plugins/dora/tasks/prev_deployment_commit_enricher.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,43 @@ func EnrichPrevSuccessDeploymentCommit(taskCtx plugin.SubTaskContext) errors.Err
data := taskCtx.GetData().(*DoraTaskData)
// step 1. select all successful deployments in the project and sort them by cicd_scope_id, repo_url, env
// and finished_date
cursor, err := db.Cursor(
var clauses = []dal.Clause{
dal.Select("dc.*"),
dal.From("cicd_deployment_commits dc"),
dal.Join("LEFT JOIN project_mapping pm ON (pm.table = 'cicd_scopes' AND pm.row_id = dc.cicd_scope_id)"),
dal.Where(
`
dc.finished_date IS NOT NULL
AND dc.environment IS NOT NULL AND dc.environment != ''
AND dc.repo_url IS NOT NULL AND dc.repo_url != ''
AND pm.project_name = ? AND dc.result = ?
`,
data.Options.ProjectName, devops.RESULT_SUCCESS,
),
dal.Orderby(`dc.cicd_scope_id, dc.repo_url, dc.environment, dc.finished_date`),
)
}
if data.Options.ScopeId != nil {
clauses = append(clauses,
dal.Where(
`
dc.finished_date IS NOT NULL
AND dc.environment IS NOT NULL
AND dc.environment != ''
AND dc.repo_url IS NOT NULL
AND dc.repo_url != ''
AND dc.cicd_scope_id = ?
AND dc.result = ?
`,
data.Options.ScopeId, devops.RESULT_SUCCESS,
),
dal.Orderby(`dc.cicd_scope_id, dc.repo_url, dc.environment, dc.finished_date`),
)
} else {
clauses = append(clauses,
dal.Join("LEFT JOIN project_mapping pm ON (pm.table = 'cicd_scopes' AND pm.row_id = dc.cicd_scope_id)"),
dal.Where(
`
dc.finished_date IS NOT NULL
AND dc.environment IS NOT NULL AND dc.environment != ''
AND dc.repo_url IS NOT NULL AND dc.repo_url != ''
AND pm.project_name = ? AND dc.result = ?
`,
data.Options.ProjectName, devops.RESULT_SUCCESS,
),
dal.Orderby(`dc.repo_url, dc.environment, dc.finished_date`),
)
}

cursor, err := db.Cursor(clauses...)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion backend/plugins/dora/tasks/task_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ type DoraApiParams struct {
type DoraOptions struct {
Tasks []string `json:"tasks,omitempty"`
Since string
ProjectName string `json:"projectName"`
ProjectName string `json:"projectName"`
ScopeId *string `json:"scopeId,omitempty"`
}

type DoraTaskData struct {
Expand Down

0 comments on commit cbb42c5

Please sign in to comment.