Allow to change forge status messages (#900)

Allow to change the status message via template option

Closes https://github.com/woodpecker-ci/woodpecker/issues/855
This commit is contained in:
qwerty287 2022-05-12 19:07:33 +02:00 committed by GitHub
parent acbcc53872
commit 6568751320
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 83 additions and 20 deletions

View file

@ -206,6 +206,12 @@ var flags = []cli.Flag{
Usage: "status context prefix", Usage: "status context prefix",
Value: "ci/woodpecker", Value: "ci/woodpecker",
}, },
&cli.StringFlag{
EnvVars: []string{"WOODPECKER_STATUS_CONTEXT_FORMAT"},
Name: "status-context-format",
Usage: "status context format",
Value: "{{ .context }}/{{ .event }}/{{ .pipeline }}",
},
// //
// resource limit parameters // resource limit parameters
// //

View file

@ -317,6 +317,7 @@ func setupEvilGlobals(c *cli.Context, v store.Store, r remote.Remote) {
server.Config.Server.Port = c.String("server-addr") server.Config.Server.Port = c.String("server-addr")
server.Config.Server.Docs = c.String("docs") server.Config.Server.Docs = c.String("docs")
server.Config.Server.StatusContext = c.String("status-context") server.Config.Server.StatusContext = c.String("status-context")
server.Config.Server.StatusContextFormat = c.String("status-context-format")
server.Config.Server.SessionExpires = c.Duration("session-expires") server.Config.Server.SessionExpires = c.Duration("session-expires")
server.Config.Pipeline.Networks = c.StringSlice("network") server.Config.Pipeline.Networks = c.StringSlice("network")
server.Config.Pipeline.Volumes = c.StringSlice("volume") server.Config.Pipeline.Volumes = c.StringSlice("volume")

View file

@ -305,6 +305,17 @@ Read the value for `WOODPECKER_PROMETHEUS_AUTH_TOKEN` from the specified filepat
Context prefix Woodpecker will use to publish status messages to SCM. You probably will only need to change it if you run multiple Woodpecker instances for a single repository. Context prefix Woodpecker will use to publish status messages to SCM. You probably will only need to change it if you run multiple Woodpecker instances for a single repository.
### `WOODPECKER_STATUS_CONTEXT_FORMAT`
> Default: `{{ .context }}/{{ .event }}/{{ .pipeline }}`
Template for the status messages published to forges, uses [Go templates](https://pkg.go.dev/text/template) as template language.
Supported variables:
- `context`: Woodpecker's context (see `WOODPECKER_STATUS_CONTEXT`)
- `event`: the event which started the pipeline
- `pipeline`: the pipeline's name
- `owner`: the repo's owner
- `repo`: the repo's name
--- ---
### `WOODPECKER_LIMIT_MEM_SWAP` ### `WOODPECKER_LIMIT_MEM_SWAP`

View file

@ -51,15 +51,16 @@ var Config = struct {
// Secrets model.SecretStore // Secrets model.SecretStore
} }
Server struct { Server struct {
Key string Key string
Cert string Cert string
OAuthHost string OAuthHost string
Host string Host string
Port string Port string
Pass string Pass string
Docs string Docs string
StatusContext string StatusContext string
SessionExpires time.Duration StatusContextFormat string
SessionExpires time.Duration
// Open bool // Open bool
// Orgs map[string]struct{} // Orgs map[string]struct{}
// Admins map[string]struct{} // Admins map[string]struct{}

View file

@ -1,32 +1,41 @@
package common package common
import ( import (
"bytes"
"fmt" "fmt"
"text/template"
"github.com/woodpecker-ci/woodpecker/server" "github.com/woodpecker-ci/woodpecker/server"
"github.com/woodpecker-ci/woodpecker/server/model" "github.com/woodpecker-ci/woodpecker/server/model"
) )
func GetBuildStatusContext(repo *model.Repo, build *model.Build, proc *model.Proc) string { func GetBuildStatusContext(repo *model.Repo, build *model.Build, proc *model.Proc) string {
name := server.Config.Server.StatusContext event := string(build.Event)
switch build.Event { switch build.Event {
case model.EventPull: case model.EventPull:
name += "/pr" event = "pr"
default:
if len(build.Event) > 0 {
name += "/" + string(build.Event)
}
} }
if proc != nil { tmpl, err := template.New("context").Parse(server.Config.Server.StatusContextFormat)
name += "/" + proc.Name if err != nil {
return ""
}
var ctx bytes.Buffer
err = tmpl.Execute(&ctx, map[string]interface{}{
"context": server.Config.Server.StatusContext,
"event": event,
"pipeline": proc.Name,
"owner": repo.Owner,
"repo": repo.Name,
})
if err != nil {
return ""
} }
return name return ctx.String()
} }
// getBuildStatusDescription is a helper function that generates a description // GetBuildStatusDescription is a helper function that generates a description
// message for the current build status. // message for the current build status.
func GetBuildStatusDescription(status model.StatusValue) string { func GetBuildStatusDescription(status model.StatusValue) string {
switch status { switch status {

View file

@ -0,0 +1,35 @@
package common
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/woodpecker-ci/woodpecker/server"
"github.com/woodpecker-ci/woodpecker/server/model"
)
func TestGetBuildStatusContext(t *testing.T) {
origFormat := server.Config.Server.StatusContextFormat
origCtx := server.Config.Server.StatusContext
defer func() {
server.Config.Server.StatusContextFormat = origFormat
server.Config.Server.StatusContext = origCtx
}()
repo := &model.Repo{Owner: "user1", Name: "repo1"}
build := &model.Build{Event: model.EventPull}
proc := &model.Proc{Name: "lint"}
assert.EqualValues(t, "", GetBuildStatusContext(repo, build, proc))
server.Config.Server.StatusContext = "ci/woodpecker"
server.Config.Server.StatusContextFormat = "{{ .context }}/{{ .event }}/{{ .pipeline }}"
assert.EqualValues(t, "ci/woodpecker/pr/lint", GetBuildStatusContext(repo, build, proc))
build.Event = model.EventPush
assert.EqualValues(t, "ci/woodpecker/push/lint", GetBuildStatusContext(repo, build, proc))
server.Config.Server.StatusContext = "ci"
server.Config.Server.StatusContextFormat = "{{ .context }}:{{ .owner }}/{{ .repo }}:{{ .event }}:{{ .pipeline }}"
assert.EqualValues(t, "ci:user1/repo1:push:lint", GetBuildStatusContext(repo, build, proc))
}