1 Commits

4 changed files with 68 additions and 106 deletions

View File

@@ -41,7 +41,6 @@ const (
AppName = "obs-status-service"
)
var obs *common.ObsClient
type RepoBuildCounters struct {
@@ -244,16 +243,20 @@ func main() {
disableTls := flag.Bool("no-tls", false, "Disable TLS")
ObsUrl = flag.String("obs-url", obsUrlDef, "OBS API endpoint for package buildlog information")
debug := flag.Bool("debug", false, "Enable debug logging")
testRun := flag.Bool("test-run", false, "Run service in test mode without Redis")
flag.Parse()
if *debug {
common.SetLoggingLevel(common.LogLevelDebug)
}
if redisUrl := os.Getenv("REDIS"); len(redisUrl) > 0 {
RedisConnect(redisUrl)
if *testRun {
redisClient = &MockRedisClient{}
common.LogInfo("Running in TEST MODE (mock Redis)")
} else if redisURL := os.Getenv("REDIS"); redisURL != "" {
RedisConnect(redisURL)
} else {
common.LogError("REDIS needs to contains URL of the OBS Redis instance with login information")
common.LogError("REDIS environment variable is required unless --test-run is used")
return
}
@@ -272,7 +275,8 @@ func main() {
res.WriteHeader(500)
return
}
http.ServeFile(res, req, "static/index.html")
res.WriteHeader(404)
res.Write([]byte("404 page not found\n"))
})
http.HandleFunc("GET /status/{Project}", func(res http.ResponseWriter, req *http.Request) {
mime := ParseMimeHeader(req)

View File

@@ -11,10 +11,15 @@ import (
"src.opensuse.org/autogits/common"
)
type RedisClient interface {
HGetAll(ctx context.Context, key string) *redis.MapStringStringCmd
ScanType(ctx context.Context, cursor uint64, match string, count int64, keyType string) *redis.ScanCmd
}
var RepoStatus []*common.BuildResult = []*common.BuildResult{}
var RepoStatusLock *sync.RWMutex = &sync.RWMutex{}
var redisClient *redis.Client
var redisClient RedisClient
func RedisConnect(RedisUrl string) {
opts, err := redis.ParseURL(RedisUrl)

View File

@@ -0,0 +1,53 @@
package main
import (
"context"
"github.com/redis/go-redis/v9"
)
// MockRedisClient implements RedisClient interface
// and provides static data for local development (--test-run).
type MockRedisClient struct{}
// ScanType mocks redisClient.ScanType
func (m *MockRedisClient) ScanType(
ctx context.Context,
cursor uint64,
match string,
count int64,
keyType string,
) *redis.ScanCmd {
keys := []string{
"result.devel:languages:python:Factory/openSUSE_Tumbleweed/x86_64",
"result.devel:Factory:git-workflow/openSUSE_Tumbleweed/x86_64",
}
// cmdable is nil because this is a mock
cmd := redis.NewScanCmd(ctx, nil, "SCAN")
cmd.SetVal(keys, 0)
return cmd
}
// HGetAll mocks redisClient.HGetAll
func (m *MockRedisClient) HGetAll(
ctx context.Context,
key string,
) *redis.MapStringStringCmd {
data := map[string]map[string]string{
"result.devel:languages:python:Factory/openSUSE_Tumbleweed/x86_64": {
"python313": "succeeded",
"python314": "failed",
},
"result.devel:Factory:git-workflow/openSUSE_Tumbleweed/x86_64": {
"autogits": "succeeded",
},
}
// cmdable is nil because this is a mock
cmd := redis.NewMapStringStringCmd(ctx, nil, "HGETALL")
cmd.SetVal(data[key])
return cmd
}

View File

@@ -1,100 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>openSUSE OBS Status Service</title>
<style>
body {
font-family: sans-serif;
max-width: 900px;
margin: auto;
padding: 20px;
}
input {
width: 100%;
margin: 6px 0;
padding: 6px;
}
button {
padding: 6px 12px;
margin-top: 8px;
}
pre {
background: #f5f5f5;
padding: 10px;
overflow-x: auto;
}
img {
margin-top: 10px;
}
</style>
</head>
<body>
<h1>OBS Status Service</h1>
<p>
This service reports build results from the openSUSE Build Service (OBS)
as easily embeddable SVG images. Repository build results are cached to
provide low-overhead access.
</p>
<h2>Usage</h2>
<p>Requests for individual build results:</p>
<pre>/status/obs:project/package/repo/arch</pre>
<p><em>package, repo and arch are optional.</em></p>
<p>Requests for project results:</p>
<pre>/status/obs:project</pre>
<p>
By default, SVG output is generated. JSON and XML output are available
by setting the <code>Accept</code> request header.
</p>
<h2>Generate Build Result Image</h2>
<label>Project (required)</label>
<input id="project" placeholder="devel:languages:python:Factory">
<label>Package (optional)</label>
<input id="package" placeholder="python313">
<label>Repo &amp; Arch (optional)</label>
<input id="repo" placeholder="openSUSE_Tumbleweed/x86_64">
<button onclick="generate()">Generate</button>
<h3>Link</h3>
<pre id="link"></pre>
<h3>Markdown</h3>
<pre id="markdown"></pre>
<h3>Preview</h3>
<img id="preview" alt="Build status preview">
<script>
function generate() {
const project = document.getElementById("project").value.trim();
const pkg = document.getElementById("package").value.trim();
const repo = document.getElementById("repo").value.trim();
if (!project) {
alert("Project is required");
return;
}
let url = "https://br.opensuse.org/status/" + project;
if (pkg) url += "/" + pkg;
if (repo) url += "/" + repo;
document.getElementById("link").textContent = url;
document.getElementById("markdown").textContent =
"![br](" + url + ")";
document.getElementById("preview").src = url;
}
</script>
</body>
</html>