internal/httpd/queries.go

6521399c1c78577eb00f1bcf8947c0900b9cb62e
gitbay/internal/httpd/queries.go

136 lines · 4077 bytes

  1package httpd
  2
  3import (
  4	"net/http"
  5	"net/url"
  6	"strconv"
  7	"strings"
  8
  9	"gitbay.org/gitbay/internal/control"
 10	"gitbay.org/gitbay/internal/protocol"
 11	"gitbay.org/gitbay/internal/store"
 12)
 13
 14// queryPerPage is how many rows a saved query's page shows before
 15// offering the next, through query run's own cursor.
 16const queryPerPage = 50
 17
 18// queriesPage lists the viewer's saved queries (/{owner}/-/queries), runs
 19// an ad-hoc query (?q=...), or runs a saved one (/{owner}/-/queries/{name}).
 20// Saved queries are private: under anyone else's name the page is not found.
 21func (s *Server) queriesPage(w http.ResponseWriter, r *http.Request, viewer store.User) {
 22	if r.PathValue("owner") != viewer.Username {
 23		s.notFound(w, r)
 24		return
 25	}
 26	name := r.PathValue("name")
 27	adHoc := strings.TrimSpace(r.URL.Query().Get("q"))
 28	data := struct {
 29		basePage
 30		Tab    string
 31		Saved  []control.SavedQueryOut
 32		Query  control.SavedQueryOut
 33		AdHoc  string
 34		Count  int
 35		Items  []control.QueryItem
 36		Next   string
 37		Notice string
 38	}{basePage: s.baseFor(viewer), Tab: "dashboard", AdHoc: adHoc, Notice: s.takeFlash(w, r)}
 39	if name == "" {
 40		if msg, ok := s.runControlInto(viewer, []string{"query", "list"}, &data.Saved); !ok {
 41			http.Error(w, msg, http.StatusInternalServerError)
 42			return
 43		}
 44		if adHoc != "" {
 45			items, count, next, err := control.RunAdHocQuery(s.st, viewer, adHoc, r.URL.Query().Get("cursor"), queryPerPage)
 46			if err != nil {
 47				data.Notice = err.Error()
 48			} else {
 49				data.Items = items
 50				data.Count = count
 51				if next != "" {
 52					data.Next = "?" + url.Values{"q": {adHoc}, "cursor": {next}}.Encode()
 53				}
 54			}
 55		}
 56		s.render(w, "queries.html", data)
 57		return
 58	}
 59	switch code, msg := s.runControlIntoCode(viewer, []string{"query", "show", name}, &data.Query); code {
 60	case protocol.ExitOK:
 61	case protocol.ExitNotFound:
 62		s.notFound(w, r)
 63		return
 64	default:
 65		http.Error(w, msg, http.StatusInternalServerError)
 66		return
 67	}
 68	if data.Query.Count != nil {
 69		data.Count = *data.Query.Count
 70	}
 71	argv := []string{"query", "run", name, "--limit", strconv.Itoa(queryPerPage)}
 72	if cursor := r.URL.Query().Get("cursor"); cursor != "" {
 73		argv = append(argv, "--cursor", cursor)
 74	}
 75	var page struct {
 76		Items []control.QueryItem `json:"items"`
 77		Next  string              `json:"next"`
 78	}
 79	switch code, msg := s.runControlIntoCode(viewer, argv, &page); code {
 80	case protocol.ExitOK:
 81	case protocol.ExitUsage:
 82		http.Error(w, msg, http.StatusBadRequest)
 83		return
 84	default:
 85		http.Error(w, msg, http.StatusInternalServerError)
 86		return
 87	}
 88	data.Items = page.Items
 89	if page.Next != "" {
 90		data.Next = "?" + url.Values{"cursor": {page.Next}}.Encode()
 91	}
 92	s.render(w, "queries.html", data)
 93}
 94
 95// queriesSubmit saves, pins, unpins, or removes a saved query through the
 96// same query commands the CLI runs.
 97func (s *Server) queriesSubmit(w http.ResponseWriter, r *http.Request, viewer store.User) {
 98	if r.PathValue("owner") != viewer.Username {
 99		s.notFound(w, r)
100		return
101	}
102	base := "/" + viewer.Username + "/-/queries"
103	action := r.FormValue("action")
104	name := strings.TrimSpace(r.FormValue("name"))
105	switch action {
106	case "save":
107		qText := strings.TrimSpace(r.FormValue("query"))
108		argv := []string{"query", "save", name, qText}
109		if r.FormValue("force") == "on" {
110			argv = append(argv, "--force")
111		}
112		if _, msg, ok := s.runControl(viewer, argv); !ok {
113			s.setFlash(w, msg)
114			dest := base
115			if qText != "" {
116				dest += "?" + url.Values{"q": {qText}}.Encode()
117			}
118			http.Redirect(w, r, dest, http.StatusSeeOther)
119			return
120		}
121		http.Redirect(w, r, base+"/"+url.PathEscape(name), http.StatusSeeOther)
122	case "pin", "unpin":
123		if _, msg, ok := s.runControl(viewer, []string{"query", action, name}); !ok {
124			s.setFlash(w, msg)
125		}
126		http.Redirect(w, r, safeReturnTo(r, base), http.StatusSeeOther)
127	case "remove":
128		if _, msg, ok := s.runControl(viewer, []string{"query", "remove", name}); !ok {
129			s.setFlash(w, msg)
130		}
131		http.Redirect(w, r, base, http.StatusSeeOther)
132	default:
133		s.setFlash(w, "unknown query action")
134		http.Redirect(w, r, base, http.StatusSeeOther)
135	}
136}
Keyboard shortcuts

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form