Browse Source

Add Dingtalk webhook support (#4773)

* Add dingtalk webhook into html template

* Add Dingtalk's icon

* Insert dingtalk into repo's webhook page template

* Insert dingtalk into org's webhook page

* Add dingtalk into default webhook config

* Add locale string for add_dingtalk_hook_desc

* Update bindata

* Add dingtalk webhook form validator

* Add dingtalk hook task

* Add dingtalk hook create handler

* Add dingtalk hook edit handler

* Add dingtalk router

* Add dingtalk webhook task skeleton

* Add markdown link formatter

* Add Dingtalk ActionCard create wrapper

* Add support for dingtalk create event payload

* Add support for dingtalk delete event payload

* Add support for dingtalk fork event payload

* Add support for dingtalk push event payload

* Add support for dingtalk issue event payload

* Add support for dingtalk issue comment payload

* Add support for dingtalk pull event payload

* Add support for dingtalk release event payload
pull/4789/merge
Athurg Feng 7 years ago committed by 无闻
parent
commit
52f3833811
  1. 4
      cmd/web.go
  2. 4
      conf/app.ini
  3. 1
      conf/locale/locale_bg-BG.ini
  4. 1
      conf/locale/locale_cs-CZ.ini
  5. 1
      conf/locale/locale_de-DE.ini
  6. 1
      conf/locale/locale_en-GB.ini
  7. 1
      conf/locale/locale_en-US.ini
  8. 1
      conf/locale/locale_es-ES.ini
  9. 1
      conf/locale/locale_fi-FI.ini
  10. 1
      conf/locale/locale_fr-FR.ini
  11. 1
      conf/locale/locale_gl-ES.ini
  12. 1
      conf/locale/locale_hu-HU.ini
  13. 1
      conf/locale/locale_it-IT.ini
  14. 1
      conf/locale/locale_ja-JP.ini
  15. 1
      conf/locale/locale_ko-KR.ini
  16. 1
      conf/locale/locale_lv-LV.ini
  17. 1
      conf/locale/locale_nl-NL.ini
  18. 1
      conf/locale/locale_pl-PL.ini
  19. 1
      conf/locale/locale_pt-BR.ini
  20. 1
      conf/locale/locale_ru-RU.ini
  21. 1
      conf/locale/locale_sr-SP.ini
  22. 1
      conf/locale/locale_sv-SE.ini
  23. 1
      conf/locale/locale_tr-TR.ini
  24. 1
      conf/locale/locale_uk-UA.ini
  25. 1
      conf/locale/locale_zh-CN.ini
  26. 1
      conf/locale/locale_zh-HK.ini
  27. 1
      conf/locale/locale_zh-TW.ini
  28. 6
      models/webhook.go
  29. 260
      models/webhook_dingtalk.go
  30. 506
      pkg/bindata/bindata.go
  31. 9
      pkg/form/repo.go
  32. BIN
      public/img/dingtalk.png
  33. 71
      routes/repo/webhook.go
  34. 1
      templates/org/settings/webhook_new.tmpl
  35. 11
      templates/repo/settings/webhook/dingtalk.tmpl
  36. 4
      templates/repo/settings/webhook/list.tmpl
  37. 1
      templates/repo/settings/webhook/new.tmpl

4
cmd/web.go

@ -387,10 +387,12 @@ func runWeb(c *cli.Context) error {
m.Post("/gogs/new", bindIgnErr(form.NewWebhook{}), repo.WebHooksNewPost)
m.Post("/slack/new", bindIgnErr(form.NewSlackHook{}), repo.SlackHooksNewPost)
m.Post("/discord/new", bindIgnErr(form.NewDiscordHook{}), repo.DiscordHooksNewPost)
m.Post("/dingtalk/new", bindIgnErr(form.NewDingtalkHook{}), repo.DingtalkHooksNewPost)
m.Get("/:id", repo.WebHooksEdit)
m.Post("/gogs/:id", bindIgnErr(form.NewWebhook{}), repo.WebHooksEditPost)
m.Post("/slack/:id", bindIgnErr(form.NewSlackHook{}), repo.SlackHooksEditPost)
m.Post("/discord/:id", bindIgnErr(form.NewDiscordHook{}), repo.DiscordHooksEditPost)
m.Post("/dingtalk/:id", bindIgnErr(form.NewDingtalkHook{}), repo.DingtalkHooksEditPost)
})
m.Route("/delete", "GET,POST", org.SettingsDelete)
@ -439,9 +441,11 @@ func runWeb(c *cli.Context) error {
m.Post("/gogs/new", bindIgnErr(form.NewWebhook{}), repo.WebHooksNewPost)
m.Post("/slack/new", bindIgnErr(form.NewSlackHook{}), repo.SlackHooksNewPost)
m.Post("/discord/new", bindIgnErr(form.NewDiscordHook{}), repo.DiscordHooksNewPost)
m.Post("/dingtalk/new", bindIgnErr(form.NewDingtalkHook{}), repo.DingtalkHooksNewPost)
m.Post("/gogs/:id", bindIgnErr(form.NewWebhook{}), repo.WebHooksEditPost)
m.Post("/slack/:id", bindIgnErr(form.NewSlackHook{}), repo.SlackHooksEditPost)
m.Post("/discord/:id", bindIgnErr(form.NewDiscordHook{}), repo.DiscordHooksEditPost)
m.Post("/dingtalk/:id", bindIgnErr(form.NewDingtalkHook{}), repo.DingtalkHooksEditPost)
m.Group("/:id", func() {
m.Get("", repo.WebHooksEdit)

4
conf/app.ini

@ -209,8 +209,8 @@ ENABLE_REVERSE_PROXY_AUTO_REGISTRATION = false
ENABLE_CAPTCHA = true
[webhook]
; Types are enabled for users to use, can be "gogs", "slack", "discord"
TYPES = gogs, slack, discord
; Types are enabled for users to use, can be "gogs", "slack", "discord", "dingtalk"
TYPES = gogs, slack, discord, dingtalk
; Hook task queue length, increase if webhook shooting starts hanging
QUEUE_LENGTH = 1000
; Deliver timeout in seconds

1
conf/locale/locale_bg-BG.ini

@ -830,6 +830,7 @@ settings.recent_deliveries=Последни изпращания
settings.hook_type=Тип на куката
settings.add_slack_hook_desc=Добавяне на интеграция със <a href="%s">Slack</a> във Вашето хранилище.
settings.add_discord_hook_desc=Add <a href="%s">Discord</a> integration to your repository.
settings.add_dingtalk_hook_desc=Add <a href="%s">Dingtalk</a> integration to your repository.
settings.slack_token=API ключ
settings.slack_domain=Домейн
settings.slack_channel=Канал

1
conf/locale/locale_cs-CZ.ini

@ -830,6 +830,7 @@ settings.recent_deliveries=Nedávné dodávky
settings.hook_type=Typ háčku
settings.add_slack_hook_desc=Přidat integraci <a href="%s">Slacku</a> do vašeho repositáře.
settings.add_discord_hook_desc=Přidat integraci <a href="%s">Discord</a> do vašeho repositáře.
settings.add_dingtalk_hook_desc=Přidat integraci <a href="%s">Dingtalk</a> do vašeho repositáře.
settings.slack_token=Poukázka
settings.slack_domain=Doména
settings.slack_channel=Kanál

1
conf/locale/locale_de-DE.ini

@ -830,6 +830,7 @@ settings.recent_deliveries=Letzte Zustellungen
settings.hook_type=Hook Typ
settings.add_slack_hook_desc=Fügen Sie <a href="%s">Slack</a>-Integration zu Ihrem Repository hinzu.
settings.add_discord_hook_desc=Fügen Sie <a href="%s">Discord</a>-Integration zu Ihrem Repository hinzu.
settings.add_dingtalk_hook_desc=Fügen Sie <a href="%s">Dingtalk</a>-Integration zu Ihrem Repository hinzu.
settings.slack_token=Token
settings.slack_domain=Domain
settings.slack_channel=Kanal

1
conf/locale/locale_en-GB.ini

@ -830,6 +830,7 @@ settings.recent_deliveries=Recent Deliveries
settings.hook_type=Hook Type
settings.add_slack_hook_desc=Add <a href="%s">Slack</a> integration to your repository.
settings.add_discord_hook_desc=Add <a href="%s">Discord</a> integration to your repository.
settings.add_dingtalk_hook_desc=Add <a href="%s">Dingtalk</a> integration to your repository.
settings.slack_token=Token
settings.slack_domain=Domain
settings.slack_channel=Channel

1
conf/locale/locale_en-US.ini

@ -831,6 +831,7 @@ settings.recent_deliveries = Recent Deliveries
settings.hook_type = Hook Type
settings.add_slack_hook_desc = Add <a href="%s">Slack</a> integration to your repository.
settings.add_discord_hook_desc = Add <a href="%s">Discord</a> integration to your repository.
settings.add_dingtalk_hook_desc = Add <a href="%s">Dingtalk</a> integration to your repository.
settings.slack_token = Token
settings.slack_domain = Domain
settings.slack_channel = Channel

1
conf/locale/locale_es-ES.ini

@ -830,6 +830,7 @@ settings.recent_deliveries=Envíos Recientes
settings.hook_type=Tipo de Hook
settings.add_slack_hook_desc=Añade integración con <a href="%s">Slack</a> a tu repositorio.
settings.add_discord_hook_desc=Añade integración con <a href="%s">Slack</a> a tu repositorio.
settings.add_dingtalk_hook_desc=Añade integración con <a href="%s">Dingtalk</a> a tu repositorio.
settings.slack_token=Token
settings.slack_domain=Dominio
settings.slack_channel=Canal

1
conf/locale/locale_fi-FI.ini

@ -830,6 +830,7 @@ settings.recent_deliveries=Viimeisimmät toimitukset
settings.hook_type=Koukkutyyppi
settings.add_slack_hook_desc=Lisää <a href="%s">Slack</a> integraatio repoosi.
settings.add_discord_hook_desc=Lisää <a href="%s">Discord</a> integraatio repositoryysi.
settings.add_dingtalk_hook_desc=Lisää <a href="%s">Dingtalk</a> integraatio repositoryysi.
settings.slack_token=Pääsymerkki
settings.slack_domain=Verkkotunnus
settings.slack_channel=Kanava

1
conf/locale/locale_fr-FR.ini

@ -830,6 +830,7 @@ settings.recent_deliveries=Livraisons récentes
settings.hook_type=Type de Hook
settings.add_slack_hook_desc=Intégrer <a href="%s"> Slack</a> à votre dépôt.
settings.add_discord_hook_desc=Ajouter l'intégration de <a href="%s">Discord</a> à votre dépôt.
settings.add_dingtalk_hook_desc=Ajouter l'intégration de <a href="%s">Dingtalk</a> à votre dépôt.
settings.slack_token=Jeton
settings.slack_domain=Domaine
settings.slack_channel=Canal

1
conf/locale/locale_gl-ES.ini

@ -830,6 +830,7 @@ settings.recent_deliveries=Envíos recentes
settings.hook_type=Tipo de Hook
settings.add_slack_hook_desc=Engade integración con <a href="%s">Slack</a> ao teu repositorio.
settings.add_discord_hook_desc=Add <a href="%s">Discord</a> integration to your repository.
settings.add_dingtalk_hook_desc=Add <a href="%s">Dingtalk</a> integration to your repository.
settings.slack_token=Token
settings.slack_domain=Dominio
settings.slack_channel=Canle

1
conf/locale/locale_hu-HU.ini

@ -830,6 +830,7 @@ settings.recent_deliveries=Legutóbbi Küldések
settings.hook_type=Hook Típusa
settings.add_slack_hook_desc=<a href="%s">Slack</a> integráció hozzáadása a repository-hoz.
settings.add_discord_hook_desc=<a href="%s">Discord</a> integráció hozzáadva a térolóhoz.
settings.add_dingtalk_hook_desc=<a href="%s">Dingtalk</a> integráció hozzáadva a térolóhoz.
settings.slack_token=Token
settings.slack_domain=Domain
settings.slack_channel=Csatorna

1
conf/locale/locale_it-IT.ini

@ -830,6 +830,7 @@ settings.recent_deliveries=Recenti Deliveries
settings.hook_type=Tipo di Hook
settings.add_slack_hook_desc=Aggiungi <a href="%s"> Slack</a> integrazione al tuo repository.
settings.add_discord_hook_desc=Add <a href="%s">Discord</a> integration to your repository.
settings.add_dingtalk_hook_desc=Add <a href="%s">Dingtalk</a> integration to your repository.
settings.slack_token=Token
settings.slack_domain=Dominio
settings.slack_channel=Canale

1
conf/locale/locale_ja-JP.ini

@ -830,6 +830,7 @@ settings.recent_deliveries=最近のデリバリー
settings.hook_type=フックタイプ
settings.add_slack_hook_desc=リポジトリに <a href="%s"> Slack</a> 連携を追加します。
settings.add_discord_hook_desc=リポジトリに <a href="%s">Discord</a> 連携を追加します。
settings.add_dingtalk_hook_desc=リポジトリに <a href="%s">Dingtalk</a> 連携を追加します。
settings.slack_token=トークン
settings.slack_domain=ドメイン
settings.slack_channel=チャンネル

1
conf/locale/locale_ko-KR.ini

@ -831,6 +831,7 @@ settings.recent_deliveries=최근의 Deliveries
settings.hook_type=훅 타입
settings.add_slack_hook_desc=저장소에 <a href="%s">슬랙</a>연동을 추가
settings.add_discord_hook_desc=Add <a href="%s">Discord</a> integration to your repository.
settings.add_dingtalk_hook_desc=Add <a href="%s">Dingtalk</a> integration to your repository.
settings.slack_token=토큰
settings.slack_domain=도메인
settings.slack_channel=채널

1
conf/locale/locale_lv-LV.ini

@ -830,6 +830,7 @@ settings.recent_deliveries=Pēdējās piegādes
settings.hook_type=Āķa veids
settings.add_slack_hook_desc=PIevienot <a href="%s">Slack</a> integrāciju Jūsu repozitorijā.
settings.add_discord_hook_desc=Add <a href="%s">Discord</a> integration to your repository.
settings.add_dingtalk_hook_desc=Add <a href="%s">Dingtalk</a> integration to your repository.
settings.slack_token=Talons
settings.slack_domain=Domēns
settings.slack_channel=Kanāls

1
conf/locale/locale_nl-NL.ini

@ -830,6 +830,7 @@ settings.recent_deliveries=Recente bezorgingen
settings.hook_type=Type hook
settings.add_slack_hook_desc=<a href="%s"> toegestane vertraging</a> integratie toevoegen aan uw repository.
settings.add_discord_hook_desc=Add <a href="%s">Discord</a> integration to your repository.
settings.add_dingtalk_hook_desc=Add <a href="%s">Dingtalk</a> integration to your repository.
settings.slack_token=Slack token
settings.slack_domain=Slack domein
settings.slack_channel=Slack kanaal

1
conf/locale/locale_pl-PL.ini

@ -830,6 +830,7 @@ settings.recent_deliveries=Ostatnie wywołania
settings.hook_type=Typ hooka
settings.add_slack_hook_desc=Dodaj integrację ze <a href="%s">Slackiem</a> do Twojego repozytorium.
settings.add_discord_hook_desc=Dodaj integrację <a href="%s">Discord</a> do Twojego repozytorium.
settings.add_dingtalk_hook_desc=Dodaj integrację <a href="%s">Dingtalk</a> do Twojego repozytorium.
settings.slack_token=Token
settings.slack_domain=Domena
settings.slack_channel=Kanał

1
conf/locale/locale_pt-BR.ini

@ -830,6 +830,7 @@ settings.recent_deliveries=Entregas Recentes
settings.hook_type=Tipo de Hook
settings.add_slack_hook_desc=Adicionar <a href="%s">Slack</a> de integração para o seu repositório.
settings.add_discord_hook_desc=Add <a href="%s">Discord</a> integration to your repository.
settings.add_dingtalk_hook_desc=Add <a href="%s">Dingtalk</a> integration to your repository.
settings.slack_token=Token
settings.slack_domain=Domínio
settings.slack_channel=Canal

1
conf/locale/locale_ru-RU.ini

@ -830,6 +830,7 @@ settings.recent_deliveries=Недавние рассылки
settings.hook_type=Тип перехватчика
settings.add_slack_hook_desc=Добавить интеграцию с <a href="%s">Slack</a> в ваш репозиторий.
settings.add_discord_hook_desc=Добавить интеграцию с <a href="%s">Discord</a> в ваш репозиторий.
settings.add_dingtalk_hook_desc=Добавить интеграцию с <a href="%s">Dingtalk</a> в ваш репозиторий.
settings.slack_token=Token
settings.slack_domain=Домен
settings.slack_channel=Канал

1
conf/locale/locale_sr-SP.ini

@ -830,6 +830,7 @@ settings.recent_deliveries=Недавне испоруке
settings.hook_type=Тип Hook-а
settings.add_slack_hook_desc=Додавање интеграције <a href="%s">Slack</a> у спремиште.
settings.add_discord_hook_desc=Додавати <a href="%s">Discord</a> интеграцију вашем спремишту.
settings.add_dingtalk_hook_desc=Додавати <a href="%s">Dingtalk</a> интеграцију вашем спремишту.
settings.slack_token=Токен
settings.slack_domain=Домен
settings.slack_channel=Канал

1
conf/locale/locale_sv-SE.ini

@ -830,6 +830,7 @@ settings.recent_deliveries=Färska leveranser
settings.hook_type=Kroktyp
settings.add_slack_hook_desc=Lägg till <a href="%s">Slack</a>-integration till ditt förråd.
settings.add_discord_hook_desc=Add <a href="%s">Discord</a> integration to your repository.
settings.add_dingtalk_hook_desc=Add <a href="%s">Dingtalk</a> integration to your repository.
settings.slack_token=Pollett
settings.slack_domain=Domän
settings.slack_channel=Kanal

1
conf/locale/locale_tr-TR.ini

@ -830,6 +830,7 @@ settings.recent_deliveries=Son Dağıtımlar
settings.hook_type=İstek Türü
settings.add_slack_hook_desc=Deponuza <a href="%s">Slack</a> entegrasyonunu ekleyin.
settings.add_discord_hook_desc=Add <a href="%s">Discord</a> integration to your repository.
settings.add_dingtalk_hook_desc=Add <a href="%s">Dingtalk</a> integration to your repository.
settings.slack_token=Erişim Anahtarı
settings.slack_domain=Alan Adı
settings.slack_channel=Kanal

1
conf/locale/locale_uk-UA.ini

@ -830,6 +830,7 @@ settings.recent_deliveries=Недавні розсилки
settings.hook_type=Тип хуку
settings.add_slack_hook_desc=Додати <a href="%s">Slack</a>-інтеграцію до вашого репозиторію.
settings.add_discord_hook_desc=Додати <a href="%s">Discord</a>-інтеграцію до репозиторію.
settings.add_dingtalk_hook_desc=Додати <a href="%s">Dingtalk</a>-інтеграцію до репозиторію.
settings.slack_token=Жетон
settings.slack_domain=Домен
settings.slack_channel=Канал

1
conf/locale/locale_zh-CN.ini

@ -830,6 +830,7 @@ settings.recent_deliveries=最近推送记录
settings.hook_type=钩子类型
settings.add_slack_hook_desc=为您的仓库增加 <a href="%s">Slack</a> 集成。
settings.add_discord_hook_desc=为您的仓库增加 <a href="%s">Discord</a> 集成。
settings.add_dingtalk_hook_desc=为您的仓库增加 <a href="%s">钉钉机器人</a> 集成。
settings.slack_token=令牌
settings.slack_domain=域名
settings.slack_channel=频道

1
conf/locale/locale_zh-HK.ini

@ -830,6 +830,7 @@ settings.recent_deliveries=最近推送記錄
settings.hook_type=鉤子類型
settings.add_slack_hook_desc=為您的倉庫增加 <a href="%s">Slack</a> 集成。
settings.add_discord_hook_desc=Add <a href="%s">Discord</a> integration to your repository.
settings.add_dingtalk_hook_desc=Add <a href="%s">釘釘機器人</a> integration to your repository.
settings.slack_token=令牌
settings.slack_domain=域名
settings.slack_channel=頻道

1
conf/locale/locale_zh-TW.ini

@ -830,6 +830,7 @@ settings.recent_deliveries=最近推送記錄
settings.hook_type=鉤子類型
settings.add_slack_hook_desc=為您的倉庫增加 <a href="%s">Slack</a> 集成。
settings.add_discord_hook_desc=Hook <a href="%s">Discord</a> 到你的 repository。
settings.add_dingtalk_hook_desc=Add <a href="%s">釘釘機器人</a> 到你的 repository.
settings.slack_token=令牌
settings.slack_domain=域名
settings.slack_channel=頻道

6
models/webhook.go

@ -349,6 +349,7 @@ const (
GOGS HookTaskType = iota + 1
SLACK
DISCORD
DINGTALK
)
var hookTaskTypes = map[string]HookTaskType{
@ -571,6 +572,11 @@ func prepareHookTasks(e Engine, repo *Repository, event HookEventType, p api.Pay
if err != nil {
return fmt.Errorf("GetDiscordPayload: %v", err)
}
case DINGTALK:
payloader, err = GetDingtalkPayload(p, event)
if err != nil {
return fmt.Errorf("GetDingtalkPayload: %v", err)
}
default:
payloader = p
}

260
models/webhook_dingtalk.go

@ -0,0 +1,260 @@
// Copyright 2017 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package models
import (
"encoding/json"
"fmt"
"strings"
"github.com/gogits/git-module"
api "github.com/gogits/go-gogs-client"
)
const (
DingtalkNotificationTitle = "Gogs Notification"
)
//Refer: https://open-doc.dingtalk.com/docs/doc.htm?treeId=257&articleId=105735&docType=1
type DingtalkActionCard struct {
Title string `json:"title"`
Text string `json:"text"`
HideAvatar string `json:"hideAvatar"`
BtnOrientation string `json:"btnOrientation"`
SingleTitle string `json:"singleTitle"`
SingleURL string `json:"singleURL"`
}
//Refer: https://open-doc.dingtalk.com/docs/doc.htm?treeId=257&articleId=105735&docType=1
type DingtalkAtObject struct {
AtMobiles []string `json:"atMobiles"`
IsAtAll bool `json:"isAtAll"`
}
//Refer: https://open-doc.dingtalk.com/docs/doc.htm?treeId=257&articleId=105735&docType=1
type DingtalkPayload struct {
MsgType string `json:"msgtype"`
At DingtalkAtObject `json:"at"`
ActionCard DingtalkActionCard `json:"actionCard"`
}
func (p *DingtalkPayload) JSONPayload() ([]byte, error) {
data, err := json.MarshalIndent(p, "", " ")
if err != nil {
return []byte{}, err
}
return data, nil
}
func NewDingtalkActionCard(singleTitle, singleURL string) DingtalkActionCard {
return DingtalkActionCard{
Title: DingtalkNotificationTitle,
SingleURL: singleURL,
SingleTitle: singleTitle,
}
}
//TODO: add content
func GetDingtalkPayload(p api.Payloader, event HookEventType) (payload *DingtalkPayload, err error) {
switch event {
case HOOK_EVENT_CREATE:
payload, err = getDingtalkCreatePayload(p.(*api.CreatePayload))
case HOOK_EVENT_DELETE:
payload, err = getDingtalkDeletePayload(p.(*api.DeletePayload))
case HOOK_EVENT_FORK:
payload, err = getDingtalkForkPayload(p.(*api.ForkPayload))
case HOOK_EVENT_PUSH:
payload, err = getDingtalkPushPayload(p.(*api.PushPayload))
case HOOK_EVENT_ISSUES:
payload, err = getDingtalkIssuesPayload(p.(*api.IssuesPayload))
case HOOK_EVENT_ISSUE_COMMENT:
payload, err = getDingtalkIssueCommentPayload(p.(*api.IssueCommentPayload))
case HOOK_EVENT_PULL_REQUEST:
payload, err = getDingtalkPullRequestPayload(p.(*api.PullRequestPayload))
case HOOK_EVENT_RELEASE:
payload, err = getDingtalkReleasePayload(p.(*api.ReleasePayload))
}
if err != nil {
return nil, fmt.Errorf("event '%s': %v", event, err)
}
return payload, nil
}
func getDingtalkCreatePayload(p *api.CreatePayload) (*DingtalkPayload, error) {
refName := git.RefEndName(p.Ref)
refType := strings.Title(p.RefType)
actionCard := NewDingtalkActionCard("View "+refType, p.Repo.HTMLURL+"/src/"+refName)
actionCard.Text += "# New " + refType + " Create Event"
actionCard.Text += "\n- Repo: **" + MarkdownLinkFormatter(p.Repo.HTMLURL, p.Repo.Name) + "**"
actionCard.Text += "\n- New " + refType + ": **" + MarkdownLinkFormatter(p.Repo.HTMLURL+"/src/"+refName, refName) + "**"
return &DingtalkPayload{MsgType: "actionCard", ActionCard: actionCard}, nil
}
func getDingtalkDeletePayload(p *api.DeletePayload) (*DingtalkPayload, error) {
refName := git.RefEndName(p.Ref)
refType := strings.Title(p.RefType)
actionCard := NewDingtalkActionCard("View Repo", p.Repo.HTMLURL)
actionCard.Text += "# " + refType + " Delete Event"
actionCard.Text += "\n- Repo: **" + MarkdownLinkFormatter(p.Repo.HTMLURL, p.Repo.Name) + "**"
actionCard.Text += "\n- " + refType + ": **" + refName + "**"
return &DingtalkPayload{MsgType: "actionCard", ActionCard: actionCard}, nil
}
func getDingtalkForkPayload(p *api.ForkPayload) (*DingtalkPayload, error) {
actionCard := NewDingtalkActionCard("View Forkee", p.Forkee.HTMLURL)
actionCard.Text += "# Repo Fork Event"
actionCard.Text += "\n- From Repo: **" + MarkdownLinkFormatter(p.Repo.HTMLURL, p.Repo.Name) + "**"
actionCard.Text += "\n- To Repo: **" + MarkdownLinkFormatter(p.Forkee.HTMLURL, p.Forkee.FullName) + "**"
return &DingtalkPayload{MsgType: "actionCard", ActionCard: actionCard}, nil
}
func getDingtalkPushPayload(p *api.PushPayload) (*DingtalkPayload, error) {
refName := git.RefEndName(p.Ref)
pusher := p.Pusher.FullName
if pusher == "" {
pusher = p.Pusher.UserName
}
var detail string
for i, commit := range p.Commits {
msg := strings.Split(commit.Message, "\n")[0]
commitLink := MarkdownLinkFormatter(commit.URL, commit.ID[:7])
detail += fmt.Sprintf("> %d. %s %s - %s\n", i, commitLink, commit.Author.Name, msg)
}
actionCard := NewDingtalkActionCard("View Changes", p.CompareURL)
actionCard.Text += "# Repo Push Event"
actionCard.Text += "\n- Repo: **" + MarkdownLinkFormatter(p.Repo.HTMLURL, p.Repo.Name) + "**"
actionCard.Text += "\n- Ref: **" + MarkdownLinkFormatter(p.Repo.HTMLURL+"/src/"+refName, refName) + "**"
actionCard.Text += "\n- Pusher: **" + pusher + "**"
actionCard.Text += "\n## " + fmt.Sprintf("Total %d commits(s)", len(p.Commits))
actionCard.Text += "\n" + detail
return &DingtalkPayload{MsgType: "actionCard", ActionCard: actionCard}, nil
}
func getDingtalkIssuesPayload(p *api.IssuesPayload) (*DingtalkPayload, error) {
issueName := fmt.Sprintf("#%d %s", p.Index, p.Issue.Title)
issueURL := fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Index)
actionCard := NewDingtalkActionCard("View Issue", issueURL)
actionCard.Text += "# Issue Event " + strings.Title(string(p.Action))
actionCard.Text += "\n- Issue: **" + MarkdownLinkFormatter(issueURL, issueName) + "**"
if p.Action == api.HOOK_ISSUE_ASSIGNED {
actionCard.Text += "\n- New Assignee: **" + p.Issue.Assignee.UserName + "**"
} else if p.Action == api.HOOK_ISSUE_MILESTONED {
actionCard.Text += "\n- New Milestone: **" + p.Issue.Milestone.Title + "**"
} else if p.Action == api.HOOK_ISSUE_LABEL_UPDATED {
if len(p.Issue.Labels) > 0 {
labels := make([]string, len(p.Issue.Labels))
for i, label := range p.Issue.Labels {
labels[i] = "**" + label.Name + "**"
}
actionCard.Text += "\n- Labels: " + strings.Join(labels, ",")
} else {
actionCard.Text += "\n- Labels: **empty**"
}
}
if p.Issue.Body != "" {
actionCard.Text += "\n> " + p.Issue.Body
}
return &DingtalkPayload{MsgType: "actionCard", ActionCard: actionCard}, nil
}
func getDingtalkIssueCommentPayload(p *api.IssueCommentPayload) (*DingtalkPayload, error) {
issueName := fmt.Sprintf("#%d %s", p.Issue.Index, p.Issue.Title)
commentURL := fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Issue.Index)
if p.Action != api.HOOK_ISSUE_COMMENT_DELETED {
commentURL += "#" + CommentHashTag(p.Comment.ID)
}
issueURL := fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Issue.Index)
actionCard := NewDingtalkActionCard("View Issue Comment", commentURL)
actionCard.Text += "# Issue Comment " + strings.Title(string(p.Action))
actionCard.Text += "\n- Issue: " + MarkdownLinkFormatter(issueURL, issueName)
actionCard.Text += "\n- Comment content: "
actionCard.Text += "\n> " + p.Comment.Body
return &DingtalkPayload{MsgType: "actionCard", ActionCard: actionCard}, nil
}
func getDingtalkPullRequestPayload(p *api.PullRequestPayload) (*DingtalkPayload, error) {
title := "# Pull Request " + strings.Title(string(p.Action))
if p.Action == api.HOOK_ISSUE_CLOSED && p.PullRequest.HasMerged {
title = "# Pull Request Merged"
}
pullRequestURL := fmt.Sprintf("%s/pulls/%d", p.Repository.HTMLURL, p.Index)
content := "- PR: " + MarkdownLinkFormatter(pullRequestURL, fmt.Sprintf("#%d %s", p.Index, p.PullRequest.Title))
if p.Action == api.HOOK_ISSUE_ASSIGNED {
content += "\n- New Assignee: **" + p.PullRequest.Assignee.UserName + "**"
} else if p.Action == api.HOOK_ISSUE_MILESTONED {
content += "\n- New Milestone: *" + p.PullRequest.Milestone.Title + "*"
} else if p.Action == api.HOOK_ISSUE_LABEL_UPDATED {
labels := make([]string, len(p.PullRequest.Labels))
for i, label := range p.PullRequest.Labels {
labels[i] = "**" + label.Name + "**"
}
content += "\n- New Labels: " + strings.Join(labels, ",")
}
actionCard := NewDingtalkActionCard("View Pull Request", pullRequestURL)
actionCard.Text += title + "\n" + content
if p.Action == api.HOOK_ISSUE_OPENED || p.Action == api.HOOK_ISSUE_EDITED {
actionCard.Text += "\n> " + p.PullRequest.Body
}
return &DingtalkPayload{MsgType: "actionCard", ActionCard: actionCard}, nil
}
func getDingtalkReleasePayload(p *api.ReleasePayload) (*DingtalkPayload, error) {
releaseURL := p.Repository.HTMLURL + "/src/" + p.Release.TagName
author := p.Release.Author.FullName
if author == "" {
author = p.Release.Author.UserName
}
actionCard := NewDingtalkActionCard("View Release", releaseURL)
actionCard.Text += "# New Release Published"
actionCard.Text += "\n- Repo: " + MarkdownLinkFormatter(p.Repository.HTMLURL, p.Repository.Name)
actionCard.Text += "\n- Tag: " + MarkdownLinkFormatter(releaseURL, p.Release.TagName)
actionCard.Text += "\n- Author: " + author
actionCard.Text += fmt.Sprintf("\n- Draft?: %t", p.Release.Draft)
actionCard.Text += fmt.Sprintf("\n- Pre Release?: %t", p.Release.Prerelease)
actionCard.Text += "\n- Title: " + p.Release.Name
if p.Release.Body != "" {
actionCard.Text += "\n- Note: " + p.Release.Body
}
return &DingtalkPayload{MsgType: "actionCard", ActionCard: actionCard}, nil
}
//Format link addr and title into markdown style
func MarkdownLinkFormatter(link, text string) string {
return "[" + text + "](" + link + ")"
}

506
pkg/bindata/bindata.go

File diff suppressed because one or more lines are too long

9
pkg/form/repo.go

@ -195,6 +195,15 @@ func (f *NewDiscordHook) Validate(ctx *macaron.Context, errs binding.Errors) bin
return validate(errs, ctx.Data, f, ctx.Locale)
}
type NewDingtalkHook struct {
PayloadURL string `binding:"Required;Url"`
Webhook
}
func (f *NewDingtalkHook) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
return validate(errs, ctx.Data, f, ctx.Locale)
}
// .___
// | | ______ ________ __ ____
// | |/ ___// ___/ | \_/ __ \

