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
ac831818
Commit
ac831818
authored
Nov 13, 2019
by
智勇
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
sonar
parent
8ad6bfef
Changes
9
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
134 additions
and
6 deletions
+134
-6
pipeline.js
app/controller/pipeline.js
+3
-2
pipelineback.js
app/controller/pipelineback.js
+2
-2
proconfig.js
app/controller/proconfig.js
+9
-0
sonar.js
app/controller/sonar.js
+70
-0
index.js
app/index.js
+2
-1
sonarCheck.js
app/jobs/sonarCheck.js
+41
-0
sleep.js
app/utils/sleep.js
+5
-0
server.js
bin/server.js
+1
-1
app.config.js
src/app.config.js
+1
-0
No files found.
app/controller/pipeline.js
View file @
ac831818
...
...
@@ -100,8 +100,9 @@ const getMaster = async function (ctx) {
const
{
name
,
gitlabAddress
}
=
ctx
.
request
.
body
let
projectId
const
version
=
gitlabAddress
===
'
gitabc.xyqb.com
'
?
'
v3
'
:
'
v4
'
const
token
=
config
.
gitlab
.
token
const
data
=
await
awaitRequest
({
url
:
`http://
${
gitlabAddress
}
/api/
${
version
}
/projects?per_page=200&search=
${
name
}
&private_token=
${
config
.
gitlab
.
token
}
`
,
url
:
`http://
${
gitlabAddress
}
/api/
${
version
}
/projects?per_page=200&search=
${
name
}
&private_token=
${
token
}
`
,
method
:
'
GET
'
,
})
if
(
JSON
.
parse
(
data
).
length
===
0
)
{
...
...
@@ -115,7 +116,7 @@ const getMaster = async function (ctx) {
}
})
const
dataMaster
=
await
awaitRequest
({
url
:
`http://
${
gitlabAddress
}
/api/
${
version
}
/projects/
${
projectId
}
/repository/branches?private_token=
${
config
.
gitlab
.
token
}
`
,
url
:
`http://
${
gitlabAddress
}
/api/
${
version
}
/projects/
${
projectId
}
/repository/branches?private_token=
${
token
}
&per_page=100
`
,
method
:
'
GET
'
,
})
ctx
.
body
=
ctx
.
ok
(
JSON
.
parse
(
dataMaster
))
...
...
app/controller/pipelineback.js
View file @
ac831818
const
log
=
require
(
'
koa-log4
'
).
getLogger
(
'
service:pipelines
'
)
const
Router
=
require
(
'
koa-router
'
)
const
moment
=
require
(
'
moment
'
)
const
sleep
=
require
(
'
sleep
'
)
const
bodyParser
=
require
(
'
koa-bodyparser
'
)
const
request
=
require
(
'
request
'
)
const
sleep
=
require
(
'
../utils/sleep
'
)
const
config
=
require
(
'
../../src
'
);
const
{
PipeLine
,
PipeLinePush
}
=
require
(
'
../service/mongoService
'
)
const
jenkinsService
=
require
(
'
../service/jenkinsService
'
)
...
...
@@ -32,7 +32,7 @@ const webhooks = async function (ctx) {
// order 缓存数据 方便以后找到对应的钉钉地址
const
order
=
[]
data
.
forEach
(
async
(
item
,
index
)
=>
{
sleep
.
m
sleep
(
3
)
await
sleep
(
3
)
let
images
=
{}
item
.
repos
.
forEach
((
value
)
=>
{
if
(
value
.
repository
===
projectName
&&
value
.
ref
.
name
===
branchName
)
{
...
...
app/controller/proconfig.js
View file @
ac831818
...
...
@@ -30,6 +30,14 @@ const getProject = async function (ctx) {
ctx
.
body
=
ctx
.
ok
(
item
)
}
const
getProjectForJenkins
=
async
function
(
ctx
)
{
const
data
=
ctx
.
request
.
query
const
query
=
data
.
type
?
{
is_active
:
true
,
type
:
data
.
type
}
:
{
is_active
:
true
}
let
item
=
await
ProConfig
.
findByQuery
(
query
,
{
project_name
:
1
})
item
=
item
.
map
(
i
=>
i
.
project_name
)
ctx
.
body
=
item
.
join
(
'
\n
'
)
}
const
save
=
async
function
(
ctx
)
{
const
rule
=
{
git_path
:
'
required
'
,
...
...
@@ -138,6 +146,7 @@ router
.
use
(
bodyParser
())
.
get
(
'
/get_project_by_type
'
,
getProjectByType
)
.
get
(
'
/get_project
'
,
getProject
)
.
get
(
'
/get_project_for_jenkins
'
,
getProjectForJenkins
)
.
get
(
'
/get_info
'
,
getInfo
)
.
get
(
'
/get_new_port
'
,
getNewPort
)
.
get
(
'
/delete
'
,
remove
)
...
...
app/controller/sonar.js
0 → 100644
View file @
ac831818
const
Router
=
require
(
'
koa-router
'
)
const
bodyParser
=
require
(
'
koa-bodyparser
'
)
const
logger
=
require
(
'
koa-log4
'
).
getLogger
(
'
sonar
'
)
const
awaitRequest
=
require
(
'
../utils/awaitRequest
'
)
const
sleep
=
require
(
'
../utils/sleep
'
)
const
config
=
require
(
global
.
configPath
)
const
sendData
=
async
(
projectName
,
branchName
,
item
)
=>
{
const
text
=
`> 项目名称 :
${
projectName
}
\n\n`
+
`> 分支名称 :
${
branchName
}
\n\n`
+
`> 扫描结果 :
${
item
.
status
.
qualityGateStatus
}
\n\n`
// + `[查看详情](${config.sonarHost}/dashboard?id=${projectName})`
+
`[查看详情](
${
config
.
sonarHost
}
/project/activity?id=
${
projectName
}
&&selected_date=
${
item
.
analysisDate
}
)`
const
dingData
=
{
msgtype
:
'
markdown
'
,
markdown
:
{
title
:
'
扫描结果
'
,
text
,
},
}
const
address
=
'
https://oapi.dingtalk.com/robot/send?access_token=5c4c7fb82ed9c5fd2d9a959037eb79d21d5c76ceeffd00b2d1ace86409deae2c
'
if
(
address
!==
''
)
{
await
awaitRequest
({
url
:
address
,
method
:
'
POST
'
,
headers
:
{
'
Content-Type
'
:
'
application/json;charset=utf-8
'
,
},
body
:
JSON
.
stringify
(
dingData
),
})
logger
.
info
(
`发送 Sonar dingtalk
${
JSON
.
stringify
(
dingData
)}
`
)
}
}
const
callback
=
async
(
ctx
)
=>
{
const
data
=
ctx
.
request
.
body
logger
.
info
(
'
sonar callback info
'
,
data
)
let
flag
=
true
while
(
flag
)
{
const
ceTaskResult
=
await
awaitRequest
({
url
:
`
${
data
.
content
.
ceTaskUrl
}
`
,
method
:
'
GET
'
,
})
logger
.
info
(
1
,
ceTaskResult
)
if
(
ceTaskResult
.
task
.
status
===
'
SUCCESS
'
)
{
flag
=
false
}
await
sleep
(
1
*
1000
)
}
const
res
=
await
awaitRequest
({
url
:
`
${
config
.
sonarHost
}
/api/project_branches/list?project=
${
data
.
content
.
projectKey
}
`
,
method
:
'
GET
'
,
})
logger
.
info
(
'
analysisResult
'
,
data
.
content
.
projectKey
,
JSON
.
stringify
(
res
))
for
(
const
i
of
res
.
branches
)
{
if
(
i
.
name
===
'
master
'
&&
i
.
status
.
qualityGateStatus
!==
'
OK
'
)
{
sendData
(
data
.
content
.
projectKey
,
data
.
branch
,
i
)
}
}
ctx
.
body
=
ctx
.
ok
()
}
const
router
=
new
Router
()
router
.
use
(
bodyParser
())
.
post
(
'
/callback
'
,
callback
)
module
.
exports
=
router
;
app/index.js
View file @
ac831818
...
...
@@ -29,6 +29,7 @@ const pipeline = require('./controller/pipeline')
const
resource
=
require
(
'
./controller/resource
'
)
const
cluster
=
require
(
'
./controller/cluster
'
)
const
notify
=
require
(
'
./controller/notify
'
)
const
sonar
=
require
(
'
./controller/sonar
'
)
// todo: 引入auth中间件
router
...
...
@@ -56,6 +57,6 @@ router
.
use
(
'
/resource
'
,
resource
.
routes
())
.
use
(
'
/cluster
'
,
cluster
.
routes
())
.
use
(
'
/notify
'
,
notify
.
routes
())
.
use
(
'
/sonar
'
,
sonar
.
routes
())
module
.
exports
=
router
app/jobs/sonarCheck.js
0 → 100644
View file @
ac831818
const
schedule
=
require
(
'
node-schedule
'
)
const
logger
=
require
(
'
koa-log4
'
).
getLogger
(
'
sonarCheck
'
)
const
{
ProConfig
}
=
require
(
'
../service/mongoService
'
)
const
jenkinsService
=
require
(
'
../service/jenkinsService
'
)
const
sleep
=
(
time
=
0
)
=>
new
Promise
((
resolve
)
=>
{
setTimeout
(()
=>
{
resolve
();
},
time
);
});
const
sonarCheck
=
async
()
=>
{
logger
.
info
(
'
daily sonarCheck start
'
)
let
projects
=
await
ProConfig
.
findByQuery
({
is_active
:
true
,
type
:
'
java
'
},
{
project_name
:
1
})
projects
=
projects
.
map
(
i
=>
i
.
project_name
)
// for test
const
testProject
=
[
'
acs-batch
'
,
'
cash-loan-flow
'
]
projects
=
projects
.
filter
(
item
=>
testProject
.
includes
(
item
))
for
(
const
project
of
projects
)
{
const
data
=
{
GIT_REPO_NAME
:
project
,
GIT_BRANCH
:
'
master
'
,
}
await
jenkinsService
.
build_with_params
(
'
tke-sonar
'
,
data
)
logger
.
info
(
'
daily sonarCheck
'
,
data
)
await
sleep
(
60
*
1000
)
}
logger
.
info
(
'
daily sonarCheck end
'
)
}
// module.exports = schedule.scheduleJob('*/2 * * * *', async () => {
module
.
exports
=
schedule
.
scheduleJob
(
'
0 10 * * *
'
,
async
()
=>
{
try
{
await
sonarCheck
()
}
catch
(
e
)
{
logger
.
info
(
'
daily sonarCheck
'
,
e
)
}
});
app/utils/sleep.js
0 → 100644
View file @
ac831818
module
.
exports
=
(
time
=
0
)
=>
new
Promise
((
resolve
)
=>
{
setTimeout
(()
=>
{
resolve
();
},
time
);
});
bin/server.js
View file @
ac831818
const
Koa
=
require
(
'
koa
'
)
const
path
=
require
(
'
path
'
)
const
compress
=
require
(
'
koa-compress
'
)
...
...
@@ -15,6 +14,7 @@ const auth = require('../app/middleware/auth')
require
(
'
../app/jobs/cpuRate
'
)
require
(
'
../app/jobs/memoryRate
'
)
require
(
'
../app/jobs/recovery
'
)
require
(
'
../app/jobs/sonarCheck
'
)
log4js
.
configure
(
config
.
log4js
)
const
logger
=
log4js
.
getLogger
()
...
...
src/app.config.js
View file @
ac831818
...
...
@@ -49,4 +49,5 @@ module.exports = {
+
'
=08fc72710502b4fec2aeab456ae1ff87e1d75d07f111b067bd1fa85bbedd5473
'
,
dingdingMemory
:
'
https://oapi.dingtalk.com/robot/send?access_token
'
+
'
=7cab1f948d67c3565a30323c58773c0ba77c7bd1a3eb1c11f8b2d1b6b4a395df
'
,
sonarHost
:
'
http://sonar.quantgroups.com
'
,
}
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