5
5
package repo
6
6
7
7
import (
8
- "encoding/json"
9
-
10
- "github.com/Unknwon/com"
11
-
12
8
api "code.gitea.io/sdk/gitea"
13
9
14
10
"code.gitea.io/gitea/models"
15
11
"code.gitea.io/gitea/modules/context"
16
12
"code.gitea.io/gitea/routers/api/v1/convert"
13
+ "code.gitea.io/gitea/routers/api/v1/utils"
17
14
)
18
15
19
16
// ListHooks list all hooks of a repository
@@ -32,146 +29,31 @@ func ListHooks(ctx *context.APIContext) {
32
29
ctx .JSON (200 , & apiHooks )
33
30
}
34
31
35
- // CreateHook create a hook for a repository
36
- // see https://github.com/gogits/go-gogs-client/wiki/Repositories#create-a-hook
37
- func CreateHook (ctx * context.APIContext , form api.CreateHookOption ) {
38
- if ! models .IsValidHookTaskType (form .Type ) {
39
- ctx .Error (422 , "" , "Invalid hook type" )
40
- return
41
- }
42
- for _ , name := range []string {"url" , "content_type" } {
43
- if _ , ok := form .Config [name ]; ! ok {
44
- ctx .Error (422 , "" , "Missing config option: " + name )
45
- return
46
- }
47
- }
48
- if ! models .IsValidHookContentType (form .Config ["content_type" ]) {
49
- ctx .Error (422 , "" , "Invalid content type" )
32
+ // GetHook get a repo's hook by id
33
+ func GetHook (ctx * context.APIContext ) {
34
+ repo := ctx .Repo
35
+ hookID := ctx .ParamsInt64 (":id" )
36
+ hook , err := utils .GetRepoHook (ctx , repo .Repository .ID , hookID )
37
+ if err != nil {
50
38
return
51
39
}
40
+ ctx .JSON (200 , convert .ToHook (repo .RepoLink , hook ))
41
+ }
52
42
53
- if len (form .Events ) == 0 {
54
- form .Events = []string {"push" }
55
- }
56
- w := & models.Webhook {
57
- RepoID : ctx .Repo .Repository .ID ,
58
- URL : form .Config ["url" ],
59
- ContentType : models .ToHookContentType (form .Config ["content_type" ]),
60
- Secret : form .Config ["secret" ],
61
- HookEvent : & models.HookEvent {
62
- ChooseEvents : true ,
63
- HookEvents : models.HookEvents {
64
- Create : com .IsSliceContainsStr (form .Events , string (models .HookEventCreate )),
65
- Push : com .IsSliceContainsStr (form .Events , string (models .HookEventPush )),
66
- PullRequest : com .IsSliceContainsStr (form .Events , string (models .HookEventPullRequest )),
67
- },
68
- },
69
- IsActive : form .Active ,
70
- HookTaskType : models .ToHookTaskType (form .Type ),
71
- }
72
- if w .HookTaskType == models .SLACK {
73
- channel , ok := form .Config ["channel" ]
74
- if ! ok {
75
- ctx .Error (422 , "" , "Missing config option: channel" )
76
- return
77
- }
78
- meta , err := json .Marshal (& models.SlackMeta {
79
- Channel : channel ,
80
- Username : form .Config ["username" ],
81
- IconURL : form .Config ["icon_url" ],
82
- Color : form .Config ["color" ],
83
- })
84
- if err != nil {
85
- ctx .Error (500 , "slack: JSON marshal failed" , err )
86
- return
87
- }
88
- w .Meta = string (meta )
89
- }
90
-
91
- if err := w .UpdateEvent (); err != nil {
92
- ctx .Error (500 , "UpdateEvent" , err )
93
- return
94
- } else if err := models .CreateWebhook (w ); err != nil {
95
- ctx .Error (500 , "CreateWebhook" , err )
43
+ // CreateHook create a hook for a repository
44
+ // see https://github.com/gogits/go-gogs-client/wiki/Repositories#create-a-hook
45
+ func CreateHook (ctx * context.APIContext , form api.CreateHookOption ) {
46
+ if ! utils .CheckCreateHookOption (ctx , & form ) {
96
47
return
97
48
}
98
-
99
- ctx .JSON (201 , convert .ToHook (ctx .Repo .RepoLink , w ))
49
+ utils .AddRepoHook (ctx , & form )
100
50
}
101
51
102
52
// EditHook modify a hook of a repository
103
53
// see https://github.com/gogits/go-gogs-client/wiki/Repositories#edit-a-hook
104
54
func EditHook (ctx * context.APIContext , form api.EditHookOption ) {
105
55
hookID := ctx .ParamsInt64 (":id" )
106
- w , err := models .GetWebhookByRepoID (ctx .Repo .Repository .ID , hookID )
107
- if err != nil {
108
- if models .IsErrWebhookNotExist (err ) {
109
- ctx .Status (404 )
110
- } else {
111
- ctx .Error (500 , "GetWebhookByID" , err )
112
- }
113
- return
114
- }
115
-
116
- if form .Config != nil {
117
- if url , ok := form .Config ["url" ]; ok {
118
- w .URL = url
119
- }
120
- if ct , ok := form .Config ["content_type" ]; ok {
121
- if ! models .IsValidHookContentType (ct ) {
122
- ctx .Error (422 , "" , "Invalid content type" )
123
- return
124
- }
125
- w .ContentType = models .ToHookContentType (ct )
126
- }
127
-
128
- if w .HookTaskType == models .SLACK {
129
- if channel , ok := form .Config ["channel" ]; ok {
130
- meta , err := json .Marshal (& models.SlackMeta {
131
- Channel : channel ,
132
- Username : form .Config ["username" ],
133
- IconURL : form .Config ["icon_url" ],
134
- Color : form .Config ["color" ],
135
- })
136
- if err != nil {
137
- ctx .Error (500 , "slack: JSON marshal failed" , err )
138
- return
139
- }
140
- w .Meta = string (meta )
141
- }
142
- }
143
- }
144
-
145
- // Update events
146
- if len (form .Events ) == 0 {
147
- form .Events = []string {"push" }
148
- }
149
- w .PushOnly = false
150
- w .SendEverything = false
151
- w .ChooseEvents = true
152
- w .Create = com .IsSliceContainsStr (form .Events , string (models .HookEventCreate ))
153
- w .Push = com .IsSliceContainsStr (form .Events , string (models .HookEventPush ))
154
- w .PullRequest = com .IsSliceContainsStr (form .Events , string (models .HookEventPullRequest ))
155
- if err = w .UpdateEvent (); err != nil {
156
- ctx .Error (500 , "UpdateEvent" , err )
157
- return
158
- }
159
-
160
- if form .Active != nil {
161
- w .IsActive = * form .Active
162
- }
163
-
164
- if err := models .UpdateWebhook (w ); err != nil {
165
- ctx .Error (500 , "UpdateWebhook" , err )
166
- return
167
- }
168
-
169
- updated , err := models .GetWebhookByRepoID (ctx .Repo .Repository .ID , hookID )
170
- if err != nil {
171
- ctx .Error (500 , "GetWebhookByRepoID" , err )
172
- return
173
- }
174
- ctx .JSON (200 , convert .ToHook (ctx .Repo .RepoLink , updated ))
56
+ utils .EditRepoHook (ctx , & form , hookID )
175
57
}
176
58
177
59
// DeleteHook delete a hook of a repository
0 commit comments