BIN
public/img/dingtalk.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

71
routes/repo/webhook.go

@ -268,6 +268,44 @@ func DiscordHooksNewPost(c *context.Context, f form.NewDiscordHook) {
c.Redirect(orCtx.Link + "/settings/hooks")
}
func DingtalkHooksNewPost(c *context.Context, f form.NewDingtalkHook) {
c.Data["Title"] = c.Tr("repo.settings")
c.Data["PageIsSettingsHooks"] = true
c.Data["PageIsSettingsHooksNew"] = true
c.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
orCtx, err := getOrgRepoCtx(c)
if err != nil {
c.Handle(500, "getOrgRepoCtx", err)
return
}
if c.HasError() {
c.HTML(200, orCtx.NewTemplate)
return
}
w := &models.Webhook{
RepoID: orCtx.RepoID,
URL: f.PayloadURL,
ContentType: models.JSON,
HookEvent: ParseHookEvent(f.Webhook),
IsActive: f.Active,
HookTaskType: models.DINGTALK,
OrgID: orCtx.OrgID,
}
if err := w.UpdateEvent(); err != nil {
c.Handle(500, "UpdateEvent", err)
return
} else if err := models.CreateWebhook(w); err != nil {
c.Handle(500, "CreateWebhook", err)
return
}
c.Flash.Success(c.Tr("repo.settings.add_hook_success"))
c.Redirect(orCtx.Link + "/settings/hooks")
}
func checkWebhook(c *context.Context) (*OrgRepoCtx, *models.Webhook) {
c.Data["RequireHighlightJS"] = true
@ -296,6 +334,8 @@ func checkWebhook(c *context.Context) (*OrgRepoCtx, *models.Webhook) {
case models.DISCORD:
c.Data["SlackHook"] = w.GetSlackHook()
c.Data["HookType"] = "discord"
case models.DINGTALK:
c.Data["HookType"] = "dingtalk"
default:
c.Data["HookType"] = "gogs"
}
@ -445,6 +485,37 @@ func DiscordHooksEditPost(c *context.Context, f form.NewDiscordHook) {
c.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, w.ID))
}
func DingtalkHooksEditPost(c *context.Context, f form.NewDingtalkHook) {
c.Data["Title"] = c.Tr("repo.settings")
c.Data["PageIsSettingsHooks"] = true
c.Data["PageIsSettingsHooksEdit"] = true
orCtx, w := checkWebhook(c)
if c.Written() {
return
}
c.Data["Webhook"] = w
if c.HasError() {
c.HTML(200, orCtx.NewTemplate)
return
}
w.URL = f.PayloadURL
w.HookEvent = ParseHookEvent(f.Webhook)
w.IsActive = f.Active
if err := w.UpdateEvent(); err != nil {
c.Handle(500, "UpdateEvent", err)
return
} else if err := models.UpdateWebhook(w); err != nil {
c.Handle(500, "UpdateWebhook", err)
return
}
c.Flash.Success(c.Tr("repo.settings.update_hook_success"))
c.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, w.ID))
}
func TestWebhook(c *context.Context) {
var authorUsername, committerUsername string

1
templates/org/settings/webhook_new.tmpl

@ -20,6 +20,7 @@
{{template "repo/settings/webhook/gogs" .}}
{{template "repo/settings/webhook/slack" .}}
{{template "repo/settings/webhook/discord" .}}
{{template "repo/settings/webhook/dingtalk" .}}
</div>
{{template "repo/settings/webhook/history" .}}

11
templates/repo/settings/webhook/dingtalk.tmpl

@ -0,0 +1,11 @@
{{if eq .HookType "dingtalk"}}
<p>{{.i18n.Tr "repo.settings.add_dingtalk_hook_desc" "https://open-doc.dingtalk.com/" | Str2html}}</p>
<form class="ui form" action="{{.BaseLink}}/settings/hooks/dingtalk/{{if .PageIsSettingsHooksNew}}new{{else}}{{.Webhook.ID}}{{end}}" method="post">
{{.CSRFTokenHTML}}
<div class="required field {{if .Err_PayloadURL}}error{{end}}">
<label for="payload_url">{{.i18n.Tr "repo.settings.payload_url"}}</label>
<input id="payload_url" name="payload_url" type="url" value="{{.Webhook.URL}}" placeholder="https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxx" autofocus required>
</div>
{{template "repo/settings/webhook/settings" .}}
</form>
{{end}}

4
templates/repo/settings/webhook/list.tmpl

@ -20,6 +20,10 @@
<a class="item" href="{{$.BaseLink}}/settings/hooks/discord/new">
<img class="img-12" src="{{AppSubURL}}/img/discord.png">Discord
</a>
{{else if eq . "dingtalk"}}
<a class="item" href="{{$.BaseLink}}/settings/hooks/dingtalk/new">
<img class="img-12" src="{{AppSubURL}}/img/dingtalk.png">Dingtalk
</a>
{{end}}
{{end}}
</div>

1
templates/repo/settings/webhook/new.tmpl

@ -20,6 +20,7 @@
{{template "repo/settings/webhook/gogs" .}}
{{template "repo/settings/webhook/slack" .}}
{{template "repo/settings/webhook/discord" .}}
{{template "repo/settings/webhook/dingtalk" .}}
</div>
{{template "repo/settings/webhook/history" .}}

Loading…
Cancel
Save