Unify active task loading across API handlers
This commit is contained in:
@@ -2,7 +2,6 @@ package server
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
|
||||||
@@ -13,19 +12,8 @@ import (
|
|||||||
var objectID = regexp.MustCompile(`^[0-9a-f]{40}([0-9a-f]{24})?$`)
|
var objectID = regexp.MustCompile(`^[0-9a-f]{40}([0-9a-f]{24})?$`)
|
||||||
|
|
||||||
func (s *Server) GetTaskChanges(w http.ResponseWriter, r *http.Request, taskID string, params api.GetTaskChangesParams) {
|
func (s *Server) GetTaskChanges(w http.ResponseWriter, r *http.Request, taskID string, params api.GetTaskChangesParams) {
|
||||||
t, err := s.store.GetTask(r.Context(), taskID)
|
t, p, ok := s.activeTaskProject(w, r, taskID)
|
||||||
if err != nil {
|
if !ok {
|
||||||
writeStoreError(w, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if t.Status != api.TaskStatusActive {
|
|
||||||
writeError(w, http.StatusConflict, fmt.Errorf("task %s is %s and has no worktree", t.Id, t.Status))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
p, err := s.store.GetProject(r.Context(), t.ProjectId)
|
|
||||||
if err != nil {
|
|
||||||
writeStoreError(w, err)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -96,13 +84,8 @@ func (s *Server) stepBase(r *http.Request, t api.Task, snap api.Snapshot) (strin
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) GetTaskDiff(w http.ResponseWriter, r *http.Request, taskID string, params api.GetTaskDiffParams) {
|
func (s *Server) GetTaskDiff(w http.ResponseWriter, r *http.Request, taskID string, params api.GetTaskDiffParams) {
|
||||||
t, err := s.store.GetTask(r.Context(), taskID)
|
t, ok := s.activeTask(w, r, taskID)
|
||||||
if err != nil {
|
if !ok {
|
||||||
writeStoreError(w, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if t.Status != api.TaskStatusActive {
|
|
||||||
writeError(w, http.StatusConflict, fmt.Errorf("task %s is %s and has no worktree", t.Id, t.Status))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if !objectID.MatchString(params.Base) || !objectID.MatchString(params.Tree) {
|
if !objectID.MatchString(params.Base) || !objectID.MatchString(params.Tree) {
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (s *Server) GetTaskDir(w http.ResponseWriter, r *http.Request, taskID string, params api.GetTaskDirParams) {
|
func (s *Server) GetTaskDir(w http.ResponseWriter, r *http.Request, taskID string, params api.GetTaskDirParams) {
|
||||||
t, _, ok := s.activeTask(w, r, taskID)
|
t, ok := s.activeTask(w, r, taskID)
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -155,7 +155,7 @@ func dirEntry(root *os.Root, name string, de fs.DirEntry) api.DirEntry {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) GetTaskFile(w http.ResponseWriter, r *http.Request, taskID string, params api.GetTaskFileParams) {
|
func (s *Server) GetTaskFile(w http.ResponseWriter, r *http.Request, taskID string, params api.GetTaskFileParams) {
|
||||||
t, _, ok := s.activeTask(w, r, taskID)
|
t, ok := s.activeTask(w, r, taskID)
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,13 +23,8 @@ var worktreeImageTypes = map[string]string{
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) GetTaskImage(w http.ResponseWriter, r *http.Request, taskID string, params api.GetTaskImageParams) {
|
func (s *Server) GetTaskImage(w http.ResponseWriter, r *http.Request, taskID string, params api.GetTaskImageParams) {
|
||||||
t, err := s.store.GetTask(r.Context(), taskID)
|
t, ok := s.activeTask(w, r, taskID)
|
||||||
if err != nil {
|
if !ok {
|
||||||
writeStoreError(w, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if t.Status != api.TaskStatusActive {
|
|
||||||
writeError(w, http.StatusConflict, fmt.Errorf("task %s is %s and has no worktree", t.Id, t.Status))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+16
-10
@@ -477,7 +477,7 @@ func (s *Server) GetTask(w http.ResponseWriter, r *http.Request, taskID string)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) GetUnmerged(w http.ResponseWriter, r *http.Request, taskID string) {
|
func (s *Server) GetUnmerged(w http.ResponseWriter, r *http.Request, taskID string) {
|
||||||
t, p, ok := s.activeTask(w, r, taskID)
|
t, p, ok := s.activeTaskProject(w, r, taskID)
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -517,7 +517,7 @@ func (s *Server) MergeTask(w http.ResponseWriter, r *http.Request, taskID string
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
t, p, ok := s.activeTask(w, r, taskID)
|
t, p, ok := s.activeTaskProject(w, r, taskID)
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -621,7 +621,7 @@ func (s *Server) DismissReady(w http.ResponseWriter, r *http.Request, taskID str
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) MergeIn(w http.ResponseWriter, r *http.Request, taskID string) {
|
func (s *Server) MergeIn(w http.ResponseWriter, r *http.Request, taskID string) {
|
||||||
t, p, ok := s.activeTask(w, r, taskID)
|
t, p, ok := s.activeTaskProject(w, r, taskID)
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -641,7 +641,7 @@ func (s *Server) MergeIn(w http.ResponseWriter, r *http.Request, taskID string)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) DiscardTask(w http.ResponseWriter, r *http.Request, taskID string) {
|
func (s *Server) DiscardTask(w http.ResponseWriter, r *http.Request, taskID string) {
|
||||||
t, _, ok := s.activeTask(w, r, taskID)
|
t, ok := s.activeTask(w, r, taskID)
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -659,7 +659,7 @@ func (s *Server) PostToBoard(w http.ResponseWriter, r *http.Request, taskID stri
|
|||||||
if !readJSON(w, r, &req) {
|
if !readJSON(w, r, &req) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
t, _, ok := s.activeTask(w, r, taskID)
|
t, ok := s.activeTask(w, r, taskID)
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -672,18 +672,24 @@ func (s *Server) PostToBoard(w http.ResponseWriter, r *http.Request, taskID stri
|
|||||||
w.WriteHeader(http.StatusNoContent)
|
w.WriteHeader(http.StatusNoContent)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) activeTask(w http.ResponseWriter, r *http.Request, taskID string) (api.Task, api.Project, bool) {
|
func (s *Server) activeTask(w http.ResponseWriter, r *http.Request, taskID string) (api.Task, bool) {
|
||||||
t, err := s.store.GetTask(r.Context(), taskID)
|
t, err := s.store.GetTask(r.Context(), taskID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeStoreError(w, err)
|
writeStoreError(w, err)
|
||||||
return t, api.Project{}, false
|
return t, false
|
||||||
}
|
}
|
||||||
|
|
||||||
if t.Status != api.TaskStatusActive {
|
if t.Status != api.TaskStatusActive {
|
||||||
writeError(w, http.StatusConflict, fmt.Errorf("task %s is %s", t.Id, t.Status))
|
writeError(w, http.StatusConflict, fmt.Errorf("task %s is %s", t.Id, t.Status))
|
||||||
|
return t, false
|
||||||
|
}
|
||||||
|
return t, true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) activeTaskProject(w http.ResponseWriter, r *http.Request, taskID string) (api.Task, api.Project, bool) {
|
||||||
|
t, ok := s.activeTask(w, r, taskID)
|
||||||
|
if !ok {
|
||||||
return t, api.Project{}, false
|
return t, api.Project{}, false
|
||||||
}
|
}
|
||||||
|
|
||||||
p, err := s.store.GetProject(r.Context(), t.ProjectId)
|
p, err := s.store.GetProject(r.Context(), t.ProjectId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeStoreError(w, err)
|
writeStoreError(w, err)
|
||||||
@@ -826,7 +832,7 @@ func (s *Server) ListPreviews(w http.ResponseWriter, r *http.Request, taskID str
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) ForkSnapshot(w http.ResponseWriter, r *http.Request, taskID string, n int) {
|
func (s *Server) ForkSnapshot(w http.ResponseWriter, r *http.Request, taskID string, n int) {
|
||||||
t, p, ok := s.activeTask(w, r, taskID)
|
t, p, ok := s.activeTaskProject(w, r, taskID)
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (s *Server) GetTaskTerminal(w http.ResponseWriter, r *http.Request, taskID string, params api.GetTaskTerminalParams) {
|
func (s *Server) GetTaskTerminal(w http.ResponseWriter, r *http.Request, taskID string, params api.GetTaskTerminalParams) {
|
||||||
t, _, ok := s.activeTask(w, r, taskID)
|
t, ok := s.activeTask(w, r, taskID)
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user