-
Notifications
You must be signed in to change notification settings - Fork 28
/
hook.go
150 lines (120 loc) · 3.17 KB
/
hook.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
143
144
145
146
147
148
149
150
// SPDX-License-Identifier: Apache-2.0
//nolint:dupl // ignore duplicate with user code
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 (
// HookResp represents a JSON return for a single hook.
HookResp = `{
"id": 1,
"repo_id": 1,
"build_id": 1,
"number": 1,
"source_id": "c8da1302-07d6-11ea-882f-4893bca275b8",
"created": 1563475419,
"host": "github.com",
"event": "push",
"event_action": "",
"webhook_id": 1234,
"branch": "main",
"error": "",
"status": "success",
"link": "https://github.com/github/octocat/settings/hooks/1"
}`
// HooksResp represents a JSON return for one to many hooks.
HooksResp = `[
{
"id": 2,
"repo_id": 1,
"build_id": 1,
"number": 2,
"source_id": "c8da1302-07d6-11ea-882f-4893bca275b8",
"created": 1563475420,
"host": "github.com",
"event": "push",
"branch": "main",
"error": "",
"status": "success",
"link": "https://github.com/github/octocat/settings/hooks/1"
},
{
"id": 1,
"repo_id": 1,
"build_id": 1,
"number": 1,
"source_id": "c8da1302-07d6-11ea-882f-4893bca275b8",
"created": 1563475419,
"host": "github.com",
"event": "push",
"branch": "main",
"error": "",
"status": "success",
"link": "https://github.com/github/octocat/settings/hooks/1"
}
]`
)
// getHooks returns mock JSON for a http GET.
func getHooks(c *gin.Context) {
data := []byte(HooksResp)
var body []library.Hook
_ = json.Unmarshal(data, &body)
c.JSON(http.StatusOK, body)
}
// getHook has a param :hook returns mock JSON for a http GET.
//
// Pass "0" to :hook to test receiving a http 404 response.
func getHook(c *gin.Context) {
s := c.Param("hook")
if strings.EqualFold(s, "0") {
msg := fmt.Sprintf("Hook %s does not exist", s)
c.AbortWithStatusJSON(http.StatusNotFound, types.Error{Message: &msg})
return
}
data := []byte(HookResp)
var body library.Hook
_ = json.Unmarshal(data, &body)
c.JSON(http.StatusOK, body)
}
// addHook returns mock JSON for a http POST.
func addHook(c *gin.Context) {
data := []byte(HookResp)
var body library.Hook
_ = json.Unmarshal(data, &body)
c.JSON(http.StatusCreated, body)
}
// updateHook has a param :hook returns mock JSON for a http PUT.
//
// Pass "0" to :hook to test receiving a http 404 response.
func updateHook(c *gin.Context) {
if !strings.Contains(c.FullPath(), "admin") {
s := c.Param("hook")
if strings.EqualFold(s, "0") {
msg := fmt.Sprintf("Hook %s does not exist", s)
c.AbortWithStatusJSON(http.StatusNotFound, types.Error{Message: &msg})
return
}
}
data := []byte(HookResp)
var body library.Hook
_ = json.Unmarshal(data, &body)
c.JSON(http.StatusOK, body)
}
// removeHook has a param :hook returns mock JSON for a http DELETE.
//
// Pass "0" to :hook to test receiving a http 404 response.
func removeHook(c *gin.Context) {
s := c.Param("hook")
if strings.EqualFold(s, "0") {
msg := fmt.Sprintf("Hook %s does not exist", s)
c.AbortWithStatusJSON(http.StatusNotFound, types.Error{Message: &msg})
return
}
c.JSON(http.StatusOK, fmt.Sprintf("Hook %s removed", s))
}