-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
114 lines (94 loc) · 2.83 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
require('dotenv').config()
require('crypto')
const simpleGit = require('simple-git/promise')
const express = require('express')
const app = express()
const port = process.env.PORT || 3000
// helper
const helper = require('./helper')
// loggin git
const debug = require('debug')
debug.enable('simple-git,simple-git:*')
// middleware
const middle = require('./middleware')
app.use(express.json())
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.use(middle.onlyPushPingEvent)
app.use(middle.verifyPostData)
app.post('/watchme', async (req, res) => {
const payload = req.body
const repoConfig = helper.findRepoByGithubURL(payload.repository.html_url)
if (repoConfig === false) {
return res.json({
message: 'nothing forwarded'
})
}
if (req.get('X-Github-Event') === 'ping') {
return res.json({
message: 'pong'
})
}
const pushedBranch = payload.ref.replace(/^refs\/heads\//, '')
if (!repoConfig.branch.includes(pushedBranch)) {
return res.json({
message: `nothing forwarded on branch ${pushedBranch}`
})
}
// check if delete branch
if (payload.deleted) {
return res.json({
message: `nothing forwarded on delete branch ${pushedBranch}`
})
}
try {
const git = simpleGit(repoConfig.origin_path, {})
// check remote git
const remoteShouldHave = [repoConfig.origin_remote, repoConfig.destination_remote]
const remotes = await git.getRemotes()
const remoteMap = remotes.map(v => v.name)
if (!helper.checkRemoteExist(remoteShouldHave, remoteMap)) {
return res.json({
message: 'remote not valid/complete'
})
}
// fetch (in case dont have any branch)
const resFetch = await git.fetch(repoConfig.origin_remote, pushedBranch)
console.log('fetch: ', resFetch)
// branch (check local branch exist)
const resBranch = await git.branchLocal()
if (!resBranch.all.includes(pushedBranch)) {
// create new branch
const resCheckout = await git.checkoutBranch(pushedBranch, `${repoConfig.origin_remote}/${pushedBranch}`)
console.log('checkout: ', resCheckout)
} else {
// checkout branch
const resCheckout = await git.checkout(pushedBranch)
console.log('checkout: ', resCheckout)
}
// pull
const resPull = await git.pull(repoConfig.origin_remote, pushedBranch)
console.log('pull: ', resPull)
// push
const resPush = await git.push(repoConfig.destination_remote, pushedBranch)
console.log('push: ', resPush)
return res.json({
message: 'forwarded'
})
} catch (err) {
console.error(err)
throw err
}
})
app.use((err, req, res, next) => {
if (err) {
console.error(err)
return res.status(500).json({
message: 'Something went wrong'
})
}
})
app.listen(port, () => {
console.log(`Webhook listening at http://localhost:${port}`)
})