89 lines
1.9 KiB
Go
89 lines
1.9 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"time"
|
|
|
|
"src.opensuse.org/autogits/common"
|
|
)
|
|
|
|
const (
|
|
ListenAddr = "[::1]:8003"
|
|
AppName = "obs-status-service"
|
|
)
|
|
|
|
type BuildStatusCacheItem struct {
|
|
CacheTime time.Time
|
|
Result []*common.BuildResult
|
|
}
|
|
|
|
var obs *common.ObsClient
|
|
var buildStatusCache map[string]BuildStatusCacheItem
|
|
|
|
func CacheBuildStatus(prj, pkg string) ([]common.BuildResult, error) {
|
|
list, err := obs.BuildStatus(prj, pkg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
func PackageBuildStatus(prj, pkg string) (common.ObsBuildStatusDetail, error) {
|
|
for _, r := range list.Result {
|
|
|
|
}
|
|
}
|
|
|
|
func PackageStatusSvg(
|
|
|
|
func PackageStatusSummarySvg(buildStatus common.ObsBuildStatusDetail) []byte {
|
|
fillColor := "orange"
|
|
textColor := "grey"
|
|
if buildStatus.Finished {
|
|
textColor = "black"
|
|
|
|
if buildStatus.Success {
|
|
fillColor = "green"
|
|
} else {
|
|
fillColor = "red"
|
|
}
|
|
}
|
|
|
|
return []byte(
|
|
`<svg version="1.1"
|
|
width="200" height="20"
|
|
xmlns="http://www.w3.org/2000/svg">
|
|
<rect width="100%" height="100%" fill="` + fillColor + `" />
|
|
<text x="20" y="25" font-size="60" text-anchor="middle" fill="` + textColor + `">` + buildStatus.Code + `</text>
|
|
</svg>`)
|
|
}
|
|
|
|
func main() {
|
|
common.RequireObsSecretToken()
|
|
obsHost := os.Getenv("OBS_HOSTNAME")
|
|
if len(obsHost) == 0 {
|
|
log.Fatal("OBS_HOSTNAME env required.")
|
|
}
|
|
if obs, err := common.NewObsClient(obsHost); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
http.HandleFunc("GET /{ObsProject}", func(res http.ResponseWriter, req *http.Request) {
|
|
res.WriteHeader(http.StatusBadRequest)
|
|
})
|
|
http.HandleFunc("GET /{ObsProject}/{Package}", func(res http.ResponseWriter, req *http.Request) {
|
|
obsPrj := req.PathValue("ObsProject")
|
|
obsPkg := req.PathValue("ObsPackage")
|
|
|
|
svg := PackageStatusSvg(PackageBuildStatus(obsPrj, obsPkg))
|
|
|
|
res.Header().Add("content-type", "image/svg+xml")
|
|
res.Header().Add("size", fmt.Sprint(len(svg)))
|
|
res.Write(svg)
|
|
})
|
|
|
|
log.Fatal(http.ListenAndServe(ListenAddr, nil))
|
|
}
|