@@ -44,16 +44,7 @@ func (d *DingtalkPayload) Create(p *api.CreatePayload) (api.Payloader, error) {
44
44
refName := git .RefEndName (p .Ref )
45
45
title := fmt .Sprintf ("[%s] %s %s created" , p .Repo .FullName , p .RefType , refName )
46
46
47
- return & DingtalkPayload {
48
- MsgType : "actionCard" ,
49
- ActionCard : dingtalk.ActionCard {
50
- Text : title ,
51
- Title : title ,
52
- HideAvatar : "0" ,
53
- SingleTitle : fmt .Sprintf ("view ref %s" , refName ),
54
- SingleURL : p .Repo .HTMLURL + "/src/" + refName ,
55
- },
56
- }, nil
47
+ return createDingtalkPayload (title , title , fmt .Sprintf ("view ref %s" , refName ), p .Repo .HTMLURL + "/src/" + refName ), nil
57
48
}
58
49
59
50
// Delete implements PayloadConvertor Delete method
@@ -62,32 +53,14 @@ func (d *DingtalkPayload) Delete(p *api.DeletePayload) (api.Payloader, error) {
62
53
refName := git .RefEndName (p .Ref )
63
54
title := fmt .Sprintf ("[%s] %s %s deleted" , p .Repo .FullName , p .RefType , refName )
64
55
65
- return & DingtalkPayload {
66
- MsgType : "actionCard" ,
67
- ActionCard : dingtalk.ActionCard {
68
- Text : title ,
69
- Title : title ,
70
- HideAvatar : "0" ,
71
- SingleTitle : fmt .Sprintf ("view ref %s" , refName ),
72
- SingleURL : p .Repo .HTMLURL + "/src/" + refName ,
73
- },
74
- }, nil
56
+ return createDingtalkPayload (title , title , fmt .Sprintf ("view ref %s" , refName ), p .Repo .HTMLURL + "/src/" + refName ), nil
75
57
}
76
58
77
59
// Fork implements PayloadConvertor Fork method
78
60
func (d * DingtalkPayload ) Fork (p * api.ForkPayload ) (api.Payloader , error ) {
79
61
title := fmt .Sprintf ("%s is forked to %s" , p .Forkee .FullName , p .Repo .FullName )
80
62
81
- return & DingtalkPayload {
82
- MsgType : "actionCard" ,
83
- ActionCard : dingtalk.ActionCard {
84
- Text : title ,
85
- Title : title ,
86
- HideAvatar : "0" ,
87
- SingleTitle : fmt .Sprintf ("view forked repo %s" , p .Repo .FullName ),
88
- SingleURL : p .Repo .HTMLURL ,
89
- },
90
- }, nil
63
+ return createDingtalkPayload (title , title , fmt .Sprintf ("view forked repo %s" , p .Repo .FullName ), p .Repo .HTMLURL ), nil
91
64
}
92
65
93
66
// Push implements PayloadConvertor Push method
@@ -124,70 +97,32 @@ func (d *DingtalkPayload) Push(p *api.PushPayload) (api.Payloader, error) {
124
97
strings .TrimRight (commit .Message , "\r \n " )) + authorName
125
98
// add linebreak to each commit but the last
126
99
if i < len (p .Commits )- 1 {
127
- text += "\n "
100
+ text += "\r \ n "
128
101
}
129
102
}
130
103
131
- return & DingtalkPayload {
132
- MsgType : "actionCard" ,
133
- ActionCard : dingtalk.ActionCard {
134
- Text : text ,
135
- Title : title ,
136
- HideAvatar : "0" ,
137
- SingleTitle : linkText ,
138
- SingleURL : titleLink ,
139
- },
140
- }, nil
104
+ return createDingtalkPayload (title , text , linkText , titleLink ), nil
141
105
}
142
106
143
107
// Issue implements PayloadConvertor Issue method
144
108
func (d * DingtalkPayload ) Issue (p * api.IssuePayload ) (api.Payloader , error ) {
145
109
text , issueTitle , attachmentText , _ := getIssuesPayloadInfo (p , noneLinkFormatter , true )
146
110
147
- return & DingtalkPayload {
148
- MsgType : "actionCard" ,
149
- ActionCard : dingtalk.ActionCard {
150
- Text : text + "\r \n \r \n " + attachmentText ,
151
- //Markdown: "# " + title + "\n" + text,
152
- Title : issueTitle ,
153
- HideAvatar : "0" ,
154
- SingleTitle : "view issue" ,
155
- SingleURL : p .Issue .HTMLURL ,
156
- },
157
- }, nil
111
+ return createDingtalkPayload (issueTitle , text + "\r \n \r \n " + attachmentText , "view issue" , p .Issue .HTMLURL ), nil
158
112
}
159
113
160
114
// IssueComment implements PayloadConvertor IssueComment method
161
115
func (d * DingtalkPayload ) IssueComment (p * api.IssueCommentPayload ) (api.Payloader , error ) {
162
116
text , issueTitle , _ := getIssueCommentPayloadInfo (p , noneLinkFormatter , true )
163
117
164
- return & DingtalkPayload {
165
- MsgType : "actionCard" ,
166
- ActionCard : dingtalk.ActionCard {
167
- Text : text + "\r \n \r \n " + p .Comment .Body ,
168
- Title : issueTitle ,
169
- HideAvatar : "0" ,
170
- SingleTitle : "view issue comment" ,
171
- SingleURL : p .Comment .HTMLURL ,
172
- },
173
- }, nil
118
+ return createDingtalkPayload (issueTitle , text + "\r \n \r \n " + p .Comment .Body , "view issue comment" , p .Comment .HTMLURL ), nil
174
119
}
175
120
176
121
// PullRequest implements PayloadConvertor PullRequest method
177
122
func (d * DingtalkPayload ) PullRequest (p * api.PullRequestPayload ) (api.Payloader , error ) {
178
123
text , issueTitle , attachmentText , _ := getPullRequestPayloadInfo (p , noneLinkFormatter , true )
179
124
180
- return & DingtalkPayload {
181
- MsgType : "actionCard" ,
182
- ActionCard : dingtalk.ActionCard {
183
- Text : text + "\r \n \r \n " + attachmentText ,
184
- //Markdown: "# " + title + "\n" + text,
185
- Title : issueTitle ,
186
- HideAvatar : "0" ,
187
- SingleTitle : "view pull request" ,
188
- SingleURL : p .PullRequest .HTMLURL ,
189
- },
190
- }, nil
125
+ return createDingtalkPayload (issueTitle , text + "\r \n \r \n " + attachmentText , "view pull request" , p .PullRequest .HTMLURL ), nil
191
126
}
192
127
193
128
// Review implements PayloadConvertor Review method
@@ -205,37 +140,17 @@ func (d *DingtalkPayload) Review(p *api.PullRequestPayload, event models.HookEve
205
140
206
141
}
207
142
208
- return & DingtalkPayload {
209
- MsgType : "actionCard" ,
210
- ActionCard : dingtalk.ActionCard {
211
- Text : title + "\r \n \r \n " + text ,
212
- Title : title ,
213
- HideAvatar : "0" ,
214
- SingleTitle : "view pull request" ,
215
- SingleURL : p .PullRequest .HTMLURL ,
216
- },
217
- }, nil
143
+ return createDingtalkPayload (title , title + "\r \n \r \n " + text , "view pull request" , p .PullRequest .HTMLURL ), nil
218
144
}
219
145
220
146
// Repository implements PayloadConvertor Repository method
221
147
func (d * DingtalkPayload ) Repository (p * api.RepositoryPayload ) (api.Payloader , error ) {
222
- var title , url string
223
148
switch p .Action {
224
149
case api .HookRepoCreated :
225
- title = fmt .Sprintf ("[%s] Repository created" , p .Repository .FullName )
226
- url = p .Repository .HTMLURL
227
- return & DingtalkPayload {
228
- MsgType : "actionCard" ,
229
- ActionCard : dingtalk.ActionCard {
230
- Text : title ,
231
- Title : title ,
232
- HideAvatar : "0" ,
233
- SingleTitle : "view repository" ,
234
- SingleURL : url ,
235
- },
236
- }, nil
150
+ title := fmt .Sprintf ("[%s] Repository created" , p .Repository .FullName )
151
+ return createDingtalkPayload (title , title , "view repository" , p .Repository .HTMLURL ), nil
237
152
case api .HookRepoDeleted :
238
- title = fmt .Sprintf ("[%s] Repository deleted" , p .Repository .FullName )
153
+ title : = fmt .Sprintf ("[%s] Repository deleted" , p .Repository .FullName )
239
154
return & DingtalkPayload {
240
155
MsgType : "text" ,
241
156
Text : struct {
@@ -253,16 +168,20 @@ func (d *DingtalkPayload) Repository(p *api.RepositoryPayload) (api.Payloader, e
253
168
func (d * DingtalkPayload ) Release (p * api.ReleasePayload ) (api.Payloader , error ) {
254
169
text , _ := getReleasePayloadInfo (p , noneLinkFormatter , true )
255
170
171
+ return createDingtalkPayload (text , text , "view release" , p .Release .URL ), nil
172
+ }
173
+
174
+ func createDingtalkPayload (title , text , singleTitle , singleURL string ) * DingtalkPayload {
256
175
return & DingtalkPayload {
257
176
MsgType : "actionCard" ,
258
177
ActionCard : dingtalk.ActionCard {
259
- Text : text ,
260
- Title : text ,
178
+ Text : strings . TrimSpace ( text ) ,
179
+ Title : strings . TrimSpace ( title ) ,
261
180
HideAvatar : "0" ,
262
- SingleTitle : "view release" ,
263
- SingleURL : p . Release . URL ,
181
+ SingleTitle : singleTitle ,
182
+ SingleURL : singleURL ,
264
183
},
265
- }, nil
184
+ }
266
185
}
267
186
268
187
// GetDingtalkPayload converts a ding talk webhook into a DingtalkPayload
0 commit comments