-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathpushes.js
125 lines (111 loc) · 3.19 KB
/
pushes.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
115
116
117
118
119
120
121
122
123
124
125
const fs = require('fs');
const _ = require('lodash');
const Datastore = require('@seald-io/nedb');
const Action = require('../../proxy/actions/Action').Action;
const toClass = require('../helper').toClass;
const repo = require('./repo');
if (!fs.existsSync('./.data')) fs.mkdirSync('./.data');
if (!fs.existsSync('./.data/db')) fs.mkdirSync('./.data/db');
const db = new Datastore({ filename: './.data/db/pushes.db', autoload: true });
const defaultPushQuery = {
error: false,
blocked: true,
allowPush: false,
authorised: false,
};
const getPushes = (query, logger) => {
if (!query) query = defaultPushQuery;
return new Promise((resolve, reject) => {
db.find(query, (err, docs) => {
if (err) {
reject(err);
} else {
resolve(
_.chain(docs)
.map((x) => toClass(x, Action.prototype))
.value(),
);
}
});
});
};
const getPush = async (id, logger) => {
return new Promise((resolve, reject) => {
db.findOne({ id: id }, (err, doc) => {
if (err) {
reject(err);
} else {
if (!doc) {
resolve(null);
} else {
resolve(toClass(doc, Action.prototype));
}
}
});
});
};
const writeAudit = async (action, logger) => {
return new Promise((resolve, reject) => {
const options = { multi: false, upsert: true };
db.update({ id: action.id }, action, options, (err) => {
if (err) {
reject(err);
} else {
resolve(null);
}
});
});
};
const authorise = async (id, attestation) => {
const action = await getPush(id);
action.authorised = true;
action.canceled = false;
action.rejected = false;
action.attestation = attestation;
await writeAudit(action);
return { message: `authorised ${id}` };
};
const reject = async (id, logger) => {
const action = await getPush(id, logger);
action.authorised = false;
action.canceled = false;
action.rejected = true;
await writeAudit(action);
return { message: `reject ${id}` };
};
const cancel = async (id, logger) => {
const action = await getPush(id, logger);
action.authorised = false;
action.canceled = true;
action.rejected = false;
await writeAudit(action);
return { message: `cancel ${id}` };
};
const canUserCancelPush = async (id, user) => {
return new Promise(async (resolve) => {
const pushDetail = await getPush(id);
const repoUrl = pushDetail.repo.url;
const isAllowed = await repo.isUserPushAllowed(repoUrl, user);
if (isAllowed) {
resolve(true);
} else {
resolve(false);
}
});
};
const canUserApproveRejectPush = async (id, user) => {
return new Promise(async (resolve) => {
const action = await getPush(id);
const repoName = action.repoName.replace('.git', '');
const isAllowed = await repo.canUserApproveRejectPushRepo(repoName, user);
resolve(isAllowed);
});
};
module.exports.getPushes = getPushes;
module.exports.writeAudit = writeAudit;
module.exports.getPush = getPush;
module.exports.authorise = authorise;
module.exports.reject = reject;
module.exports.cancel = cancel;
module.exports.canUserCancelPush = canUserCancelPush;
module.exports.canUserApproveRejectPush = canUserApproveRejectPush;