add a page for tickets from gitea
This commit is contained in:
191
internal/gitea/gitea.go
Normal file
191
internal/gitea/gitea.go
Normal file
@@ -0,0 +1,191 @@
|
||||
// Package gitea provides a minimal Gitea API client for label management.
|
||||
package gitea
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Client is a minimal Gitea API client.
|
||||
type Client struct {
|
||||
BaseURL string
|
||||
Token string
|
||||
http *http.Client
|
||||
}
|
||||
|
||||
// New creates a new Gitea client.
|
||||
func New(baseURL, token string) *Client {
|
||||
return &Client{
|
||||
BaseURL: baseURL,
|
||||
Token: token,
|
||||
http: &http.Client{Timeout: 10 * time.Second},
|
||||
}
|
||||
}
|
||||
|
||||
// Issue represents a Gitea issue.
|
||||
type Issue struct {
|
||||
Number int `json:"number"`
|
||||
Title string `json:"title"`
|
||||
HTMLURL string `json:"html_url"`
|
||||
Labels []Label `json:"labels"`
|
||||
}
|
||||
|
||||
// HasLabel reports whether the issue carries a label with the given name.
|
||||
func (i Issue) HasLabel(name string) bool {
|
||||
for _, l := range i.Labels {
|
||||
if l.Name == name {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// LabelID returns the ID of the first label matching name, or 0.
|
||||
func (i Issue) LabelID(name string) int64 {
|
||||
for _, l := range i.Labels {
|
||||
if l.Name == name {
|
||||
return l.ID
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// Label represents a Gitea label.
|
||||
type Label struct {
|
||||
ID int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Color string `json:"color"`
|
||||
}
|
||||
|
||||
func (c *Client) newRequest(method, path string, body []byte) (*http.Request, error) {
|
||||
var req *http.Request
|
||||
var err error
|
||||
if body != nil {
|
||||
req, err = http.NewRequest(method, c.BaseURL+path, bytes.NewReader(body))
|
||||
} else {
|
||||
req, err = http.NewRequest(method, c.BaseURL+path, nil)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Authorization", "token "+c.Token)
|
||||
if body != nil {
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
}
|
||||
return req, nil
|
||||
}
|
||||
|
||||
// ListOpenIssues returns open issues for the given owner/repo (up to 50).
|
||||
func (c *Client) ListOpenIssues(owner, repo string) ([]Issue, error) {
|
||||
return c.listIssues(owner, repo, "")
|
||||
}
|
||||
|
||||
// ListIssuesByLabel returns open issues that carry the named label (up to 50).
|
||||
func (c *Client) ListIssuesByLabel(owner, repo, label string) ([]Issue, error) {
|
||||
return c.listIssues(owner, repo, label)
|
||||
}
|
||||
|
||||
func (c *Client) listIssues(owner, repo, label string) ([]Issue, error) {
|
||||
path := fmt.Sprintf("/api/v1/repos/%s/%s/issues?type=issues&state=open&limit=50", owner, repo)
|
||||
if label != "" {
|
||||
path += "&labels=" + label
|
||||
}
|
||||
req, err := c.newRequest("GET", path, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := c.http.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("gitea: list issues: %s", resp.Status)
|
||||
}
|
||||
var issues []Issue
|
||||
if err := json.NewDecoder(resp.Body).Decode(&issues); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return issues, nil
|
||||
}
|
||||
|
||||
// GetOrCreateLabel finds a repo label by name, creating it if absent.
|
||||
func (c *Client) GetOrCreateLabel(owner, repo, name, color string) (*Label, error) {
|
||||
path := fmt.Sprintf("/api/v1/repos/%s/%s/labels", owner, repo)
|
||||
req, err := c.newRequest("GET", path, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := c.http.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
var labels []Label
|
||||
json.NewDecoder(resp.Body).Decode(&labels) //nolint:errcheck
|
||||
for _, l := range labels {
|
||||
if l.Name == name {
|
||||
return &l, nil
|
||||
}
|
||||
}
|
||||
|
||||
// Create the label.
|
||||
body, _ := json.Marshal(map[string]string{"name": name, "color": color})
|
||||
req2, err := c.newRequest("POST", path, body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp2, err := c.http.Do(req2)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp2.Body.Close()
|
||||
if resp2.StatusCode >= 300 {
|
||||
return nil, fmt.Errorf("gitea: create label: %s", resp2.Status)
|
||||
}
|
||||
var label Label
|
||||
if err := json.NewDecoder(resp2.Body).Decode(&label); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &label, nil
|
||||
}
|
||||
|
||||
// AddLabel adds a label to an issue by label ID.
|
||||
func (c *Client) AddLabel(owner, repo string, issueNum int, labelID int64) error {
|
||||
path := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/labels", owner, repo, issueNum)
|
||||
body, _ := json.Marshal(map[string][]int64{"labels": {labelID}})
|
||||
req, err := c.newRequest("POST", path, body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
resp, err := c.http.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode >= 300 {
|
||||
return fmt.Errorf("gitea: add label: %s", resp.Status)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemoveLabel removes a label from an issue by label ID.
|
||||
func (c *Client) RemoveLabel(owner, repo string, issueNum int, labelID int64) error {
|
||||
path := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/labels/%d", owner, repo, issueNum, labelID)
|
||||
req, err := c.newRequest("DELETE", path, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
resp, err := c.http.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode >= 300 && resp.StatusCode != http.StatusNoContent {
|
||||
return fmt.Errorf("gitea: remove label: %s", resp.Status)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -7,11 +7,13 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"ridgwaysystems.org/website/internal/blog"
|
||||
"ridgwaysystems.org/website/internal/changelog"
|
||||
"ridgwaysystems.org/website/internal/gitea"
|
||||
"ridgwaysystems.org/website/internal/newsletter"
|
||||
"ridgwaysystems.org/website/internal/status"
|
||||
)
|
||||
@@ -91,6 +93,13 @@ func (h *Handler) AdminRouter(w http.ResponseWriter, r *http.Request) {
|
||||
h.requireAuth(h.adminNewsletter)(w, r)
|
||||
}
|
||||
|
||||
case path == "/admin/outages":
|
||||
if r.Method == http.MethodPost {
|
||||
h.requireAuth(h.adminOutagesPost)(w, r)
|
||||
} else {
|
||||
h.requireAuth(h.adminOutagesGet)(w, r)
|
||||
}
|
||||
|
||||
default:
|
||||
h.renderErr(w, http.StatusNotFound, "Admin page not found.")
|
||||
}
|
||||
@@ -549,6 +558,74 @@ func (h *Handler) adminChangelogDelete(w http.ResponseWriter, r *http.Request) {
|
||||
http.Redirect(w, r, "/admin/changelog?flash=Entry+deleted", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
// --- Outage tagger ---
|
||||
|
||||
type adminOutagesData struct {
|
||||
Issues []gitea.Issue
|
||||
Label string
|
||||
Flash string
|
||||
Error string
|
||||
}
|
||||
|
||||
func (h *Handler) adminOutagesGet(w http.ResponseWriter, r *http.Request) {
|
||||
if h.gitea == nil || h.giteaOwner == "" || h.giteaRepo == "" {
|
||||
h.render(w, "admin-outages", adminOutagesData{Error: "Gitea not configured. Set GITEA_URL, GITEA_TOKEN, GITEA_OWNER, and GITEA_REPO."})
|
||||
return
|
||||
}
|
||||
issues, err := h.gitea.ListOpenIssues(h.giteaOwner, h.giteaRepo)
|
||||
if err != nil {
|
||||
h.render(w, "admin-outages", adminOutagesData{Error: "Could not load issues: " + err.Error()})
|
||||
return
|
||||
}
|
||||
h.render(w, "admin-outages", adminOutagesData{
|
||||
Issues: issues,
|
||||
Label: h.giteaLabel,
|
||||
Flash: r.URL.Query().Get("flash"),
|
||||
})
|
||||
}
|
||||
|
||||
func (h *Handler) adminOutagesPost(w http.ResponseWriter, r *http.Request) {
|
||||
if err := r.ParseForm(); err != nil {
|
||||
h.renderErr(w, http.StatusBadRequest, "Bad form data.")
|
||||
return
|
||||
}
|
||||
if h.gitea == nil || h.giteaOwner == "" || h.giteaRepo == "" {
|
||||
h.renderErr(w, http.StatusServiceUnavailable, "Gitea not configured.")
|
||||
return
|
||||
}
|
||||
|
||||
action := r.FormValue("action") // "add" or "remove"
|
||||
issueNum, err := strconv.Atoi(r.FormValue("issue"))
|
||||
if err != nil || issueNum <= 0 {
|
||||
h.renderErr(w, http.StatusBadRequest, "Invalid issue number.")
|
||||
return
|
||||
}
|
||||
|
||||
label, err := h.gitea.GetOrCreateLabel(h.giteaOwner, h.giteaRepo, h.giteaLabel, "#e11d48")
|
||||
if err != nil {
|
||||
h.renderErr(w, http.StatusInternalServerError, "Label error: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
switch action {
|
||||
case "add":
|
||||
if err := h.gitea.AddLabel(h.giteaOwner, h.giteaRepo, issueNum, label.ID); err != nil {
|
||||
h.renderErr(w, http.StatusInternalServerError, "Could not add label: "+err.Error())
|
||||
return
|
||||
}
|
||||
case "remove":
|
||||
if err := h.gitea.RemoveLabel(h.giteaOwner, h.giteaRepo, issueNum, label.ID); err != nil {
|
||||
h.renderErr(w, http.StatusInternalServerError, "Could not remove label: "+err.Error())
|
||||
return
|
||||
}
|
||||
default:
|
||||
h.renderErr(w, http.StatusBadRequest, "Unknown action.")
|
||||
return
|
||||
}
|
||||
|
||||
http.Redirect(w, r, "/admin/outages?flash=Updated+#"+strconv.Itoa(issueNum), http.StatusSeeOther)
|
||||
}
|
||||
|
||||
// sanitizeSlug ensures a slug is filesystem-safe.
|
||||
func sanitizeSlug(s string) string {
|
||||
s = strings.ToLower(strings.TrimSpace(s))
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
"time"
|
||||
|
||||
"ridgwaysystems.org/website/internal/blog"
|
||||
"ridgwaysystems.org/website/internal/gitea"
|
||||
"ridgwaysystems.org/website/internal/newsletter"
|
||||
"ridgwaysystems.org/website/internal/ratelimit"
|
||||
"ridgwaysystems.org/website/internal/status"
|
||||
@@ -27,6 +28,10 @@ type Handler struct {
|
||||
contactEmail string
|
||||
devMode bool
|
||||
postLimit *ratelimit.Limiter // rate-limits contact + newsletter POSTs
|
||||
gitea *gitea.Client
|
||||
giteaOwner string
|
||||
giteaRepo string
|
||||
giteaLabel string
|
||||
}
|
||||
|
||||
// New creates a Handler. dataDir is the path to the data/ directory.
|
||||
@@ -39,6 +44,12 @@ func New(store *blog.Store, news *newsletter.Store, dataDir string) *Handler {
|
||||
contactEmail: getenv("CONTACT_EMAIL", "hire@ridgwaysystems.org"),
|
||||
devMode: os.Getenv("DEV") == "1",
|
||||
postLimit: ratelimit.New(10*time.Minute, 5),
|
||||
giteaOwner: getenv("GITEA_OWNER", ""),
|
||||
giteaRepo: getenv("GITEA_REPO", ""),
|
||||
giteaLabel: getenv("GITEA_LABEL", "planned-outage"),
|
||||
}
|
||||
if url := os.Getenv("GITEA_URL"); url != "" {
|
||||
h.gitea = gitea.New(url, os.Getenv("GITEA_TOKEN"))
|
||||
}
|
||||
if !h.devMode {
|
||||
h.templates = mustLoadTemplates()
|
||||
@@ -95,6 +106,7 @@ func mustLoadTemplates() map[string]*template.Template {
|
||||
{"admin-newsletter", "templates/admin/newsletter.html"},
|
||||
{"admin-changelog", "templates/admin/changelog.html"},
|
||||
{"admin-changelog-editor", "templates/admin/changelog-editor.html"},
|
||||
{"admin-outages", "templates/admin/outages.html"},
|
||||
}
|
||||
|
||||
for _, p := range pages {
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
"ridgwaysystems.org/website/internal/blog"
|
||||
"ridgwaysystems.org/website/internal/changelog"
|
||||
"ridgwaysystems.org/website/internal/feed"
|
||||
"ridgwaysystems.org/website/internal/gitea"
|
||||
"ridgwaysystems.org/website/internal/mailer"
|
||||
"ridgwaysystems.org/website/internal/status"
|
||||
"ridgwaysystems.org/website/internal/uptime"
|
||||
@@ -166,9 +167,10 @@ type serviceHistory struct {
|
||||
|
||||
// statusData is passed to the status template.
|
||||
type statusData struct {
|
||||
Page *status.Page
|
||||
LastChecked string
|
||||
History map[string]serviceHistory // keyed by service name
|
||||
Page *status.Page
|
||||
LastChecked string
|
||||
History map[string]serviceHistory // keyed by service name
|
||||
PlannedOutages []gitea.Issue
|
||||
}
|
||||
|
||||
func (h *Handler) Status(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -188,7 +190,17 @@ func (h *Handler) Status(w http.ResponseWriter, r *http.Request) {
|
||||
UptimePct: uptime.UptimePct(uptimePath, svc.Name),
|
||||
}
|
||||
}
|
||||
h.render(w, "status", statusData{Page: p, LastChecked: lastChecked, History: history})
|
||||
// Fetch planned outages from Gitea — best-effort, failure is non-fatal.
|
||||
var plannedOutages []gitea.Issue
|
||||
if h.gitea != nil && h.giteaOwner != "" && h.giteaRepo != "" {
|
||||
if issues, err := h.gitea.ListIssuesByLabel(h.giteaOwner, h.giteaRepo, h.giteaLabel); err == nil {
|
||||
plannedOutages = issues
|
||||
} else {
|
||||
log.Printf("status: gitea planned outages: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
h.render(w, "status", statusData{Page: p, LastChecked: lastChecked, History: history, PlannedOutages: plannedOutages})
|
||||
}
|
||||
|
||||
// changelogData is passed to the changelog template.
|
||||
|
||||
Reference in New Issue
Block a user