4
4
"encoding/json"
5
5
"errors"
6
6
"fmt"
7
+ "strconv"
7
8
"strings"
8
9
9
10
"code.gitea.io/git"
@@ -56,10 +57,26 @@ type (
56
57
DiscordMeta struct {
57
58
Username string `json:"username"`
58
59
IconURL string `json:"icon_url"`
59
- Color int `json:"color"`
60
60
}
61
61
)
62
62
63
+ func color (clr string ) int {
64
+ if clr != "" {
65
+ clr = strings .TrimLeft (clr , "#" )
66
+ if s , err := strconv .ParseInt (clr , 16 , 32 ); err == nil {
67
+ return int (s )
68
+ }
69
+ }
70
+
71
+ return 0
72
+ }
73
+
74
+ var (
75
+ successColor = color ("#1ac600" )
76
+ warnColor = color ("#ffd930" )
77
+ failedColor = color ("#ff3232" )
78
+ )
79
+
63
80
// SetSecret sets the slack secret
64
81
func (p * DiscordPayload ) SetSecret (_ string ) {}
65
82
@@ -72,84 +89,71 @@ func (p *DiscordPayload) JSONPayload() ([]byte, error) {
72
89
return data , nil
73
90
}
74
91
75
- func replaceBadCharsForDiscord (in string ) string {
76
- return strings .NewReplacer ("[" , "" , "]" , ":" , ":" , "/" ).Replace (in )
77
- }
78
-
79
92
func getDiscordCreatePayload (p * api.CreatePayload , meta * DiscordMeta ) (* DiscordPayload , error ) {
80
93
// created tag/branch
81
94
refName := git .RefEndName (p .Ref )
82
-
83
- repoLink := SlackLinkFormatter (p .Repo .HTMLURL , p .Repo .Name )
84
- refLink := SlackLinkFormatter (p .Repo .HTMLURL + "/src/" + refName , refName )
85
-
86
- format := "[%s:%s] %s created by %s"
87
- format = replaceBadCharsForDiscord (format )
88
- text := fmt .Sprintf (format , repoLink , refLink , p .RefType , p .Sender .UserName )
89
-
90
- var username = meta .Username
91
- if username == "" {
92
- username = "Gitea"
93
- }
95
+ title := fmt .Sprintf ("[%s] %s %s created" , p .Repo .FullName , p .RefType , refName )
94
96
95
97
return & DiscordPayload {
96
- Content : text ,
97
- Username : username ,
98
+ Username : meta .Username ,
98
99
AvatarURL : meta .IconURL ,
100
+ Embeds : []DiscordEmbed {
101
+ {
102
+ Title : title ,
103
+ URL : p .Repo .HTMLURL + "/src/" + refName ,
104
+ Color : successColor ,
105
+ Author : DiscordEmbedAuthor {
106
+ Name : p .Sender .UserName ,
107
+ URL : setting .AppURL + p .Sender .UserName ,
108
+ IconURL : p .Sender .AvatarURL ,
109
+ },
110
+ },
111
+ },
99
112
}, nil
100
113
}
101
114
102
115
func getDiscordPushPayload (p * api.PushPayload , meta * DiscordMeta ) (* DiscordPayload , error ) {
103
- // n new commits
104
116
var (
105
- branchName = git .RefEndName (p .Ref )
106
- commitDesc string
107
- commitString string
117
+ branchName = git .RefEndName (p .Ref )
118
+ commitDesc string
108
119
)
109
120
121
+ var titleLink string
110
122
if len (p .Commits ) == 1 {
111
123
commitDesc = "1 new commit"
124
+ titleLink = p .Commits [0 ].URL
112
125
} else {
113
126
commitDesc = fmt .Sprintf ("%d new commits" , len (p .Commits ))
127
+ titleLink = p .CompareURL
114
128
}
115
- if len (p .CompareURL ) > 0 {
116
- commitString = SlackLinkFormatter (p .CompareURL , commitDesc )
117
- } else {
118
- commitString = commitDesc
129
+ if titleLink == "" {
130
+ titleLink = p .Repo .HTMLURL + "/src/" + branchName
119
131
}
120
132
121
- repoLink := SlackLinkFormatter (p .Repo .HTMLURL , p .Repo .Name )
122
- branchLink := SlackLinkFormatter (p .Repo .HTMLURL + "/src/" + branchName , branchName )
123
-
124
- format := "[%s:%s] %s pushed by %s"
125
- format = replaceBadCharsForDiscord (format )
126
- text := fmt .Sprintf (format , repoLink , branchLink , commitString , p .Pusher .UserName )
133
+ title := fmt .Sprintf ("[%s:%s] %s" , p .Repo .FullName , branchName , commitDesc )
127
134
128
- var attachmentText string
135
+ var text string
129
136
// for each commit, generate attachment text
130
137
for i , commit := range p .Commits {
131
- attachmentText += fmt .Sprintf ("%s: %s - %s" , SlackLinkFormatter (commit .URL , commit .ID [:7 ]), SlackShortTextFormatter (commit .Message ), SlackTextFormatter (commit .Author .Name ))
138
+ text += fmt .Sprintf ("[%s](%s) %s - %s" , commit .ID [:7 ], commit .URL ,
139
+ strings .TrimRight (commit .Message , "\r \n " ), commit .Author .Name )
132
140
// add linebreak to each commit but the last
133
141
if i < len (p .Commits )- 1 {
134
- attachmentText += "\n "
142
+ text += "\n "
135
143
}
136
144
}
137
145
138
- var username = meta .Username
139
- if username == "" {
140
- username = "Gitea"
141
- }
146
+ fmt .Println (text )
142
147
143
148
return & DiscordPayload {
144
- //Content: text,
145
- Username : username ,
149
+ Username : meta .Username ,
146
150
AvatarURL : meta .IconURL ,
147
151
Embeds : []DiscordEmbed {
148
152
{
149
- Title : text ,
150
- Description : attachmentText ,
151
- // URL: branchLink, // FIXME
152
- Color : meta . Color ,
153
+ Title : title ,
154
+ Description : text ,
155
+ URL : titleLink ,
156
+ Color : successColor ,
153
157
Author : DiscordEmbedAuthor {
154
158
Name : p .Sender .UserName ,
155
159
URL : setting .AppURL + p .Sender .UserName ,
@@ -161,55 +165,62 @@ func getDiscordPushPayload(p *api.PushPayload, meta *DiscordMeta) (*DiscordPaylo
161
165
}
162
166
163
167
func getDiscordPullRequestPayload (p * api.PullRequestPayload , meta * DiscordMeta ) (* DiscordPayload , error ) {
164
- senderLink := SlackLinkFormatter (setting .AppURL + p .Sender .UserName , p .Sender .UserName )
165
- titleLink := SlackLinkFormatter (fmt .Sprintf ("%s/pulls/%d" , p .Repository .HTMLURL , p .Index ),
166
- fmt .Sprintf ("#%d %s" , p .Index , p .PullRequest .Title ))
167
168
var text , title string
169
+ var color int
168
170
switch p .Action {
169
171
case api .HookIssueOpened :
170
- text = fmt .Sprintf ("[%s] Pull request submitted by %s" , p .Repository .FullName , senderLink )
171
- title = titleLink
172
- //attachmentText = SlackTextFormatter(p.PullRequest.Body)
172
+ title = fmt .Sprintf ("[%s] Pull request opened: #%d %s" , p .Repository .FullName , p . Index , p . PullRequest . Title )
173
+ text = p . PullRequest . Body
174
+ color = warnColor
173
175
case api .HookIssueClosed :
174
176
if p .PullRequest .HasMerged {
175
- text = fmt .Sprintf ("[%s] Pull request merged: %s by %s" , p .Repository .FullName , titleLink , senderLink )
177
+ title = fmt .Sprintf ("[%s] Pull request merged: #%d %s" , p .Repository .FullName , p .Index , p .PullRequest .Title )
178
+ color = successColor
176
179
} else {
177
- text = fmt .Sprintf ("[%s] Pull request closed: %s by %s" , p .Repository .FullName , titleLink , senderLink )
180
+ title = fmt .Sprintf ("[%s] Pull request closed: #%d %s" , p .Repository .FullName , p .Index , p .PullRequest .Title )
181
+ color = failedColor
178
182
}
183
+ text = p .PullRequest .Body
179
184
case api .HookIssueReOpened :
180
- text = fmt .Sprintf ("[%s] Pull request re-opened: %s by %s" , p .Repository .FullName , titleLink , senderLink )
185
+ title = fmt .Sprintf ("[%s] Pull request re-opened: #%d %s" , p .Repository .FullName , p .Index , p .PullRequest .Title )
186
+ text = p .PullRequest .Body
187
+ color = warnColor
181
188
case api .HookIssueEdited :
182
- text = fmt .Sprintf ("[%s] Pull request edited: %s by %s" , p .Repository .FullName , titleLink , senderLink )
183
- //attachmentText = SlackTextFormatter(p.PullRequest.Body)
189
+ title = fmt .Sprintf ("[%s] Pull request edited: #%d %s" , p .Repository .FullName , p .Index , p .PullRequest .Title )
190
+ text = p .PullRequest .Body
191
+ color = warnColor
184
192
case api .HookIssueAssigned :
185
- text = fmt .Sprintf ("[%s] Pull request assigned to %s: %s by %s" , p .Repository .FullName ,
186
- SlackLinkFormatter (setting .AppURL + p .PullRequest .Assignee .UserName , p .PullRequest .Assignee .UserName ),
187
- titleLink , senderLink )
193
+ title = fmt .Sprintf ("[%s] Pull request assigned to %s: #%d %s" , p .Repository .FullName ,
194
+ p .PullRequest .Assignee .UserName , p .Index , p .PullRequest .Title )
195
+ text = p .PullRequest .Body
196
+ color = successColor
188
197
case api .HookIssueUnassigned :
189
- text = fmt .Sprintf ("[%s] Pull request unassigned: %s by %s" , p .Repository .FullName , titleLink , senderLink )
198
+ title = fmt .Sprintf ("[%s] Pull request unassigned: #%d %s" , p .Repository .FullName , p .Index , p .PullRequest .Title )
199
+ text = p .PullRequest .Body
200
+ color = warnColor
190
201
case api .HookIssueLabelUpdated :
191
- text = fmt .Sprintf ("[%s] Pull request labels updated: %s by %s" , p .Repository .FullName , titleLink , senderLink )
202
+ title = fmt .Sprintf ("[%s] Pull request labels updated: #%d %s" , p .Repository .FullName , p .Index , p .PullRequest .Title )
203
+ text = p .PullRequest .Body
204
+ color = warnColor
192
205
case api .HookIssueLabelCleared :
193
- text = fmt .Sprintf ("[%s] Pull request labels cleared: %s by %s" , p .Repository .FullName , titleLink , senderLink )
206
+ title = fmt .Sprintf ("[%s] Pull request labels cleared: #%d %s" , p .Repository .FullName , p .Index , p .PullRequest .Title )
207
+ text = p .PullRequest .Body
208
+ color = warnColor
194
209
case api .HookIssueSynchronized :
195
- text = fmt .Sprintf ("[%s] Pull request synchronized: %s by %s" , p .Repository .FullName , titleLink , senderLink )
196
- }
197
-
198
- var username = meta .Username
199
- if username == "" {
200
- username = "Gitea"
210
+ title = fmt .Sprintf ("[%s] Pull request synchronized: #%d %s" , p .Repository .FullName , p .Index , p .PullRequest .Title )
211
+ text = p .PullRequest .Body
212
+ color = warnColor
201
213
}
202
214
203
215
return & DiscordPayload {
204
- //Content: text,
205
- Username : username ,
216
+ Username : meta .Username ,
206
217
AvatarURL : meta .IconURL ,
207
218
Embeds : []DiscordEmbed {
208
219
{
209
220
Title : title ,
210
221
Description : text ,
211
222
URL : p .PullRequest .HTMLURL ,
212
- Color : meta . Color ,
223
+ Color : color ,
213
224
Author : DiscordEmbedAuthor {
214
225
Name : p .Sender .UserName ,
215
226
URL : setting .AppURL + p .Sender .UserName ,
0 commit comments