feat: add label multi-select to Create Issue form
Add dynamic label selection when creating a new issue: - New GET /issues/new/labels endpoint returns label checkboxes for a repo - When a repo is selected, labels are fetched via HTMX and displayed as checkboxes with colored label badges - CreateIssue handler now parses label_ids from form and passes them to the Gitea API - Shows "No labels available" message when repo has no labels Closes leeworks-agents/gitea-mobile#67 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -39,6 +39,7 @@ func (h *Handler) RegisterRoutes(mux *http.ServeMux) {
|
||||
// Issues.
|
||||
mux.HandleFunc("GET /issues", h.ListIssues)
|
||||
mux.HandleFunc("GET /issues/new", h.NewIssue)
|
||||
mux.HandleFunc("GET /issues/new/labels", h.NewIssueLabels)
|
||||
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)
|
||||
@@ -567,6 +568,38 @@ func (h *Handler) NewIssue(w http.ResponseWriter, r *http.Request) {
|
||||
renderPage(w, r, "New Issue", "issues", buf.String())
|
||||
}
|
||||
|
||||
// NewIssueLabels handles GET /issues/new/labels — returns label checkboxes for a repo.
|
||||
func (h *Handler) NewIssueLabels(w http.ResponseWriter, r *http.Request) {
|
||||
token := getToken(r)
|
||||
owner := r.URL.Query().Get("owner")
|
||||
repo := r.URL.Query().Get("repo")
|
||||
|
||||
if owner == "" || repo == "" {
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
fmt.Fprint(w, `<span class="empty">Select a repository first.</span>`)
|
||||
return
|
||||
}
|
||||
|
||||
labels, err := h.Client.GetRepoLabels(r.Context(), token, owner, repo)
|
||||
if err != nil {
|
||||
slog.Error("failed to fetch labels", "error", err, "owner", owner, "repo", repo)
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
fmt.Fprint(w, `<span class="empty">Error loading labels.</span>`)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
if len(labels) == 0 {
|
||||
fmt.Fprint(w, `<span class="empty">No labels available for this repository.</span>`)
|
||||
return
|
||||
}
|
||||
|
||||
for _, l := range labels {
|
||||
fmt.Fprintf(w, `<label style="display:inline-block;margin:0.25rem 0.5rem 0.25rem 0;cursor:pointer;"><input type="checkbox" name="label_ids" value="%d" style="margin-right:0.25rem;"> <span class="label" style="color:#%s;border:1px solid #%s">%s</span></label>`,
|
||||
l.ID, template.HTMLEscapeString(l.Color), template.HTMLEscapeString(l.Color), template.HTMLEscapeString(l.Name))
|
||||
}
|
||||
}
|
||||
|
||||
// CreateIssue handles POST /issues.
|
||||
func (h *Handler) CreateIssue(w http.ResponseWriter, r *http.Request) {
|
||||
token := getToken(r)
|
||||
@@ -580,6 +613,15 @@ func (h *Handler) CreateIssue(w http.ResponseWriter, r *http.Request) {
|
||||
title := r.FormValue("title")
|
||||
body := r.FormValue("body")
|
||||
|
||||
// Parse label IDs from form checkboxes.
|
||||
var labelIDs []int64
|
||||
for _, idStr := range r.Form["label_ids"] {
|
||||
id, err := strconv.ParseInt(idStr, 10, 64)
|
||||
if err == nil {
|
||||
labelIDs = append(labelIDs, id)
|
||||
}
|
||||
}
|
||||
|
||||
if owner == "" || repo == "" || title == "" {
|
||||
if isHTMX(r) {
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
@@ -591,7 +633,7 @@ func (h *Handler) CreateIssue(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
issue, err := h.Client.CreateIssue(r.Context(), token, owner, repo, title, body, nil)
|
||||
issue, err := h.Client.CreateIssue(r.Context(), token, owner, repo, title, body, labelIDs)
|
||||
if err != nil {
|
||||
slog.Error("failed to create issue", "error", err)
|
||||
if isHTMX(r) {
|
||||
|
||||
@@ -20,6 +20,11 @@
|
||||
<input type="hidden" name="repo" id="repo-input">
|
||||
</div>
|
||||
|
||||
<div class="form-group" id="label-section" style="display:none;">
|
||||
<label>Labels</label>
|
||||
<div id="label-list"></div>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="title">Title</label>
|
||||
<input type="text" id="title" name="title" placeholder="Issue title" required>
|
||||
@@ -52,7 +57,19 @@
|
||||
}
|
||||
}
|
||||
|
||||
repoSelect.addEventListener('change', splitOwnerRepo);
|
||||
repoSelect.addEventListener('change', function() {
|
||||
splitOwnerRepo();
|
||||
var labelSection = document.getElementById('label-section');
|
||||
var labelList = document.getElementById('label-list');
|
||||
if (ownerInput.value && repoInput.value) {
|
||||
labelList.innerHTML = '<span class="empty">Loading labels...</span>';
|
||||
labelSection.style.display = 'block';
|
||||
htmx.ajax('GET', '/issues/new/labels?owner=' + encodeURIComponent(ownerInput.value) + '&repo=' + encodeURIComponent(repoInput.value), {target: '#label-list', swap: 'innerHTML'});
|
||||
} else {
|
||||
labelSection.style.display = 'none';
|
||||
labelList.innerHTML = '';
|
||||
}
|
||||
});
|
||||
|
||||
// Validate before HTMX submit.
|
||||
document.getElementById('create-issue-form').addEventListener('htmx:configRequest', function(evt) {
|
||||
|
||||
Reference in New Issue
Block a user