-
Notifications
You must be signed in to change notification settings - Fork 28
/
schedule.go
142 lines (113 loc) · 2.94 KB
/
schedule.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// SPDX-License-Identifier: Apache-2.0
package server
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/gin-gonic/gin"
"github.com/go-vela/types"
"github.com/go-vela/types/library"
)
const (
// ScheduleResp represents a JSON return for a single schedule.
ScheduleResp = `{
"id": 2,
"repo_id": 1,
"active": true,
"name": "foo",
"entry": "@weekly",
"created_at": 1683154980,
"created_by": "octocat",
"updated_at": 1683154980,
"updated_by": "octocat",
"scheduled_at": 0,
"branch": "main"
}`
SchedulesResp = `[
{
"id": 2,
"active": true,
"name": "foo",
"entry": "@weekly",
"created_at": 1683154980,
"created_by": "octocat",
"updated_at": 1683154980,
"updated_by": "octocat",
"scheduled_at": 0,
"repo_id": 1,
"branch": "main"
},
{
"id": 1,
"active": true,
"name": "bar",
"entry": "@weekly",
"created_at": 1683154974,
"created_by": "octocat",
"updated_at": 1683154974,
"updated_by": "octocat",
"scheduled_at": 0,
"repo_id": 1,
"branch": "main
}
]`
)
// getSchedules returns mock JSON for a http GET.
func getSchedules(c *gin.Context) {
data := []byte(SchedulesResp)
var body []library.Schedule
_ = json.Unmarshal(data, &body)
c.JSON(http.StatusOK, body)
}
// getSchedule has a param :schedule returns mock JSON for a http GET.
//
// Pass "not-found" to :schedule to test receiving a http 404 response.
func getSchedule(c *gin.Context) {
s := c.Param("schedule")
if strings.Contains(s, "not-found") {
msg := fmt.Sprintf("Schedule %s does not exist", s)
c.AbortWithStatusJSON(http.StatusNotFound, types.Error{Message: &msg})
return
}
data := []byte(ScheduleResp)
var body library.Schedule
_ = json.Unmarshal(data, &body)
c.JSON(http.StatusOK, body)
}
// addSchedule returns mock JSON for a http POST.
func addSchedule(c *gin.Context) {
data := []byte(ScheduleResp)
var body library.Schedule
_ = json.Unmarshal(data, &body)
c.JSON(http.StatusCreated, body)
}
// updateSchedule has a param :schedule returns mock JSON for a http PUT.
//
// Pass "not-found" to :schedule to test receiving a http 404 response.
func updateSchedule(c *gin.Context) {
if !strings.Contains(c.FullPath(), "admin") {
s := c.Param("schedule")
if strings.Contains(s, "not-found") {
msg := fmt.Sprintf("Schedule %s does not exist", s)
c.AbortWithStatusJSON(http.StatusNotFound, types.Error{Message: &msg})
return
}
}
data := []byte(ScheduleResp)
var body library.Schedule
_ = json.Unmarshal(data, &body)
c.JSON(http.StatusOK, body)
}
// removeSchedule has a param :schedule returns mock JSON for a http DELETE.
//
// Pass "not-found" to :schedule to test receiving a http 404 response.
func removeSchedule(c *gin.Context) {
s := c.Param("schedule")
if strings.Contains(s, "not-found") {
msg := fmt.Sprintf("Schedule %s does not exist", s)
c.AbortWithStatusJSON(http.StatusNotFound, types.Error{Message: &msg})
return
}
c.JSON(http.StatusOK, fmt.Sprintf("schedule %s deleted", s))
}