2021-12-28 16:02:49 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2022-05-12 17:07:33 +00:00
|
|
|
"bytes"
|
2021-12-28 16:02:49 +00:00
|
|
|
"fmt"
|
2022-05-12 17:07:33 +00:00
|
|
|
"text/template"
|
2021-12-28 16:02:49 +00:00
|
|
|
|
|
|
|
"github.com/woodpecker-ci/woodpecker/server"
|
|
|
|
"github.com/woodpecker-ci/woodpecker/server/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
func GetBuildStatusContext(repo *model.Repo, build *model.Build, proc *model.Proc) string {
|
2022-05-12 17:07:33 +00:00
|
|
|
event := string(build.Event)
|
2021-12-28 16:02:49 +00:00
|
|
|
switch build.Event {
|
|
|
|
case model.EventPull:
|
2022-05-12 17:07:33 +00:00
|
|
|
event = "pr"
|
2021-12-28 16:02:49 +00:00
|
|
|
}
|
|
|
|
|
2022-05-12 17:07:33 +00:00
|
|
|
tmpl, err := template.New("context").Parse(server.Config.Server.StatusContextFormat)
|
|
|
|
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 ""
|
2021-12-28 16:02:49 +00:00
|
|
|
}
|
|
|
|
|
2022-05-12 17:07:33 +00:00
|
|
|
return ctx.String()
|
2021-12-28 16:02:49 +00:00
|
|
|
}
|
|
|
|
|
2022-05-12 17:07:33 +00:00
|
|
|
// GetBuildStatusDescription is a helper function that generates a description
|
2021-12-28 16:02:49 +00:00
|
|
|
// message for the current build status.
|
|
|
|
func GetBuildStatusDescription(status model.StatusValue) string {
|
|
|
|
switch status {
|
|
|
|
case model.StatusPending:
|
|
|
|
return "Pipeline is pending"
|
|
|
|
case model.StatusRunning:
|
|
|
|
return "Pipeline is running"
|
|
|
|
case model.StatusSuccess:
|
|
|
|
return "Pipeline was successful"
|
|
|
|
case model.StatusFailure, model.StatusError:
|
|
|
|
return "Pipeline failed"
|
|
|
|
case model.StatusKilled:
|
|
|
|
return "Pipeline was canceled"
|
|
|
|
case model.StatusBlocked:
|
|
|
|
return "Pipeline is pending approval"
|
|
|
|
case model.StatusDeclined:
|
|
|
|
return "Pipeline was rejected"
|
|
|
|
default:
|
|
|
|
return "unknown status"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetBuildStatusLink(repo *model.Repo, build *model.Build, proc *model.Proc) string {
|
|
|
|
if proc == nil {
|
|
|
|
return fmt.Sprintf("%s/%s/build/%d", server.Config.Server.Host, repo.FullName, build.Number)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%s/%s/build/%d/%d", server.Config.Server.Host, repo.FullName, build.Number, proc.PID)
|
|
|
|
}
|