-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathindex.js
89 lines (78 loc) · 2.65 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
// You Can Use The Commands Below To Generate A Self Signed Certificate For Use With This Tutorial
// These Commands Require That You have 'openssl' installed on your system
// openssl genrsa -out privatekey.pem 1024
// openssl req -new -key privatekey.pem -out certrequest.csr
// openssl x509 -req -in certrequest.csr -signkey privatekey.pem -out certificate.pem
let type = 'http';
process.argv.slice(2).forEach((arg) => {
switch (arg) {
case 'https':
case '--https':
type = 'https';
break;
}
});
const fs = require('fs');
const path = require('path');
const { Git : Server } = require("../");
const port = process.env.PORT || 7005;
const repos = new Server(path.normalize(path.resolve(__dirname, 'tmp')), {
autoCreate: true,
authenticate: async ({ type, repo, getUser, headers }) => {
console.log(type, repo, headers); // eslint-disable-line
if (type == 'push') {
const [username, password] = await getUser();
// Decide if this user is allowed to perform this action against this repo.
if (username === '42' && password === '42') {
// This return value can be whatever you want - it is accessible from events.
return {
protectedBranches: ["docs", "main"],
};
} else {
throw Error('wrong password');
}
}
},
});
repos.on('push', (push) => {
console.log(`push ${push.repo} / ${push.commit} ( ${push.branch} )`); // eslint-disable-line
repos.list((err, results) => {
push.log(' ');
push.log('Hey!');
push.log('Checkout these other repos:');
for (const repo of results) {
push.log(`- ${repo}`);
}
push.log(' ');
});
if (push.context.protectedBranches.indexOf(push.branch) !== -1) {
push.log('You do not have permission to write to this branch');
push.reject();
} else {
push.accept();
}
});
repos.on('fetch', (fetch) => {
console.log(`username ${fetch.username}`); // eslint-disable-line
console.log(`fetch ${fetch.repo}/${fetch.commit}`); // eslint-disable-line
fetch.accept();
});
repos.listen(
port,
{
type,
key: fs.readFileSync(path.resolve(__dirname, 'privatekey.pem')),
cert: fs.readFileSync(path.resolve(__dirname, 'certificate.pem')),
},
(error) => {
if(error) return console.error(`failed to start git-server because of error ${error}`); // eslint-disable-line
console.log(`node-git-server running at ${type}://localhost:${port}`); // eslint-disable-line
repos.list((err, result) => {
if (!result) {
console.log("No repositories available..."); // eslint-disable-line
} else {
console.log(result); // eslint-disable-line
}
});
}
);