Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update status badge #696

Merged
merged 4 commits into from
Oct 28, 2024
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
2 changes: 2 additions & 0 deletions api/buildstatus/buildstatus_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/equinor/radix-api/models"
radixv1 "github.com/equinor/radix-operator/pkg/apis/radix/v1"
"github.com/gorilla/mux"
"github.com/rs/zerolog/log"
)

const rootPath = "/applications/{appName}/environments/{envName}"
Expand Down Expand Up @@ -77,6 +78,7 @@ func (bsc *buildStatusController) GetBuildStatus(accounts models.Accounts, w htt
buildStatus, err := buildStatusHandler.GetBuildStatusForApplication(r.Context(), appName, env, pipeline)

if err != nil {
log.Error().Err(err).Msg("Error getting build status")
w.WriteHeader(http.StatusInternalServerError)
return
}
Expand Down
59 changes: 30 additions & 29 deletions api/buildstatus/models/badges/build-status.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
62 changes: 29 additions & 33 deletions api/buildstatus/models/buildstatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"strings"

v1 "github.com/equinor/radix-operator/pkg/apis/radix/v1"
"github.com/marstr/guid"
)

// embed https://golang.org/pkg/embed/ - For embedding a single file, a variable of type []byte or string is often best
Expand All @@ -17,16 +16,16 @@ import (
var defaultBadgeTemplate string

const (
buildStatusFailing = "failing"
buildStatusSuccess = "success"
buildStatusStopped = "stopped"
buildStatusPending = "pending"
buildStatusRunning = "running"
buildStatusUnknown = "unknown"
buildStatusFailing = "Failing"
buildStatusSuccess = "Succeeded"
buildStatusStopped = "Stopped"
buildStatusPending = "Pending"
buildStatusRunning = "Running"
buildStatusUnknown = "Unknown"
)

const (
pipelineStatusSuccessColor = "#4c1"
pipelineStatusSuccessColor = "#34d058"
pipelineStatusFailedColor = "#e05d44"
pipelineStatusStoppedColor = "#e05d44"
pipelineStatusRunningColor = "#33cccc"
Expand All @@ -44,17 +43,11 @@ func NewPipelineBadge() PipelineBadge {
}

type pipelineBadgeData struct {
Operation string
Status string
ColorLeft string
ColorRight string
ColorShadow string
ColorFont string
Width int
Height int
StatusOffset int
OperationTextId string
StatusTextId string
Operation string
OperationWidth int
Status string
StatusColor string
StatusWidth int
}

type pipelineBadge struct {
Expand All @@ -70,23 +63,17 @@ func (rbs *pipelineBadge) getBadge(condition v1.RadixJobCondition, pipeline v1.R
status := translateCondition(condition)
color := getColor(condition)
operationWidth := calculateWidth(10, operation)
statusWidth := calculateWidth(10, status) + 24

statusWidth := calculateWidth(10, status)
badgeData := pipelineBadgeData{
Operation: operation,
Status: status,
ColorRight: color,
ColorLeft: "#aaa",
ColorShadow: "#010101",
ColorFont: "#fff",
Width: statusWidth + operationWidth,
Height: 30,
StatusOffset: operationWidth,
OperationTextId: guid.NewGUID().String(),
StatusTextId: guid.NewGUID().String(),
Operation: operation,
OperationWidth: operationWidth,
Status: status,
StatusColor: color,
StatusWidth: statusWidth,
}

svgTemplate := template.New("status-badge.svg")
funcMap := template.FuncMap{"sum": TemplateSum}
svgTemplate := template.New("status-badge.svg").Funcs(funcMap)
_, err := svgTemplate.Parse(rbs.badgeTemplate)
if err != nil {
return nil, err
Expand All @@ -113,6 +100,15 @@ func calculateWidth(charWidth float32, value string) int {
return int(width + 5)
}

func TemplateSum(arg0 int, args ...int) int {
x := arg0
for _, arg := range args {
x += arg
}

return x
}

func translateCondition(condition v1.RadixJobCondition) string {
switch condition {
case v1.JobSucceeded:
Expand Down
20 changes: 10 additions & 10 deletions api/buildstatus/models/buildstatus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,71 +13,71 @@ func Test_PipelineBadgeBuilder(t *testing.T) {

t.Run("failed condition", func(t *testing.T) {
t.Parallel()
expected := "-failing"
expected := "-Failing"
actual, err := badgeBuilder.GetBadge(v1.JobFailed, v1.RadixPipelineType(""))
assert.Nil(t, err)
assert.Equal(t, expected, string(actual))
})
t.Run("queued condition", func(t *testing.T) {
t.Parallel()
expected := "-pending"
expected := "-Pending"
actual, err := badgeBuilder.GetBadge(v1.JobQueued, v1.RadixPipelineType(""))
assert.Nil(t, err)
assert.Equal(t, expected, string(actual))
})
t.Run("running condition", func(t *testing.T) {
t.Parallel()
expected := "-running"
expected := "-Running"
actual, err := badgeBuilder.GetBadge(v1.JobRunning, v1.RadixPipelineType(""))
assert.Nil(t, err)
assert.Equal(t, expected, string(actual))
})
t.Run("stopped condition", func(t *testing.T) {
t.Parallel()
expected := "-stopped"
expected := "-Stopped"
actual, err := badgeBuilder.GetBadge(v1.JobStopped, v1.RadixPipelineType(""))
assert.Nil(t, err)
assert.Equal(t, expected, string(actual))
})
t.Run("succeeded condition", func(t *testing.T) {
t.Parallel()
expected := "-success"
expected := "-Succeeded"
actual, err := badgeBuilder.GetBadge(v1.JobSucceeded, v1.RadixPipelineType(""))
assert.Nil(t, err)
assert.Equal(t, expected, string(actual))
})
t.Run("waiting condition", func(t *testing.T) {
t.Parallel()
expected := "-pending"
expected := "-Pending"
actual, err := badgeBuilder.GetBadge(v1.JobWaiting, v1.RadixPipelineType(""))
assert.Nil(t, err)
assert.Equal(t, expected, string(actual))
})

t.Run("build-deploy type", func(t *testing.T) {
t.Parallel()
expected := "build-deploy-success"
expected := "build-deploy-Succeeded"
actual, err := badgeBuilder.GetBadge(v1.JobSucceeded, v1.BuildDeploy)
assert.Nil(t, err)
assert.Equal(t, expected, string(actual))
})
t.Run("promote type", func(t *testing.T) {
t.Parallel()
expected := "promote-success"
expected := "promote-Succeeded"
actual, err := badgeBuilder.GetBadge(v1.JobSucceeded, v1.Promote)
assert.Nil(t, err)
assert.Equal(t, expected, string(actual))
})
t.Run("deploy type", func(t *testing.T) {
t.Parallel()
expected := "deploy-success"
expected := "deploy-Succeeded"
actual, err := badgeBuilder.GetBadge(v1.JobSucceeded, v1.Deploy)
assert.Nil(t, err)
assert.Equal(t, expected, string(actual))
})
t.Run("unhandled type", func(t *testing.T) {
t.Parallel()
expected := "-success"
expected := "-Succeeded"
actual, err := badgeBuilder.GetBadge(v1.JobSucceeded, v1.RadixPipelineType("unhandled"))
assert.Nil(t, err)
assert.Equal(t, expected, string(actual))
Expand Down
Loading