mirror of
https://github.com/xuexb/github-bot.git
synced 2025-12-08 17:36:07 +00:00
* feat: fix some eslint bug * feat: add logger system feature close #26 * feat: divide log into different types * fix: errors log write bug * fix: remove unuse console.log * feat: add pm2 support * feat: make logger type configurable * fix: remove unuse test code
This commit is contained in:
parent
3b9c6666d8
commit
d5ee048b3e
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,5 +1,6 @@
|
||||
# Logs
|
||||
logs
|
||||
log
|
||||
*.log
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
|
||||
26
README.md
26
README.md
@ -3,6 +3,7 @@
|
||||
github 机器人:在服务端上启动一个基于 [koajs](http://koajs.com/) 的 http server ,建立一些项目的规范(如 issue 格式、 pull request 格式、配置一些指定 label 根据的 owner 、统一 git commit log 格式等),基于 [github webhooks](https://developer.github.com/webhooks/) 和 [github api](https://developer.github.com/v3/) 让机器人(通常是一个单独的帐号,如 [@jiandansousuo-bot](https://github.com/jiandansousuo-bot) )自动处理一些事情,从而达到快速响应、自动化、解放人力的效果。
|
||||
|
||||
[](https://travis-ci.org/xuexb/github-bot)
|
||||
[](http://standardjs.com)
|
||||
|
||||
## 声明
|
||||
|
||||
@ -85,15 +86,36 @@ https://github.com/用户名/项目名/settings/hooks/new
|
||||
- trigger: Send me everything.
|
||||
- Secret: xxx (_需要在 .env 里配置_)
|
||||
|
||||
### 3. run server
|
||||
### 3. 开发运行
|
||||
|
||||
```
|
||||
```bash
|
||||
npm install
|
||||
cp env .env
|
||||
vim .env
|
||||
npm start
|
||||
```
|
||||
|
||||
### 4. 部署
|
||||
|
||||
本项目使用 [pm2](https://github.com/Unitech/pm2) 进行服务管理,发布前请先全局安装 [pm2](https://github.com/Unitech/pm2)
|
||||
|
||||
```bash
|
||||
npm install pm2 -g
|
||||
npm run deploy
|
||||
```
|
||||
|
||||
后台启动该服务后,可以通过 `pm2 ls` 来查看服务名称为 `github-bot` 的运行状态。具体 [pm2](https://github.com/Unitech/pm2) 使用,请访问:https://github.com/Unitech/pm2
|
||||
|
||||
### 5. 日志系统说明
|
||||
|
||||
本系统 `logger` 服务基于 [log4js](https://github.com/log4js-node/log4js-node)。
|
||||
在根目录的 `.env` 文件中有个参数 `LOG_TYPE` 默认为 `console`,参数值说明:
|
||||
|
||||
```
|
||||
console - 通过 console 输出log。
|
||||
file - 将所有相关log输出到更根目录的 `log` 文件夹中。
|
||||
```
|
||||
|
||||
## contributors
|
||||
|
||||
> [用户贡献指南](.github/CONTRIBUTING.md)
|
||||
|
||||
3
env
3
env
@ -5,3 +5,6 @@ GITHUB_TOKEN=token
|
||||
|
||||
# Webhook secret token, see https://developer.github.com/webhooks/securing/
|
||||
GITHUB_SECRET_TOKEN=secret
|
||||
|
||||
# Logger type: default is console, if you want write log to file, set 'file'
|
||||
LOG_TYPE=console
|
||||
|
||||
@ -5,7 +5,8 @@
|
||||
"main": "src/app.js",
|
||||
"scripts": {
|
||||
"start": "NODE_ENV=development node src/app",
|
||||
"lint": "eslint src/**/* --quiet",
|
||||
"lint": "eslint src/**/*.js --quiet",
|
||||
"deploy": "pm2 start src/app.js --name=github-bot",
|
||||
"precommit": "npm run lint",
|
||||
"commitmsg": "validate-commit-msg"
|
||||
},
|
||||
@ -26,6 +27,7 @@
|
||||
"github": "^11.0.0",
|
||||
"koa": "^2.3.0",
|
||||
"koa-bodyparser": "^4.2.0",
|
||||
"log4js": "^2.3.10",
|
||||
"require-dir": "^0.3.2",
|
||||
"string-template": "^1.0.0"
|
||||
},
|
||||
|
||||
@ -15,6 +15,7 @@ const pullRequestActions = requireDir('./modules/pull_request')
|
||||
const releasesActions = requireDir('./modules/releases')
|
||||
const app = new Koa()
|
||||
const githubEvent = new EventEmitter()
|
||||
const { appLog, accessLog } = require('./logger')
|
||||
|
||||
app.use(bodyParser())
|
||||
|
||||
@ -28,7 +29,7 @@ app.use(ctx => {
|
||||
eventName += `_${action}`
|
||||
}
|
||||
|
||||
console.log(`receive event: ${eventName}`)
|
||||
accessLog.info(`receive event: ${eventName}`)
|
||||
|
||||
githubEvent.emit(eventName, {
|
||||
repo: payload.repository.name,
|
||||
@ -44,9 +45,9 @@ app.use(ctx => {
|
||||
const actions = Object.assign({}, issueActions, pullRequestActions, releasesActions)
|
||||
Object.keys(actions).forEach((key) => {
|
||||
actions[key](githubEvent.on.bind(githubEvent))
|
||||
console.log(`bind ${key} success!`)
|
||||
appLog.info(`bind ${key} success!`)
|
||||
})
|
||||
|
||||
const port = 8000
|
||||
app.listen(port)
|
||||
console.log(`Listening on http://0.0.0.0:${port}`)
|
||||
appLog.info('Listening on http://0.0.0.0:', port)
|
||||
|
||||
@ -5,6 +5,7 @@
|
||||
|
||||
const GitHub = require('github')
|
||||
const { toArray } = require('./utils')
|
||||
const { appLog } = require('./logger')
|
||||
|
||||
const github = new GitHub({
|
||||
debug: process.env.NODE_ENV === 'development'
|
||||
@ -36,6 +37,7 @@ module.exports = {
|
||||
})
|
||||
return res.data.map(v => v.name).indexOf(label) > -1
|
||||
} catch (e) {
|
||||
appLog.error(new Error(e))
|
||||
return false
|
||||
}
|
||||
},
|
||||
@ -60,6 +62,7 @@ module.exports = {
|
||||
})
|
||||
return res.data.map(v => v.name).indexOf(label) > -1
|
||||
} catch (e) {
|
||||
appLog.error(new Error(e))
|
||||
return false
|
||||
}
|
||||
},
|
||||
@ -85,6 +88,7 @@ module.exports = {
|
||||
})
|
||||
return true
|
||||
} catch (e) {
|
||||
appLog.error(new Error(e))
|
||||
return false
|
||||
}
|
||||
},
|
||||
@ -110,6 +114,7 @@ module.exports = {
|
||||
})
|
||||
return true
|
||||
} catch (e) {
|
||||
appLog.error(new Error(e))
|
||||
return false
|
||||
}
|
||||
},
|
||||
@ -134,6 +139,7 @@ module.exports = {
|
||||
})
|
||||
return true
|
||||
} catch (e) {
|
||||
appLog.error(new Error(e))
|
||||
return false
|
||||
}
|
||||
},
|
||||
@ -159,6 +165,7 @@ module.exports = {
|
||||
})
|
||||
return true
|
||||
} catch (e) {
|
||||
appLog.error(new Error(e))
|
||||
return false
|
||||
}
|
||||
},
|
||||
@ -184,6 +191,7 @@ module.exports = {
|
||||
})
|
||||
return true
|
||||
} catch (e) {
|
||||
appLog.error(new Error(e))
|
||||
return false
|
||||
}
|
||||
},
|
||||
@ -209,6 +217,7 @@ module.exports = {
|
||||
})
|
||||
return true
|
||||
} catch (e) {
|
||||
appLog.error(new Error(e))
|
||||
return false
|
||||
}
|
||||
},
|
||||
@ -234,6 +243,7 @@ module.exports = {
|
||||
})
|
||||
return true
|
||||
} catch (e) {
|
||||
appLog.error(new Error(e))
|
||||
return false
|
||||
}
|
||||
},
|
||||
@ -258,6 +268,7 @@ module.exports = {
|
||||
})
|
||||
return true
|
||||
} catch (e) {
|
||||
appLog.error(new Error(e))
|
||||
return false
|
||||
}
|
||||
},
|
||||
@ -290,6 +301,7 @@ module.exports = {
|
||||
})
|
||||
return true
|
||||
} catch (e) {
|
||||
appLog.error(new Error(e))
|
||||
return false
|
||||
}
|
||||
},
|
||||
@ -313,6 +325,7 @@ module.exports = {
|
||||
})
|
||||
return res.data
|
||||
} catch (e) {
|
||||
appLog.error(new Error(e))
|
||||
return null
|
||||
}
|
||||
},
|
||||
@ -340,6 +353,7 @@ module.exports = {
|
||||
})
|
||||
return true
|
||||
} catch (e) {
|
||||
appLog.error(new Error(e))
|
||||
return false
|
||||
}
|
||||
},
|
||||
@ -360,6 +374,7 @@ module.exports = {
|
||||
})
|
||||
return res.data
|
||||
} catch (e) {
|
||||
appLog.error(new Error(e))
|
||||
return []
|
||||
}
|
||||
},
|
||||
@ -384,6 +399,7 @@ module.exports = {
|
||||
})
|
||||
return res.data
|
||||
} catch (e) {
|
||||
appLog.error(new Error(e))
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
62
src/logger.js
Normal file
62
src/logger.js
Normal file
@ -0,0 +1,62 @@
|
||||
/**
|
||||
* Created Date: Wednesday, November 1st 2017, 4:03:55 pm
|
||||
* Author: yugasun
|
||||
* Email: yuga.sun.bj@gmail.com
|
||||
* -----
|
||||
* Last Modified:
|
||||
* Modified By:
|
||||
* -----
|
||||
* Copyright (c) 2017 yugasun
|
||||
*/
|
||||
|
||||
const log4js = require('log4js')
|
||||
|
||||
const fileConfig = {
|
||||
pm2: true,
|
||||
appenders: {
|
||||
app: {
|
||||
'type': 'file',
|
||||
'filename': 'log/app.log',
|
||||
'maxLogSize': 10485760,
|
||||
'numBackups': 3
|
||||
},
|
||||
access: {
|
||||
'type': 'dateFile',
|
||||
'filename': 'log/access.log',
|
||||
'pattern': '-yyyy-MM-dd',
|
||||
'category': 'http'
|
||||
},
|
||||
errorFile: { type: 'file', filename: 'log/errors.log' },
|
||||
errors: {
|
||||
'type': 'logLevelFilter',
|
||||
'level': 'error',
|
||||
'appender': 'errorFile'
|
||||
}
|
||||
},
|
||||
categories: {
|
||||
default: { appenders: ['app', 'errors'], level: 'trace' },
|
||||
http: { appenders: ['access'], level: 'info' }
|
||||
}
|
||||
}
|
||||
const consoleConfig = {
|
||||
pm2: true,
|
||||
appenders: {
|
||||
console: { type: 'console' }
|
||||
},
|
||||
categories: {
|
||||
default: { appenders: ['console'], level: 'info' },
|
||||
http: { appenders: ['console'], level: 'info' }
|
||||
}
|
||||
}
|
||||
|
||||
const config = process.env.LOG_TYPE === 'file' ? fileConfig : consoleConfig
|
||||
|
||||
log4js.configure(config)
|
||||
|
||||
const appLog = log4js.getLogger('app')
|
||||
const accessLog = log4js.getLogger('http')
|
||||
|
||||
module.exports = {
|
||||
appLog,
|
||||
accessLog
|
||||
}
|
||||
@ -3,7 +3,12 @@
|
||||
* @author xuexb <fe.xiaowu@gmail.com>
|
||||
*/
|
||||
|
||||
const { getTags, compareCommits, getReleaseByTag, createRelease } = require('../../github')
|
||||
const {
|
||||
getTags,
|
||||
compareCommits,
|
||||
getReleaseByTag,
|
||||
createRelease
|
||||
} = require('../../github')
|
||||
|
||||
const RELEASE_CHANGE_MAP = {
|
||||
document: 'docs',
|
||||
|
||||
@ -2,14 +2,8 @@
|
||||
* @file 工具集
|
||||
* @author xuexb <fe.xiaowu@gmail.com>
|
||||
*/
|
||||
|
||||
const format = require('string-template')
|
||||
const path = require('path')
|
||||
const fs = require('fs')
|
||||
const crypto = require('crypto')
|
||||
const { fixedTimeComparison } = require('cryptiles')
|
||||
const { execSync } = require('child_process')
|
||||
// const gitPullOrClone = require('git-pull-or-clone')
|
||||
|
||||
const utils = {
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user