2024-08-16 17:17:26 +02:00
|
|
|
package main
|
|
|
|
|
2024-09-10 18:24:41 +02:00
|
|
|
/*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2024-08-16 17:17:26 +02:00
|
|
|
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
|
2024-08-19 17:14:20 +02:00
|
|
|
Result []*common.BuildResult
|
2024-08-16 17:17:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var obs *common.ObsClient
|
2024-08-19 17:14:20 +02:00
|
|
|
var buildStatusCache map[string]BuildStatusCacheItem
|
2024-09-10 18:24:41 +02:00
|
|
|
|
2024-08-19 17:14:20 +02:00
|
|
|
/*
|
2024-09-10 18:24:41 +02:00
|
|
|
func CacheBuildStatus(prj, pkg string) ([]common.BuildResult, error) {
|
|
|
|
list, err := obs.BuildStatus(prj, pkg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-08-16 17:17:26 +02:00
|
|
|
|
2024-09-10 18:24:41 +02:00
|
|
|
return
|
|
|
|
}
|
2024-08-19 17:14:20 +02:00
|
|
|
*/
|
2024-08-16 17:17:26 +02:00
|
|
|
func PackageBuildStatus(prj, pkg string) (common.ObsBuildStatusDetail, error) {
|
2024-09-10 18:24:41 +02:00
|
|
|
return common.ObsBuildStatusDetail{
|
|
|
|
Code: "succeeded",
|
2024-08-19 17:14:20 +02:00
|
|
|
Description: "stuff",
|
2024-09-10 18:24:41 +02:00
|
|
|
Success: true,
|
|
|
|
Finished: true,
|
2024-08-19 17:14:20 +02:00
|
|
|
}, nil
|
|
|
|
}
|
2024-09-10 18:24:41 +02:00
|
|
|
|
2024-08-19 17:14:20 +02:00
|
|
|
/*
|
2024-09-10 18:24:41 +02:00
|
|
|
func PackageStatusSvg(buildStatus []common.ObsBuildStatusDetail) []byte {
|
|
|
|
return
|
|
|
|
}
|
2024-08-19 17:14:20 +02:00
|
|
|
*/
|
2024-08-16 17:17:26 +02:00
|
|
|
func PackageStatusSummarySvg(buildStatus common.ObsBuildStatusDetail) []byte {
|
|
|
|
fillColor := "orange"
|
|
|
|
textColor := "grey"
|
|
|
|
if buildStatus.Finished {
|
|
|
|
textColor = "black"
|
|
|
|
|
|
|
|
if buildStatus.Success {
|
|
|
|
fillColor = "green"
|
|
|
|
} else {
|
|
|
|
fillColor = "red"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-19 17:14:20 +02:00
|
|
|
return []byte(`
|
|
|
|
<svg version="1.1" width="200" height="20" xmlns="http://www.w3.org/2000/svg">
|
2024-08-16 17:17:26 +02:00
|
|
|
<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()
|
2024-08-19 17:14:20 +02:00
|
|
|
go ProcessingObsMessages("rabbit.opensuse.org", "opensuse", "opensuse", "pubsub")
|
|
|
|
|
2024-08-16 17:17:26 +02:00
|
|
|
obsHost := os.Getenv("OBS_HOSTNAME")
|
|
|
|
if len(obsHost) == 0 {
|
|
|
|
log.Fatal("OBS_HOSTNAME env required.")
|
|
|
|
}
|
2024-08-19 17:14:20 +02:00
|
|
|
/*
|
2024-09-10 18:24:41 +02:00
|
|
|
if obs, err := common.NewObsClient(obsHost); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2024-08-19 17:14:20 +02:00
|
|
|
*/
|
2024-08-16 17:17:26 +02:00
|
|
|
|
|
|
|
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")
|
|
|
|
|
2024-08-19 17:14:20 +02:00
|
|
|
status, _ := PackageBuildStatus(obsPrj, obsPkg)
|
|
|
|
svg := PackageStatusSummarySvg(status)
|
2024-08-16 17:17:26 +02:00
|
|
|
|
|
|
|
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))
|
|
|
|
}
|