This repository was archived by the owner on Dec 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
72 lines (56 loc) · 1.87 KB
/
index.js
File metadata and controls
72 lines (56 loc) · 1.87 KB
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
'use strict'
const clone = require('git-clone/promise')
const fs = require('fs')
const path = require('path')
const del = require('del')
function buildFileTree(rootDir) {
var dir = {}
fs.readdirSync(rootDir).forEach(file => {
if (file == ".git")
return
let filePath = path.resolve(rootDir, file)
if (fs.lstatSync(filePath).isDirectory()) {
dir[file] = buildFileTree(filePath)
} else {
dir[file] = filePath
}
})
return dir
}
const multihook = function (clonePaths, cloneOptions, pat) {
return function (req, res, next = () => { }) {
req.setEncoding('utf8')
req.rawBody = ''
req.on('data', function (chunk) {
req.rawBody += chunk
})
req.on('end', async function () {
// all data received
// decode string
req.body = decodeURIComponent(req.rawBody.substring(8, req.rawBody.length))
var data = JSON.parse(req.body)
// event received for a different repo then specified
if (!Object.keys(clonePaths).includes(data.repository.full_name))
res.status(403).end()
let cloneUrl;
if(pat){
cloneUrl = `https://user:${pat}@github.com/${data.repository.full_name}`
}else{
if(data.repository.private)
throw new Error("Received hook from private repository, but no PAT was supplied!")
cloneUrl = `https://github.com/${data.repository.full_name}`
}
var clonePath = clonePaths[data.repository.full_name]
del.sync([clonePath + "/**", clonePath + "/.**", "!" + clonePath])
await clone(cloneUrl, clonePath, {...cloneOptions, args: ["--recursive", "-j8", ...(cloneOptions.args||[])]})
res.locals.files = buildFileTree(clonePath)
next()
})
}
}
module.exports = {
multihook,
hook: function (repository, clonePath, options, pat) {
return multihook({ [repository]: clonePath }, options, pat)
}
}