Commit d20f1886 authored by 智勇's avatar 智勇

sonar

parent 4d15b11d
...@@ -4,7 +4,7 @@ const _ = require('lodash') ...@@ -4,7 +4,7 @@ const _ = require('lodash')
const { ProConfig } = require('../service/mongoService') const { ProConfig } = require('../service/mongoService')
const getProjectByType = async function (ctx) { const getProjectByType = async function (ctx) {
let item = await ProConfig.findByQuery({ des: { $ne: '海外' } }, { is_active: -1, project_name: 1 }) let item = await ProConfig.findByQuery({ des: { $ne: '海外' } }, {}, { is_active: -1, project_name: 1 })
item = _.groupBy(item, 'type') item = _.groupBy(item, 'type')
const arr = [] const arr = []
...@@ -26,14 +26,14 @@ const getProject = async function (ctx) { ...@@ -26,14 +26,14 @@ const getProject = async function (ctx) {
sort = data.sort.indexOf('+') ? { [data.sort.substr(1)]: -1 } : { [data.sort.substr(1)]: 1 } sort = data.sort.indexOf('+') ? { [data.sort.substr(1)]: -1 } : { [data.sort.substr(1)]: 1 }
delete data.sort delete data.sort
} }
const item = await ProConfig.findByQuery(data, sort) const item = await ProConfig.findByQuery(data, {}, sort)
ctx.body = ctx.ok(item) ctx.body = ctx.ok(item)
} }
const getProjectForJenkins = async function (ctx) { const getProjectForJenkins = async function (ctx) {
const data = ctx.request.query const data = ctx.request.query
const query = data.type ? { is_active: true, type: data.type } : { is_active: true } const query = data.type ? { is_active: true, type: data.type, deploy_to_testenv: true } : { is_active: true, deploy_to_testenv: true }
let item = await ProConfig.findByQuery(query, { project_name: 1 }) let item = await ProConfig.findByQuery(query, { project_name: 1 }, { project_name: 1 })
item = item.map(i => i.project_name) item = item.map(i => i.project_name)
ctx.body = item.join('\n') ctx.body = item.join('\n')
} }
...@@ -78,11 +78,6 @@ const getNewPort = async function (ctx) { ...@@ -78,11 +78,6 @@ const getNewPort = async function (ctx) {
ctx.body = ctx.ok(port + 1) ctx.body = ctx.ok(port + 1)
} }
// const findByQuery = async function (ctx) {
// const item = await ProConfig.findByQuery(ctx.request.body)
// ctx.body = ctx.ok(item)
// }
const remove = async function (ctx) { const remove = async function (ctx) {
const rule = { const rule = {
_id: 'required', _id: 'required',
...@@ -141,7 +136,6 @@ const getK8sConf = async function (ctx) { ...@@ -141,7 +136,6 @@ const getK8sConf = async function (ctx) {
} }
const router = new Router() const router = new Router()
router router
.use(bodyParser()) .use(bodyParser())
.get('/get_project_by_type', getProjectByType) .get('/get_project_by_type', getProjectByType)
...@@ -151,7 +145,6 @@ router ...@@ -151,7 +145,6 @@ router
.get('/get_new_port', getNewPort) .get('/get_new_port', getNewPort)
.get('/delete', remove) .get('/delete', remove)
.post('/save', save) .post('/save', save)
// .post('/find_by_query', findByQuery)
.get('/get_type', getType) .get('/get_type', getType)
.get('/getHostName', getDomain) .get('/getHostName', getDomain)
.get('/getK8sConf', getK8sConf) .get('/getK8sConf', getK8sConf)
......
...@@ -4,14 +4,15 @@ const logger = require('koa-log4').getLogger('sonar') ...@@ -4,14 +4,15 @@ const logger = require('koa-log4').getLogger('sonar')
const awaitRequest = require('../utils/awaitRequest') const awaitRequest = require('../utils/awaitRequest')
const dingTalk = require('../utils/dingTalk') const dingTalk = require('../utils/dingTalk')
const sleep = require('../utils/sleep') const sleep = require('../utils/sleep')
const { SonarJob } = require('../service/mongoService')
const config = require(global.configPath) const config = require(global.configPath)
const sendDing = async (data, item, executionTimeMs) => { const sendDing = async (data, item, executionTimeMs) => {
const duration = data.duration / 1000 const duration = data.duration / 1000
const executionTime = executionTimeMs / 1000 const executionTime = executionTimeMs / 1000
const text = `> <font color=#FF0000> 扫描结果 : ${item.status.qualityGateStatus}\n\n` const text = `> <font color=#FF7F50> 扫描结果 : ${item.status.qualityGateStatus}\n\n`
+ `> 项目名称 : ${data.content.projectKey}\n\n` + `> <font color=#000000> 项目名称 : ${data.content.projectKey}\n\n`
+ `> 分支名称 : ${data.branch}\n\n` + `> 分支名称 : ${data.branch}\n\n`
+ `> 扫描日期 : ${item.analysisDate}\n\n` + `> 扫描日期 : ${item.analysisDate}\n\n`
+ `> 扫描时间 : ${duration}s\n\n` + `> 扫描时间 : ${duration}s\n\n`
...@@ -24,20 +25,49 @@ const sendDing = async (data, item, executionTimeMs) => { ...@@ -24,20 +25,49 @@ const sendDing = async (data, item, executionTimeMs) => {
const sendExceptionDing = async (data) => { const sendExceptionDing = async (data) => {
const text = '> <font color=#FF0000> 扫描结果 : 扫描任务执行失败 \n\n' const text = '> <font color=#FF0000> 扫描结果 : 扫描任务执行失败 \n\n'
+ `> 结果信息 : ${data.error} \n\n` + `> <font color=#000000> 结果信息 : ${data.error} \n\n`
+ `> 项目名称 : ${data.project}\n\n` + `> 项目名称 : ${data.project}\n\n`
+ `> 分支名称 : ${data.branch}\n\n` + `> 分支名称 : ${data.branch}\n\n`
+ `[查看详情](${data.absoluteUrl}/console)` + `[查看详情](${data.absoluteUrl}console)`
const address = data.dingRobotAddr || config.snorDingRobotAddr const address = data.dingRobotAddr || config.snorDingRobotAddr
await dingTalk('扫描结果如下', text, address) await dingTalk('扫描结果如下', text, address)
} }
const sendExistDing = async (data, dingRobotAddr) => {
const text = `> <font color=#FF7F50> 本次执行的GITHASH曾经扫描过,扫描结果 : ${data.sonarResult}\n\n`
+ `> <font color=#000000> 项目名称 : ${data.projectName}\n\n`
+ `> 分支名称 : ${data.branchName}\n\n`
+ `> GITHASH : ${data.branchHash}\n\n`
+ `> 扫描分析日期 : ${data.analysisDate}\n\n`
+ `[查看详情](${config.sonarHost}/project/activity?id=${data.projectName}&&selected_date=${data.analysisDate})`
const address = dingRobotAddr || config.snorDingRobotAddr
await dingTalk('扫描结果如下', text, address)
}
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.sonarJob) {
logger.info(data.sonarJob.projectName, data.sonarJob.branchName, data.sonarJob.branchHash, 'git hash 曾经 扫描过')
if (data.sonarJob.sonarResult !== 'OK' && data.dingRobotAddr) {
sendExistDing(data.sonarJob, data.dingRobotAddr)
}
ctx.body = ctx.ok()
return
}
const saveData = {
buildId: data.buildId,
buildJob: data.buildJob,
branchName: data.branch,
}
if (data.error) { if (data.error) {
saveData.buildResult = 'error'
saveData.projectName = data.project
sendExceptionDing(data) sendExceptionDing(data)
// await SonarJob.saveSonarJob(saveData)
ctx.body = ctx.ok() ctx.body = ctx.ok()
return return
} }
...@@ -61,17 +91,34 @@ const callback = async (ctx) => { ...@@ -61,17 +91,34 @@ 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' && data.dingRobotAddr) {
logger.info(data.content.projectKey, '触发质量阀,发送ding消息') logger.info(data.content.projectKey, '触发质量阀,发送ding消息')
sendDing(data, i, executionTimeMs) sendDing(data, i, executionTimeMs)
} }
saveData.projectName = data.content.projectKey
saveData.buildResult = 'success'
saveData.sonarResult = i.status.qualityGateStatus
saveData.analysisDate = i.analysisDate
saveData.branchHash = data.branchHash
const sonarJob = await SonarJob.getOneSonarJob({ branchHash: data.branchHash })
if (sonarJob) {
await SonarJob.updateSonarJob(saveData)
} else {
await SonarJob.saveSonarJob(saveData)
}
} }
ctx.body = ctx.ok() ctx.body = ctx.ok()
} }
const getSonarJob = async (ctx) => {
const sonarJob = await SonarJob.getOneSonarJob(ctx.request.query)
ctx.body = ctx.ok(sonarJob)
}
const router = new Router() const router = new Router()
router router
.use(bodyParser()) .use(bodyParser())
.post('/callback', callback) .post('/callback', callback)
.get('/getSonarJob', getSonarJob)
module.exports = router; module.exports = router;
...@@ -9,6 +9,7 @@ const schema = new Schema({ ...@@ -9,6 +9,7 @@ const schema = new Schema({
port: { type: Number }, port: { type: Number },
is_active: { type: Boolean }, is_active: { type: Boolean },
deploy_to_testenv: { type: Boolean }, deploy_to_testenv: { type: Boolean },
sonar: { type: Boolean },
type: { type: String, required: true }, type: { type: String, required: true },
git_path: { git_path: {
type: String, unique: true, required: true, index: true, type: String, unique: true, required: true, index: true,
...@@ -55,15 +56,15 @@ schema.statics.getActiveProjectsHostName = function () { ...@@ -55,15 +56,15 @@ schema.statics.getActiveProjectsHostName = function () {
}) })
} }
schema.statics.findByQuery = function (query, sort) { schema.statics.findByQuery = function (query, column, sort) {
if (query.project_name) { if (query.project_name) {
query.project_name = new RegExp(query.project_name) query.project_name = new RegExp(query.project_name)
} }
return this.find(query).sort(sort).lean() return this.find(query, column).sort(sort).lean()
} }
schema.statics.getOneProject = function (query, column, sort) { schema.statics.getOneProject = function (query, column) {
return this.findOne(query, column).sort(sort) return this.findOne(query, column)
} }
schema.statics.saveProject = function (doc) { schema.statics.saveProject = function (doc) {
......
const { Schema } = require('mongoose')
// 表结构
const schema = new Schema({
buildId: { type: Number },
buildJob: { type: String },
projectName: { type: String },
branchName: { type: String },
branchHash: {
type: String, unique: true, required: true, index: true,
},
buildResult: { type: String },
sonarResult: { type: String },
analysisDate: { type: String },
}, {
versionKey: false,
collection: 'sonarJob', // 表名
timestamps: true,
})
schema.statics.saveSonarJob = function (doc) {
return this.create(doc)
}
schema.statics.updateSonarJob = function (doc) {
return this.updateOne({
branchHash: doc.branchHash,
}, doc)
}
schema.statics.getOneSonarJob = function (query) {
return this.findOne(query, { _id: 0 })
}
module.exports = schema
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment