-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathrepo.js
102 lines (86 loc) · 2.94 KB
/
repo.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
const connect = require('./helper').connect;
const cnName = 'repos';
const isBlank = (str) => {
return !str || /^\s*$/.test(str);
};
exports.getRepos = async (query = {}) => {
const collection = await connect(cnName);
return collection.find().toArray();
};
exports.getRepo = async (name) => {
const collection = await connect(cnName);
return collection.findOne({ name: { $eq: name } });
};
exports.getRepoByUrl = async (url) => {
const collection = await connect(cnName);
return collection.findOne({ url: { $eq: url}});
}
exports.createRepo = async (repo) => {
console.log(`creating new repo ${JSON.stringify(repo)}`);
if (isBlank(repo.project)) {
throw new Error('Project name cannot be empty');
}
if (isBlank(repo.name)) {
throw new Error('Repository name cannot be empty');
}
if (isBlank(repo.url)) {
throw new Error('URL cannot be empty');
}
repo.users = {
canPush: [],
canAuthorise: [],
};
const collection = await connect(cnName);
await collection.insertOne(repo);
console.log(`created new repo ${JSON.stringify(repo)}`);
};
exports.addUserCanPush = async (name, user) => {
name = name.toLowerCase();
const collection = await connect(cnName);
await collection.updateOne({ name: name }, { $push: { 'users.canPush': user } });
};
exports.addUserCanAuthorise = async (name, user) => {
name = name.toLowerCase();
const collection = await connect(cnName);
await collection.updateOne({ name: name }, { $push: { 'users.canAuthorise': user } });
};
exports.removeUserCanPush = async (name, user) => {
name = name.toLowerCase();
const collection = await connect(cnName);
await collection.updateOne({ name: name }, { $pull: { 'users.canPush': user } });
};
exports.removeUserCanAuthorise = async (name, user) => {
name = name.toLowerCase();
const collection = await connect(cnName);
await collection.updateOne({ name: name }, { $pull: { 'users.canAuthorise': user } });
};
exports.deleteRepo = async (name) => {
const collection = await connect(cnName);
await collection.deleteMany({ name: name });
};
exports.isUserPushAllowed = async (url, user) => {
return new Promise(async (resolve, reject) => {
const repo = await exports.getRepoByUrl(url);
console.log(repo.users.canPush);
console.log(repo.users.canAuthorise);
if (repo.users.canPush.includes(user) || repo.users.canAuthorise.includes(user)) {
resolve(true);
} else {
resolve(false);
}
});
};
exports.canUserApproveRejectPushRepo = async (name, user) => {
name = name.toLowerCase();
console.log(`checking if user ${user} can approve/reject for ${name}`);
return new Promise(async (resolve, reject) => {
const repo = await exports.getRepo(name);
if (repo.users.canAuthorise.includes(user)) {
console.log(`user ${user} can approve/reject to repo ${name}`);
resolve(true);
} else {
console.log(`user ${user} cannot approve/reject to repo ${name}`);
resolve(false);
}
});
};