mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2025-03-28 23:25:32 +00:00
Move Badge & CCMenue into own packages (#908)
* move badge generation into own package * move ccmenue away from models * fix misspell
This commit is contained in:
parent
be0dfb280c
commit
c350af645b
5 changed files with 88 additions and 57 deletions
|
@ -19,24 +19,18 @@ package api
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/woodpecker-ci/woodpecker/server"
|
||||
"github.com/woodpecker-ci/woodpecker/server/model"
|
||||
"github.com/woodpecker-ci/woodpecker/server/badges"
|
||||
"github.com/woodpecker-ci/woodpecker/server/ccmenu"
|
||||
"github.com/woodpecker-ci/woodpecker/server/store"
|
||||
)
|
||||
|
||||
var (
|
||||
badgeSuccess = `<svg xmlns="http://www.w3.org/2000/svg" width="91" height="20"><linearGradient id="a" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><rect rx="3" width="91" height="20" fill="#555"/><rect rx="3" x="37" width="54" height="20" fill="#4c1"/><path fill="#4c1" d="M37 0h4v20h-4z"/><rect rx="3" width="91" height="20" fill="url(#a)"/><g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11"><text x="19.5" y="15" fill="#010101" fill-opacity=".3">build</text><text x="19.5" y="14">build</text><text x="63" y="15" fill="#010101" fill-opacity=".3">success</text><text x="63" y="14">success</text></g></svg>`
|
||||
badgeFailure = `<svg xmlns="http://www.w3.org/2000/svg" width="83" height="20"><linearGradient id="a" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><rect rx="3" width="83" height="20" fill="#555"/><rect rx="3" x="37" width="46" height="20" fill="#e05d44"/><path fill="#e05d44" d="M37 0h4v20h-4z"/><rect rx="3" width="83" height="20" fill="url(#a)"/><g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11"><text x="19.5" y="15" fill="#010101" fill-opacity=".3">build</text><text x="19.5" y="14">build</text><text x="59" y="15" fill="#010101" fill-opacity=".3">failure</text><text x="59" y="14">failure</text></g></svg>`
|
||||
badgeStarted = `<svg xmlns="http://www.w3.org/2000/svg" width="87" height="20"><linearGradient id="a" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><rect rx="3" width="87" height="20" fill="#555"/><rect rx="3" x="37" width="50" height="20" fill="#dfb317"/><path fill="#dfb317" d="M37 0h4v20h-4z"/><rect rx="3" width="87" height="20" fill="url(#a)"/><g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11"><text x="19.5" y="15" fill="#010101" fill-opacity=".3">build</text><text x="19.5" y="14">build</text><text x="61" y="15" fill="#010101" fill-opacity=".3">started</text><text x="61" y="14">started</text></g></svg>`
|
||||
badgeError = `<svg xmlns="http://www.w3.org/2000/svg" width="76" height="20"><linearGradient id="a" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><rect rx="3" width="76" height="20" fill="#555"/><rect rx="3" x="37" width="39" height="20" fill="#9f9f9f"/><path fill="#9f9f9f" d="M37 0h4v20h-4z"/><rect rx="3" width="76" height="20" fill="url(#a)"/><g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11"><text x="19.5" y="15" fill="#010101" fill-opacity=".3">build</text><text x="19.5" y="14">build</text><text x="55.5" y="15" fill="#010101" fill-opacity=".3">error</text><text x="55.5" y="14">error</text></g></svg>`
|
||||
badgeNone = `<svg xmlns="http://www.w3.org/2000/svg" width="75" height="20"><linearGradient id="a" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><rect rx="3" width="75" height="20" fill="#555"/><rect rx="3" x="37" width="38" height="20" fill="#9f9f9f"/><path fill="#9f9f9f" d="M37 0h4v20h-4z"/><rect rx="3" width="75" height="20" fill="url(#a)"/><g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11"><text x="19.5" y="15" fill="#010101" fill-opacity=".3">build</text><text x="19.5" y="14">build</text><text x="55" y="15" fill="#010101" fill-opacity=".3">none</text><text x="55" y="14">none</text></g></svg>`
|
||||
)
|
||||
|
||||
func GetBadge(c *gin.Context) {
|
||||
_store := store.FromContext(c)
|
||||
repo, err := _store.GetRepoName(c.Param("owner") + "/" + c.Param("name"))
|
||||
|
@ -45,10 +39,6 @@ func GetBadge(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
// an SVG response is always served, even when error, so
|
||||
// we can go ahead and set the content type appropriately.
|
||||
c.Writer.Header().Set("Content-Type", "image/svg+xml")
|
||||
|
||||
// if no commit was found then display
|
||||
// the 'none' badge, instead of throwing
|
||||
// an error response
|
||||
|
@ -60,22 +50,12 @@ func GetBadge(c *gin.Context) {
|
|||
build, err := _store.GetBuildLast(repo, branch)
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Msg("")
|
||||
c.String(200, badgeNone)
|
||||
return
|
||||
build = nil
|
||||
}
|
||||
|
||||
switch build.Status {
|
||||
case model.StatusSuccess:
|
||||
c.String(200, badgeSuccess)
|
||||
case model.StatusFailure:
|
||||
c.String(200, badgeFailure)
|
||||
case model.StatusError, model.StatusKilled:
|
||||
c.String(200, badgeError)
|
||||
case model.StatusPending, model.StatusRunning:
|
||||
c.String(200, badgeStarted)
|
||||
default:
|
||||
c.String(200, badgeNone)
|
||||
}
|
||||
// we serve an SVG, so set content type appropriately.
|
||||
c.Writer.Header().Set("Content-Type", "image/svg+xml")
|
||||
c.String(http.StatusOK, badges.Generate(build))
|
||||
}
|
||||
|
||||
func GetCC(c *gin.Context) {
|
||||
|
@ -93,6 +73,6 @@ func GetCC(c *gin.Context) {
|
|||
}
|
||||
|
||||
url := fmt.Sprintf("%s/%s/%d", server.Config.Server.Host, repo.FullName, builds[0].Number)
|
||||
cc := model.NewCC(repo, builds[0], url)
|
||||
cc := ccmenu.New(repo, builds[0], url)
|
||||
c.XML(200, cc)
|
||||
}
|
||||
|
|
41
server/badges/badges.go
Normal file
41
server/badges/badges.go
Normal file
|
@ -0,0 +1,41 @@
|
|||
// Copyright 2022 Woodpecker Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package badges
|
||||
|
||||
import "github.com/woodpecker-ci/woodpecker/server/model"
|
||||
|
||||
var (
|
||||
badgeSuccess = `<svg xmlns="http://www.w3.org/2000/svg" width="91" height="20"><linearGradient id="a" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><rect rx="3" width="91" height="20" fill="#555"/><rect rx="3" x="37" width="54" height="20" fill="#4c1"/><path fill="#4c1" d="M37 0h4v20h-4z"/><rect rx="3" width="91" height="20" fill="url(#a)"/><g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11"><text x="19.5" y="15" fill="#010101" fill-opacity=".3">build</text><text x="19.5" y="14">build</text><text x="63" y="15" fill="#010101" fill-opacity=".3">success</text><text x="63" y="14">success</text></g></svg>`
|
||||
badgeFailure = `<svg xmlns="http://www.w3.org/2000/svg" width="83" height="20"><linearGradient id="a" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><rect rx="3" width="83" height="20" fill="#555"/><rect rx="3" x="37" width="46" height="20" fill="#e05d44"/><path fill="#e05d44" d="M37 0h4v20h-4z"/><rect rx="3" width="83" height="20" fill="url(#a)"/><g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11"><text x="19.5" y="15" fill="#010101" fill-opacity=".3">build</text><text x="19.5" y="14">build</text><text x="59" y="15" fill="#010101" fill-opacity=".3">failure</text><text x="59" y="14">failure</text></g></svg>`
|
||||
badgeStarted = `<svg xmlns="http://www.w3.org/2000/svg" width="87" height="20"><linearGradient id="a" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><rect rx="3" width="87" height="20" fill="#555"/><rect rx="3" x="37" width="50" height="20" fill="#dfb317"/><path fill="#dfb317" d="M37 0h4v20h-4z"/><rect rx="3" width="87" height="20" fill="url(#a)"/><g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11"><text x="19.5" y="15" fill="#010101" fill-opacity=".3">build</text><text x="19.5" y="14">build</text><text x="61" y="15" fill="#010101" fill-opacity=".3">started</text><text x="61" y="14">started</text></g></svg>`
|
||||
badgeError = `<svg xmlns="http://www.w3.org/2000/svg" width="76" height="20"><linearGradient id="a" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><rect rx="3" width="76" height="20" fill="#555"/><rect rx="3" x="37" width="39" height="20" fill="#9f9f9f"/><path fill="#9f9f9f" d="M37 0h4v20h-4z"/><rect rx="3" width="76" height="20" fill="url(#a)"/><g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11"><text x="19.5" y="15" fill="#010101" fill-opacity=".3">build</text><text x="19.5" y="14">build</text><text x="55.5" y="15" fill="#010101" fill-opacity=".3">error</text><text x="55.5" y="14">error</text></g></svg>`
|
||||
badgeNone = `<svg xmlns="http://www.w3.org/2000/svg" width="75" height="20"><linearGradient id="a" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><rect rx="3" width="75" height="20" fill="#555"/><rect rx="3" x="37" width="38" height="20" fill="#9f9f9f"/><path fill="#9f9f9f" d="M37 0h4v20h-4z"/><rect rx="3" width="75" height="20" fill="url(#a)"/><g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11"><text x="19.5" y="15" fill="#010101" fill-opacity=".3">build</text><text x="19.5" y="14">build</text><text x="55" y="15" fill="#010101" fill-opacity=".3">none</text><text x="55" y="14">none</text></g></svg>`
|
||||
)
|
||||
|
||||
// Generate an SVG badge based on a build
|
||||
func Generate(build *model.Build) string {
|
||||
switch build.Status {
|
||||
case model.StatusSuccess:
|
||||
return badgeSuccess
|
||||
case model.StatusFailure:
|
||||
return badgeFailure
|
||||
case model.StatusError, model.StatusKilled:
|
||||
return badgeError
|
||||
case model.StatusPending, model.StatusRunning:
|
||||
return badgeStarted
|
||||
default:
|
||||
return badgeNone
|
||||
}
|
||||
}
|
|
@ -1,3 +1,4 @@
|
|||
// Copyright 2022 Woodpecker Authors
|
||||
// Copyright 2018 Drone.IO Inc.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
|
@ -12,14 +13,21 @@
|
|||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package model
|
||||
package ccmenu
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/woodpecker-ci/woodpecker/server/model"
|
||||
)
|
||||
|
||||
// CCMenu displays the build status of projects on a ci server as an item in the Mac's menu bar.
|
||||
// It started as part of the CruiseControl project that built the first continuous integration server.
|
||||
//
|
||||
// http://ccmenu.org/
|
||||
|
||||
type CCProjects struct {
|
||||
XMLName xml.Name `xml:"Projects"`
|
||||
Project *CCProject `xml:"Project"`
|
||||
|
@ -35,7 +43,7 @@ type CCProject struct {
|
|||
WebURL string `xml:"webUrl,attr"`
|
||||
}
|
||||
|
||||
func NewCC(r *Repo, b *Build, link string) *CCProjects {
|
||||
func New(r *model.Repo, b *model.Build, link string) *CCProjects {
|
||||
proj := &CCProject{
|
||||
Name: r.FullName,
|
||||
WebURL: link,
|
||||
|
@ -46,8 +54,8 @@ func NewCC(r *Repo, b *Build, link string) *CCProjects {
|
|||
|
||||
// if the build is not currently running then
|
||||
// we can return the latest build status.
|
||||
if b.Status != StatusPending &&
|
||||
b.Status != StatusRunning {
|
||||
if b.Status != model.StatusPending &&
|
||||
b.Status != model.StatusRunning {
|
||||
proj.Activity = "Sleeping"
|
||||
proj.LastBuildTime = time.Unix(b.Started, 0).Format(time.RFC3339)
|
||||
proj.LastBuildLabel = strconv.FormatInt(b.Number, 10)
|
||||
|
@ -56,11 +64,11 @@ func NewCC(r *Repo, b *Build, link string) *CCProjects {
|
|||
// ensure the last build Status accepts a valid
|
||||
// ccmenu enumeration
|
||||
switch b.Status {
|
||||
case StatusError, StatusKilled:
|
||||
case model.StatusError, model.StatusKilled:
|
||||
proj.LastBuildStatus = "Exception"
|
||||
case StatusSuccess:
|
||||
case model.StatusSuccess:
|
||||
proj.LastBuildStatus = "Success"
|
||||
case StatusFailure:
|
||||
case model.StatusFailure:
|
||||
proj.LastBuildStatus = "Failure"
|
||||
}
|
||||
|
|
@ -12,13 +12,15 @@
|
|||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package model
|
||||
package ccmenu
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/franela/goblin"
|
||||
|
||||
"github.com/woodpecker-ci/woodpecker/server/model"
|
||||
)
|
||||
|
||||
func TestCC(t *testing.T) {
|
||||
|
@ -27,15 +29,15 @@ func TestCC(t *testing.T) {
|
|||
g.It("Should create a project", func() {
|
||||
now := time.Now().Unix()
|
||||
nowFmt := time.Unix(now, 0).Format(time.RFC3339)
|
||||
r := &Repo{
|
||||
r := &model.Repo{
|
||||
FullName: "foo/bar",
|
||||
}
|
||||
b := &Build{
|
||||
Status: StatusSuccess,
|
||||
b := &model.Build{
|
||||
Status: model.StatusSuccess,
|
||||
Number: 1,
|
||||
Started: now,
|
||||
}
|
||||
cc := NewCC(r, b, "http://localhost/foo/bar/1")
|
||||
cc := New(r, b, "http://localhost/foo/bar/1")
|
||||
|
||||
g.Assert(cc.Project.Name).Equal("foo/bar")
|
||||
g.Assert(cc.Project.Activity).Equal("Sleeping")
|
||||
|
@ -46,49 +48,49 @@ func TestCC(t *testing.T) {
|
|||
})
|
||||
|
||||
g.It("Should properly label exceptions", func() {
|
||||
r := &Repo{FullName: "foo/bar"}
|
||||
b := &Build{
|
||||
Status: StatusError,
|
||||
r := &model.Repo{FullName: "foo/bar"}
|
||||
b := &model.Build{
|
||||
Status: model.StatusError,
|
||||
Number: 1,
|
||||
Started: 1257894000,
|
||||
}
|
||||
cc := NewCC(r, b, "http://localhost/foo/bar/1")
|
||||
cc := New(r, b, "http://localhost/foo/bar/1")
|
||||
g.Assert(cc.Project.LastBuildStatus).Equal("Exception")
|
||||
g.Assert(cc.Project.Activity).Equal("Sleeping")
|
||||
})
|
||||
|
||||
g.It("Should properly label success", func() {
|
||||
r := &Repo{FullName: "foo/bar"}
|
||||
b := &Build{
|
||||
Status: StatusSuccess,
|
||||
r := &model.Repo{FullName: "foo/bar"}
|
||||
b := &model.Build{
|
||||
Status: model.StatusSuccess,
|
||||
Number: 1,
|
||||
Started: 1257894000,
|
||||
}
|
||||
cc := NewCC(r, b, "http://localhost/foo/bar/1")
|
||||
cc := New(r, b, "http://localhost/foo/bar/1")
|
||||
g.Assert(cc.Project.LastBuildStatus).Equal("Success")
|
||||
g.Assert(cc.Project.Activity).Equal("Sleeping")
|
||||
})
|
||||
|
||||
g.It("Should properly label failure", func() {
|
||||
r := &Repo{FullName: "foo/bar"}
|
||||
b := &Build{
|
||||
Status: StatusFailure,
|
||||
r := &model.Repo{FullName: "foo/bar"}
|
||||
b := &model.Build{
|
||||
Status: model.StatusFailure,
|
||||
Number: 1,
|
||||
Started: 1257894000,
|
||||
}
|
||||
cc := NewCC(r, b, "http://localhost/foo/bar/1")
|
||||
cc := New(r, b, "http://localhost/foo/bar/1")
|
||||
g.Assert(cc.Project.LastBuildStatus).Equal("Failure")
|
||||
g.Assert(cc.Project.Activity).Equal("Sleeping")
|
||||
})
|
||||
|
||||
g.It("Should properly label running", func() {
|
||||
r := &Repo{FullName: "foo/bar"}
|
||||
b := &Build{
|
||||
Status: StatusRunning,
|
||||
r := &model.Repo{FullName: "foo/bar"}
|
||||
b := &model.Build{
|
||||
Status: model.StatusRunning,
|
||||
Number: 1,
|
||||
Started: 1257894000,
|
||||
}
|
||||
cc := NewCC(r, b, "http://localhost/foo/bar/1")
|
||||
cc := New(r, b, "http://localhost/foo/bar/1")
|
||||
g.Assert(cc.Project.Activity).Equal("Building")
|
||||
g.Assert(cc.Project.LastBuildStatus).Equal("Unknown")
|
||||
g.Assert(cc.Project.LastBuildLabel).Equal("Unknown")
|
|
@ -56,7 +56,7 @@ func (s storage) GetBuildLast(repo *model.Repo, branch string) (*model.Build, er
|
|||
build := &model.Build{
|
||||
RepoID: repo.ID,
|
||||
Branch: branch,
|
||||
Event: "push",
|
||||
Event: model.EventPush,
|
||||
}
|
||||
return build, wrapGet(s.engine.Desc("build_number").Get(build))
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue