2015-12-04 17:16:42 -05:00
|
|
|
package repo
|
2014-11-13 02:32:18 -05:00
|
|
|
|
|
|
|
|
import (
|
2020-03-16 01:22:27 +08:00
|
|
|
"net/http"
|
2014-11-13 12:57:00 -05:00
|
|
|
|
2026-01-22 08:20:53 -05:00
|
|
|
"github.com/cockroachdb/errors"
|
2021-05-19 13:38:13 +08:00
|
|
|
jsoniter "github.com/json-iterator/go"
|
2020-03-16 01:22:27 +08:00
|
|
|
"github.com/unknwon/com"
|
2014-11-14 17:11:30 -05:00
|
|
|
|
2021-05-19 13:38:13 +08:00
|
|
|
api "github.com/gogs/go-gogs-client"
|
|
|
|
|
|
2019-10-24 01:51:46 -07:00
|
|
|
"gogs.io/gogs/internal/context"
|
2024-02-18 19:39:41 -05:00
|
|
|
"gogs.io/gogs/internal/database"
|
2020-03-16 01:22:27 +08:00
|
|
|
"gogs.io/gogs/internal/route/api/v1/convert"
|
2014-11-13 02:32:18 -05:00
|
|
|
)
|
|
|
|
|
|
2018-05-27 08:53:48 +08:00
|
|
|
// https://github.com/gogs/go-gogs-client/wiki/Repositories#list-hooks
|
2017-06-03 07:26:09 -04:00
|
|
|
func ListHooks(c *context.APIContext) {
|
2024-02-18 19:39:41 -05:00
|
|
|
hooks, err := database.GetWebhooksByRepoID(c.Repo.Repository.ID)
|
2014-11-13 02:32:18 -05:00
|
|
|
if err != nil {
|
2020-03-16 01:22:27 +08:00
|
|
|
c.Errorf(err, "get webhooks by repository ID")
|
2014-11-13 02:32:18 -05:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-14 17:11:30 -05:00
|
|
|
apiHooks := make([]*api.Hook, len(hooks))
|
2014-11-13 02:32:18 -05:00
|
|
|
for i := range hooks {
|
2020-03-16 01:22:27 +08:00
|
|
|
apiHooks[i] = convert.ToHook(c.Repo.RepoLink, hooks[i])
|
2014-11-13 02:32:18 -05:00
|
|
|
}
|
2020-03-16 01:22:27 +08:00
|
|
|
c.JSONSuccess(&apiHooks)
|
2014-11-13 02:32:18 -05:00
|
|
|
}
|
2014-11-13 12:57:00 -05:00
|
|
|
|
2018-05-27 08:53:48 +08:00
|
|
|
// https://github.com/gogs/go-gogs-client/wiki/Repositories#create-a-hook
|
2017-06-03 07:26:09 -04:00
|
|
|
func CreateHook(c *context.APIContext, form api.CreateHookOption) {
|
2024-02-18 19:39:41 -05:00
|
|
|
if !database.IsValidHookTaskType(form.Type) {
|
2020-03-16 01:22:27 +08:00
|
|
|
c.ErrorStatus(http.StatusUnprocessableEntity, errors.New("Invalid hook type."))
|
2014-11-13 12:57:00 -05:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
for _, name := range []string{"url", "content_type"} {
|
|
|
|
|
if _, ok := form.Config[name]; !ok {
|
2020-03-16 01:22:27 +08:00
|
|
|
c.ErrorStatus(http.StatusUnprocessableEntity, errors.New("Missing config option: "+name))
|
2014-11-13 12:57:00 -05:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-18 19:39:41 -05:00
|
|
|
if !database.IsValidHookContentType(form.Config["content_type"]) {
|
2020-03-16 01:22:27 +08:00
|
|
|
c.ErrorStatus(http.StatusUnprocessableEntity, errors.New("Invalid content type."))
|
2014-11-13 12:57:00 -05:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-29 11:53:46 +08:00
|
|
|
if len(form.Events) == 0 {
|
|
|
|
|
form.Events = []string{"push"}
|
|
|
|
|
}
|
2024-02-18 19:39:41 -05:00
|
|
|
w := &database.Webhook{
|
2017-06-03 07:26:09 -04:00
|
|
|
RepoID: c.Repo.Repository.ID,
|
2015-08-26 21:45:51 +08:00
|
|
|
URL: form.Config["url"],
|
2024-02-18 19:39:41 -05:00
|
|
|
ContentType: database.ToHookContentType(form.Config["content_type"]),
|
2014-11-13 12:57:00 -05:00
|
|
|
Secret: form.Config["secret"],
|
2024-02-18 19:39:41 -05:00
|
|
|
HookEvent: &database.HookEvent{
|
2015-08-29 11:49:59 +08:00
|
|
|
ChooseEvents: true,
|
2024-02-18 19:39:41 -05:00
|
|
|
HookEvents: database.HookEvents{
|
2025-04-14 18:00:07 -04:00
|
|
|
Create: com.IsSliceContainsStr(form.Events, string(database.HookEventTypeCreate)),
|
|
|
|
|
Delete: com.IsSliceContainsStr(form.Events, string(database.HookEventTypeDelete)),
|
|
|
|
|
Fork: com.IsSliceContainsStr(form.Events, string(database.HookEventTypeFork)),
|
|
|
|
|
Push: com.IsSliceContainsStr(form.Events, string(database.HookEventTypePush)),
|
|
|
|
|
Issues: com.IsSliceContainsStr(form.Events, string(database.HookEventTypeIssues)),
|
|
|
|
|
IssueComment: com.IsSliceContainsStr(form.Events, string(database.HookEventTypeIssueComment)),
|
|
|
|
|
PullRequest: com.IsSliceContainsStr(form.Events, string(database.HookEventTypePullRequest)),
|
|
|
|
|
Release: com.IsSliceContainsStr(form.Events, string(database.HookEventTypeRelease)),
|
2015-08-29 11:49:59 +08:00
|
|
|
},
|
2014-11-13 12:57:00 -05:00
|
|
|
},
|
|
|
|
|
IsActive: form.Active,
|
2024-02-18 19:39:41 -05:00
|
|
|
HookTaskType: database.ToHookTaskType(form.Type),
|
2014-11-13 12:57:00 -05:00
|
|
|
}
|
2024-02-18 19:39:41 -05:00
|
|
|
if w.HookTaskType == database.SLACK {
|
2014-11-13 12:57:00 -05:00
|
|
|
channel, ok := form.Config["channel"]
|
|
|
|
|
if !ok {
|
2020-03-16 01:22:27 +08:00
|
|
|
c.ErrorStatus(http.StatusUnprocessableEntity, errors.New("Missing config option: channel"))
|
2014-11-13 12:57:00 -05:00
|
|
|
return
|
|
|
|
|
}
|
2024-02-18 19:39:41 -05:00
|
|
|
meta, err := jsoniter.Marshal(&database.SlackMeta{
|
2015-08-29 11:49:59 +08:00
|
|
|
Channel: channel,
|
|
|
|
|
Username: form.Config["username"],
|
|
|
|
|
IconURL: form.Config["icon_url"],
|
|
|
|
|
Color: form.Config["color"],
|
2014-11-13 12:57:00 -05:00
|
|
|
})
|
|
|
|
|
if err != nil {
|
2020-03-16 01:22:27 +08:00
|
|
|
c.Errorf(err, "marshal JSON")
|
2014-11-13 12:57:00 -05:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
w.Meta = string(meta)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := w.UpdateEvent(); err != nil {
|
2020-03-16 01:22:27 +08:00
|
|
|
c.Errorf(err, "update event")
|
2014-11-13 12:57:00 -05:00
|
|
|
return
|
2024-02-18 19:39:41 -05:00
|
|
|
} else if err := database.CreateWebhook(w); err != nil {
|
2020-03-16 01:22:27 +08:00
|
|
|
c.Errorf(err, "create webhook")
|
2014-11-13 12:57:00 -05:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-16 01:22:27 +08:00
|
|
|
c.JSON(http.StatusCreated, convert.ToHook(c.Repo.RepoLink, w))
|
2014-11-13 12:57:00 -05:00
|
|
|
}
|
|
|
|
|
|
2018-05-27 08:53:48 +08:00
|
|
|
// https://github.com/gogs/go-gogs-client/wiki/Repositories#edit-a-hook
|
2017-06-03 07:26:09 -04:00
|
|
|
func EditHook(c *context.APIContext, form api.EditHookOption) {
|
2024-02-18 19:39:41 -05:00
|
|
|
w, err := database.GetWebhookOfRepoByID(c.Repo.Repository.ID, c.ParamsInt64(":id"))
|
2014-11-13 12:57:00 -05:00
|
|
|
if err != nil {
|
2020-03-16 01:22:27 +08:00
|
|
|
c.NotFoundOrError(err, "get webhook of repository by ID")
|
2014-11-13 12:57:00 -05:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if form.Config != nil {
|
|
|
|
|
if url, ok := form.Config["url"]; ok {
|
2015-08-26 21:45:51 +08:00
|
|
|
w.URL = url
|
2014-11-13 12:57:00 -05:00
|
|
|
}
|
|
|
|
|
if ct, ok := form.Config["content_type"]; ok {
|
2024-02-18 19:39:41 -05:00
|
|
|
if !database.IsValidHookContentType(ct) {
|
2020-03-16 01:22:27 +08:00
|
|
|
c.ErrorStatus(http.StatusUnprocessableEntity, errors.New("Invalid content type."))
|
2014-11-13 12:57:00 -05:00
|
|
|
return
|
|
|
|
|
}
|
2024-02-18 19:39:41 -05:00
|
|
|
w.ContentType = database.ToHookContentType(ct)
|
2014-11-13 12:57:00 -05:00
|
|
|
}
|
|
|
|
|
|
2024-02-18 19:39:41 -05:00
|
|
|
if w.HookTaskType == database.SLACK {
|
2014-11-13 12:57:00 -05:00
|
|
|
if channel, ok := form.Config["channel"]; ok {
|
2024-02-18 19:39:41 -05:00
|
|
|
meta, err := jsoniter.Marshal(&database.SlackMeta{
|
2015-08-29 11:49:59 +08:00
|
|
|
Channel: channel,
|
|
|
|
|
Username: form.Config["username"],
|
|
|
|
|
IconURL: form.Config["icon_url"],
|
|
|
|
|
Color: form.Config["color"],
|
2014-11-13 12:57:00 -05:00
|
|
|
})
|
|
|
|
|
if err != nil {
|
2020-03-16 01:22:27 +08:00
|
|
|
c.Errorf(err, "marshal JSON")
|
2014-11-13 12:57:00 -05:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
w.Meta = string(meta)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-29 11:49:59 +08:00
|
|
|
// Update events
|
2015-08-29 11:53:46 +08:00
|
|
|
if len(form.Events) == 0 {
|
|
|
|
|
form.Events = []string{"push"}
|
|
|
|
|
}
|
2015-08-29 11:49:59 +08:00
|
|
|
w.PushOnly = false
|
|
|
|
|
w.SendEverything = false
|
|
|
|
|
w.ChooseEvents = true
|
2025-04-14 18:00:07 -04:00
|
|
|
w.Create = com.IsSliceContainsStr(form.Events, string(database.HookEventTypeCreate))
|
|
|
|
|
w.Delete = com.IsSliceContainsStr(form.Events, string(database.HookEventTypeDelete))
|
|
|
|
|
w.Fork = com.IsSliceContainsStr(form.Events, string(database.HookEventTypeFork))
|
|
|
|
|
w.Push = com.IsSliceContainsStr(form.Events, string(database.HookEventTypePush))
|
|
|
|
|
w.Issues = com.IsSliceContainsStr(form.Events, string(database.HookEventTypeIssues))
|
|
|
|
|
w.IssueComment = com.IsSliceContainsStr(form.Events, string(database.HookEventTypeIssueComment))
|
|
|
|
|
w.PullRequest = com.IsSliceContainsStr(form.Events, string(database.HookEventTypePullRequest))
|
|
|
|
|
w.Release = com.IsSliceContainsStr(form.Events, string(database.HookEventTypeRelease))
|
2015-08-29 11:49:59 +08:00
|
|
|
if err = w.UpdateEvent(); err != nil {
|
2020-03-16 01:22:27 +08:00
|
|
|
c.Errorf(err, "update event")
|
2015-08-29 11:49:59 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-13 12:57:00 -05:00
|
|
|
if form.Active != nil {
|
|
|
|
|
w.IsActive = *form.Active
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-18 19:39:41 -05:00
|
|
|
if err := database.UpdateWebhook(w); err != nil {
|
2020-03-16 01:22:27 +08:00
|
|
|
c.Errorf(err, "update webhook")
|
2014-11-13 12:57:00 -05:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-16 01:22:27 +08:00
|
|
|
c.JSONSuccess(convert.ToHook(c.Repo.RepoLink, w))
|
2014-11-13 12:57:00 -05:00
|
|
|
}
|
2016-07-17 08:33:59 +08:00
|
|
|
|
2017-06-03 07:26:09 -04:00
|
|
|
func DeleteHook(c *context.APIContext) {
|
2024-02-18 19:39:41 -05:00
|
|
|
if err := database.DeleteWebhookOfRepoByID(c.Repo.Repository.ID, c.ParamsInt64(":id")); err != nil {
|
2020-03-16 01:22:27 +08:00
|
|
|
c.Errorf(err, "delete webhook of repository by ID")
|
2016-07-17 08:33:59 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-16 01:22:27 +08:00
|
|
|
c.NoContent()
|
2016-07-17 08:33:59 +08:00
|
|
|
}
|