Skip to content

Commit 31d1a94

Browse files
Merge pull request #18 from erlandf/add-steps-support
feat: add steps from pipelineactivity
2 parents c202777 + a2680cd commit 31d1a94

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

collector/pipeline_activity.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,20 @@ func (c *PipelineActivityCollector) Start(ctx context.Context) error { // nolint
4848

4949
return nil
5050
}
51+
func SimplifyStep(coreStep jenkinsv1.CoreActivityStep) store.SimplifiedActivityStep {
5152

53+
if coreStep.Status == "" || coreStep.StartedTimestamp == nil || coreStep.CompletedTimestamp == nil {
54+
return store.SimplifiedActivityStep{}
55+
}
56+
57+
return store.SimplifiedActivityStep{
58+
Name: coreStep.Name,
59+
Status: coreStep.Status.String(),
60+
StartedTimestamp: coreStep.StartedTimestamp.Time,
61+
CompletedTimestamp: coreStep.CompletedTimestamp.Time,
62+
Duration: coreStep.CompletedTimestamp.Time.Sub(coreStep.StartedTimestamp.Time),
63+
}
64+
}
5265
func (c *PipelineActivityCollector) storePipeline(pa *jenkinsv1.PipelineActivity) {
5366
if pa == nil {
5467
return
@@ -79,6 +92,38 @@ func (c *PipelineActivityCollector) storePipeline(pa *jenkinsv1.PipelineActivity
7992
return
8093
}
8194

95+
var simplifiedSteps []store.SimplifiedActivityStep
96+
for _, step := range pa.Spec.Steps {
97+
log.WithField("step", step.Kind).Trace("Simplifying step")
98+
if step.Kind == "Stage" {
99+
for _, stageStep := range step.Stage.Steps {
100+
simplifiedStep := SimplifyStep(stageStep)
101+
if simplifiedStep.Name == "" {
102+
log.WithField("step", step.Kind).Trace("Ignoring empty step")
103+
} else {
104+
simplifiedSteps = append(simplifiedSteps, simplifiedStep)
105+
}
106+
}
107+
simplifiedSteps = append(simplifiedSteps, SimplifyStep(step.Stage.CoreActivityStep))
108+
}
109+
if step.Kind == "Promote" {
110+
simplifiedStep := SimplifyStep(step.Promote.CoreActivityStep)
111+
if simplifiedStep.Name == "" {
112+
log.WithField("step", step.Kind).Trace("Ignoring empty step")
113+
} else {
114+
simplifiedSteps = append(simplifiedSteps, simplifiedStep)
115+
}
116+
}
117+
if step.Kind == "Preview" {
118+
simplifiedStep := SimplifyStep(step.Preview.CoreActivityStep)
119+
if simplifiedStep.Name == "" {
120+
log.WithField("step", step.Kind).Trace("Ignoring empty step")
121+
} else {
122+
simplifiedSteps = append(simplifiedSteps, simplifiedStep)
123+
}
124+
}
125+
}
126+
log.WithField("steps", len(simplifiedSteps)).Trace("Simplified steps")
82127
pipeline := store.Pipeline{
83128
Owner: pa.Spec.GitOwner,
84129
Repository: pa.Spec.GitRepository,
@@ -87,6 +132,7 @@ func (c *PipelineActivityCollector) storePipeline(pa *jenkinsv1.PipelineActivity
87132
Author: pa.Spec.Author,
88133
StartTime: pa.Spec.StartedTimestamp.Time.In(time.UTC),
89134
EndTime: pa.Spec.CompletedTimestamp.Time.In(time.UTC),
135+
Steps: simplifiedSteps,
90136
}
91137
pipeline.Duration = pipeline.EndTime.Sub(pipeline.StartTime)
92138

store/pipeline.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ import (
1212

1313
type PipelineType string
1414

15+
type SimplifiedActivityStep struct {
16+
Name string
17+
Status string
18+
StartedTimestamp time.Time
19+
CompletedTimestamp time.Time
20+
Duration time.Duration
21+
}
22+
1523
const (
1624
PipelineTypeRelease = PipelineType("release")
1725
PipelineTypePullRequest = PipelineType("pullrequest")
@@ -29,6 +37,7 @@ type Pipeline struct {
2937
StartTime time.Time
3038
EndTime time.Time
3139
Duration time.Duration
40+
Steps []SimplifiedActivityStep
3241
}
3342

3443
type PipelineStore struct {
@@ -56,6 +65,20 @@ func (s *PipelineStore) Migrations() []migration.Func {
5665
duration bigint NOT NULL,
5766
CONSTRAINT pipeline_pkey PRIMARY KEY (type, owner, repository, pull_request, context, build)
5867
);
68+
CREATE TABLE pipelinesteps (
69+
type VARCHAR NOT NULL,
70+
owner VARCHAR NOT NULL,
71+
repository VARCHAR NOT NULL,
72+
pull_request int,
73+
context VARCHAR NOT NULL,
74+
build int NOT NULL,
75+
step_name VARCHAR NOT NULL,
76+
step_status VARCHAR NOT NULL,
77+
step_started_time timestamp without time zone NOT NULL,
78+
step_completed_time timestamp without time zone NOT NULL,
79+
step_duration bigint NOT NULL,
80+
CONSTRAINT pipelinesteps_pkey PRIMARY KEY (type, owner, repository, pull_request, context, build, step_name)
81+
);
5982
`),
6083
}
6184
}
@@ -74,6 +97,12 @@ func (s *PipelineStore) Add(ctx context.Context, p Pipeline) error {
7497
return fmt.Errorf("failed to add pipeline: %w", err)
7598
}
7699

100+
for _, step := range p.Steps {
101+
_, err = tx.Exec(ctx, "INSERT INTO pipelinesteps (type, owner, repository, pull_request, context, build, step_name, step_status, step_started_time, step_completed_time, step_duration) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) ON CONFLICT DO NOTHING;", p.Type, p.Owner, p.Repository, p.PullRequest, p.Context, p.Build, step.Name, step.Status, step.StartedTimestamp, step.CompletedTimestamp, step.Duration.Seconds())
102+
if err != nil {
103+
return fmt.Errorf("failed to add pipeline step: %w", err)
104+
}
105+
}
77106
if err = tx.Commit(ctx); err != nil {
78107
return fmt.Errorf("failed to commit insertion of pipeline: %w", err)
79108
}

0 commit comments

Comments
 (0)