Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
go-atomci
atomci
提交
e8acc982
atomci
项目概览
go-atomci
/
atomci
通知
0
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
atomci
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
e8acc982
编写于
9月 30, 2022
作者:
C
colynn.liu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
improve*: notification change to pkg
上级
5eb9f4e8
变更
13
隐藏空白更改
内联
并排
Showing
13 changed file
with
187 addition
and
157 deletion
+187
-157
conf/app.conf
conf/app.conf
+2
-2
conf/app.conf.template
conf/app.conf.template
+7
-3
internal/api/pipeline.go
internal/api/pipeline.go
+30
-2
internal/core/notification/impl/notify.go
internal/core/notification/impl/notify.go
+0
-51
internal/core/notification/notification_test.go
internal/core/notification/notification_test.go
+0
-44
pkg/notification/app.unittest.conf
pkg/notification/app.unittest.conf
+0
-0
pkg/notification/dingrobot.go
pkg/notification/dingrobot.go
+12
-9
pkg/notification/email.go
pkg/notification/email.go
+21
-29
pkg/notification/notification.go
pkg/notification/notification.go
+44
-0
pkg/notification/notification_test.go
pkg/notification/notification_test.go
+40
-0
pkg/notification/notifyTemplate.go
pkg/notification/notifyTemplate.go
+10
-10
pkg/notification/options.go
pkg/notification/options.go
+20
-0
pkg/notification/types/messages.go
pkg/notification/types/messages.go
+1
-7
未找到文件。
conf/app.conf
浏览文件 @
e8acc982
...
...
@@ -42,10 +42,10 @@ url = http://localhost:8080
# notification config
[
notification
]
dingEnable
=
1
dingEnable
=
false
ding
=
"https://oapi.dingtalk.com/robot/send?access_token=faketoken"
mailEnable
=
1
mailEnable
=
false
smtpHost
=
"smtp.host"
smtpPort
=
465
smtpAccount
=
"fake@mail.com"
...
...
conf/app.conf.template
浏览文件 @
e8acc982
...
...
@@ -47,10 +47,14 @@ url = http://localhost:8080
# 通知配置
[notification]
# 钉钉通知
dingEnable = 1
# dingEnable=true 开启dinding通知
# dingEnable=false 关闭dingding通知
dingEnable = true
ding = "https://oapi.dingtalk.com/robot/send?access_token=faketoken"
# 邮件通知
mailEnable = 1
# 邮件通知
# mailEnable = true 开启email通知
# mailEnable = false 关闭email通知
mailEnable = true
smtpHost = "smtp.host"
smtpPort = 465
smtpAccount = "fake@mail.com"
...
...
internal/api/pipeline.go
浏览文件 @
e8acc982
...
...
@@ -17,10 +17,11 @@ limitations under the License.
package
api
import
(
"github.com/
go-atomci/atomci/internal/core/notification/impl
"
"github.com/
astaxie/beego
"
"github.com/go-atomci/atomci/internal/core/pipelinemgr"
"github.com/go-atomci/atomci/internal/core/publish"
"github.com/go-atomci/atomci/internal/middleware/log"
"github.com/go-atomci/atomci/pkg/notification"
)
// PipelineController ...
...
...
@@ -119,8 +120,35 @@ func (p *PipelineController) RunStepCallback() {
log
.
Log
.
Error
(
"RunStep callback, update publish Order occur error: %s"
,
err
.
Error
())
return
}
publishInfo
,
_
:=
publishmgr
.
GetPublishInfo
(
publishID
)
go
notification
.
Send
(
publishID
,
publishStatus
)
dingEnable
:=
beego
.
AppConfig
.
DefaultBool
(
"notification::dingEnable"
,
false
)
mailEnable
:=
beego
.
AppConfig
.
DefaultBool
(
"notification::mailEnable"
,
false
)
dingURL
:=
beego
.
AppConfig
.
String
(
"notification::ding"
)
smtpHost
:=
beego
.
AppConfig
.
String
(
"notification::smtpHost"
)
smtpAccount
:=
beego
.
AppConfig
.
String
(
"notification::smtpAccount"
)
smtpPassword
:=
beego
.
AppConfig
.
String
(
"notification::smtpPassword"
)
smtpPort
,
_
:=
beego
.
AppConfig
.
Int
(
"notification::smtpPort"
)
pushOptions
:=
notification
.
PushNotification
{
// message
Status
:
publishStatus
,
PublishName
:
publishInfo
.
Name
,
StageName
:
publishInfo
.
StageName
,
StepName
:
publishInfo
.
Step
,
// dingding
DingURL
:
dingURL
,
DingEnable
:
dingEnable
,
// email
EmailEnable
:
mailEnable
,
EmailHost
:
smtpHost
,
EmailPort
:
smtpPort
,
EmailUser
:
smtpAccount
,
EmailPassword
:
smtpPassword
,
}
// publishID
go
notification
.
Send
(
pushOptions
)
p
.
Data
[
"json"
]
=
NewResult
(
true
,
nil
,
""
)
p
.
ServeJSON
()
...
...
internal/core/notification/impl/notify.go
已删除
100644 → 0
浏览文件 @
5eb9f4e8
package
notification
import
(
"github.com/astaxie/beego"
messages
"github.com/go-atomci/atomci/internal/core/notification/types"
"github.com/go-atomci/atomci/internal/core/publish"
)
type
INotify
interface
{
Send
(
m
messages
.
StepCallbackResult
)
error
}
func
NewHandlers
()
[]
INotify
{
dingEnable
,
_
:=
beego
.
AppConfig
.
Int
(
"notification::dingEnable"
)
mailEnable
,
_
:=
beego
.
AppConfig
.
Int
(
"notification::mailEnable"
)
var
handlers
[]
INotify
if
dingEnable
>
0
{
handlers
=
append
(
handlers
,
DingRobotHandler
())
}
if
mailEnable
>
0
{
handlers
=
append
(
handlers
,
EmailHandler
())
}
return
handlers
}
func
Send
(
publishId
int64
,
status
int64
)
{
pm
:=
publish
.
NewPublishManager
()
pub
,
_
:=
pm
.
GetPublishInfo
(
publishId
)
handlers
:=
NewHandlers
()
callbackResult
:=
messages
.
StepCallbackResult
{
PublishName
:
pub
.
Name
,
StageName
:
pub
.
StageName
,
StepName
:
pub
.
Step
,
Status
:
status
,
}
if
handlers
!=
nil
{
for
_
,
handler
:=
range
handlers
{
go
handler
.
Send
(
callbackResult
)
}
}
}
internal/core/notification/notification_test.go
已删除
100644 → 0
浏览文件 @
5eb9f4e8
package
notification
import
(
"github.com/astaxie/beego"
notification
"github.com/go-atomci/atomci/internal/core/notification/impl"
messages
"github.com/go-atomci/atomci/internal/core/notification/types"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
"net/http"
"testing"
)
func
init
()
{
_
=
beego
.
LoadAppConfig
(
"ini"
,
"./app.unittest.conf"
)
}
func
Test_SEND_SHOULD_NO_ERROR
(
t
*
testing
.
T
)
{
httpmock
.
Activate
()
defer
httpmock
.
DeactivateAndReset
()
httpmock
.
RegisterResponder
(
"POST"
,
"https://dingtalk.unittest.com"
,
func
(
req
*
http
.
Request
)
(
*
http
.
Response
,
error
)
{
resp
,
err
:=
httpmock
.
NewJsonResponse
(
200
,
""
)
if
err
!=
nil
{
return
httpmock
.
NewStringResponse
(
500
,
""
),
nil
}
return
resp
,
nil
},
)
var
temp
notification
.
INotify
mockResult
:=
new
(
messages
.
StepCallbackResult
)
temp
=
new
(
notification
.
Email
)
assert
.
NoError
(
t
,
temp
.
Send
(
*
mockResult
))
temp
=
new
(
notification
.
DingRobot
)
assert
.
NoError
(
t
,
temp
.
Send
(
*
mockResult
))
hit
:=
httpmock
.
GetTotalCallCount
()
assert
.
Equal
(
t
,
1
,
hit
)
}
internal/core
/notification/app.unittest.conf
→
pkg
/notification/app.unittest.conf
浏览文件 @
e8acc982
文件已移动
internal/core/notification/impl
/dingrobot.go
→
pkg/notification
/dingrobot.go
浏览文件 @
e8acc982
...
...
@@ -7,20 +7,23 @@ import (
"io/ioutil"
"net/http"
"github.com/astaxie/beego"
messages
"github.com/go-atomci/atomci/internal/core/notification/types"
messages
"github.com/go-atomci/atomci/pkg/notification/types"
)
type
DingRobot
struct
{}
type
DingRobot
struct
{
DingUrl
string
}
func
DingRobotHandler
()
INotify
{
notifyHandler
:=
&
DingRobot
{}
func
DingRobotHandler
(
dingUrl
string
)
INotify
{
notifyHandler
:=
&
DingRobot
{
DingUrl
:
dingUrl
,
}
return
notifyHandler
}
func
dingEventMessage
(
template
INotifyTemplate
,
result
messages
.
StepCallbackResult
)
messages
.
EventMessage
{
func
(
dingtalk
*
DingRobot
)
dingEventMessage
(
template
INotifyTemplate
,
result
PushNotification
)
messages
.
EventMessage
{
robotHost
:=
beego
.
AppConfig
.
String
(
"notification::ding"
)
robotHost
:=
dingtalk
.
DingUrl
var
buf
bytes
.
Buffer
template
.
GenSubject
(
&
buf
,
result
)
...
...
@@ -51,11 +54,11 @@ func dingEventMessage(template INotifyTemplate, result messages.StepCallbackResu
return
msg
}
func
(
dingtalk
*
DingRobot
)
Send
(
result
messages
.
StepCallbackResult
)
error
{
func
(
dingtalk
*
DingRobot
)
Send
(
result
PushNotification
)
error
{
template
:=
&
DingRobotMarkdownTemplate
{}
message
:=
dingEventMessage
(
template
,
result
)
message
:=
ding
talk
.
ding
EventMessage
(
template
,
result
)
body
,
err
:=
json
.
Marshal
(
message
.
Ding
.
EventMessage
)
if
err
!=
nil
{
...
...
internal/core/notification/impl
/email.go
→
pkg/notification
/email.go
浏览文件 @
e8acc982
...
...
@@ -2,28 +2,30 @@ package notification
import
(
"bytes"
"github.com/astaxie/beego/logs"
"strings"
"github.com/astaxie/beego"
messages
"github.com/go-atomci/atomci/internal/core/notification/types"
messages
"github.com/go-atomci/atomci/pkg/notification/types"
"github.com/go-gomail/gomail"
)
type
Email
struct
{}
type
Email
struct
{
smtpHost
string
stmpAccount
string
smtpPassword
string
smtpPort
int
}
func
EmailHandler
()
INotify
{
notifyHandler
:=
&
Email
{}
func
EmailHandler
(
host
,
user
,
password
string
,
port
int
)
INotify
{
notifyHandler
:=
&
Email
{
smtpHost
:
host
,
stmpAccount
:
user
,
smtpPassword
:
password
,
smtpPort
:
port
,
}
return
notifyHandler
}
func
emailEventMessage
(
template
INotifyTemplate
,
result
messages
.
StepCallbackResult
)
messages
.
EventMessage
{
smtpHost
:=
beego
.
AppConfig
.
String
(
"notification::smtpHost"
)
smtpAccount
:=
beego
.
AppConfig
.
String
(
"notification::smtpAccount"
)
smtpPassword
:=
beego
.
AppConfig
.
String
(
"notification::smtpPassword"
)
smtpPort
,
_
:=
beego
.
AppConfig
.
Int
(
"notification::smtpPort"
)
func
emailEventMessage
(
template
INotifyTemplate
,
result
PushNotification
)
messages
.
EventMessage
{
var
buf
bytes
.
Buffer
subject
:=
template
.
GenSubject
(
&
buf
,
result
)
buf
.
Reset
()
...
...
@@ -31,10 +33,10 @@ func emailEventMessage(template INotifyTemplate, result messages.StepCallbackRes
template
.
GenFooter
(
&
buf
,
result
)
mailMessage
:=
&
messages
.
MailMessage
{
SmtpPort
:
smtp
Port
,
SmtpHost
:
smtp
Host
,
SmtpAccount
:
smtpAccount
,
SmtpPassword
:
smtp
Password
,
SmtpPort
:
result
.
Email
Port
,
SmtpHost
:
result
.
Email
Host
,
SmtpAccount
:
result
.
EmailUser
,
SmtpPassword
:
result
.
Email
Password
,
Body
:
buf
.
String
(),
Subject
:
subject
,
}
...
...
@@ -45,7 +47,7 @@ func emailEventMessage(template INotifyTemplate, result messages.StepCallbackRes
return
msg
}
func
(
email
*
Email
)
Send
(
result
messages
.
StepCallbackResult
)
error
{
func
(
email
*
Email
)
Send
(
result
PushNotification
)
error
{
template
:=
&
EmailTemplate
{}
...
...
@@ -64,15 +66,5 @@ func (email *Email) Send(result messages.StepCallbackResult) error {
d
:=
gomail
.
NewDialer
(
message
.
Mail
.
SmtpHost
,
message
.
Mail
.
SmtpPort
,
message
.
Mail
.
SmtpAccount
,
message
.
Mail
.
SmtpPassword
)
defer
func
()
{
if
r
:=
recover
();
r
!=
nil
{
logs
.
Error
(
"%v"
,
r
)
}
}()
if
err
:=
d
.
DialAndSend
(
m
);
err
!=
nil
{
panic
(
err
)
}
return
nil
return
d
.
DialAndSend
(
m
)
}
pkg/notification/notification.go
0 → 100644
浏览文件 @
e8acc982
package
notification
type
INotify
interface
{
Send
(
m
PushNotification
)
error
}
func
NewHandlers
(
notify
*
PushNotification
)
[]
INotify
{
var
handlers
[]
INotify
if
notify
.
DingEnable
&&
len
(
notify
.
DingURL
)
>
0
{
handlers
=
append
(
handlers
,
DingRobotHandler
(
notify
.
DingURL
))
}
if
notify
.
EmailEnable
&&
len
(
notify
.
EmailHost
)
>
0
&&
len
(
notify
.
EmailUser
)
>
0
&&
len
(
notify
.
EmailPassword
)
>
0
{
handlers
=
append
(
handlers
,
EmailHandler
(
notify
.
EmailHost
,
notify
.
EmailUser
,
notify
.
EmailPassword
,
notify
.
EmailPort
))
}
return
handlers
}
func
Send
(
options
PushNotification
)
{
notify
:=
&
PushNotification
{
DingURL
:
options
.
DingURL
,
DingEnable
:
options
.
DingEnable
,
EmailEnable
:
options
.
EmailEnable
,
EmailHost
:
options
.
EmailHost
,
EmailPort
:
options
.
EmailPort
,
EmailUser
:
options
.
EmailUser
,
EmailPassword
:
options
.
EmailPassword
,
PublishName
:
options
.
PublishName
,
StageName
:
options
.
StageName
,
StepName
:
options
.
StepName
,
Status
:
options
.
Status
,
}
handlers
:=
NewHandlers
(
notify
)
for
_
,
handler
:=
range
handlers
{
go
handler
.
Send
(
*
notify
)
}
}
pkg/notification/notification_test.go
0 → 100644
浏览文件 @
e8acc982
package
notification
import
(
"testing"
"github.com/astaxie/beego"
)
func
init
()
{
_
=
beego
.
LoadAppConfig
(
"ini"
,
"./app.unittest.conf"
)
}
func
Test_SEND_SHOULD_NO_ERROR
(
t
*
testing
.
T
)
{
// httpmock.Activate()
// defer httpmock.DeactivateAndReset()
// httpmock.RegisterResponder("POST", "https://dingtalk.unittest.com",
// func(req *http.Request) (*http.Response, error) {
// resp, err := httpmock.NewJsonResponse(200, "")
// if err != nil {
// return httpmock.NewStringResponse(500, ""), nil
// }
// return resp, nil
// },
// )
// var temp notification.INotify
// mockResult := new(messages.StepCallbackResult)
// temp = new(notification.Email)
// assert.NoError(t, temp.Send(*mockResult))
// temp = new(notification.DingRobot)
// assert.NoError(t, temp.Send(*mockResult))
// hit := httpmock.GetTotalCallCount()
// assert.Equal(t, 1, hit)
}
internal/core/notification/impl
/notifyTemplate.go
→
pkg/notification
/notifyTemplate.go
浏览文件 @
e8acc982
...
...
@@ -3,18 +3,18 @@ package notification
import
(
"bytes"
messages
"github.com/go-atomci/atomci/
internal/core
/notification/types"
messages
"github.com/go-atomci/atomci/
pkg
/notification/types"
)
type
INotifyTemplate
interface
{
GenSubject
(
buf
*
bytes
.
Buffer
,
m
messages
.
StepCallbackResult
)
string
GenContent
(
buf
*
bytes
.
Buffer
,
m
messages
.
StepCallbackResult
)
string
GenFooter
(
buf
*
bytes
.
Buffer
,
m
messages
.
StepCallbackResult
)
string
GenSubject
(
buf
*
bytes
.
Buffer
,
m
PushNotification
)
string
GenContent
(
buf
*
bytes
.
Buffer
,
m
PushNotification
)
string
GenFooter
(
buf
*
bytes
.
Buffer
,
m
PushNotification
)
string
}
type
DingRobotMarkdownTemplate
struct
{}
func
(
temp
*
DingRobotMarkdownTemplate
)
GenSubject
(
buf
*
bytes
.
Buffer
,
m
messages
.
StepCallbackResult
)
string
{
func
(
temp
*
DingRobotMarkdownTemplate
)
GenSubject
(
buf
*
bytes
.
Buffer
,
m
PushNotification
)
string
{
buf
.
WriteString
(
"## "
)
buf
.
WriteString
(
messages
.
StatusCodeToChinese
(
m
.
Status
))
...
...
@@ -22,7 +22,7 @@ func (temp *DingRobotMarkdownTemplate) GenSubject(buf *bytes.Buffer, m messages.
return
buf
.
String
()
}
func
(
temp
*
DingRobotMarkdownTemplate
)
GenContent
(
buf
*
bytes
.
Buffer
,
m
messages
.
StepCallbackResult
)
string
{
func
(
temp
*
DingRobotMarkdownTemplate
)
GenContent
(
buf
*
bytes
.
Buffer
,
m
PushNotification
)
string
{
buf
.
WriteString
(
m
.
PublishName
)
buf
.
WriteString
(
"
\r\n\r\n
"
)
...
...
@@ -33,7 +33,7 @@ func (temp *DingRobotMarkdownTemplate) GenContent(buf *bytes.Buffer, m messages.
return
buf
.
String
()
}
func
(
temp
*
DingRobotMarkdownTemplate
)
GenFooter
(
buf
*
bytes
.
Buffer
,
m
messages
.
StepCallbackResult
)
string
{
func
(
temp
*
DingRobotMarkdownTemplate
)
GenFooter
(
buf
*
bytes
.
Buffer
,
m
PushNotification
)
string
{
buf
.
WriteString
(
"
\r\n\r\n
> by AtomCI"
)
...
...
@@ -42,7 +42,7 @@ func (temp *DingRobotMarkdownTemplate) GenFooter(buf *bytes.Buffer, m messages.S
type
EmailTemplate
struct
{}
func
(
temp
*
EmailTemplate
)
GenSubject
(
buf
*
bytes
.
Buffer
,
m
messages
.
StepCallbackResult
)
string
{
func
(
temp
*
EmailTemplate
)
GenSubject
(
buf
*
bytes
.
Buffer
,
m
PushNotification
)
string
{
buf
.
WriteString
(
"流水线"
)
buf
.
WriteString
(
m
.
PublishName
)
...
...
@@ -52,7 +52,7 @@ func (temp *EmailTemplate) GenSubject(buf *bytes.Buffer, m messages.StepCallback
return
buf
.
String
()
}
func
(
temp
*
EmailTemplate
)
GenContent
(
buf
*
bytes
.
Buffer
,
m
messages
.
StepCallbackResult
)
string
{
func
(
temp
*
EmailTemplate
)
GenContent
(
buf
*
bytes
.
Buffer
,
m
PushNotification
)
string
{
buf
.
WriteString
(
"<p><h2><span>流水线: </span><b>"
)
buf
.
WriteString
(
m
.
PublishName
)
...
...
@@ -67,7 +67,7 @@ func (temp *EmailTemplate) GenContent(buf *bytes.Buffer, m messages.StepCallback
return
buf
.
String
()
}
func
(
temp
*
EmailTemplate
)
GenFooter
(
buf
*
bytes
.
Buffer
,
m
messages
.
StepCallbackResult
)
string
{
func
(
temp
*
EmailTemplate
)
GenFooter
(
buf
*
bytes
.
Buffer
,
m
PushNotification
)
string
{
buf
.
WriteString
(
"<p>by AtomCI</p>"
)
...
...
pkg/notification/options.go
0 → 100644
浏览文件 @
e8acc982
package
notification
type
PushNotification
struct
{
// dingtalk
DingURL
string
DingEnable
bool
// email
EmailEnable
bool
EmailHost
string
EmailPort
int
EmailUser
string
EmailPassword
string
// message
StageName
string
PublishName
string
StepName
string
Status
int64
}
internal/core
/notification/types/messages.go
→
pkg
/notification/types/messages.go
浏览文件 @
e8acc982
...
...
@@ -2,6 +2,7 @@ package types
import
(
"fmt"
"github.com/go-atomci/atomci/internal/models"
)
...
...
@@ -60,13 +61,6 @@ type EventMessage struct {
Ding
*
DingMessage
}
type
StepCallbackResult
struct
{
StageName
string
PublishName
string
StepName
string
Status
int64
}
func
StatusCodeToChinese
(
status
int64
)
string
{
switch
status
{
case
models
.
Success
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录