Skip to content

Commit 56da256

Browse files
Telegram webhook (#4227)
1 parent 6dbd261 commit 56da256

File tree

13 files changed

+478
-1
lines changed

13 files changed

+478
-1
lines changed

models/webhook.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,15 @@ func (w *Webhook) GetDiscordHook() *DiscordMeta {
145145
return s
146146
}
147147

148+
// GetTelegramHook returns telegram metadata
149+
func (w *Webhook) GetTelegramHook() *TelegramMeta {
150+
s := &TelegramMeta{}
151+
if err := json.Unmarshal([]byte(w.Meta), s); err != nil {
152+
log.Error("webhook.GetTelegramHook(%d): %v", w.ID, err)
153+
}
154+
return s
155+
}
156+
148157
// History returns history of webhook by given conditions.
149158
func (w *Webhook) History(page int) ([]*HookTask, error) {
150159
return HookTasks(w.ID, page)
@@ -456,6 +465,7 @@ const (
456465
GITEA
457466
DISCORD
458467
DINGTALK
468+
TELEGRAM
459469
)
460470

461471
var hookTaskTypes = map[string]HookTaskType{
@@ -464,6 +474,7 @@ var hookTaskTypes = map[string]HookTaskType{
464474
"slack": SLACK,
465475
"discord": DISCORD,
466476
"dingtalk": DINGTALK,
477+
"telegram": TELEGRAM,
467478
}
468479

469480
// ToHookTaskType returns HookTaskType by given name.
@@ -484,6 +495,8 @@ func (t HookTaskType) Name() string {
484495
return "discord"
485496
case DINGTALK:
486497
return "dingtalk"
498+
case TELEGRAM:
499+
return "telegram"
487500
}
488501
return ""
489502
}
@@ -657,6 +670,11 @@ func prepareWebhook(e Engine, w *Webhook, repo *Repository, event HookEventType,
657670
if err != nil {
658671
return fmt.Errorf("GetDingtalkPayload: %v", err)
659672
}
673+
case TELEGRAM:
674+
payloader, err = GetTelegramPayload(p, event, w.Meta)
675+
if err != nil {
676+
return fmt.Errorf("GetTelegramPayload: %v", err)
677+
}
660678
default:
661679
p.SetSecret(w.Secret)
662680
payloader = p

models/webhook_telegram.go

Lines changed: 322 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,322 @@
1+
// Copyright 2019 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package models
6+
7+
import (
8+
"encoding/json"
9+
"fmt"
10+
"strings"
11+
12+
"code.gitea.io/gitea/modules/git"
13+
"code.gitea.io/gitea/modules/markup"
14+
api "code.gitea.io/sdk/gitea"
15+
)
16+
17+
type (
18+
// TelegramPayload represents
19+
TelegramPayload struct {
20+
Message string `json:"text"`
21+
ParseMode string `json:"parse_mode"`
22+
}
23+
24+
// TelegramMeta contains the telegram metadata
25+
TelegramMeta struct {
26+
BotToken string `json:"bot_token"`
27+
ChatID string `json:"chat_id"`
28+
}
29+
)
30+
31+
// SetSecret sets the telegram secret
32+
func (p *TelegramPayload) SetSecret(_ string) {}
33+
34+
// JSONPayload Marshals the TelegramPayload to json
35+
func (p *TelegramPayload) JSONPayload() ([]byte, error) {
36+
p.ParseMode = "HTML"
37+
p.Message = markup.Sanitize(p.Message)
38+
data, err := json.MarshalIndent(p, "", " ")
39+
if err != nil {
40+
return []byte{}, err
41+
}
42+
return data, nil
43+
}
44+
45+
func getTelegramCreatePayload(p *api.CreatePayload) (*TelegramPayload, error) {
46+
// created tag/branch
47+
refName := git.RefEndName(p.Ref)
48+
title := fmt.Sprintf(`[<a href="%s">%s</a>] %s <a href="%s">%s</a> created`, p.Repo.HTMLURL, p.Repo.FullName, p.RefType,
49+
p.Repo.HTMLURL+"/src/"+refName, refName)
50+
51+
return &TelegramPayload{
52+
Message: title,
53+
}, nil
54+
}
55+
56+
func getTelegramDeletePayload(p *api.DeletePayload) (*TelegramPayload, error) {
57+
// created tag/branch
58+
refName := git.RefEndName(p.Ref)
59+
title := fmt.Sprintf(`[<a href="%s">%s</a>] %s <a href="%s">%s</a> deleted`, p.Repo.HTMLURL, p.Repo.FullName, p.RefType,
60+
p.Repo.HTMLURL+"/src/"+refName, refName)
61+
62+
return &TelegramPayload{
63+
Message: title,
64+
}, nil
65+
}
66+
67+
func getTelegramForkPayload(p *api.ForkPayload) (*TelegramPayload, error) {
68+
title := fmt.Sprintf(`%s is forked to <a href="%s">%s</a>`, p.Forkee.FullName, p.Repo.HTMLURL, p.Repo.FullName)
69+
70+
return &TelegramPayload{
71+
Message: title,
72+
}, nil
73+
}
74+
75+
func getTelegramPushPayload(p *api.PushPayload) (*TelegramPayload, error) {
76+
var (
77+
branchName = git.RefEndName(p.Ref)
78+
commitDesc string
79+
)
80+
81+
var titleLink string
82+
if len(p.Commits) == 1 {
83+
commitDesc = "1 new commit"
84+
titleLink = p.Commits[0].URL
85+
} else {
86+
commitDesc = fmt.Sprintf("%d new commits", len(p.Commits))
87+
titleLink = p.CompareURL
88+
}
89+
if titleLink == "" {
90+
titleLink = p.Repo.HTMLURL + "/src/" + branchName
91+
}
92+
title := fmt.Sprintf(`[<a href="%s">%s</a>:<a href="%s">%s</a>] %s`, p.Repo.HTMLURL, p.Repo.FullName, titleLink, branchName, commitDesc)
93+
94+
var text string
95+
// for each commit, generate attachment text
96+
for i, commit := range p.Commits {
97+
var authorName string
98+
if commit.Author != nil {
99+
authorName = " - " + commit.Author.Name
100+
}
101+
text += fmt.Sprintf(`[<a href="%s">%s</a>] %s`, commit.URL, commit.ID[:7],
102+
strings.TrimRight(commit.Message, "\r\n")) + authorName
103+
// add linebreak to each commit but the last
104+
if i < len(p.Commits)-1 {
105+
text += "\n"
106+
}
107+
}
108+
109+
return &TelegramPayload{
110+
Message: title + "\n" + text,
111+
}, nil
112+
}
113+
114+
func getTelegramIssuesPayload(p *api.IssuePayload) (*TelegramPayload, error) {
115+
var text, title string
116+
switch p.Action {
117+
case api.HookIssueOpened:
118+
title = fmt.Sprintf(`[<a href="%s">%s</a>] Issue opened: <a href="%s">#%d %s</a>`, p.Repository.HTMLURL, p.Repository.FullName,
119+
p.Issue.URL, p.Index, p.Issue.Title)
120+
text = p.Issue.Body
121+
case api.HookIssueClosed:
122+
title = fmt.Sprintf(`[<a href="%s">%s</a>] Issue closed: <a href="%s">#%d %s</a>`, p.Repository.HTMLURL, p.Repository.FullName,
123+
p.Issue.URL, p.Index, p.Issue.Title)
124+
text = p.Issue.Body
125+
case api.HookIssueReOpened:
126+
title = fmt.Sprintf(`[<a href="%s">%s</a>] Issue re-opened: <a href="%s">#%d %s</a>`, p.Repository.HTMLURL, p.Repository.FullName,
127+
p.Issue.URL, p.Index, p.Issue.Title)
128+
text = p.Issue.Body
129+
case api.HookIssueEdited:
130+
title = fmt.Sprintf(`[<a href="%s">%s</a>] Issue edited: <a href="%s">#%d %s</a>`, p.Repository.HTMLURL, p.Repository.FullName,
131+
p.Issue.URL, p.Index, p.Issue.Title)
132+
text = p.Issue.Body
133+
case api.HookIssueAssigned:
134+
title = fmt.Sprintf(`[<a href="%s">%s</a>] Issue assigned to %s: <a href="%s">#%d %s</a>`, p.Repository.HTMLURL, p.Repository.FullName,
135+
p.Issue.Assignee.UserName, p.Issue.URL, p.Index, p.Issue.Title)
136+
text = p.Issue.Body
137+
case api.HookIssueUnassigned:
138+
title = fmt.Sprintf(`[<a href="%s">%s</a>] Issue unassigned: <a href="%s">#%d %s</a>`, p.Repository.HTMLURL, p.Repository.FullName,
139+
p.Issue.URL, p.Index, p.Issue.Title)
140+
text = p.Issue.Body
141+
case api.HookIssueLabelUpdated:
142+
title = fmt.Sprintf(`[<a href="%s">%s</a>] Issue labels updated: <a href="%s">#%d %s</a>`, p.Repository.HTMLURL, p.Repository.FullName,
143+
p.Issue.URL, p.Index, p.Issue.Title)
144+
text = p.Issue.Body
145+
case api.HookIssueLabelCleared:
146+
title = fmt.Sprintf(`[<a href="%s">%s</a>] Issue labels cleared: <a href="%s">#%d %s</a>`, p.Repository.HTMLURL, p.Repository.FullName,
147+
p.Issue.URL, p.Index, p.Issue.Title)
148+
text = p.Issue.Body
149+
case api.HookIssueSynchronized:
150+
title = fmt.Sprintf(`[<a href="%s">%s</a>] Issue synchronized: <a href="%s">#%d %s</a>`, p.Repository.HTMLURL, p.Repository.FullName,
151+
p.Issue.URL, p.Index, p.Issue.Title)
152+
text = p.Issue.Body
153+
case api.HookIssueMilestoned:
154+
title = fmt.Sprintf(`[<a href="%s">%s</a>] Issue milestone: <a href="%s">#%d %s</a>`, p.Repository.HTMLURL, p.Repository.FullName,
155+
p.Issue.URL, p.Index, p.Issue.Title)
156+
text = p.Issue.Body
157+
case api.HookIssueDemilestoned:
158+
title = fmt.Sprintf(`[<a href="%s">%s</a>] Issue clear milestone: <a href="%s">#%d %s</a>`, p.Repository.HTMLURL, p.Repository.FullName,
159+
p.Issue.URL, p.Index, p.Issue.Title)
160+
text = p.Issue.Body
161+
}
162+
163+
return &TelegramPayload{
164+
Message: title + "\n\n" + text,
165+
}, nil
166+
}
167+
168+
func getTelegramIssueCommentPayload(p *api.IssueCommentPayload) (*TelegramPayload, error) {
169+
url := fmt.Sprintf("%s/issues/%d#%s", p.Repository.HTMLURL, p.Issue.Index, CommentHashTag(p.Comment.ID))
170+
title := fmt.Sprintf(`<a href="%s">#%d %s</a>`, url, p.Issue.Index, p.Issue.Title)
171+
var text string
172+
switch p.Action {
173+
case api.HookIssueCommentCreated:
174+
text = "New comment: " + title
175+
text += p.Comment.Body
176+
case api.HookIssueCommentEdited:
177+
text = "Comment edited: " + title
178+
text += p.Comment.Body
179+
case api.HookIssueCommentDeleted:
180+
text = "Comment deleted: " + title
181+
text += p.Comment.Body
182+
}
183+
184+
return &TelegramPayload{
185+
Message: title + "\n" + text,
186+
}, nil
187+
}
188+
189+
func getTelegramPullRequestPayload(p *api.PullRequestPayload) (*TelegramPayload, error) {
190+
var text, title string
191+
switch p.Action {
192+
case api.HookIssueOpened:
193+
title = fmt.Sprintf(`[<a href="%s">%s</a>] Pull request opened: <a href="%s">#%d %s</a>`, p.Repository.HTMLURL, p.Repository.FullName,
194+
p.PullRequest.HTMLURL, p.Index, p.PullRequest.Title)
195+
text = p.PullRequest.Body
196+
case api.HookIssueClosed:
197+
if p.PullRequest.HasMerged {
198+
title = fmt.Sprintf(`[<a href="%s">%s</a>] Pull request merged: <a href="%s">#%d %s</a>`, p.Repository.HTMLURL, p.Repository.FullName,
199+
p.PullRequest.HTMLURL, p.Index, p.PullRequest.Title)
200+
} else {
201+
title = fmt.Sprintf(`[<a href="%s">%s</a>] Pull request closed: <a href="%s">#%d %s</a>`, p.Repository.HTMLURL, p.Repository.FullName,
202+
p.PullRequest.HTMLURL, p.Index, p.PullRequest.Title)
203+
}
204+
text = p.PullRequest.Body
205+
case api.HookIssueReOpened:
206+
title = fmt.Sprintf(`[<a href="%s">%s</a>] Pull request re-opened: <a href="%s">#%d %s</a>`, p.Repository.HTMLURL, p.Repository.FullName,
207+
p.PullRequest.HTMLURL, p.Index, p.PullRequest.Title)
208+
text = p.PullRequest.Body
209+
case api.HookIssueEdited:
210+
title = fmt.Sprintf(`[<a href="%s">%s</a>] Pull request edited: <a href="%s">#%d %s</a>`, p.Repository.HTMLURL, p.Repository.FullName,
211+
p.PullRequest.HTMLURL, p.Index, p.PullRequest.Title)
212+
text = p.PullRequest.Body
213+
case api.HookIssueAssigned:
214+
list, err := MakeAssigneeList(&Issue{ID: p.PullRequest.ID})
215+
if err != nil {
216+
return &TelegramPayload{}, err
217+
}
218+
title = fmt.Sprintf(`[<a href="%s">%s</a>] Pull request assigned to %s: <a href="%s">#%d %s</a>`, p.Repository.HTMLURL, p.Repository.FullName,
219+
list, p.PullRequest.HTMLURL, p.Index, p.PullRequest.Title)
220+
text = p.PullRequest.Body
221+
case api.HookIssueUnassigned:
222+
title = fmt.Sprintf(`[<a href="%s">%s</a>] Pull request unassigned: <a href="%s">#%d %s</a>`, p.Repository.HTMLURL, p.Repository.FullName,
223+
p.PullRequest.HTMLURL, p.Index, p.PullRequest.Title)
224+
text = p.PullRequest.Body
225+
case api.HookIssueLabelUpdated:
226+
title = fmt.Sprintf(`[<a href="%s">%s</a>] Pull request labels updated: <a href="%s">#%d %s</a>`, p.Repository.HTMLURL, p.Repository.FullName,
227+
p.PullRequest.HTMLURL, p.Index, p.PullRequest.Title)
228+
text = p.PullRequest.Body
229+
case api.HookIssueLabelCleared:
230+
title = fmt.Sprintf(`[<a href="%s">%s</a>] Pull request labels cleared: <a href="%s">#%d %s</a>`, p.Repository.HTMLURL, p.Repository.FullName,
231+
p.PullRequest.HTMLURL, p.Index, p.PullRequest.Title)
232+
text = p.PullRequest.Body
233+
case api.HookIssueSynchronized:
234+
title = fmt.Sprintf(`[<a href="%s">%s</a>] Pull request synchronized: <a href="%s">#%d %s</a>`, p.Repository.HTMLURL, p.Repository.FullName,
235+
p.PullRequest.HTMLURL, p.Index, p.PullRequest.Title)
236+
text = p.PullRequest.Body
237+
case api.HookIssueMilestoned:
238+
title = fmt.Sprintf(`[<a href="%s">%s</a>] Pull request milestone: <a href="%s">#%d %s</a>`, p.Repository.HTMLURL, p.Repository.FullName,
239+
p.PullRequest.HTMLURL, p.Index, p.PullRequest.Title)
240+
text = p.PullRequest.Body
241+
case api.HookIssueDemilestoned:
242+
title = fmt.Sprintf(`[<a href="%s">%s</a>] Pull request clear milestone: <a href="%s">#%d %s</a>`, p.Repository.HTMLURL, p.Repository.FullName,
243+
p.PullRequest.HTMLURL, p.Index, p.PullRequest.Title)
244+
text = p.PullRequest.Body
245+
}
246+
247+
return &TelegramPayload{
248+
Message: title + "\n" + text,
249+
}, nil
250+
}
251+
252+
func getTelegramRepositoryPayload(p *api.RepositoryPayload) (*TelegramPayload, error) {
253+
var title string
254+
switch p.Action {
255+
case api.HookRepoCreated:
256+
title = fmt.Sprintf(`[<a href="%s">%s</a>] Repository created`, p.Repository.HTMLURL, p.Repository.FullName)
257+
return &TelegramPayload{
258+
Message: title,
259+
}, nil
260+
case api.HookRepoDeleted:
261+
title = fmt.Sprintf("[%s] Repository deleted", p.Repository.FullName)
262+
return &TelegramPayload{
263+
Message: title,
264+
}, nil
265+
}
266+
return nil, nil
267+
}
268+
269+
func getTelegramReleasePayload(p *api.ReleasePayload) (*TelegramPayload, error) {
270+
var title, url string
271+
switch p.Action {
272+
case api.HookReleasePublished:
273+
title = fmt.Sprintf("[%s] Release created", p.Release.TagName)
274+
url = p.Release.URL
275+
return &TelegramPayload{
276+
Message: title + "\n" + url,
277+
}, nil
278+
case api.HookReleaseUpdated:
279+
title = fmt.Sprintf("[%s] Release updated", p.Release.TagName)
280+
url = p.Release.URL
281+
return &TelegramPayload{
282+
Message: title + "\n" + url,
283+
}, nil
284+
285+
case api.HookReleaseDeleted:
286+
title = fmt.Sprintf("[%s] Release deleted", p.Release.TagName)
287+
url = p.Release.URL
288+
return &TelegramPayload{
289+
Message: title + "\n" + url,
290+
}, nil
291+
}
292+
293+
return nil, nil
294+
}
295+
296+
// GetTelegramPayload converts a telegram webhook into a TelegramPayload
297+
func GetTelegramPayload(p api.Payloader, event HookEventType, meta string) (*TelegramPayload, error) {
298+
s := new(TelegramPayload)
299+
300+
switch event {
301+
case HookEventCreate:
302+
return getTelegramCreatePayload(p.(*api.CreatePayload))
303+
case HookEventDelete:
304+
return getTelegramDeletePayload(p.(*api.DeletePayload))
305+
case HookEventFork:
306+
return getTelegramForkPayload(p.(*api.ForkPayload))
307+
case HookEventIssues:
308+
return getTelegramIssuesPayload(p.(*api.IssuePayload))
309+
case HookEventIssueComment:
310+
return getTelegramIssueCommentPayload(p.(*api.IssueCommentPayload))
311+
case HookEventPush:
312+
return getTelegramPushPayload(p.(*api.PushPayload))
313+
case HookEventPullRequest:
314+
return getTelegramPullRequestPayload(p.(*api.PullRequestPayload))
315+
case HookEventRepository:
316+
return getTelegramRepositoryPayload(p.(*api.RepositoryPayload))
317+
case HookEventRelease:
318+
return getTelegramReleasePayload(p.(*api.ReleasePayload))
319+
}
320+
321+
return s, nil
322+
}

models/webhook_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,18 +197,21 @@ func TestToHookTaskType(t *testing.T) {
197197
assert.Equal(t, GOGS, ToHookTaskType("gogs"))
198198
assert.Equal(t, SLACK, ToHookTaskType("slack"))
199199
assert.Equal(t, GITEA, ToHookTaskType("gitea"))
200+
assert.Equal(t, TELEGRAM, ToHookTaskType("telegram"))
200201
}
201202

202203
func TestHookTaskType_Name(t *testing.T) {
203204
assert.Equal(t, "gogs", GOGS.Name())
204205
assert.Equal(t, "slack", SLACK.Name())
205206
assert.Equal(t, "gitea", GITEA.Name())
207+
assert.Equal(t, "telegram", TELEGRAM.Name())
206208
}
207209

208210
func TestIsValidHookTaskType(t *testing.T) {
209211
assert.True(t, IsValidHookTaskType("gogs"))
210212
assert.True(t, IsValidHookTaskType("slack"))
211213
assert.True(t, IsValidHookTaskType("gitea"))
214+
assert.True(t, IsValidHookTaskType("telegram"))
212215
assert.False(t, IsValidHookTaskType("invalid"))
213216
}
214217

0 commit comments

Comments
 (0)