forked from adamm/autogits
171 lines
4.4 KiB
Go
171 lines
4.4 KiB
Go
package main
|
|
|
|
/*
|
|
* This file is part of Autogits.
|
|
*
|
|
* Copyright © 2024 SUSE LLC
|
|
*
|
|
* Autogits is free software: you can redistribute it and/or modify it under
|
|
* the terms of the GNU General Public License as published by the Free Software
|
|
* Foundation, either version 2 of the License, or (at your option) any later
|
|
* version.
|
|
*
|
|
* Autogits is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
|
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License along with
|
|
* Foobar. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
import (
|
|
"bytes"
|
|
"flag"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
|
|
"src.opensuse.org/autogits/common"
|
|
)
|
|
|
|
const (
|
|
AppName = "obs-status-service"
|
|
)
|
|
|
|
var obs *common.ObsClient
|
|
var debug bool
|
|
|
|
func LogDebug(v ...any) {
|
|
if debug {
|
|
log.Println(v...)
|
|
}
|
|
}
|
|
|
|
func ProjectStatusSummarySvg(project string) []byte {
|
|
res := GetCurrentStatus(project)
|
|
if res == nil {
|
|
return nil
|
|
}
|
|
|
|
pkgs := res.GetPackageList()
|
|
maxLen := 0
|
|
for _, p := range pkgs {
|
|
maxLen = max(maxLen, len(p))
|
|
}
|
|
|
|
width := float32(len(res.Result))*1.5 + float32(maxLen)*0.8
|
|
height := 1.5*float32(maxLen) + 30
|
|
|
|
ret := bytes.Buffer{}
|
|
ret.WriteString(`<svg version="2.0" width="`)
|
|
ret.WriteString(fmt.Sprint(width))
|
|
ret.WriteString(`em" height="`)
|
|
ret.WriteString(fmt.Sprint(height))
|
|
ret.WriteString(`em" xmlns="http://www.w3.org/2000/svg">`)
|
|
ret.WriteString(`<defs>
|
|
<g id="f"> <!-- failed -->
|
|
<rect width="1em" height="1em" fill="#800" />
|
|
</g>
|
|
<g id="s"> <!--succeeded-->
|
|
<rect width="1em" height="1em" fill="#080" />
|
|
</g>
|
|
<g id="buidling"> <!--building-->
|
|
<rect width="1em" height="1em" fill="#880" />
|
|
</g>
|
|
</defs>`)
|
|
ret.WriteString(`<use href="#f" x="1em" y="2em"/>`)
|
|
ret.WriteString(`</svg>`)
|
|
return ret.Bytes()
|
|
}
|
|
|
|
func PackageStatusSummarySvg(status common.PackageBuildStatus) []byte {
|
|
buildStatus, ok := common.ObsBuildStatusDetails[status.Code]
|
|
if !ok {
|
|
buildStatus = common.ObsBuildStatusDetails["error"]
|
|
}
|
|
fillColor := "#480" // orange
|
|
textColor := "#888"
|
|
if buildStatus.Finished {
|
|
textColor = "#fff"
|
|
|
|
if buildStatus.Success {
|
|
fillColor = "#080"
|
|
} else {
|
|
fillColor = "#800"
|
|
}
|
|
}
|
|
|
|
log.Println(status, " -> ", buildStatus)
|
|
|
|
return []byte(`<svg version="2.0" width="8em" height="1.5em" xmlns="http://www.w3.org/2000/svg">
|
|
<rect width="100%" height="100%" fill="` + fillColor + `"/>
|
|
<text x="4em" y="1.1em" text-anchor="middle" fill="` + textColor + `">` + buildStatus.Code + `</text>
|
|
</svg>`)
|
|
}
|
|
|
|
func main() {
|
|
cert := flag.String("cert-file", "", "TLS certificates file")
|
|
key := flag.String("key-file", "", "Private key for the TLS certificate")
|
|
listen := flag.String("listen", "[::1]:8080", "Listening string")
|
|
disableTls := flag.Bool("no-tls", false, "Disable TLS")
|
|
obsHost := flag.String("obs-host", "api.opensuse.org", "OBS API endpoint for package status information")
|
|
flag.BoolVar(&debug, "debug", false, "Enable debug logging")
|
|
flag.Parse()
|
|
|
|
common.PanicOnError(common.RequireObsSecretToken())
|
|
|
|
var err error
|
|
if obs, err = common.NewObsClient(*obsHost); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
http.HandleFunc("GET /{Project}", func(res http.ResponseWriter, req *http.Request) {
|
|
res.WriteHeader(http.StatusBadRequest)
|
|
})
|
|
http.HandleFunc("GET /{Project}/{Package}", func(res http.ResponseWriter, req *http.Request) {
|
|
/*
|
|
obsPrj := req.PathValue("Project")
|
|
obsPkg := req.PathValue("Package")
|
|
|
|
status, _ := PackageBuildStatus(obsPrj, obsPkg)
|
|
svg := PackageStatusSummarySvg(status)
|
|
*/
|
|
|
|
res.Header().Add("content-type", "image/svg+xml")
|
|
//res.Header().Add("size", fmt.Sprint(len(svg)))
|
|
//res.Write(svg)
|
|
})
|
|
http.HandleFunc("GET /{Project}/{Package}/{Repository}/{Arch}", func(res http.ResponseWriter, req *http.Request) {
|
|
prj := req.PathValue("Project")
|
|
pkg := req.PathValue("Package")
|
|
repo := req.PathValue("Repository")
|
|
arch := req.PathValue("Arch")
|
|
|
|
res.Header().Add("content-type", "image/svg+xml")
|
|
|
|
prjStatus := GetCurrentStatus(prj)
|
|
if prjStatus == nil {
|
|
return
|
|
}
|
|
|
|
for _, r := range prjStatus.Result {
|
|
if r.Arch == arch && r.Repository == repo {
|
|
for _, status := range r.Status {
|
|
if status.Package == pkg {
|
|
res.Write(PackageStatusSummarySvg(status))
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|
|
})
|
|
|
|
go ProcessUpdates()
|
|
|
|
if *disableTls {
|
|
log.Fatal(http.ListenAndServe(*listen, nil))
|
|
} else {
|
|
log.Fatal(http.ListenAndServeTLS(*listen, *cert, *key, nil))
|
|
}
|
|
}
|