Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
38 changes: 37 additions & 1 deletion server/api/badge.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ func GetBadge(c *gin.Context) {
branch = repo.Branch
}

name := "unknown"
status := model.StatusDeclined

// Events to lookup, multiple separated by comma
var events []model.WebhookEvent
eventsQuery := c.Query("events")
Expand All @@ -99,11 +102,44 @@ func GetBadge(c *gin.Context) {
log.Warn().Err(err).Msg("could not get last pipeline for badge")
}
pipeline = nil
} else {
name = "pipeline"
status = pipeline.Status
}

// we serve an SVG, so set content type appropriately.
c.Writer.Header().Set("Content-Type", "image/svg+xml")
c.String(http.StatusOK, badges.Generate(pipeline))

// Allow workflow (and step) specific badges
workflowName := c.Query("workflow")
if len(workflowName) != 0 {
name = workflowName
status = model.StatusDeclined

workflows, err := _store.WorkflowGetTree(pipeline)
if err == nil {
for _, wf := range workflows {
if wf.Name == workflowName {
stepName := c.Query("step")
if len(stepName) == 0 {
status = wf.State
} else {
// If step is explicitly requested
name = name + ": " + stepName
for _, s := range wf.Children {
if s.Name == stepName {
status = s.State
break
}
}
}
break
}
}
}
}

c.String(http.StatusOK, badges.Generate(name, status))
}

// GetCC
Expand Down
28 changes: 17 additions & 11 deletions server/badges/badges.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@

package badges

import "go.woodpecker-ci.org/woodpecker/v3/server/model"
import (
"strings"

"go.woodpecker-ci.org/woodpecker/v3/server/model"
)

// cspell:words Verdana

Expand All @@ -26,21 +30,23 @@ var (
badgeNone = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="90" height="20" role="img" aria-label="pipeline: none"><title>pipeline: none</title><linearGradient id="s" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><clipPath id="r"><rect width="90" height="20" rx="3" fill="#fff"/></clipPath><g clip-path="url(#r)"><rect width="53" height="20" fill="#555"/><rect x="53" width="37" height="20" fill="#9f9f9f"/><rect width="90" height="20" fill="url(#s)"/></g><g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110"><text aria-hidden="true" x="275" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="430">pipeline</text><text x="275" y="140" transform="scale(.1)" fill="#fff" textLength="430">pipeline</text><text aria-hidden="true" x="705" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="270">none</text><text x="705" y="140" transform="scale(.1)" fill="#fff" textLength="270">none</text></g></svg>`
)

// Replace placeholder with specific name.
func SetBadgeName(badge, name string) string {
return strings.ReplaceAll(badge, "pipeline", name)
}

// Generate an SVG badge based on a pipeline.
func Generate(pipeline *model.Pipeline) string {
if pipeline == nil {
return badgeNone
}
switch pipeline.Status {
func Generate(name string, status model.StatusValue) string {
switch status {
case model.StatusSuccess:
return badgeSuccess
return SetBadgeName(badgeSuccess, name)
case model.StatusFailure:
return badgeFailure
return SetBadgeName(badgeFailure, name)
case model.StatusError, model.StatusKilled:
return badgeError
return SetBadgeName(badgeError, name)
case model.StatusPending, model.StatusRunning:
return badgeStarted
return SetBadgeName(badgeStarted, name)
default:
return badgeNone
return SetBadgeName(badgeNone, name)
}
}
14 changes: 7 additions & 7 deletions server/badges/badges_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ import (

// Generate an SVG badge based on a pipeline.
func TestGenerate(t *testing.T) {
assert.Equal(t, badgeNone, Generate(nil))
assert.Equal(t, badgeSuccess, Generate(&model.Pipeline{Status: model.StatusSuccess}))
assert.Equal(t, badgeFailure, Generate(&model.Pipeline{Status: model.StatusFailure}))
assert.Equal(t, badgeError, Generate(&model.Pipeline{Status: model.StatusError}))
assert.Equal(t, badgeError, Generate(&model.Pipeline{Status: model.StatusKilled}))
assert.Equal(t, badgeStarted, Generate(&model.Pipeline{Status: model.StatusPending}))
assert.Equal(t, badgeStarted, Generate(&model.Pipeline{Status: model.StatusRunning}))
assert.Equal(t, badgeNone, Generate("pipeline", model.StatusDeclined))
assert.Equal(t, badgeSuccess, Generate("pipeline", model.StatusSuccess))
assert.Equal(t, badgeFailure, Generate("pipeline", model.StatusFailure))
assert.Equal(t, badgeError, Generate("pipeline", model.StatusError))
assert.Equal(t, badgeError, Generate("pipeline", model.StatusKilled))
assert.Equal(t, badgeStarted, Generate("pipeline", model.StatusPending))
assert.Equal(t, badgeStarted, Generate("pipeline", model.StatusRunning))
}
4 changes: 3 additions & 1 deletion web/src/assets/locales/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,9 @@
"type_html": "HTML",
"type_markdown": "Markdown",
"type_url": "URL",
"events": "Events"
"events": "Events",
"workflow": "Workflow",
"step": "Step"
},
"crons": {
"add": "Cron hinzufügen",
Expand Down
4 changes: 3 additions & 1 deletion web/src/assets/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,9 @@
"type_markdown": "Markdown",
"type_html": "HTML",
"branch": "Branch",
"events": "Events"
"events": "Events",
"workflow": "Workflow",
"step": "Step"
},
"actions": {
"actions": "Actions",
Expand Down
17 changes: 17 additions & 0 deletions web/src/views/repo/settings/Badge.vue
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@
@update:model-value="eventsChanged"
/>
</InputField>
<InputField v-slot="{ id }" :label="$t('repo.settings.badge.workflow')">
<TextField :id="id" v-model="workflow" />
</InputField>
<InputField v-slot="{ id }" :label="$t('repo.settings.badge.step')">
<TextField :id="id" v-model="step" />
</InputField>

<div v-if="badgeContent" class="flex flex-col space-y-4">
<div>
Expand All @@ -57,6 +63,7 @@ import CheckboxesField from '~/components/form/CheckboxesField.vue';
import type { CheckboxOption, SelectOption } from '~/components/form/form.types';
import InputField from '~/components/form/InputField.vue';
import SelectField from '~/components/form/SelectField.vue';
import TextField from '~/components/form/TextField.vue';
import Settings from '~/components/layout/Settings.vue';
import useApiClient from '~/compositions/useApiClient';
import useConfig from '~/compositions/useConfig';
Expand All @@ -74,6 +81,8 @@ const defaultBranch = computed(() => repo.value.default_branch);
const branches = ref<SelectOption[]>([]);
const branch = ref<string>('');
const events = ref<string[]>([WebhookEvents.Push]);
const workflow = ref<string>('');
const step = ref<string>('');

async function loadBranches() {
branches.value = (await usePaginate((page) => apiClient.getRepoBranches(repo.value.id, { page })))
Expand Down Expand Up @@ -106,6 +115,14 @@ const badgeUrl = computed(() => {
}
}

if (workflow.value.trim().length > 0) {
params.push(`workflow=${encodeURIComponent(workflow.value.trim())}`);

if (step.value.trim().length > 0) {
params.push(`step=${encodeURIComponent(step.value.trim())}`);
}
}

return `${rootPath}/api/badges/${repo.value.id}/status.svg${params.length > 0 ? `?${params.join('&')}` : ''}`;
});
const repoUrl = computed(
Expand Down