Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Q
qa-api
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
QA
qa-api
Commits
4d15b11d
Commit
4d15b11d
authored
Nov 13, 2019
by
智勇
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
重构ding消息
parent
ac831818
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
88 additions
and
92 deletions
+88
-92
pipeline.js
app/controller/pipeline.js
+11
-25
pipelineback.js
app/controller/pipelineback.js
+10
-25
sonar.js
app/controller/sonar.js
+34
-27
sonarCheck.js
app/jobs/sonarCheck.js
+9
-15
dingTalk.js
app/utils/dingTalk.js
+23
-0
app.config.js
src/app.config.js
+1
-0
No files found.
app/controller/pipeline.js
View file @
4d15b11d
...
@@ -6,7 +6,7 @@ const logger = require('koa-log4')
...
@@ -6,7 +6,7 @@ const logger = require('koa-log4')
.
getLogger
(
'
pipeline
'
)
.
getLogger
(
'
pipeline
'
)
const
{
PipeLine
,
PipeLinePush
}
=
require
(
'
../service/mongoService
'
)
const
{
PipeLine
,
PipeLinePush
}
=
require
(
'
../service/mongoService
'
)
const
jenkinsService
=
require
(
'
../service/jenkinsService
'
)
const
jenkinsService
=
require
(
'
../service/jenkinsService
'
)
const
dingTalk
=
require
(
'
../utils/dingTalk
'
)
const
config
=
require
(
global
.
configPath
)
const
config
=
require
(
global
.
configPath
)
const
url
=
'
http://192.168.4.96:8080/view/TKE/job/tke-
'
const
url
=
'
http://192.168.4.96:8080/view/TKE/job/tke-
'
...
@@ -142,31 +142,17 @@ const deletePipes = async function (ctx) {
...
@@ -142,31 +142,17 @@ const deletePipes = async function (ctx) {
const
res
=
await
PipeLine
.
delete
(
id
)
const
res
=
await
PipeLine
.
delete
(
id
)
ctx
.
body
=
ctx
.
ok
(
res
)
ctx
.
body
=
ctx
.
ok
(
res
)
}
}
const
sendData
=
async
(
namespace
,
projectName
,
branchName
,
gitUser
,
flag
,
message
,
type
,
buildNumber
,
address
)
=>
{
const
sendData
=
async
(
namespace
,
projectName
,
branchName
,
gitUser
,
flag
,
message
,
type
,
buildNumber
,
address
)
=>
{
const
dingData
=
{
const
text
=
`> 项目名称 :
${
projectName
}
\n\n`
msgtype
:
'
markdown
'
,
+
`> 分支名称 :
${
branchName
}
\n\n`
markdown
:
{
+
`> 部署空间 :
${
namespace
}
\n\n`
title
:
'
流水线信息如下
'
,
+
`> 触发同学 :
${
gitUser
}
\n\n`
text
:
`> 项目名称 :
${
projectName
}
\n\n`
+
`> 触发时间 :
${
moment
().
format
(
'
YYYY-MM-DD HH:mm:ss
'
)}
\n\n`
+
`> 分支名称 :
${
branchName
}
\n\n`
+
`> 触发信息 :
${
message
}
\n\n`
+
`> 部署空间 :
${
namespace
}
\n\n`
+
`#### 结果 :
${
flag
===
'
success
'
?
'
成功
'
:
'
失败
'
}
[查看详情](
${
url
}${
type
}
/
${
buildNumber
}
/)`
+
`> 触发同学 :
${
gitUser
}
\n\n`
const
title
=
'
流水线信息如下
'
+
`> 触发时间 :
${
moment
().
format
(
'
YYYY-MM-DD HH:mm:ss
'
)}
\n\n`
await
dingTalk
(
title
,
text
,
address
)
+
`> 触发信息 :
${
message
}
\n\n`
+
`#### 结果 :
${
flag
===
'
success
'
?
'
成功
'
:
'
失败
'
}
[查看详情](
${
url
}${
type
}
/
${
buildNumber
}
/)`
,
},
}
if
(
address
!==
''
)
{
await
awaitRequest
({
url
:
address
,
method
:
'
POST
'
,
headers
:
{
'
Content-Type
'
:
'
application/json;charset=utf-8
'
,
},
body
:
JSON
.
stringify
(
dingData
),
})
logger
.
info
(
`发送dingtalk
${
JSON
.
stringify
(
dingData
)}
`
)
}
}
}
const
dingTalkPush
=
async
function
(
ctx
)
{
const
dingTalkPush
=
async
function
(
ctx
)
{
...
...
app/controller/pipelineback.js
View file @
4d15b11d
...
@@ -7,6 +7,7 @@ const sleep = require('../utils/sleep')
...
@@ -7,6 +7,7 @@ const sleep = require('../utils/sleep')
const
config
=
require
(
'
../../src
'
);
const
config
=
require
(
'
../../src
'
);
const
{
PipeLine
,
PipeLinePush
}
=
require
(
'
../service/mongoService
'
)
const
{
PipeLine
,
PipeLinePush
}
=
require
(
'
../service/mongoService
'
)
const
jenkinsService
=
require
(
'
../service/jenkinsService
'
)
const
jenkinsService
=
require
(
'
../service/jenkinsService
'
)
const
dingTalk
=
require
(
'
../utils/dingTalk
'
)
const
awaitRequest
=
function
(
options
)
{
const
awaitRequest
=
function
(
options
)
{
return
new
Promise
((
resolve
,
reject
)
=>
{
return
new
Promise
((
resolve
,
reject
)
=>
{
...
@@ -136,33 +137,17 @@ const dingTalkPush = async function (ctx) {
...
@@ -136,33 +137,17 @@ const dingTalkPush = async function (ctx) {
}
}
const
url
=
'
http://192.168.4.96:8080/view/TKE/job/tke-
'
const
url
=
'
http://192.168.4.96:8080/view/TKE/job/tke-
'
result
.
forEach
(
async
(
item
)
=>
{
result
.
forEach
(
async
(
item
)
=>
{
const
dingData
=
{
const
text
=
`> 项目名称 :
${
item
.
projectName
}
\n\n`
msgtype
:
'
markdown
'
,
+
`> 分支名称 :
${
item
.
branchName
}
\n\n`
markdown
:
{
+
`> 部署空间 :
${
item
.
namespace
}
\n\n`
title
:
'
流水线信息如下
'
,
+
`> 触发同学 :
${
item
.
gitUser
}
\n\n`
text
:
`> 项目名称 :
${
item
.
projectName
}
\n\n`
+
`> 触发时间 :
${
moment
().
format
(
'
YYYY-MM-DD HH:mm:ss
'
)}
\n\n`
+
`> 分支名称 :
${
item
.
branchName
}
\n\n`
+
`> 提交信息 :
${
item
.
commitMes
}
\n\n`
+
`> 部署空间 :
${
item
.
namespace
}
\n\n`
+
`#### 结果 :
${
flag
===
'
success
'
?
'
成功
'
:
'
失败
'
}
[查看详情](
${
url
}${
item
.
type
}
/
${
item
.
buildId
}
/)`
+
`> 触发同学 :
${
item
.
gitUser
}
\n\n`
const
title
=
'
流水线信息如下
'
+
`> 触发时间 :
${
moment
().
format
(
'
YYYY-MM-DD HH:mm:ss
'
)}
\n\n`
await
dingTalk
(
title
,
text
,
item
.
noticeAddress
)
+
`> 提交信息 :
${
item
.
commitMes
}
\n\n`
// + `> -----${item.objectKind}-----\n\n`
+
`#### 结果 :
${
flag
===
'
success
'
?
'
成功
'
:
'
失败
'
}
[查看详情](
${
url
}${
item
.
type
}
/
${
item
.
buildId
}
/)`
,
},
}
if
(
item
.
noticeAddress
!==
''
)
{
await
awaitRequest
({
url
:
item
.
noticeAddress
,
method
:
'
POST
'
,
headers
:
{
'
Content-Type
'
:
'
application/json;charset=utf-8
'
,
},
body
:
JSON
.
stringify
(
dingData
),
})
}
})
})
ctx
.
body
=
ctx
.
ok
(
200
)
ctx
.
body
=
ctx
.
ok
(
200
)
}
}
const
router
=
new
Router
()
const
router
=
new
Router
()
...
...
app/controller/sonar.js
View file @
4d15b11d
...
@@ -2,49 +2,55 @@ const Router = require('koa-router')
...
@@ -2,49 +2,55 @@ const Router = require('koa-router')
const
bodyParser
=
require
(
'
koa-bodyparser
'
)
const
bodyParser
=
require
(
'
koa-bodyparser
'
)
const
logger
=
require
(
'
koa-log4
'
).
getLogger
(
'
sonar
'
)
const
logger
=
require
(
'
koa-log4
'
).
getLogger
(
'
sonar
'
)
const
awaitRequest
=
require
(
'
../utils/awaitRequest
'
)
const
awaitRequest
=
require
(
'
../utils/awaitRequest
'
)
const
dingTalk
=
require
(
'
../utils/dingTalk
'
)
const
sleep
=
require
(
'
../utils/sleep
'
)
const
sleep
=
require
(
'
../utils/sleep
'
)
const
config
=
require
(
global
.
configPath
)
const
config
=
require
(
global
.
configPath
)
const
sendData
=
async
(
projectName
,
branchName
,
item
)
=>
{
const
sendDing
=
async
(
data
,
item
,
executionTimeMs
)
=>
{
const
text
=
`> 项目名称 :
${
projectName
}
\n\n`
const
duration
=
data
.
duration
/
1000
+
`> 分支名称 :
${
branchName
}
\n\n`
const
executionTime
=
executionTimeMs
/
1000
+
`> 扫描结果 :
${
item
.
status
.
qualityGateStatus
}
\n\n`
const
text
=
`> <font color=#FF0000> 扫描结果 :
${
item
.
status
.
qualityGateStatus
}
\n\n`
// + `[查看详情](${config.sonarHost}/dashboard?id=${projectName})`
+
`> 项目名称 :
${
data
.
content
.
projectKey
}
\n\n`
+
`[查看详情](
${
config
.
sonarHost
}
/project/activity?id=
${
projectName
}
&&selected_date=
${
item
.
analysisDate
}
)`
+
`> 分支名称 :
${
data
.
branch
}
\n\n`
const
dingData
=
{
+
`> 扫描日期 :
${
item
.
analysisDate
}
\n\n`
msgtype
:
'
markdown
'
,
+
`> 扫描时间 :
${
duration
}
s\n\n`
markdown
:
{
+
`> 分析时间 :
${
executionTime
}
s\n\n`
title
:
'
扫描结果
'
,
// + `[查看详情](${config.sonarHost}/dashboard?id=${data.content.projectKey})`
text
,
+
`[查看详情](
${
config
.
sonarHost
}
/project/activity?id=
${
data
.
content
.
projectKey
}
&&selected_date=
${
item
.
analysisDate
}
)`
},
const
address
=
data
.
dingRobotAddr
}
await
dingTalk
(
'
扫描结果如下
'
,
text
,
address
)
const
address
=
'
https://oapi.dingtalk.com/robot/send?access_token=5c4c7fb82ed9c5fd2d9a959037eb79d21d5c76ceeffd00b2d1ace86409deae2c
'
}
if
(
address
!==
''
)
{
await
awaitRequest
({
const
sendExceptionDing
=
async
(
data
)
=>
{
url
:
address
,
const
text
=
'
> <font color=#FF0000> 扫描结果 : 扫描任务执行失败
\n\n
'
method
:
'
POST
'
,
+
`> 结果信息 :
${
data
.
error
}
\n\n`
headers
:
{
+
`> 项目名称 :
${
data
.
project
}
\n\n`
'
Content-Type
'
:
'
application/json;charset=utf-8
'
,
+
`> 分支名称 :
${
data
.
branch
}
\n\n`
},
+
`[查看详情](
${
data
.
absoluteUrl
}
/console)`
body
:
JSON
.
stringify
(
dingData
),
const
address
=
data
.
dingRobotAddr
||
config
.
snorDingRobotAddr
})
await
dingTalk
(
'
扫描结果如下
'
,
text
,
address
)
logger
.
info
(
`发送 Sonar dingtalk
${
JSON
.
stringify
(
dingData
)}
`
)
}
}
}
const
callback
=
async
(
ctx
)
=>
{
const
callback
=
async
(
ctx
)
=>
{
const
data
=
ctx
.
request
.
body
const
data
=
ctx
.
request
.
body
logger
.
info
(
'
sonar callback info
'
,
data
)
logger
.
info
(
'
sonar callback info
'
,
data
)
if
(
data
.
error
)
{
sendExceptionDing
(
data
)
ctx
.
body
=
ctx
.
ok
()
return
}
let
flag
=
true
let
flag
=
true
let
executionTimeMs
while
(
flag
)
{
while
(
flag
)
{
const
ceTaskResult
=
await
awaitRequest
({
const
ceTaskResult
=
await
awaitRequest
({
url
:
`
${
data
.
content
.
ceTaskUrl
}
`
,
url
:
`
${
data
.
content
.
ceTaskUrl
}
`
,
method
:
'
GET
'
,
method
:
'
GET
'
,
})
})
logger
.
info
(
1
,
ceTaskResult
)
if
(
ceTaskResult
.
task
.
status
===
'
SUCCESS
'
)
{
if
(
ceTaskResult
.
task
.
status
===
'
SUCCESS
'
)
{
executionTimeMs
=
ceTaskResult
.
task
.
executionTimeMs
flag
=
false
flag
=
false
}
}
await
sleep
(
1
*
1000
)
await
sleep
(
1
*
1000
)
...
@@ -56,7 +62,8 @@ const callback = async (ctx) => {
...
@@ -56,7 +62,8 @@ const callback = async (ctx) => {
logger
.
info
(
'
analysisResult
'
,
data
.
content
.
projectKey
,
JSON
.
stringify
(
res
))
logger
.
info
(
'
analysisResult
'
,
data
.
content
.
projectKey
,
JSON
.
stringify
(
res
))
for
(
const
i
of
res
.
branches
)
{
for
(
const
i
of
res
.
branches
)
{
if
(
i
.
name
===
'
master
'
&&
i
.
status
.
qualityGateStatus
!==
'
OK
'
)
{
if
(
i
.
name
===
'
master
'
&&
i
.
status
.
qualityGateStatus
!==
'
OK
'
)
{
sendData
(
data
.
content
.
projectKey
,
data
.
branch
,
i
)
logger
.
info
(
data
.
content
.
projectKey
,
'
触发质量阀,发送ding消息
'
)
sendDing
(
data
,
i
,
executionTimeMs
)
}
}
}
}
ctx
.
body
=
ctx
.
ok
()
ctx
.
body
=
ctx
.
ok
()
...
...
app/jobs/sonarCheck.js
View file @
4d15b11d
const
schedule
=
require
(
'
node-schedule
'
)
const
schedule
=
require
(
'
node-schedule
'
)
const
logger
=
require
(
'
koa-log4
'
).
getLogger
(
'
s
onarCheck
'
)
const
logger
=
require
(
'
koa-log4
'
).
getLogger
(
'
dailyS
onarCheck
'
)
const
{
ProConfig
}
=
require
(
'
../service/mongoService
'
)
const
{
ProConfig
}
=
require
(
'
../service/mongoService
'
)
const
jenkinsService
=
require
(
'
../service/jenkinsService
'
)
const
jenkinsService
=
require
(
'
../service/jenkinsService
'
)
const
sleep
=
require
(
'
../utils/sleep
'
)
const
sleep
=
(
time
=
0
)
=>
new
Promise
((
resolve
)
=>
{
setTimeout
(()
=>
{
resolve
();
},
time
);
});
const
sonarCheck
=
async
()
=>
{
const
sonarCheck
=
async
()
=>
{
logger
.
info
(
'
daily sonarCheck start
'
)
logger
.
info
(
'
daily sonarCheck start
'
)
let
projects
=
await
ProConfig
.
findByQuery
({
is_active
:
true
,
type
:
'
java
'
},
{
project_name
:
1
})
let
projects
=
await
ProConfig
.
findByQuery
({
is_active
:
true
,
type
:
'
java
'
,
deploy_to_testenv
:
true
},
{
project_name
:
1
})
projects
=
projects
.
map
(
i
=>
i
.
project_name
)
projects
=
projects
.
map
(
i
=>
i
.
project_name
)
logger
.
info
(
'
要扫描的项目
'
,
projects
)
// for test
//
//
for test
const
testProject
=
[
'
acs-batch
'
,
'
cash-loan-flow
'
]
//
const testProject = ['acs-batch', 'cash-loan-flow']
projects
=
projects
.
filter
(
item
=>
testProject
.
includes
(
item
))
//
projects = projects.filter(item => testProject.includes(item))
for
(
const
project
of
projects
)
{
for
(
const
project
of
projects
)
{
const
data
=
{
const
data
=
{
GIT_REPO_NAME
:
project
,
GIT_REPO_NAME
:
project
,
...
@@ -26,13 +21,12 @@ const sonarCheck = async () => {
...
@@ -26,13 +21,12 @@ const sonarCheck = async () => {
}
}
await
jenkinsService
.
build_with_params
(
'
tke-sonar
'
,
data
)
await
jenkinsService
.
build_with_params
(
'
tke-sonar
'
,
data
)
logger
.
info
(
'
daily sonarCheck
'
,
data
)
logger
.
info
(
'
daily sonarCheck
'
,
data
)
await
sleep
(
6
0
*
1000
)
await
sleep
(
3
0
*
1000
)
}
}
logger
.
info
(
'
daily sonarCheck end
'
)
logger
.
info
(
'
daily sonarCheck end
'
)
}
}
// module.exports = schedule.scheduleJob('*/2 * * * *', async () => {
module
.
exports
=
schedule
.
scheduleJob
(
'
0 22 * * *
'
,
async
()
=>
{
module
.
exports
=
schedule
.
scheduleJob
(
'
0 10 * * *
'
,
async
()
=>
{
try
{
try
{
await
sonarCheck
()
await
sonarCheck
()
}
catch
(
e
)
{
}
catch
(
e
)
{
...
...
app/utils/dingTalk.js
0 → 100644
View file @
4d15b11d
const
logger
=
require
(
'
koa-log4
'
).
getLogger
(
'
dingtalk
'
)
const
awaitRequest
=
require
(
'
./awaitRequest
'
)
module
.
exports
=
async
(
title
,
text
,
url
)
=>
{
const
dingData
=
{
msgtype
:
'
markdown
'
,
markdown
:
{
title
,
text
,
},
}
if
(
url
)
{
await
awaitRequest
({
url
,
method
:
'
POST
'
,
headers
:
{
'
Content-Type
'
:
'
application/json;charset=utf-8
'
,
},
body
:
JSON
.
stringify
(
dingData
),
})
logger
.
info
(
`发送 Sonar dingtalk
${
JSON
.
stringify
(
dingData
)}
`
)
}
}
src/app.config.js
View file @
4d15b11d
...
@@ -50,4 +50,5 @@ module.exports = {
...
@@ -50,4 +50,5 @@ module.exports = {
dingdingMemory
:
'
https://oapi.dingtalk.com/robot/send?access_token
'
dingdingMemory
:
'
https://oapi.dingtalk.com/robot/send?access_token
'
+
'
=7cab1f948d67c3565a30323c58773c0ba77c7bd1a3eb1c11f8b2d1b6b4a395df
'
,
+
'
=7cab1f948d67c3565a30323c58773c0ba77c7bd1a3eb1c11f8b2d1b6b4a395df
'
,
sonarHost
:
'
http://sonar.quantgroups.com
'
,
sonarHost
:
'
http://sonar.quantgroups.com
'
,
snorDingRobotAddr
:
'
https://oapi.dingtalk.com/robot/send?access_token=5c4c7fb82ed9c5fd2d9a959037eb79d21d5c76ceeffd00b2d1ace86409deae2c
'
,
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment