feat: show the version of Sonda

- GET /version (signed-in users only) answers the version and, when the binary knows it, the commit it was built from and whether the files had uncommitted changes
- the commit is read from the build info Go records in a git checkout, so no build flag is needed
- the side menu shows the version under the privacy line, and Settings has an About section with it
- the menu's own requests (friends, version) are not counted as requests of an admin page in the Shell tests
This commit is contained in:
tgiachi 2026-11-15 17:30:14 +01:00
commit 00e5254fde
4 changed files with 337 additions and 10 deletions

View file

@ -360,6 +360,9 @@ func TestResponsesMatchTheSpec(t *testing.T) {
c.expect(c.call("GET", "/auth/me", "", nil), 401, "me without token", "unauthorized")
c.expect(c.call("GET", "/auth/me", token, nil), 200, "me")
c.expect(c.call("GET", "/version", "", nil), 401, "version without token", "unauthorized")
c.expect(c.call("GET", "/version", token, nil), 200, "version")
renewed := c.expect(c.call("POST", "/auth/refresh", "", map[string]string{"refresh_token": refresh}), 200, "refresh")
c.expect(c.call("POST", "/auth/refresh", "", map[string]string{"refresh_token": refresh}), 401, "refresh replay", "invalid_refresh_token")
c.expect(c.call("POST", "/auth/refresh", "", "not an object"), 400, "refresh bad body", "invalid_input")

View file

@ -0,0 +1,33 @@
package httpapi
import "testing"
func TestVersionIsForSignedInUsers(t *testing.T) {
h := newTestAPIWith(t, func(d *Deps) { d.Build = Build{Version: "1.2.3", Revision: "abc1234", Modified: true} })
if r := do(t, h, "GET", "/api/v1/version", "", nil); r.Code != 401 {
t.Fatalf("without a token: %d, want 401", r.Code)
}
tok := registerAndLogin(t, h)
r := do(t, h, "GET", "/api/v1/version", tok, nil)
if r.Code != 200 {
t.Fatalf("version: %d %v", r.Code, r.Body)
}
if str(r.Body, "version") != "1.2.3" || str(r.Body, "revision") != "abc1234" || r.Body["modified"] != true {
t.Errorf("body = %v", r.Body)
}
}
func TestVersionWithoutARevisionHasNone(t *testing.T) {
h := newTestAPIWith(t, func(d *Deps) { d.Build = Build{Version: "1.2.3"} })
tok := registerAndLogin(t, h)
r := do(t, h, "GET", "/api/v1/version", tok, nil)
if r.Code != 200 || str(r.Body, "version") != "1.2.3" {
t.Fatalf("version: %d %v", r.Code, r.Body)
}
if _, ok := r.Body["revision"]; ok {
t.Errorf("revision should be left out: %v", r.Body)
}
if _, ok := r.Body["modified"]; ok {
t.Errorf("modified should be left out: %v", r.Body)
}
}

View file

@ -212,9 +212,18 @@ type libraryService interface {
Delete(ctx context.Context, id int64) error
}
// Build says which Sonda is running: its version, and the commit it was built from when that is known.
type Build struct {
Version string `json:"version"`
Revision string `json:"revision,omitempty"`
// Modified is true when the files had changes that were not committed.
Modified bool `json:"modified,omitempty"`
}
// Deps are the services the API needs.
type Deps struct {
Logger *zap.Logger
Build Build
Auth authService
Tokens tokenParser
Listening listenService
@ -306,6 +315,7 @@ func NewRouter(d Deps) *gin.Engine {
// Any signed-in user.
protected := v1.Group("", requireAuth(d.Tokens, d.Auth, false))
protected.GET("/auth/me", a.me)
protected.GET("/version", func(c *gin.Context) { c.JSON(http.StatusOK, d.Build) })
protected.POST("/auth/password", a.changePassword)
l := &listenHandlers{svc: d.Listening, announce: d.Mastodon}
protected.POST("/listens", l.record)

View file

@ -68,6 +68,35 @@ paths:
properties:
status: {type: string, enum: [ok]}
/version:
get:
tags: [System]
operationId: getVersion
summary: Which Sonda is running
description: |
The version, and the commit it was built from when the binary knows it. Only a signed-in
user can ask: the version of a server is not told to a stranger.
security: [{bearerAuth: []}]
responses:
'200':
description: The version of this server.
content:
application/json:
schema:
type: object
additionalProperties: false
required: [version]
properties:
version: {type: string, example: 0.1.0}
revision:
type: string
description: The first characters of the commit. Left out when the build did not record it.
example: 69c85a7
modified:
type: boolean
description: True when the files had changes that were not committed. Left out when false.
'401': {$ref: '#/components/responses/Unauthorized'}
/openapi.yaml:
get:
tags: [System]