Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/app/pipedv1/controller/planner.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
zap.String("deployment-id", d.Id),
zap.String("app-id", d.ApplicationId),
zap.String("project-id", d.ProjectId),
zap.String("app-kind", d.Kind.String()),
zap.String("labels", d.GetLabelsString()),

Check warning on line 108 in pkg/app/pipedv1/controller/planner.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/controller/planner.go#L108

Added line #L108 was not covered by tests
zap.String("working-dir", workingDir),
)

Expand Down Expand Up @@ -173,8 +173,8 @@
"Plan",
trace.WithAttributes(
attribute.String("application-id", p.deployment.ApplicationId),
attribute.String("kind", p.deployment.Kind.String()),
attribute.String("deployment-id", p.deployment.Id),
attribute.String("labels", p.deployment.GetLabelsString()),

Check warning on line 177 in pkg/app/pipedv1/controller/planner.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/controller/planner.go#L177

Added line #L177 was not covered by tests
))
defer span.End()

Expand Down
8 changes: 4 additions & 4 deletions pkg/app/pipedv1/controller/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
zap.String("deployment-id", d.Id),
zap.String("app-id", d.ApplicationId),
zap.String("project-id", d.ProjectId),
zap.String("app-kind", d.Kind.String()),
zap.String("labels", d.GetLabelsString()),

Check warning on line 94 in pkg/app/pipedv1/controller/scheduler.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/controller/scheduler.go#L94

Added line #L94 was not covered by tests
zap.String("working-dir", workingDir),
)

Expand Down Expand Up @@ -265,8 +265,8 @@
"Deploy",
trace.WithAttributes(
attribute.String("application-id", s.deployment.ApplicationId),
attribute.String("kind", s.deployment.Kind.String()),
attribute.String("deployment-id", s.deployment.Id),
attribute.String("labels", s.deployment.GetLabelsString()),

Check warning on line 269 in pkg/app/pipedv1/controller/scheduler.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/controller/scheduler.go#L269

Added line #L269 was not covered by tests
))
defer span.End()

