mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 16:40:24 +08:00 
			
		
		
		
	refactor webhook *NewPost (#20729)
* refactor webhook *NewPost * remove empty values * always show errs.Message * remove utils.IsValidSlackChannel * move IsValidSlackChannel to services/webhook package * binding: handle empty Message case * make IsValidSlackChannel more strict
This commit is contained in:
		@@ -185,14 +185,22 @@ func ParseHookEvent(form forms.WebhookForm) *webhook.HookEvent {
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// GiteaHooksNewPost response for creating Gitea webhook
 | 
			
		||||
func GiteaHooksNewPost(ctx *context.Context) {
 | 
			
		||||
	form := web.GetForm(ctx).(*forms.NewWebhookForm)
 | 
			
		||||
type webhookCreationParams struct {
 | 
			
		||||
	URL         string
 | 
			
		||||
	ContentType webhook.HookContentType
 | 
			
		||||
	Secret      string
 | 
			
		||||
	HTTPMethod  string
 | 
			
		||||
	WebhookForm forms.WebhookForm
 | 
			
		||||
	Type        string
 | 
			
		||||
	Meta        interface{}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func createWebhook(ctx *context.Context, params webhookCreationParams) {
 | 
			
		||||
	ctx.Data["Title"] = ctx.Tr("repo.settings.add_webhook")
 | 
			
		||||
	ctx.Data["PageIsSettingsHooks"] = true
 | 
			
		||||
	ctx.Data["PageIsSettingsHooksNew"] = true
 | 
			
		||||
	ctx.Data["Webhook"] = webhook.Webhook{HookEvent: &webhook.HookEvent{}}
 | 
			
		||||
	ctx.Data["HookType"] = webhook.GITEA
 | 
			
		||||
	ctx.Data["HookType"] = params.Type
 | 
			
		||||
 | 
			
		||||
	orCtx, err := getOrgRepoCtx(ctx)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
@@ -206,20 +214,25 @@ func GiteaHooksNewPost(ctx *context.Context) {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	contentType := webhook.ContentTypeJSON
 | 
			
		||||
	if webhook.HookContentType(form.ContentType) == webhook.ContentTypeForm {
 | 
			
		||||
		contentType = webhook.ContentTypeForm
 | 
			
		||||
	var meta []byte
 | 
			
		||||
	if params.Meta != nil {
 | 
			
		||||
		meta, err = json.Marshal(params.Meta)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			ctx.ServerError("Marshal", err)
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	w := &webhook.Webhook{
 | 
			
		||||
		RepoID:          orCtx.RepoID,
 | 
			
		||||
		URL:             form.PayloadURL,
 | 
			
		||||
		HTTPMethod:      form.HTTPMethod,
 | 
			
		||||
		ContentType:     contentType,
 | 
			
		||||
		Secret:          form.Secret,
 | 
			
		||||
		HookEvent:       ParseHookEvent(form.WebhookForm),
 | 
			
		||||
		IsActive:        form.Active,
 | 
			
		||||
		Type:            webhook.GITEA,
 | 
			
		||||
		URL:             params.URL,
 | 
			
		||||
		HTTPMethod:      params.HTTPMethod,
 | 
			
		||||
		ContentType:     params.ContentType,
 | 
			
		||||
		Secret:          params.Secret,
 | 
			
		||||
		HookEvent:       ParseHookEvent(params.WebhookForm),
 | 
			
		||||
		IsActive:        params.WebhookForm.Active,
 | 
			
		||||
		Type:            params.Type,
 | 
			
		||||
		Meta:            string(meta),
 | 
			
		||||
		OrgID:           orCtx.OrgID,
 | 
			
		||||
		IsSystemWebhook: orCtx.IsSystemWebhook,
 | 
			
		||||
	}
 | 
			
		||||
@@ -235,503 +248,175 @@ func GiteaHooksNewPost(ctx *context.Context) {
 | 
			
		||||
	ctx.Redirect(orCtx.Link)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// GiteaHooksNewPost response for creating Gitea webhook
 | 
			
		||||
func GiteaHooksNewPost(ctx *context.Context) {
 | 
			
		||||
	form := web.GetForm(ctx).(*forms.NewWebhookForm)
 | 
			
		||||
 | 
			
		||||
	contentType := webhook.ContentTypeJSON
 | 
			
		||||
	if webhook.HookContentType(form.ContentType) == webhook.ContentTypeForm {
 | 
			
		||||
		contentType = webhook.ContentTypeForm
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	createWebhook(ctx, webhookCreationParams{
 | 
			
		||||
		URL:         form.PayloadURL,
 | 
			
		||||
		ContentType: contentType,
 | 
			
		||||
		Secret:      form.Secret,
 | 
			
		||||
		HTTPMethod:  form.HTTPMethod,
 | 
			
		||||
		WebhookForm: form.WebhookForm,
 | 
			
		||||
		Type:        webhook.GITEA,
 | 
			
		||||
	})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// GogsHooksNewPost response for creating webhook
 | 
			
		||||
func GogsHooksNewPost(ctx *context.Context) {
 | 
			
		||||
	form := web.GetForm(ctx).(*forms.NewGogshookForm)
 | 
			
		||||
	newGogsWebhookPost(ctx, *form, webhook.GOGS)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// newGogsWebhookPost response for creating gogs hook
 | 
			
		||||
func newGogsWebhookPost(ctx *context.Context, form forms.NewGogshookForm, kind webhook.HookType) {
 | 
			
		||||
	ctx.Data["Title"] = ctx.Tr("repo.settings.add_webhook")
 | 
			
		||||
	ctx.Data["PageIsSettingsHooks"] = true
 | 
			
		||||
	ctx.Data["PageIsSettingsHooksNew"] = true
 | 
			
		||||
	ctx.Data["Webhook"] = webhook.Webhook{HookEvent: &webhook.HookEvent{}}
 | 
			
		||||
	ctx.Data["HookType"] = webhook.GOGS
 | 
			
		||||
 | 
			
		||||
	orCtx, err := getOrgRepoCtx(ctx)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		ctx.ServerError("getOrgRepoCtx", err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	ctx.Data["BaseLink"] = orCtx.LinkNew
 | 
			
		||||
 | 
			
		||||
	if ctx.HasError() {
 | 
			
		||||
		ctx.HTML(http.StatusOK, orCtx.NewTemplate)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	contentType := webhook.ContentTypeJSON
 | 
			
		||||
	if webhook.HookContentType(form.ContentType) == webhook.ContentTypeForm {
 | 
			
		||||
		contentType = webhook.ContentTypeForm
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	w := &webhook.Webhook{
 | 
			
		||||
		RepoID:          orCtx.RepoID,
 | 
			
		||||
		URL:             form.PayloadURL,
 | 
			
		||||
		ContentType:     contentType,
 | 
			
		||||
		Secret:          form.Secret,
 | 
			
		||||
		HookEvent:       ParseHookEvent(form.WebhookForm),
 | 
			
		||||
		IsActive:        form.Active,
 | 
			
		||||
		Type:            kind,
 | 
			
		||||
		OrgID:           orCtx.OrgID,
 | 
			
		||||
		IsSystemWebhook: orCtx.IsSystemWebhook,
 | 
			
		||||
	}
 | 
			
		||||
	if err := w.UpdateEvent(); err != nil {
 | 
			
		||||
		ctx.ServerError("UpdateEvent", err)
 | 
			
		||||
		return
 | 
			
		||||
	} else if err := webhook.CreateWebhook(ctx, w); err != nil {
 | 
			
		||||
		ctx.ServerError("CreateWebhook", err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
 | 
			
		||||
	ctx.Redirect(orCtx.Link)
 | 
			
		||||
	createWebhook(ctx, webhookCreationParams{
 | 
			
		||||
		URL:         form.PayloadURL,
 | 
			
		||||
		ContentType: contentType,
 | 
			
		||||
		Secret:      form.Secret,
 | 
			
		||||
		WebhookForm: form.WebhookForm,
 | 
			
		||||
		Type:        webhook.GOGS,
 | 
			
		||||
	})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// DiscordHooksNewPost response for creating discord hook
 | 
			
		||||
func DiscordHooksNewPost(ctx *context.Context) {
 | 
			
		||||
	form := web.GetForm(ctx).(*forms.NewDiscordHookForm)
 | 
			
		||||
	ctx.Data["Title"] = ctx.Tr("repo.settings")
 | 
			
		||||
	ctx.Data["PageIsSettingsHooks"] = true
 | 
			
		||||
	ctx.Data["PageIsSettingsHooksNew"] = true
 | 
			
		||||
	ctx.Data["Webhook"] = webhook.Webhook{HookEvent: &webhook.HookEvent{}}
 | 
			
		||||
	ctx.Data["HookType"] = webhook.DISCORD
 | 
			
		||||
 | 
			
		||||
	orCtx, err := getOrgRepoCtx(ctx)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		ctx.ServerError("getOrgRepoCtx", err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if ctx.HasError() {
 | 
			
		||||
		ctx.HTML(http.StatusOK, orCtx.NewTemplate)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	meta, err := json.Marshal(&webhook_service.DiscordMeta{
 | 
			
		||||
		Username: form.Username,
 | 
			
		||||
		IconURL:  form.IconURL,
 | 
			
		||||
	createWebhook(ctx, webhookCreationParams{
 | 
			
		||||
		URL:         form.PayloadURL,
 | 
			
		||||
		ContentType: webhook.ContentTypeJSON,
 | 
			
		||||
		WebhookForm: form.WebhookForm,
 | 
			
		||||
		Type:        webhook.DISCORD,
 | 
			
		||||
		Meta: &webhook_service.DiscordMeta{
 | 
			
		||||
			Username: form.Username,
 | 
			
		||||
			IconURL:  form.IconURL,
 | 
			
		||||
		},
 | 
			
		||||
	})
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		ctx.ServerError("Marshal", err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	w := &webhook.Webhook{
 | 
			
		||||
		RepoID:          orCtx.RepoID,
 | 
			
		||||
		URL:             form.PayloadURL,
 | 
			
		||||
		ContentType:     webhook.ContentTypeJSON,
 | 
			
		||||
		HookEvent:       ParseHookEvent(form.WebhookForm),
 | 
			
		||||
		IsActive:        form.Active,
 | 
			
		||||
		Type:            webhook.DISCORD,
 | 
			
		||||
		Meta:            string(meta),
 | 
			
		||||
		OrgID:           orCtx.OrgID,
 | 
			
		||||
		IsSystemWebhook: orCtx.IsSystemWebhook,
 | 
			
		||||
	}
 | 
			
		||||
	if err := w.UpdateEvent(); err != nil {
 | 
			
		||||
		ctx.ServerError("UpdateEvent", err)
 | 
			
		||||
		return
 | 
			
		||||
	} else if err := webhook.CreateWebhook(ctx, w); err != nil {
 | 
			
		||||
		ctx.ServerError("CreateWebhook", err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
 | 
			
		||||
	ctx.Redirect(orCtx.Link)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// DingtalkHooksNewPost response for creating dingtalk hook
 | 
			
		||||
func DingtalkHooksNewPost(ctx *context.Context) {
 | 
			
		||||
	form := web.GetForm(ctx).(*forms.NewDingtalkHookForm)
 | 
			
		||||
	ctx.Data["Title"] = ctx.Tr("repo.settings")
 | 
			
		||||
	ctx.Data["PageIsSettingsHooks"] = true
 | 
			
		||||
	ctx.Data["PageIsSettingsHooksNew"] = true
 | 
			
		||||
	ctx.Data["Webhook"] = webhook.Webhook{HookEvent: &webhook.HookEvent{}}
 | 
			
		||||
	ctx.Data["HookType"] = webhook.DINGTALK
 | 
			
		||||
 | 
			
		||||
	orCtx, err := getOrgRepoCtx(ctx)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		ctx.ServerError("getOrgRepoCtx", err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if ctx.HasError() {
 | 
			
		||||
		ctx.HTML(http.StatusOK, orCtx.NewTemplate)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	w := &webhook.Webhook{
 | 
			
		||||
		RepoID:          orCtx.RepoID,
 | 
			
		||||
		URL:             form.PayloadURL,
 | 
			
		||||
		ContentType:     webhook.ContentTypeJSON,
 | 
			
		||||
		HookEvent:       ParseHookEvent(form.WebhookForm),
 | 
			
		||||
		IsActive:        form.Active,
 | 
			
		||||
		Type:            webhook.DINGTALK,
 | 
			
		||||
		Meta:            "",
 | 
			
		||||
		OrgID:           orCtx.OrgID,
 | 
			
		||||
		IsSystemWebhook: orCtx.IsSystemWebhook,
 | 
			
		||||
	}
 | 
			
		||||
	if err := w.UpdateEvent(); err != nil {
 | 
			
		||||
		ctx.ServerError("UpdateEvent", err)
 | 
			
		||||
		return
 | 
			
		||||
	} else if err := webhook.CreateWebhook(ctx, w); err != nil {
 | 
			
		||||
		ctx.ServerError("CreateWebhook", err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
 | 
			
		||||
	ctx.Redirect(orCtx.Link)
 | 
			
		||||
	createWebhook(ctx, webhookCreationParams{
 | 
			
		||||
		URL:         form.PayloadURL,
 | 
			
		||||
		ContentType: webhook.ContentTypeJSON,
 | 
			
		||||
		WebhookForm: form.WebhookForm,
 | 
			
		||||
		Type:        webhook.DINGTALK,
 | 
			
		||||
	})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// TelegramHooksNewPost response for creating telegram hook
 | 
			
		||||
func TelegramHooksNewPost(ctx *context.Context) {
 | 
			
		||||
	form := web.GetForm(ctx).(*forms.NewTelegramHookForm)
 | 
			
		||||
	ctx.Data["Title"] = ctx.Tr("repo.settings")
 | 
			
		||||
	ctx.Data["PageIsSettingsHooks"] = true
 | 
			
		||||
	ctx.Data["PageIsSettingsHooksNew"] = true
 | 
			
		||||
	ctx.Data["Webhook"] = webhook.Webhook{HookEvent: &webhook.HookEvent{}}
 | 
			
		||||
	ctx.Data["HookType"] = webhook.TELEGRAM
 | 
			
		||||
 | 
			
		||||
	orCtx, err := getOrgRepoCtx(ctx)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		ctx.ServerError("getOrgRepoCtx", err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if ctx.HasError() {
 | 
			
		||||
		ctx.HTML(http.StatusOK, orCtx.NewTemplate)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	meta, err := json.Marshal(&webhook_service.TelegramMeta{
 | 
			
		||||
		BotToken: form.BotToken,
 | 
			
		||||
		ChatID:   form.ChatID,
 | 
			
		||||
	createWebhook(ctx, webhookCreationParams{
 | 
			
		||||
		URL:         fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage?chat_id=%s", url.PathEscape(form.BotToken), url.QueryEscape(form.ChatID)),
 | 
			
		||||
		ContentType: webhook.ContentTypeJSON,
 | 
			
		||||
		WebhookForm: form.WebhookForm,
 | 
			
		||||
		Type:        webhook.TELEGRAM,
 | 
			
		||||
		Meta: &webhook_service.TelegramMeta{
 | 
			
		||||
			BotToken: form.BotToken,
 | 
			
		||||
			ChatID:   form.ChatID,
 | 
			
		||||
		},
 | 
			
		||||
	})
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		ctx.ServerError("Marshal", err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	w := &webhook.Webhook{
 | 
			
		||||
		RepoID:          orCtx.RepoID,
 | 
			
		||||
		URL:             fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage?chat_id=%s", url.PathEscape(form.BotToken), url.QueryEscape(form.ChatID)),
 | 
			
		||||
		ContentType:     webhook.ContentTypeJSON,
 | 
			
		||||
		HookEvent:       ParseHookEvent(form.WebhookForm),
 | 
			
		||||
		IsActive:        form.Active,
 | 
			
		||||
		Type:            webhook.TELEGRAM,
 | 
			
		||||
		Meta:            string(meta),
 | 
			
		||||
		OrgID:           orCtx.OrgID,
 | 
			
		||||
		IsSystemWebhook: orCtx.IsSystemWebhook,
 | 
			
		||||
	}
 | 
			
		||||
	if err := w.UpdateEvent(); err != nil {
 | 
			
		||||
		ctx.ServerError("UpdateEvent", err)
 | 
			
		||||
		return
 | 
			
		||||
	} else if err := webhook.CreateWebhook(ctx, w); err != nil {
 | 
			
		||||
		ctx.ServerError("CreateWebhook", err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
 | 
			
		||||
	ctx.Redirect(orCtx.Link)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// MatrixHooksNewPost response for creating a Matrix hook
 | 
			
		||||
func MatrixHooksNewPost(ctx *context.Context) {
 | 
			
		||||
	form := web.GetForm(ctx).(*forms.NewMatrixHookForm)
 | 
			
		||||
	ctx.Data["Title"] = ctx.Tr("repo.settings")
 | 
			
		||||
	ctx.Data["PageIsSettingsHooks"] = true
 | 
			
		||||
	ctx.Data["PageIsSettingsHooksNew"] = true
 | 
			
		||||
	ctx.Data["Webhook"] = webhook.Webhook{HookEvent: &webhook.HookEvent{}}
 | 
			
		||||
	ctx.Data["HookType"] = webhook.MATRIX
 | 
			
		||||
 | 
			
		||||
	orCtx, err := getOrgRepoCtx(ctx)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		ctx.ServerError("getOrgRepoCtx", err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if ctx.HasError() {
 | 
			
		||||
		ctx.HTML(http.StatusOK, orCtx.NewTemplate)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	meta, err := json.Marshal(&webhook_service.MatrixMeta{
 | 
			
		||||
		HomeserverURL: form.HomeserverURL,
 | 
			
		||||
		Room:          form.RoomID,
 | 
			
		||||
		AccessToken:   form.AccessToken,
 | 
			
		||||
		MessageType:   form.MessageType,
 | 
			
		||||
	createWebhook(ctx, webhookCreationParams{
 | 
			
		||||
		URL:         fmt.Sprintf("%s/_matrix/client/r0/rooms/%s/send/m.room.message", form.HomeserverURL, url.PathEscape(form.RoomID)),
 | 
			
		||||
		ContentType: webhook.ContentTypeJSON,
 | 
			
		||||
		HTTPMethod:  http.MethodPut,
 | 
			
		||||
		WebhookForm: form.WebhookForm,
 | 
			
		||||
		Type:        webhook.MATRIX,
 | 
			
		||||
		Meta: &webhook_service.MatrixMeta{
 | 
			
		||||
			HomeserverURL: form.HomeserverURL,
 | 
			
		||||
			Room:          form.RoomID,
 | 
			
		||||
			AccessToken:   form.AccessToken,
 | 
			
		||||
			MessageType:   form.MessageType,
 | 
			
		||||
		},
 | 
			
		||||
	})
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		ctx.ServerError("Marshal", err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	w := &webhook.Webhook{
 | 
			
		||||
		RepoID:          orCtx.RepoID,
 | 
			
		||||
		URL:             fmt.Sprintf("%s/_matrix/client/r0/rooms/%s/send/m.room.message", form.HomeserverURL, url.PathEscape(form.RoomID)),
 | 
			
		||||
		ContentType:     webhook.ContentTypeJSON,
 | 
			
		||||
		HTTPMethod:      "PUT",
 | 
			
		||||
		HookEvent:       ParseHookEvent(form.WebhookForm),
 | 
			
		||||
		IsActive:        form.Active,
 | 
			
		||||
		Type:            webhook.MATRIX,
 | 
			
		||||
		Meta:            string(meta),
 | 
			
		||||
		OrgID:           orCtx.OrgID,
 | 
			
		||||
		IsSystemWebhook: orCtx.IsSystemWebhook,
 | 
			
		||||
	}
 | 
			
		||||
	if err := w.UpdateEvent(); err != nil {
 | 
			
		||||
		ctx.ServerError("UpdateEvent", err)
 | 
			
		||||
		return
 | 
			
		||||
	} else if err := webhook.CreateWebhook(ctx, w); err != nil {
 | 
			
		||||
		ctx.ServerError("CreateWebhook", err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
 | 
			
		||||
	ctx.Redirect(orCtx.Link)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// MSTeamsHooksNewPost response for creating MS Teams hook
 | 
			
		||||
func MSTeamsHooksNewPost(ctx *context.Context) {
 | 
			
		||||
	form := web.GetForm(ctx).(*forms.NewMSTeamsHookForm)
 | 
			
		||||
	ctx.Data["Title"] = ctx.Tr("repo.settings")
 | 
			
		||||
	ctx.Data["PageIsSettingsHooks"] = true
 | 
			
		||||
	ctx.Data["PageIsSettingsHooksNew"] = true
 | 
			
		||||
	ctx.Data["Webhook"] = webhook.Webhook{HookEvent: &webhook.HookEvent{}}
 | 
			
		||||
	ctx.Data["HookType"] = webhook.MSTEAMS
 | 
			
		||||
 | 
			
		||||
	orCtx, err := getOrgRepoCtx(ctx)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		ctx.ServerError("getOrgRepoCtx", err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if ctx.HasError() {
 | 
			
		||||
		ctx.HTML(http.StatusOK, orCtx.NewTemplate)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	w := &webhook.Webhook{
 | 
			
		||||
		RepoID:          orCtx.RepoID,
 | 
			
		||||
		URL:             form.PayloadURL,
 | 
			
		||||
		ContentType:     webhook.ContentTypeJSON,
 | 
			
		||||
		HookEvent:       ParseHookEvent(form.WebhookForm),
 | 
			
		||||
		IsActive:        form.Active,
 | 
			
		||||
		Type:            webhook.MSTEAMS,
 | 
			
		||||
		Meta:            "",
 | 
			
		||||
		OrgID:           orCtx.OrgID,
 | 
			
		||||
		IsSystemWebhook: orCtx.IsSystemWebhook,
 | 
			
		||||
	}
 | 
			
		||||
	if err := w.UpdateEvent(); err != nil {
 | 
			
		||||
		ctx.ServerError("UpdateEvent", err)
 | 
			
		||||
		return
 | 
			
		||||
	} else if err := webhook.CreateWebhook(ctx, w); err != nil {
 | 
			
		||||
		ctx.ServerError("CreateWebhook", err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
 | 
			
		||||
	ctx.Redirect(orCtx.Link)
 | 
			
		||||
	createWebhook(ctx, webhookCreationParams{
 | 
			
		||||
		URL:         form.PayloadURL,
 | 
			
		||||
		ContentType: webhook.ContentTypeJSON,
 | 
			
		||||
		WebhookForm: form.WebhookForm,
 | 
			
		||||
		Type:        webhook.MSTEAMS,
 | 
			
		||||
	})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// SlackHooksNewPost response for creating slack hook
 | 
			
		||||
func SlackHooksNewPost(ctx *context.Context) {
 | 
			
		||||
	form := web.GetForm(ctx).(*forms.NewSlackHookForm)
 | 
			
		||||
	ctx.Data["Title"] = ctx.Tr("repo.settings")
 | 
			
		||||
	ctx.Data["PageIsSettingsHooks"] = true
 | 
			
		||||
	ctx.Data["PageIsSettingsHooksNew"] = true
 | 
			
		||||
	ctx.Data["Webhook"] = webhook.Webhook{HookEvent: &webhook.HookEvent{}}
 | 
			
		||||
	ctx.Data["HookType"] = webhook.SLACK
 | 
			
		||||
 | 
			
		||||
	orCtx, err := getOrgRepoCtx(ctx)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		ctx.ServerError("getOrgRepoCtx", err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if ctx.HasError() {
 | 
			
		||||
		ctx.HTML(http.StatusOK, orCtx.NewTemplate)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if form.HasInvalidChannel() {
 | 
			
		||||
		ctx.Flash.Error(ctx.Tr("repo.settings.add_webhook.invalid_channel_name"))
 | 
			
		||||
		ctx.Redirect(orCtx.LinkNew + "/slack/new")
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	meta, err := json.Marshal(&webhook_service.SlackMeta{
 | 
			
		||||
		Channel:  strings.TrimSpace(form.Channel),
 | 
			
		||||
		Username: form.Username,
 | 
			
		||||
		IconURL:  form.IconURL,
 | 
			
		||||
		Color:    form.Color,
 | 
			
		||||
	createWebhook(ctx, webhookCreationParams{
 | 
			
		||||
		URL:         form.PayloadURL,
 | 
			
		||||
		ContentType: webhook.ContentTypeJSON,
 | 
			
		||||
		WebhookForm: form.WebhookForm,
 | 
			
		||||
		Type:        webhook.SLACK,
 | 
			
		||||
		Meta: &webhook_service.SlackMeta{
 | 
			
		||||
			Channel:  strings.TrimSpace(form.Channel),
 | 
			
		||||
			Username: form.Username,
 | 
			
		||||
			IconURL:  form.IconURL,
 | 
			
		||||
			Color:    form.Color,
 | 
			
		||||
		},
 | 
			
		||||
	})
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		ctx.ServerError("Marshal", err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	w := &webhook.Webhook{
 | 
			
		||||
		RepoID:          orCtx.RepoID,
 | 
			
		||||
		URL:             form.PayloadURL,
 | 
			
		||||
		ContentType:     webhook.ContentTypeJSON,
 | 
			
		||||
		HookEvent:       ParseHookEvent(form.WebhookForm),
 | 
			
		||||
		IsActive:        form.Active,
 | 
			
		||||
		Type:            webhook.SLACK,
 | 
			
		||||
		Meta:            string(meta),
 | 
			
		||||
		OrgID:           orCtx.OrgID,
 | 
			
		||||
		IsSystemWebhook: orCtx.IsSystemWebhook,
 | 
			
		||||
	}
 | 
			
		||||
	if err := w.UpdateEvent(); err != nil {
 | 
			
		||||
		ctx.ServerError("UpdateEvent", err)
 | 
			
		||||
		return
 | 
			
		||||
	} else if err := webhook.CreateWebhook(ctx, w); err != nil {
 | 
			
		||||
		ctx.ServerError("CreateWebhook", err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
 | 
			
		||||
	ctx.Redirect(orCtx.Link)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// FeishuHooksNewPost response for creating feishu hook
 | 
			
		||||
func FeishuHooksNewPost(ctx *context.Context) {
 | 
			
		||||
	form := web.GetForm(ctx).(*forms.NewFeishuHookForm)
 | 
			
		||||
	ctx.Data["Title"] = ctx.Tr("repo.settings")
 | 
			
		||||
	ctx.Data["PageIsSettingsHooks"] = true
 | 
			
		||||
	ctx.Data["PageIsSettingsHooksNew"] = true
 | 
			
		||||
	ctx.Data["Webhook"] = webhook.Webhook{HookEvent: &webhook.HookEvent{}}
 | 
			
		||||
	ctx.Data["HookType"] = webhook.FEISHU
 | 
			
		||||
 | 
			
		||||
	orCtx, err := getOrgRepoCtx(ctx)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		ctx.ServerError("getOrgRepoCtx", err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if ctx.HasError() {
 | 
			
		||||
		ctx.HTML(http.StatusOK, orCtx.NewTemplate)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	w := &webhook.Webhook{
 | 
			
		||||
		RepoID:          orCtx.RepoID,
 | 
			
		||||
		URL:             form.PayloadURL,
 | 
			
		||||
		ContentType:     webhook.ContentTypeJSON,
 | 
			
		||||
		HookEvent:       ParseHookEvent(form.WebhookForm),
 | 
			
		||||
		IsActive:        form.Active,
 | 
			
		||||
		Type:            webhook.FEISHU,
 | 
			
		||||
		Meta:            "",
 | 
			
		||||
		OrgID:           orCtx.OrgID,
 | 
			
		||||
		IsSystemWebhook: orCtx.IsSystemWebhook,
 | 
			
		||||
	}
 | 
			
		||||
	if err := w.UpdateEvent(); err != nil {
 | 
			
		||||
		ctx.ServerError("UpdateEvent", err)
 | 
			
		||||
		return
 | 
			
		||||
	} else if err := webhook.CreateWebhook(ctx, w); err != nil {
 | 
			
		||||
		ctx.ServerError("CreateWebhook", err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
 | 
			
		||||
	ctx.Redirect(orCtx.Link)
 | 
			
		||||
	createWebhook(ctx, webhookCreationParams{
 | 
			
		||||
		URL:         form.PayloadURL,
 | 
			
		||||
		ContentType: webhook.ContentTypeJSON,
 | 
			
		||||
		WebhookForm: form.WebhookForm,
 | 
			
		||||
		Type:        webhook.FEISHU,
 | 
			
		||||
	})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// WechatworkHooksNewPost response for creating wechatwork hook
 | 
			
		||||
func WechatworkHooksNewPost(ctx *context.Context) {
 | 
			
		||||
	form := web.GetForm(ctx).(*forms.NewWechatWorkHookForm)
 | 
			
		||||
 | 
			
		||||
	ctx.Data["Title"] = ctx.Tr("repo.settings")
 | 
			
		||||
	ctx.Data["PageIsSettingsHooks"] = true
 | 
			
		||||
	ctx.Data["PageIsSettingsHooksNew"] = true
 | 
			
		||||
	ctx.Data["Webhook"] = webhook.Webhook{HookEvent: &webhook.HookEvent{}}
 | 
			
		||||
	ctx.Data["HookType"] = webhook.WECHATWORK
 | 
			
		||||
 | 
			
		||||
	orCtx, err := getOrgRepoCtx(ctx)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		ctx.ServerError("getOrgRepoCtx", err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if ctx.HasError() {
 | 
			
		||||
		ctx.HTML(http.StatusOK, orCtx.NewTemplate)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	w := &webhook.Webhook{
 | 
			
		||||
		RepoID:          orCtx.RepoID,
 | 
			
		||||
		URL:             form.PayloadURL,
 | 
			
		||||
		ContentType:     webhook.ContentTypeJSON,
 | 
			
		||||
		HookEvent:       ParseHookEvent(form.WebhookForm),
 | 
			
		||||
		IsActive:        form.Active,
 | 
			
		||||
		Type:            webhook.WECHATWORK,
 | 
			
		||||
		Meta:            "",
 | 
			
		||||
		OrgID:           orCtx.OrgID,
 | 
			
		||||
		IsSystemWebhook: orCtx.IsSystemWebhook,
 | 
			
		||||
	}
 | 
			
		||||
	if err := w.UpdateEvent(); err != nil {
 | 
			
		||||
		ctx.ServerError("UpdateEvent", err)
 | 
			
		||||
		return
 | 
			
		||||
	} else if err := webhook.CreateWebhook(ctx, w); err != nil {
 | 
			
		||||
		ctx.ServerError("CreateWebhook", err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
 | 
			
		||||
	ctx.Redirect(orCtx.Link)
 | 
			
		||||
	createWebhook(ctx, webhookCreationParams{
 | 
			
		||||
		URL:         form.PayloadURL,
 | 
			
		||||
		ContentType: webhook.ContentTypeJSON,
 | 
			
		||||
		WebhookForm: form.WebhookForm,
 | 
			
		||||
		Type:        webhook.WECHATWORK,
 | 
			
		||||
	})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// PackagistHooksNewPost response for creating packagist hook
 | 
			
		||||
func PackagistHooksNewPost(ctx *context.Context) {
 | 
			
		||||
	form := web.GetForm(ctx).(*forms.NewPackagistHookForm)
 | 
			
		||||
	ctx.Data["Title"] = ctx.Tr("repo.settings")
 | 
			
		||||
	ctx.Data["PageIsSettingsHooks"] = true
 | 
			
		||||
	ctx.Data["PageIsSettingsHooksNew"] = true
 | 
			
		||||
	ctx.Data["Webhook"] = webhook.Webhook{HookEvent: &webhook.HookEvent{}}
 | 
			
		||||
	ctx.Data["HookType"] = webhook.PACKAGIST
 | 
			
		||||
 | 
			
		||||
	orCtx, err := getOrgRepoCtx(ctx)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		ctx.ServerError("getOrgRepoCtx", err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if ctx.HasError() {
 | 
			
		||||
		ctx.HTML(http.StatusOK, orCtx.NewTemplate)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	meta, err := json.Marshal(&webhook_service.PackagistMeta{
 | 
			
		||||
		Username:   form.Username,
 | 
			
		||||
		APIToken:   form.APIToken,
 | 
			
		||||
		PackageURL: form.PackageURL,
 | 
			
		||||
	createWebhook(ctx, webhookCreationParams{
 | 
			
		||||
		URL:         fmt.Sprintf("https://packagist.org/api/update-package?username=%s&apiToken=%s", url.QueryEscape(form.Username), url.QueryEscape(form.APIToken)),
 | 
			
		||||
		ContentType: webhook.ContentTypeJSON,
 | 
			
		||||
		WebhookForm: form.WebhookForm,
 | 
			
		||||
		Type:        webhook.PACKAGIST,
 | 
			
		||||
		Meta: &webhook_service.PackagistMeta{
 | 
			
		||||
			Username:   form.Username,
 | 
			
		||||
			APIToken:   form.APIToken,
 | 
			
		||||
			PackageURL: form.PackageURL,
 | 
			
		||||
		},
 | 
			
		||||
	})
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		ctx.ServerError("Marshal", err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	w := &webhook.Webhook{
 | 
			
		||||
		RepoID:          orCtx.RepoID,
 | 
			
		||||
		URL:             fmt.Sprintf("https://packagist.org/api/update-package?username=%s&apiToken=%s", url.QueryEscape(form.Username), url.QueryEscape(form.APIToken)),
 | 
			
		||||
		ContentType:     webhook.ContentTypeJSON,
 | 
			
		||||
		HookEvent:       ParseHookEvent(form.WebhookForm),
 | 
			
		||||
		IsActive:        form.Active,
 | 
			
		||||
		Type:            webhook.PACKAGIST,
 | 
			
		||||
		Meta:            string(meta),
 | 
			
		||||
		OrgID:           orCtx.OrgID,
 | 
			
		||||
		IsSystemWebhook: orCtx.IsSystemWebhook,
 | 
			
		||||
	}
 | 
			
		||||
	if err := w.UpdateEvent(); err != nil {
 | 
			
		||||
		ctx.ServerError("UpdateEvent", err)
 | 
			
		||||
		return
 | 
			
		||||
	} else if err := webhook.CreateWebhook(ctx, w); err != nil {
 | 
			
		||||
		ctx.ServerError("CreateWebhook", err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
 | 
			
		||||
	ctx.Redirect(orCtx.Link)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func checkWebhook(ctx *context.Context) (*orgRepoCtx, *webhook.Webhook) {
 | 
			
		||||
@@ -894,12 +579,6 @@ func SlackHooksEditPost(ctx *context.Context) {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if form.HasInvalidChannel() {
 | 
			
		||||
		ctx.Flash.Error(ctx.Tr("repo.settings.add_webhook.invalid_channel_name"))
 | 
			
		||||
		ctx.Redirect(fmt.Sprintf("%s/%d", orCtx.Link, w.ID))
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	meta, err := json.Marshal(&webhook_service.SlackMeta{
 | 
			
		||||
		Channel:  strings.TrimSpace(form.Channel),
 | 
			
		||||
		Username: form.Username,
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user