-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_runs.go
66 lines (54 loc) · 1.32 KB
/
list_runs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package handlers
import (
"net/http"
"strconv"
"github.com/replicatedhq/kurl-testgrid/tgapi/pkg/logger"
"github.com/replicatedhq/kurl-testgrid/tgapi/pkg/testrun"
"github.com/replicatedhq/kurl-testgrid/tgapi/pkg/testrun/types"
)
type ListRunsResponse struct {
Runs []types.TestRun `json:"runs"`
Total int `json:"total"`
}
func ListRuns(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "content-type, origin, accept, authorization")
if r.Method == "OPTIONS" {
w.WriteHeader(200)
return
}
pSize := r.URL.Query().Get("pageSize")
cPage := r.URL.Query().Get("currentPage")
searchRef := r.URL.Query().Get("searchRef")
var pageSize int
if pSize != "" {
var err error
pageSize, err = strconv.Atoi(pSize)
if err != nil {
pageSize = 20
}
} else {
pageSize = 20
}
var currentPage int
if cPage != "" {
currentPage, _ = strconv.Atoi(cPage)
}
testRuns, err := testrun.List(pageSize, currentPage*pageSize, searchRef)
if err != nil {
logger.Error(err)
JSON(w, 500, nil)
return
}
total, err := testrun.Total(searchRef)
if err != nil {
logger.Error(err)
JSON(w, 500, nil)
return
}
listRunsResponse := ListRunsResponse{
Runs: testRuns,
Total: total,
}
JSON(w, 200, listRunsResponse)
}