|
|
|
@@ -38,8 +38,11 @@ func (h *Handler) RegisterRoutes(mux *http.ServeMux) {
|
|
|
|
|
|
|
|
|
|
// Issues.
|
|
|
|
|
mux.HandleFunc("GET /issues", h.ListIssues)
|
|
|
|
|
mux.HandleFunc("GET /issues/new", h.NewIssue)
|
|
|
|
|
mux.HandleFunc("POST /issues", h.CreateIssue)
|
|
|
|
|
mux.HandleFunc("POST /issues/{owner}/{repo}/{index}/labels", h.ApplyLabels)
|
|
|
|
|
mux.HandleFunc("POST /issues/{owner}/{repo}/{index}/close", h.CloseIssue)
|
|
|
|
|
mux.HandleFunc("POST /issues/{owner}/{repo}/{index}/comment", h.AddComment)
|
|
|
|
|
|
|
|
|
|
// Issue detail.
|
|
|
|
|
mux.HandleFunc("GET /issues/{owner}/{repo}/{index}", h.IssueDetail)
|
|
|
|
@@ -180,158 +183,155 @@ func (h *Handler) Dashboard(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
token := getToken(r)
|
|
|
|
|
orgs := h.getUserOrgs(r)
|
|
|
|
|
|
|
|
|
|
type dashboardData struct {
|
|
|
|
|
Items []giteaclient.TriageItem
|
|
|
|
|
Error string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var data dashboardData
|
|
|
|
|
|
|
|
|
|
if len(orgs) == 0 {
|
|
|
|
|
renderPage(w, r, "Dashboard", "dashboard",
|
|
|
|
|
`<h1>Dashboard</h1><p class="empty">No organizations found. Check your token permissions.</p>`)
|
|
|
|
|
return
|
|
|
|
|
data.Error = "No organizations found. Check your token permissions."
|
|
|
|
|
} else {
|
|
|
|
|
queue, err := h.Client.GetTriageQueue(r.Context(), token, orgs)
|
|
|
|
|
if err != nil {
|
|
|
|
|
slog.Error("failed to get triage queue", "error", err)
|
|
|
|
|
data.Error = "Error loading triage queue."
|
|
|
|
|
} else {
|
|
|
|
|
data.Items = queue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
queue, err := h.Client.GetTriageQueue(r.Context(), token, orgs)
|
|
|
|
|
tmpl, err := template.ParseFiles("internal/templates/dashboard.html")
|
|
|
|
|
if err != nil {
|
|
|
|
|
slog.Error("failed to get triage queue", "error", err)
|
|
|
|
|
renderPage(w, r, "Dashboard", "dashboard",
|
|
|
|
|
`<h1>Dashboard</h1><p class="empty">Error loading triage queue.</p>`)
|
|
|
|
|
slog.Error("failed to parse dashboard template", "error", err)
|
|
|
|
|
http.Error(w, "template error", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(queue) == 0 {
|
|
|
|
|
renderPage(w, r, "Dashboard", "dashboard",
|
|
|
|
|
`<h1>Dashboard</h1><p class="empty">No items need attention. Nice work!</p>`)
|
|
|
|
|
var buf strings.Builder
|
|
|
|
|
if err := tmpl.ExecuteTemplate(&buf, "content", data); err != nil {
|
|
|
|
|
slog.Error("failed to execute dashboard template", "error", err)
|
|
|
|
|
http.Error(w, "template error", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
content := `<h1>Dashboard</h1>`
|
|
|
|
|
for _, item := range queue {
|
|
|
|
|
typeBadge := `<span class="type-badge type-issue">issue</span>`
|
|
|
|
|
if item.Type == "pull" {
|
|
|
|
|
typeBadge = `<span class="type-badge type-pull">PR</span>`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
labels := ""
|
|
|
|
|
for _, l := range item.Labels {
|
|
|
|
|
color := "#8b949e"
|
|
|
|
|
switch l {
|
|
|
|
|
case "P1":
|
|
|
|
|
color = "#f85149"
|
|
|
|
|
case "P2":
|
|
|
|
|
color = "#d29922"
|
|
|
|
|
case "P3":
|
|
|
|
|
color = "#58a6ff"
|
|
|
|
|
}
|
|
|
|
|
labels += fmt.Sprintf(`<span class="label" style="color:%s;border:1px solid %s">%s</span>`, color, color, template.HTMLEscapeString(l))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
content += fmt.Sprintf(`<div class="card">
|
|
|
|
|
<div class="card-title">%s %s</div>
|
|
|
|
|
<div class="card-meta">%s/%s #%d %s</div>
|
|
|
|
|
</div>`, typeBadge, template.HTMLEscapeString(item.Title),
|
|
|
|
|
template.HTMLEscapeString(item.RepoOwner),
|
|
|
|
|
template.HTMLEscapeString(item.RepoName),
|
|
|
|
|
item.Number, labels)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
renderPage(w, r, "Dashboard", "dashboard", content)
|
|
|
|
|
renderPage(w, r, "Dashboard", "dashboard", buf.String())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ListIssues handles GET /issues.
|
|
|
|
|
func (h *Handler) ListIssues(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
token := getToken(r)
|
|
|
|
|
orgs := h.getUserOrgs(r)
|
|
|
|
|
orgNames := h.getUserOrgs(r)
|
|
|
|
|
|
|
|
|
|
if len(orgs) == 0 {
|
|
|
|
|
renderPage(w, r, "Issues", "issues",
|
|
|
|
|
`<h1>Issues</h1><p class="empty">No organizations found.</p>`)
|
|
|
|
|
return
|
|
|
|
|
type issuesData struct {
|
|
|
|
|
Issues []giteaclient.Issue
|
|
|
|
|
Orgs []string
|
|
|
|
|
SelectedOrg string
|
|
|
|
|
SelectedState string
|
|
|
|
|
HasMore bool
|
|
|
|
|
NextPage int
|
|
|
|
|
Error string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
issues, err := h.Client.ListAllIssues(r.Context(), token, orgs)
|
|
|
|
|
selectedOrg := r.URL.Query().Get("org")
|
|
|
|
|
selectedState := r.URL.Query().Get("state")
|
|
|
|
|
if selectedState == "" {
|
|
|
|
|
selectedState = "open"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data := issuesData{
|
|
|
|
|
Orgs: orgNames,
|
|
|
|
|
SelectedOrg: selectedOrg,
|
|
|
|
|
SelectedState: selectedState,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(orgNames) == 0 {
|
|
|
|
|
data.Error = "No organizations found."
|
|
|
|
|
} else {
|
|
|
|
|
// Filter to selected org if specified.
|
|
|
|
|
queryOrgs := orgNames
|
|
|
|
|
if selectedOrg != "" {
|
|
|
|
|
queryOrgs = []string{selectedOrg}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
issues, err := h.Client.ListAllIssues(r.Context(), token, queryOrgs)
|
|
|
|
|
if err != nil {
|
|
|
|
|
slog.Error("failed to list issues", "error", err)
|
|
|
|
|
data.Error = "Error loading issues."
|
|
|
|
|
} else {
|
|
|
|
|
data.Issues = issues
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tmpl, err := template.ParseFiles("internal/templates/issues.html")
|
|
|
|
|
if err != nil {
|
|
|
|
|
slog.Error("failed to list issues", "error", err)
|
|
|
|
|
renderPage(w, r, "Issues", "issues",
|
|
|
|
|
`<h1>Issues</h1><p class="empty">Error loading issues.</p>`)
|
|
|
|
|
slog.Error("failed to parse issues template", "error", err)
|
|
|
|
|
http.Error(w, "template error", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(issues) == 0 {
|
|
|
|
|
renderPage(w, r, "Issues", "issues",
|
|
|
|
|
`<h1>Issues</h1><p class="empty">No open issues found.</p>`)
|
|
|
|
|
var buf strings.Builder
|
|
|
|
|
if err := tmpl.ExecuteTemplate(&buf, "content", data); err != nil {
|
|
|
|
|
slog.Error("failed to execute issues template", "error", err)
|
|
|
|
|
http.Error(w, "template error", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
content := `<h1>Issues</h1>`
|
|
|
|
|
for _, issue := range issues {
|
|
|
|
|
labels := ""
|
|
|
|
|
for _, l := range issue.Labels {
|
|
|
|
|
labels += fmt.Sprintf(`<span class="label" style="color:#%s;border:1px solid #%s">%s</span>`,
|
|
|
|
|
l.Color, l.Color, template.HTMLEscapeString(l.Name))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assignee := ""
|
|
|
|
|
if issue.Assignee != nil {
|
|
|
|
|
assignee = fmt.Sprintf(` · %s`, template.HTMLEscapeString(issue.Assignee.Login))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
content += fmt.Sprintf(`<div class="card">
|
|
|
|
|
<div class="card-title">%s</div>
|
|
|
|
|
<div class="card-meta">%s/%s #%d %s%s</div>
|
|
|
|
|
</div>`, template.HTMLEscapeString(issue.Title),
|
|
|
|
|
template.HTMLEscapeString(issue.RepoOwner),
|
|
|
|
|
template.HTMLEscapeString(issue.RepoName),
|
|
|
|
|
issue.Number, labels, assignee)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
renderPage(w, r, "Issues", "issues", content)
|
|
|
|
|
renderPage(w, r, "Issues", "issues", buf.String())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ListPulls handles GET /pulls.
|
|
|
|
|
func (h *Handler) ListPulls(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
token := getToken(r)
|
|
|
|
|
orgs := h.getUserOrgs(r)
|
|
|
|
|
orgNames := h.getUserOrgs(r)
|
|
|
|
|
|
|
|
|
|
if len(orgs) == 0 {
|
|
|
|
|
renderPage(w, r, "Pull Requests", "pulls",
|
|
|
|
|
`<h1>Pull Requests</h1><p class="empty">No organizations found.</p>`)
|
|
|
|
|
return
|
|
|
|
|
type pullsData struct {
|
|
|
|
|
Pulls []giteaclient.PullRequest
|
|
|
|
|
Orgs []string
|
|
|
|
|
SelectedOrg string
|
|
|
|
|
Error string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
prs, err := h.Client.ListAllPullRequests(r.Context(), token, orgs)
|
|
|
|
|
selectedOrg := r.URL.Query().Get("org")
|
|
|
|
|
|
|
|
|
|
data := pullsData{
|
|
|
|
|
Orgs: orgNames,
|
|
|
|
|
SelectedOrg: selectedOrg,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(orgNames) == 0 {
|
|
|
|
|
data.Error = "No organizations found."
|
|
|
|
|
} else {
|
|
|
|
|
queryOrgs := orgNames
|
|
|
|
|
if selectedOrg != "" {
|
|
|
|
|
queryOrgs = []string{selectedOrg}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
prs, err := h.Client.ListAllPullRequests(r.Context(), token, queryOrgs)
|
|
|
|
|
if err != nil {
|
|
|
|
|
slog.Error("failed to list pull requests", "error", err)
|
|
|
|
|
data.Error = "Error loading pull requests."
|
|
|
|
|
} else {
|
|
|
|
|
data.Pulls = prs
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tmpl, err := template.ParseFiles("internal/templates/pulls.html")
|
|
|
|
|
if err != nil {
|
|
|
|
|
slog.Error("failed to list pull requests", "error", err)
|
|
|
|
|
renderPage(w, r, "Pull Requests", "pulls",
|
|
|
|
|
`<h1>Pull Requests</h1><p class="empty">Error loading pull requests.</p>`)
|
|
|
|
|
slog.Error("failed to parse pulls template", "error", err)
|
|
|
|
|
http.Error(w, "template error", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(prs) == 0 {
|
|
|
|
|
renderPage(w, r, "Pull Requests", "pulls",
|
|
|
|
|
`<h1>Pull Requests</h1><p class="empty">No open pull requests found.</p>`)
|
|
|
|
|
var buf strings.Builder
|
|
|
|
|
if err := tmpl.ExecuteTemplate(&buf, "content", data); err != nil {
|
|
|
|
|
slog.Error("failed to execute pulls template", "error", err)
|
|
|
|
|
http.Error(w, "template error", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
content := `<h1>Pull Requests</h1>`
|
|
|
|
|
for _, pr := range prs {
|
|
|
|
|
labels := ""
|
|
|
|
|
for _, l := range pr.Labels {
|
|
|
|
|
labels += fmt.Sprintf(`<span class="label" style="color:#%s;border:1px solid #%s">%s</span>`,
|
|
|
|
|
l.Color, l.Color, template.HTMLEscapeString(l.Name))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stats := fmt.Sprintf(`<span style="color:#3fb950">+%d</span> <span style="color:#f85149">-%d</span>`, pr.Additions, pr.Deletions)
|
|
|
|
|
mergeStatus := ""
|
|
|
|
|
if pr.Mergeable {
|
|
|
|
|
mergeStatus = `<span style="color:#3fb950;font-size:0.7rem;">mergeable</span>`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
content += fmt.Sprintf(`<div class="card">
|
|
|
|
|
<div class="card-title"><span class="type-badge type-pull">PR</span> %s</div>
|
|
|
|
|
<div class="card-meta">%s/%s #%d %s %s %s</div>
|
|
|
|
|
</div>`, template.HTMLEscapeString(pr.Title),
|
|
|
|
|
template.HTMLEscapeString(pr.RepoOwner),
|
|
|
|
|
template.HTMLEscapeString(pr.RepoName),
|
|
|
|
|
pr.Number, labels, stats, mergeStatus)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
renderPage(w, r, "Pull Requests", "pulls", content)
|
|
|
|
|
renderPage(w, r, "Pull Requests", "pulls", buf.String())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IssueDetail handles GET /issues/{owner}/{repo}/{index}.
|
|
|
|
@@ -444,6 +444,41 @@ func (h *Handler) PullDetail(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
renderPage(w, r, fmt.Sprintf("PR #%d", index), "pulls", buf.String())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewIssue handles GET /issues/new — renders the create-issue form.
|
|
|
|
|
func (h *Handler) NewIssue(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
token := getToken(r)
|
|
|
|
|
|
|
|
|
|
repos, err := h.Client.ListOrgsAndRepos(r.Context(), token)
|
|
|
|
|
if err != nil {
|
|
|
|
|
slog.Error("failed to list repos for new issue form", "error", err)
|
|
|
|
|
renderPage(w, r, "New Issue", "issues",
|
|
|
|
|
`<h1>New Issue</h1><p class="empty">Error loading repositories.</p>`)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tmpl, err := template.ParseFiles("internal/templates/create_issue.html")
|
|
|
|
|
if err != nil {
|
|
|
|
|
slog.Error("failed to parse create_issue template", "error", err)
|
|
|
|
|
http.Error(w, "template error", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type templateData struct {
|
|
|
|
|
Repos map[string][]giteaclient.Repo
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data := templateData{Repos: repos}
|
|
|
|
|
|
|
|
|
|
var buf strings.Builder
|
|
|
|
|
if err := tmpl.ExecuteTemplate(&buf, "content", data); err != nil {
|
|
|
|
|
slog.Error("failed to execute create_issue template", "error", err)
|
|
|
|
|
http.Error(w, "template error", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
renderPage(w, r, "New Issue", "issues", buf.String())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CreateIssue handles POST /issues.
|
|
|
|
|
func (h *Handler) CreateIssue(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
token := getToken(r)
|
|
|
|
@@ -537,6 +572,77 @@ func (h *Handler) ApplyLabels(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
http.Redirect(w, r, fmt.Sprintf("/issues/%s/%s/%d", owner, repo, index), http.StatusSeeOther)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CloseIssue handles POST /issues/{owner}/{repo}/{index}/close.
|
|
|
|
|
func (h *Handler) CloseIssue(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
token := getToken(r)
|
|
|
|
|
owner := r.PathValue("owner")
|
|
|
|
|
repo := r.PathValue("repo")
|
|
|
|
|
indexStr := r.PathValue("index")
|
|
|
|
|
|
|
|
|
|
index, err := strconv.ParseInt(indexStr, 10, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(w, "invalid issue index", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := h.Client.CloseIssue(r.Context(), token, owner, repo, index); err != nil {
|
|
|
|
|
slog.Error("failed to close issue", "error", err, "owner", owner, "repo", repo, "index", index)
|
|
|
|
|
http.Error(w, "failed to close issue", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if isHTMX(r) {
|
|
|
|
|
w.Header().Set("HX-Redirect", fmt.Sprintf("/issues/%s/%s/%d", owner, repo, index))
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
http.Redirect(w, r, fmt.Sprintf("/issues/%s/%s/%d", owner, repo, index), http.StatusSeeOther)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// AddComment handles POST /issues/{owner}/{repo}/{index}/comment.
|
|
|
|
|
func (h *Handler) AddComment(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
token := getToken(r)
|
|
|
|
|
owner := r.PathValue("owner")
|
|
|
|
|
repo := r.PathValue("repo")
|
|
|
|
|
indexStr := r.PathValue("index")
|
|
|
|
|
|
|
|
|
|
index, err := strconv.ParseInt(indexStr, 10, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(w, "invalid issue index", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := r.ParseForm(); err != nil {
|
|
|
|
|
http.Error(w, "bad request", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
body := r.FormValue("body")
|
|
|
|
|
if body == "" {
|
|
|
|
|
http.Error(w, "comment body is required", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
comment, err := h.Client.PostComment(r.Context(), token, owner, repo, index, body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
slog.Error("failed to post comment", "error", err, "owner", owner, "repo", repo, "index", index)
|
|
|
|
|
http.Error(w, "failed to post comment", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if isHTMX(r) {
|
|
|
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
|
|
|
fmt.Fprintf(w, `<div class="card comment">
|
|
|
|
|
<div class="card-meta">%s · %s</div>
|
|
|
|
|
<div class="card-body">%s</div>
|
|
|
|
|
</div>`, template.HTMLEscapeString(comment.User), template.HTMLEscapeString(comment.CreatedAt), template.HTMLEscapeString(comment.Body))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
http.Redirect(w, r, fmt.Sprintf("/issues/%s/%s/%d", owner, repo, index), http.StatusSeeOther)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SubmitReview handles POST /pulls/{owner}/{repo}/{index}/review.
|
|
|
|
|
func (h *Handler) SubmitReview(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
token := getToken(r)
|
|
|
|
|