Skip to content

Commit

Permalink
first blood
Browse files Browse the repository at this point in the history
  • Loading branch information
daolou committed Feb 20, 2019
1 parent 7ea312a commit 5c7686a
Show file tree
Hide file tree
Showing 12 changed files with 293 additions and 0 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
19 changes: 19 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
//当使用flow语法或者实验性质的特性(eslint不支持但是babel支持的特性)需要使用babel-eslint,如不则使用默认即可
"parser": "esprima",
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module",
"ecmaFeatures": {
"impliedStrict": true
}
},
"env": {
"node": true,
"es6": true
},
"extends": ["eslint:recommended"],
"rules": {
"no-console": "off"
}
}
64 changes: 64 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/
package-lock.json
# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

.DS_Store
.vscode/
.idea/
__dist/
.dist/
15 changes: 15 additions & 0 deletions config/data.example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const DIR = '/var/nodejs';
module.exports = {
'github-hook': [
{
cwd: `${DIR}/github-hook`,
script: 'npm run build',
branch: 'master'
},
{
cwd: `${DIR}/github-hook`,
script: 'npm run build',
branch: 'next'
}
]
}
10 changes: 10 additions & 0 deletions config/data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const DIR = '/var/nodejs';
module.exports = {
'github-hook': [
{
cwd: `${DIR}/github-hook`,
script: 'npm run restart',
branch: 'master'
},
]
}
3 changes: 3 additions & 0 deletions config/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
appSecrcet: 'jzgautoci'
}
20 changes: 20 additions & 0 deletions ecosystem.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module.exports = {
/**
* Application configuration section
* http://pm2.keymetrics.io/docs/usage/application-declaration/
*/
apps : [

// First application
{
name : 'GITHUB_HOOK',
script : './src/index.js',
env: {
COMMON_VARIABLE: 'true'
},
env_production : {
NODE_ENV: 'production'
}
}
]
};
39 changes: 39 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "github-hook",
"version": "1.0.0",
"description": "github webhooks auto",
"main": "index.js",
"scripts": {
"test": "cross-env DEBUG=github_hook NODE_ENV=development nodemon ./src/index.js",
"start": "pm2 start ./ecosystem.config.js",
"restart": "pm2 restart ./ecosystem.config.js",
"stop": "pm2 stop ./ecosystem.config.js"

},
"repository": {
"type": "git",
"url": "git+https://github.com/Mr-jiangzhiguo/github-hook.git"
},
"keywords": [
"webhooks",
"auto",
"push"
],
"author": "jzg",
"license": "ISC",
"bugs": {
"url": "https://github.com/Mr-jiangzhiguo/github-hook/issues"
},
"homepage": "https://github.com/Mr-jiangzhiguo/github-hook#readme",
"devDependencies": {
"eslint": "^4.5.0"
},
"dependencies": {
"cross-env": "^5.0.5",
"debug": "^3.0.1",
"koa": "^2.3.0",
"koa-body": "^2.3.0",
"koa-router": "^7.2.1",
"nodemon": "^1.11.0"
}
}
23 changes: 23 additions & 0 deletions src/controlers/token.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const crypto = require('crypto');
const debug = require('debug')('router')
const secret = require('../../config/index').appSecrcet;
let verify = async (ctx,next)=>{
let signature = ctx.get('X-Hub-Signature');
debug(signature)
let body = ctx.request.body
if(!signature || !body){
ctx.throw(401)
}else{
let hash = 'sha1=' + crypto.createHmac('sha1',secret).update(JSON.stringify(ctx.request.body)).digest('hex');
if(signature != hash){
ctx.throw(401)
}else{
await next()
}
}
}


module.exports = {
verify
}
15 changes: 15 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const debug = require('debug')('app')
const koa = require('koa')

const koaBody = require('koa-body')

const routes = require('./routes/index');

const app = new koa()

app.use(koaBody({ multipart: true }))
app.use(routes())
app.listen(9002,()=>{
debug('running')
console.log('running')
})
65 changes: 65 additions & 0 deletions src/jobs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const {spawn,spawnSync} = require('child_process')
const DATA = require('../../config/data')

const prepareScripts = ['git add .','git stash','git stash clear', 'git checkout {{branch}}', 'git pull origin {{branch}}']

let build = (obj, ref) => {
let branch = ref.split('/').pop();
if(obj && obj.branch == branch){
let reg = new RegExp(`{{branch}}`,'ig')
let flag = true;
prepareScripts.map(item=>{
if(!flag) return;
item = item.replace(reg,branch)
console.log('doing script: ' + item)
item = item.split(' ')
let result = spawnSync(item[0],item.slice(1),{cwd: obj.cwd,stdio:'inherit'})
if(result.status != 0){
flag = false;
}
})
if(flag){
if(typeof obj.script == 'string'){
let script = obj.script.split(' ')
let task = spawn(script[0],script.slice(1),{cwd: obj.cwd,stdio:'inherit'})
task.on('close', (code) => {
if(code!=0) flag = false;
if(flag){
console.log(`task exited with code ${code}`)
}else{
console.log('build failed!')
}
});
}else if(Array.isArray(obj.script)){
obj.script.forEach(item=>{
console.log(`running ${item}`)
item = item.split(' ')
let result = spawnSync(item[0],item.slice(1),{cwd: obj.cwd,stdio:'inherit'})
if(result.status != 0){
console.log(`${item.join(' ')} success`)
}else{
console.log(`${item.join(' ')} failed!`)
}
})
}
}else{
console.log('build failed!')
}

}
}

let run = (name, ref) => {
let arr = DATA[name];
if(Array.isArray(arr)){
arr.forEach(obj=>{
build(obj, ref)
})
}else if(typeof arr == 'object'){
build(arr, ref)
}
}

module.exports = {
run
}
19 changes: 19 additions & 0 deletions src/routes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const Router = require('koa-router')
// const debug = require('debug')('pyload')
const router = new Router();
const verify = require('../controlers/token').verify
const jobs = require('../jobs')
router.post('/payload/:name',verify, async (ctx,next)=>{
const {name} = ctx.params;
const {ref} = ctx.request.body;
if(ref){
jobs.run(name,ref);
}
ctx.status = 200;
ctx.body={msg:'ok'}
})

router.use(router.allowedMethods())
module.exports = ()=>{
return router.routes()
}

0 comments on commit 5c7686a

Please sign in to comment.