Expand Down Expand Up @@ -325,8 +325,8 @@
go func() {
_, span := s.tracer.Start(ctx, ps.Name, trace.WithAttributes(
attribute.String("application-id", s.deployment.ApplicationId),
attribute.String("kind", s.deployment.Kind.String()),
attribute.String("deployment-id", s.deployment.Id),
attribute.String("labels", s.deployment.GetLabelsString()),

Check warning on line 329 in pkg/app/pipedv1/controller/scheduler.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/controller/scheduler.go#L329

Added line #L329 was not covered by tests
attribute.String("stage-id", ps.Id),
))
defer span.End()
Expand Down Expand Up @@ -435,8 +435,8 @@

_, span := s.tracer.Start(ctx, rbs.Name, trace.WithAttributes(
attribute.String("application-id", s.deployment.ApplicationId),
attribute.String("kind", s.deployment.Kind.String()),
attribute.String("deployment-id", s.deployment.Id),
attribute.String("labels", s.deployment.GetLabelsString()),

Check warning on line 439 in pkg/app/pipedv1/controller/scheduler.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/controller/scheduler.go#L439

Added line #L439 was not covered by tests
attribute.String("stage-id", rbs.Id),
))
defer span.End()
Expand Down
4 changes: 2 additions & 2 deletions pkg/app/pipedv1/notifier/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@
fields = []slackField{
{"Project", truncateText(d.ProjectId, 8), true},
{"Application", makeSlackLink(d.ApplicationName, fmt.Sprintf("%s/applications/%s?project=%s", webURL, d.ApplicationId, d.ProjectId)), true},
{"Kind", strings.ToLower(d.Kind.String()), true},
{"Labels", d.GetLabelsString(), true},

Check warning on line 215 in pkg/app/pipedv1/notifier/slack.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/notifier/slack.go#L215

Added line #L215 was not covered by tests
{"Deployment", makeSlackLink(truncateText(d.Id, 8), link), true},
{"Triggered By", d.TriggeredBy(), true},
{"Mention To Users", accountsStr, true},
Expand Down Expand Up @@ -262,7 +262,7 @@
fields = []slackField{
{"Project", truncateText(d.ProjectId, 8), true},
{"Application", makeSlackLink(d.ApplicationName, fmt.Sprintf("%s/applications/%s?project=%s", webURL, d.ApplicationId, d.ProjectId)), true},
{"Kind", strings.ToLower(d.Kind.String()), true},
{"Labels", d.GetLabelsString(), true},

Check warning on line 265 in pkg/app/pipedv1/notifier/slack.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/notifier/slack.go#L265

Added line #L265 was not covered by tests
{"Deployment", makeSlackLink(truncateText(d.Id, 8), link), true},
{"Stage", s.Name, true},
{"Triggered By", d.TriggeredBy(), true},
Expand Down
1 change: 0 additions & 1 deletion pkg/app/pipedv1/trigger/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ func buildDeployment(
ApplicationName: app.Name,
PipedId: app.PipedId,
ProjectId: app.ProjectId,
Kind: app.Kind,
Trigger: &model.DeploymentTrigger{
Commit: &model.Commit{
Hash: commit.Hash,
Expand Down
63 changes: 36 additions & 27 deletions pkg/model/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import (
"fmt"
"strings"

"google.golang.org/protobuf/proto"
)
Expand Down Expand Up @@ -92,6 +93,28 @@
return false
}

// DeploymentStatusesFromStrings converts a list of strings to list of DeploymentStatus.
func DeploymentStatusesFromStrings(statuses []string) ([]DeploymentStatus, error) {
out := make([]DeploymentStatus, 0, len(statuses))
for _, s := range statuses {
status, ok := DeploymentStatus_value[s]
if !ok {
return nil, fmt.Errorf("invalid status %s", s)
}
out = append(out, DeploymentStatus(status))

Check warning on line 104 in pkg/model/deployment.go

View check run for this annotation

Codecov / codecov/patch

pkg/model/deployment.go#L97-L104

Added lines #L97 - L104 were not covered by tests
}
return out, nil

Check warning on line 106 in pkg/model/deployment.go

View check run for this annotation

Codecov / codecov/patch

pkg/model/deployment.go#L106

Added line #L106 was not covered by tests
}

// DeploymentStatusStrings returns a list of available deployment statuses in string.
func DeploymentStatusStrings() []string {
out := make([]string, 0, len(DeploymentStatus_value))
for s := range DeploymentStatus_value {
out = append(out, s)
}
return out

Check warning on line 115 in pkg/model/deployment.go

View check run for this annotation

Codecov / codecov/patch

pkg/model/deployment.go#L110-L115

Added lines #L110 - L115 were not covered by tests
}

// StageMap returns the map of id and the stage.
func (d *Deployment) StageMap() map[string]*PipelineStage {
stage := make(map[string]*PipelineStage, len(d.Stages))
Expand All @@ -111,11 +134,6 @@
return nil, false
}

// IsSkippable checks whether skippable or not.
func (p *PipelineStage) IsSkippable() bool {
return p.Name == StageAnalysis.String()
}

// CommitHash returns the hash value of trigger commit.
func (d *Deployment) CommitHash() string {
return d.Trigger.Commit.Hash
Expand Down Expand Up @@ -171,28 +189,6 @@
return rollbackStages, len(rollbackStages) > 0
}

// DeploymentStatusesFromStrings converts a list of strings to list of DeploymentStatus.
func DeploymentStatusesFromStrings(statuses []string) ([]DeploymentStatus, error) {
out := make([]DeploymentStatus, 0, len(statuses))
for _, s := range statuses {
status, ok := DeploymentStatus_value[s]
if !ok {
return nil, fmt.Errorf("invalid status %s", s)
}
out = append(out, DeploymentStatus(status))
}
return out, nil
}

// DeploymentStatusStrings returns a list of available deployment statuses in string.
func DeploymentStatusStrings() []string {
out := make([]string, 0, len(DeploymentStatus_value))
for s := range DeploymentStatus_value {
out = append(out, s)
}
return out
}

// ContainLabels checks if it has all the given labels.
func (d *Deployment) ContainLabels(labels map[string]string) bool {
if len(d.Labels) < len(labels) {
Expand Down Expand Up @@ -230,6 +226,19 @@
return dps.GetDeployTargets()
}

func (d *Deployment) GetLabelsString() string {
labels := make([]string, 0, len(d.Labels))
for k, v := range d.Labels {
labels = append(labels, fmt.Sprintf("%s=%s", k, v))
}
return strings.Join(labels, ",")
}

// IsSkippable checks whether skippable or not.
func (p *PipelineStage) IsSkippable() bool {
return p.Name == StageAnalysis.String()

Check warning on line 239 in pkg/model/deployment.go

View check run for this annotation

Codecov / codecov/patch

pkg/model/deployment.go#L238-L239

Added lines #L238 - L239 were not covered by tests
}

// Implement sort.Interface for PipelineStages.
type PipelineStages []*PipelineStage

Expand Down
56 changes: 56 additions & 0 deletions pkg/model/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package model

import (
"sort"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -725,3 +726,58 @@ func TestDeployment_GetDeployTargets(t *testing.T) {
})
}
}

func TestDeployment_GetLabelsString(t *testing.T) {
t.Parallel()

tests := []struct {
name string
deployment *Deployment
expected string
}{
{
name: "nil labels",
deployment: &Deployment{Labels: nil},
expected: "",
},
{
name: "empty labels",
deployment: &Deployment{Labels: map[string]string{}},
expected: "",
},
{
name: "single label",
deployment: &Deployment{
Labels: map[string]string{
"env": "prod",
},
},
expected: "env=prod",
},
{
name: "multiple labels",
deployment: &Deployment{
Labels: map[string]string{
"env": "prod",
"version": "v1.0.0",
"region": "us-west-1",
},
},
expected: "env=prod,version=v1.0.0,region=us-west-1",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

result := tt.deployment.GetLabelsString()
assert.Equal(t, len(tt.expected), len(result))
expectedParts := strings.Split(tt.expected, ",")
resultParts := strings.Split(result, ",")
sort.Strings(expectedParts)
sort.Strings(resultParts)
assert.Equal(t, expectedParts, resultParts)
})
}
}