Follow terdut-server into teams: switch team, per-team schedule
terdut-server v0.12 made everything team-scoped and v0.20 is what this
client now targets. Against it the old client was wrong in three ways:
the schedule moved to /api/teams/{id}/schedule, GET /api/schedule/current
became a list with one entry per team, and users, incidents, alerts and
schedule entries all grew fields the client ignored.
T steps through all teams and then each of yours. The header names what
is showing, and incident and alert rows gain a Team column when more than
one team can appear. team: in config.yaml picks the team to start on, by
name or id; an unknown one is reported and falls back to all teams.
The schedule is one team's rota, so it shows the active team, or with
all teams showing the first one you own. Writes need an owner or an
administrator, and the picker offers only the team's members, since the
server answers 404 for anybody else. Both are checked up front and the
reason goes in the status bar, rather than surfacing as a 403 after the
user has picked somebody. Stats are not team-scoped by the server and
stay that way here.
Users shows an admin/disabled Flags column. Creating and deleting users
is administrators only, and topic, keys and password work on your own
row or on anyone's for an administrator; the server enforces the same
rule, this only explains it before the round trip.
The server has no version endpoint, so an older one is recognised by
GET /api/teams answering 404, and the TUI says it needs v0.20 or later.
Connecting now also loads /api/teams and /api/me with the key, which
means a wrong key fails on start instead of on the first list; /healthz
does not check it. There is no fallback to the pre-team paths.
Rebuilding a table whose column count changes under loaded rows panicked
inside bubbles, because it re-renders the old rows on SetColumns. The
rows are now cleared first and the cursor put back, so a refresh still
does not jump to the top.
Escalation ladders, invites, integrations and the admin settings are
left to the server's web UI. Checked against a real v0.20.1 server with
two teams, an administrator and a plain member.
Breaking: requires terdut-server v0.20.0 or later. Use terdut-tui v0.9.x
with servers before v0.12.
This commit is contained in:
+59
-38
@@ -72,14 +72,18 @@ func (c *Client) do(req *http.Request, out any) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// ListAlerts fetches alerts. status may be "firing", "resolved", or "" for all.
|
||||
// ListAlerts fetches alerts. teamID limits them to one team; 0 means every team
|
||||
// the caller belongs to. status may be "firing", "resolved", or "" for all.
|
||||
// Set archived=true to fetch only archived alerts; false returns only non-archived.
|
||||
//
|
||||
// Alerts are read-only on the server — there is nothing to acknowledge or
|
||||
// archive here. This is the raw feed, useful for checking what Alertmanager is
|
||||
// actually sending; the work queue is ListIncidents.
|
||||
func (c *Client) ListAlerts(status string, archived bool, limit int) ([]Alert, error) {
|
||||
func (c *Client) ListAlerts(teamID int64, status string, archived bool, limit int) ([]Alert, error) {
|
||||
q := url.Values{}
|
||||
if teamID > 0 {
|
||||
q.Set("team_id", strconv.FormatInt(teamID, 10))
|
||||
}
|
||||
if status != "" {
|
||||
q.Set("status", status)
|
||||
}
|
||||
@@ -138,12 +142,16 @@ func (c *Client) GetAlert(id int64) (*Alert, error) {
|
||||
|
||||
// ── Incidents ──────────────────────────────────────────────────────────────
|
||||
|
||||
// ListIncidents fetches the work queue. status may be "triggered",
|
||||
// ListIncidents fetches the work queue. teamID limits it to one team; 0 means
|
||||
// every team the caller belongs to. status may be "triggered",
|
||||
// "acknowledged", "resolved", or "" for the server default of open incidents
|
||||
// only. archived and snoozed each switch the list to that set rather than
|
||||
// adding to it, matching the server's filters.
|
||||
func (c *Client) ListIncidents(status string, archived, snoozed bool, limit int) ([]Incident, error) {
|
||||
func (c *Client) ListIncidents(teamID int64, status string, archived, snoozed bool, limit int) ([]Incident, error) {
|
||||
q := url.Values{}
|
||||
if teamID > 0 {
|
||||
q.Set("team_id", strconv.FormatInt(teamID, 10))
|
||||
}
|
||||
if status != "" {
|
||||
q.Set("status", status)
|
||||
}
|
||||
@@ -329,8 +337,37 @@ func (c *Client) GetStatsByDay() ([]DayStat, error) {
|
||||
return result, c.do(req, &result)
|
||||
}
|
||||
|
||||
func (c *Client) GetSchedule(from, to string) ([]ScheduleEntry, error) {
|
||||
req, err := c.newRequest(http.MethodGet, fmt.Sprintf("/api/schedule?from=%s&to=%s", from, to))
|
||||
// ── Teams ──────────────────────────────────────────────────────────────────
|
||||
|
||||
// ListTeams returns the teams the caller belongs to, with the caller's role in
|
||||
// each. Everything else the server returns is scoped to these. A server that
|
||||
// predates teams (v0.12) answers 404, which is how the TUI spots one.
|
||||
func (c *Client) ListTeams() ([]Team, error) {
|
||||
req, err := c.newRequest(http.MethodGet, "/api/teams")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var teams []Team
|
||||
return teams, c.do(req, &teams)
|
||||
}
|
||||
|
||||
// ListTeamMembers returns who belongs to a team. A schedule can only be given to
|
||||
// its own members, so this is the assignee list for one.
|
||||
func (c *Client) ListTeamMembers(teamID int64) ([]TeamMember, error) {
|
||||
req, err := c.newRequest(http.MethodGet, fmt.Sprintf("/api/teams/%d/members", teamID))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var members []TeamMember
|
||||
return members, c.do(req, &members)
|
||||
}
|
||||
|
||||
// ── Schedule ───────────────────────────────────────────────────────────────
|
||||
|
||||
// GetSchedule returns a team's on-call entries between two YYYY-MM-DD dates.
|
||||
func (c *Client) GetSchedule(teamID int64, from, to string) ([]ScheduleEntry, error) {
|
||||
q := url.Values{"from": {from}, "to": {to}}
|
||||
req, err := c.newRequest(http.MethodGet, fmt.Sprintf("/api/teams/%d/schedule?%s", teamID, q.Encode()))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -338,49 +375,30 @@ func (c *Client) GetSchedule(from, to string) ([]ScheduleEntry, error) {
|
||||
return entries, c.do(req, &entries)
|
||||
}
|
||||
|
||||
// GetCurrentOnCall returns today's on-call entry, or nil if nobody is scheduled.
|
||||
func (c *Client) GetCurrentOnCall() (*ScheduleEntry, error) {
|
||||
// GetCurrentOnCall returns today's on-call entries, one per team that has
|
||||
// somebody scheduled. It is empty, not an error, when nobody is.
|
||||
func (c *Client) GetCurrentOnCall() ([]ScheduleEntry, error) {
|
||||
req, err := c.newRequest(http.MethodGet, "/api/schedule/current")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode == http.StatusNotFound {
|
||||
return nil, nil
|
||||
}
|
||||
if resp.StatusCode >= 400 {
|
||||
var e struct {
|
||||
Error string `json:"error"`
|
||||
}
|
||||
_ = json.NewDecoder(resp.Body).Decode(&e)
|
||||
if e.Error != "" {
|
||||
return nil, fmt.Errorf("server returned %d: %s", resp.StatusCode, e.Error)
|
||||
}
|
||||
return nil, fmt.Errorf("server returned %d", resp.StatusCode)
|
||||
}
|
||||
var entry ScheduleEntry
|
||||
if err := json.NewDecoder(resp.Body).Decode(&entry); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &entry, nil
|
||||
var entries []ScheduleEntry
|
||||
return entries, c.do(req, &entries)
|
||||
}
|
||||
|
||||
// AssignSchedule puts one user on call for the given dates.
|
||||
// AssignSchedule puts one team member on call for the given dates. Only a team
|
||||
// owner or an administrator may.
|
||||
//
|
||||
// The server holds one person per day and refuses a date somebody already has,
|
||||
// so replace is what takes a shift off its current holder. It is all-or-nothing
|
||||
// either way: a week of free and taken days moves as a unit, or not at all.
|
||||
func (c *Client) AssignSchedule(userID int64, dates []string, replace bool) ([]ScheduleEntry, error) {
|
||||
func (c *Client) AssignSchedule(teamID, userID int64, dates []string, replace bool) ([]ScheduleEntry, error) {
|
||||
body := struct {
|
||||
UserID int64 `json:"user_id"`
|
||||
Dates []string `json:"dates"`
|
||||
Replace bool `json:"replace,omitempty"`
|
||||
}{UserID: userID, Dates: dates, Replace: replace}
|
||||
req, err := c.newRequestWithBody(http.MethodPost, "/api/schedule", body)
|
||||
req, err := c.newRequestWithBody(http.MethodPost, fmt.Sprintf("/api/teams/%d/schedule", teamID), body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -388,14 +406,16 @@ func (c *Client) AssignSchedule(userID int64, dates []string, replace bool) ([]S
|
||||
return entries, c.do(req, &entries)
|
||||
}
|
||||
|
||||
func (c *Client) DeleteScheduleEntry(id int64) error {
|
||||
req, err := c.newRequest(http.MethodDelete, fmt.Sprintf("/api/schedule/%d", id))
|
||||
func (c *Client) DeleteScheduleEntry(teamID, id int64) error {
|
||||
req, err := c.newRequest(http.MethodDelete, fmt.Sprintf("/api/teams/%d/schedule/%d", teamID, id))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return c.do(req, nil)
|
||||
}
|
||||
|
||||
// ── Users ──────────────────────────────────────────────────────────────────
|
||||
|
||||
func (c *Client) ListUsers() ([]User, error) {
|
||||
req, err := c.newRequest(http.MethodGet, "/api/users")
|
||||
if err != nil {
|
||||
@@ -464,7 +484,7 @@ func (c *Client) DeleteAPIKey(userID, keyID int64) error {
|
||||
}
|
||||
|
||||
// Me returns the user the API key belongs to, and whether they have a web UI
|
||||
// password. Needs terdut-server v0.10.2 or later; older servers answer 404.
|
||||
// password.
|
||||
func (c *Client) Me() (*Me, error) {
|
||||
req, err := c.newRequest(http.MethodGet, "/api/me")
|
||||
if err != nil {
|
||||
@@ -488,7 +508,8 @@ func (c *Client) SetPassword(userID int64, password, current string) error {
|
||||
return c.do(req, nil)
|
||||
}
|
||||
|
||||
// HealthCheck calls GET /healthz (unauthenticated path, no auth needed but we send it anyway).
|
||||
// HealthCheck calls GET /healthz, which is unauthenticated and does no database
|
||||
// check, so it says the process is up, not that the API key works.
|
||||
func (c *Client) HealthCheck() error {
|
||||
req, err := http.NewRequest(http.MethodGet, c.baseURL+"/healthz", nil)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user