@@ -44,16 +44,7 @@ func (d *DingtalkPayload) Create(p *api.CreatePayload) (api.Payloader, error) {
4444 refName := git .RefEndName (p .Ref )
4545 title := fmt .Sprintf ("[%s] %s %s created" , p .Repo .FullName , p .RefType , refName )
4646
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
5748}
5849
5950// Delete implements PayloadConvertor Delete method
@@ -62,32 +53,14 @@ func (d *DingtalkPayload) Delete(p *api.DeletePayload) (api.Payloader, error) {
6253 refName := git .RefEndName (p .Ref )
6354 title := fmt .Sprintf ("[%s] %s %s deleted" , p .Repo .FullName , p .RefType , refName )
6455
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
7557}
7658
7759// Fork implements PayloadConvertor Fork method
7860func (d * DingtalkPayload ) Fork (p * api.ForkPayload ) (api.Payloader , error ) {
7961 title := fmt .Sprintf ("%s is forked to %s" , p .Forkee .FullName , p .Repo .FullName )
8062
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
9164}
9265
9366// Push implements PayloadConvertor Push method
@@ -124,70 +97,32 @@ func (d *DingtalkPayload) Push(p *api.PushPayload) (api.Payloader, error) {
12497 strings .TrimRight (commit .Message , "\r \n " )) + authorName
12598 // add linebreak to each commit but the last
12699 if i < len (p .Commits )- 1 {
127- text += "\n "
100+ text += "\r \ n "
128101 }
129102 }
130103
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
141105}
142106
143107// Issue implements PayloadConvertor Issue method
144108func (d * DingtalkPayload ) Issue (p * api.IssuePayload ) (api.Payloader , error ) {
145109 text , issueTitle , attachmentText , _ := getIssuesPayloadInfo (p , noneLinkFormatter , true )
146110
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
158112}
159113
160114// IssueComment implements PayloadConvertor IssueComment method
161115func (d * DingtalkPayload ) IssueComment (p * api.IssueCommentPayload ) (api.Payloader , error ) {
162116 text , issueTitle , _ := getIssueCommentPayloadInfo (p , noneLinkFormatter , true )
163117
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
174119}
175120
176121// PullRequest implements PayloadConvertor PullRequest method
177122func (d * DingtalkPayload ) PullRequest (p * api.PullRequestPayload ) (api.Payloader , error ) {
178123 text , issueTitle , attachmentText , _ := getPullRequestPayloadInfo (p , noneLinkFormatter , true )
179124
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
191126}
192127
193128// Review implements PayloadConvertor Review method
@@ -205,37 +140,17 @@ func (d *DingtalkPayload) Review(p *api.PullRequestPayload, event models.HookEve
205140
206141 }
207142
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
218144}
219145
220146// Repository implements PayloadConvertor Repository method
221147func (d * DingtalkPayload ) Repository (p * api.RepositoryPayload ) (api.Payloader , error ) {
222- var title , url string
223148 switch p .Action {
224149 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
237152 case api .HookRepoDeleted :
238- title = fmt .Sprintf ("[%s] Repository deleted" , p .Repository .FullName )
153+ title : = fmt .Sprintf ("[%s] Repository deleted" , p .Repository .FullName )
239154 return & DingtalkPayload {
240155 MsgType : "text" ,
241156 Text : struct {
@@ -253,16 +168,20 @@ func (d *DingtalkPayload) Repository(p *api.RepositoryPayload) (api.Payloader, e
253168func (d * DingtalkPayload ) Release (p * api.ReleasePayload ) (api.Payloader , error ) {
254169 text , _ := getReleasePayloadInfo (p , noneLinkFormatter , true )
255170
171+ return createDingtalkPayload (text , text , "view release" , p .Release .URL ), nil
172+ }
173+
174+ func createDingtalkPayload (title , text , singleTitle , singleURL string ) * DingtalkPayload {
256175 return & DingtalkPayload {
257176 MsgType : "actionCard" ,
258177 ActionCard : dingtalk.ActionCard {
259- Text : text ,
260- Title : text ,
178+ Text : strings . TrimSpace ( text ) ,
179+ Title : strings . TrimSpace ( title ) ,
261180 HideAvatar : "0" ,
262- SingleTitle : "view release" ,
263- SingleURL : p . Release . URL ,
181+ SingleTitle : singleTitle ,
182+ SingleURL : singleURL ,
264183 },
265- }, nil
184+ }
266185}
267186
268187// GetDingtalkPayload converts a ding talk webhook into a DingtalkPayload
0 commit comments