Skip to content

Commit a3834ab

Browse files
authored
Merge pull request #979 from kriswest/948-neDB-implementation-issues
fix: neDB implementation issues
2 parents 7c3bd62 + b10763f commit a3834ab

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+924
-280
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"prepare": "node ./scripts/prepare.js",
2020
"lint": "eslint \"src/**/*.{js,jsx,ts,tsx,json}\" \"test/**/*.{js,jsx,ts,tsx,json}\"",
2121
"lint:fix": "eslint --fix \"src/**/*.{js,jsx,ts,tsx,json}\" \"test/**/*.{js,jsx,ts,tsx,json}\"",
22-
"format": "prettier --write src/**/*.{js,jsx,ts,tsx,css,md,json,scss} test/**/*.{js,jsx,ts,tsx,json} --config ./.prettierrc",
22+
"format": "prettier --write src/**/*.{js,jsx,ts,tsx,css,md,json,scss} test/**/*.{js,jsx,ts,tsx,json} packages/git-proxy-cli/test/**/*.{js,jsx,ts,tsx,json} packages/git-proxy-cli/index.js --config ./.prettierrc",
2323
"gen-schema-doc": "node ./scripts/doc-schema.js",
2424
"cypress:run": "cypress run"
2525
},

packages/git-proxy-cli/test/testCli.proxy.config.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
{
22
"tempPassword": {
33
"sendEmail": false,
4-
"emailConfig": {
5-
}
4+
"emailConfig": {}
65
},
76
"authorisedList": [
87
{
@@ -22,9 +21,9 @@
2221
{
2322
"type": "mongo",
2423
"connectionString": "mongodb://localhost:27017/gitproxy",
25-
"options": {
24+
"options": {
2625
"useUnifiedTopology": true
27-
},
26+
},
2827
"enabled": false
2928
}
3029
],

src/config/env.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ const {
99
GIT_PROXY_SERVER_PORT = 8000,
1010
GIT_PROXY_HTTPS_SERVER_PORT = 8443,
1111
GIT_PROXY_UI_HOST = 'http://localhost',
12-
GIT_PROXY_UI_PORT = 8080
12+
GIT_PROXY_UI_PORT = 8080,
1313
} = process.env;
1414

1515
export const serverConfig: ServerConfig = {
1616
GIT_PROXY_SERVER_PORT,
1717
GIT_PROXY_HTTPS_SERVER_PORT,
1818
GIT_PROXY_UI_HOST,
19-
GIT_PROXY_UI_PORT
19+
GIT_PROXY_UI_PORT,
2020
};

src/db/file/pushes.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,17 @@ import { toClass } from '../helper';
66
import * as repo from './repo';
77
import { PushQuery } from '../types';
88

9+
const COMPACTION_INTERVAL = 1000 * 60 * 60 * 24; // once per day
10+
11+
// these don't get coverage in tests as they have already been run once before the test
12+
/* istanbul ignore if */
913
if (!fs.existsSync('./.data')) fs.mkdirSync('./.data');
14+
/* istanbul ignore if */
1015
if (!fs.existsSync('./.data/db')) fs.mkdirSync('./.data/db');
1116

1217
const db = new Datastore({ filename: './.data/db/pushes.db', autoload: true });
18+
db.ensureIndex({ fieldName: 'id', unique: true });
19+
db.setAutocompactionInterval(COMPACTION_INTERVAL);
1320

1421
const defaultPushQuery: PushQuery = {
1522
error: false,
@@ -22,6 +29,8 @@ export const getPushes = (query: PushQuery) => {
2229
if (!query) query = defaultPushQuery;
2330
return new Promise((resolve, reject) => {
2431
db.find(query, (err: Error, docs: Action[]) => {
32+
// ignore for code coverage as neDB rarely returns errors even for an invalid query
33+
/* istanbul ignore if */
2534
if (err) {
2635
reject(err);
2736
} else {
@@ -38,6 +47,8 @@ export const getPushes = (query: PushQuery) => {
3847
export const getPush = async (id: string) => {
3948
return new Promise<Action | null>((resolve, reject) => {
4049
db.findOne({ id: id }, (err, doc) => {
50+
// ignore for code coverage as neDB rarely returns errors even for an invalid query
51+
/* istanbul ignore if */
4152
if (err) {
4253
reject(err);
4354
} else {
@@ -54,6 +65,8 @@ export const getPush = async (id: string) => {
5465
export const deletePush = async (id: string) => {
5566
return new Promise<void>((resolve, reject) => {
5667
db.remove({ id }, (err) => {
68+
// ignore for code coverage as neDB rarely returns errors even for an invalid query
69+
/* istanbul ignore if */
5770
if (err) {
5871
reject(err);
5972
} else {
@@ -67,6 +80,8 @@ export const writeAudit = async (action: Action) => {
6780
return new Promise((resolve, reject) => {
6881
const options = { multi: false, upsert: true };
6982
db.update({ id: action.id }, action, options, (err) => {
83+
// ignore for code coverage as neDB rarely returns errors even for an invalid query
84+
/* istanbul ignore if */
7085
if (err) {
7186
reject(err);
7287
} else {
@@ -115,7 +130,7 @@ export const cancel = async (id: string) => {
115130
return { message: `cancel ${id}` };
116131
};
117132

118-
export const canUserCancelPush = async (id: string, user: any) => {
133+
export const canUserCancelPush = async (id: string, user: string) => {
119134
return new Promise<boolean>(async (resolve) => {
120135
const pushDetail = await getPush(id);
121136
if (!pushDetail) {
@@ -134,14 +149,14 @@ export const canUserCancelPush = async (id: string, user: any) => {
134149
});
135150
};
136151

137-
export const canUserApproveRejectPush = async (id: string, user: any) => {
152+
export const canUserApproveRejectPush = async (id: string, user: string) => {
138153
return new Promise<boolean>(async (resolve) => {
139154
const action = await getPush(id);
140155
if (!action) {
141156
resolve(false);
142157
return;
143158
}
144-
const repoName = action?.repoName.replace('.git', '');
159+
const repoName = action.repoName.replace('.git', '');
145160
const isAllowed = await repo.canUserApproveRejectPushRepo(repoName, user);
146161

147162
resolve(isAllowed);

src/db/file/repo.ts

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,31 @@
11
import fs from 'fs';
2-
import Datastore from '@seald-io/nedb'
2+
import Datastore from '@seald-io/nedb';
33
import { Repo } from '../types';
44

5+
const COMPACTION_INTERVAL = 1000 * 60 * 60 * 24; // once per day
6+
7+
// these don't get coverage in tests as they have already been run once before the test
8+
/* istanbul ignore if */
59
if (!fs.existsSync('./.data')) fs.mkdirSync('./.data');
10+
/* istanbul ignore if */
611
if (!fs.existsSync('./.data/db')) fs.mkdirSync('./.data/db');
712

813
const db = new Datastore({ filename: './.data/db/repos.db', autoload: true });
14+
db.ensureIndex({ fieldName: 'name', unique: false });
15+
db.setAutocompactionInterval(COMPACTION_INTERVAL);
16+
17+
const isBlank = (str: string) => {
18+
return !str || /^\s*$/.test(str);
19+
};
920

1021
export const getRepos = async (query: any = {}) => {
22+
if (query?.name) {
23+
query.name = query.name.toLowerCase();
24+
}
1125
return new Promise<Repo[]>((resolve, reject) => {
12-
db.find({}, (err: Error, docs: Repo[]) => {
26+
db.find(query, (err: Error, docs: Repo[]) => {
27+
// ignore for code coverage as neDB rarely returns errors even for an invalid query
28+
/* istanbul ignore if */
1329
if (err) {
1430
reject(err);
1531
} else {
@@ -21,7 +37,9 @@ export const getRepos = async (query: any = {}) => {
2137

2238
export const getRepo = async (name: string) => {
2339
return new Promise<Repo | null>((resolve, reject) => {
24-
db.findOne({ name }, (err: Error | null, doc: Repo) => {
40+
db.findOne({ name: name.toLowerCase() }, (err: Error | null, doc: Repo) => {
41+
// ignore for code coverage as neDB rarely returns errors even for an invalid query
42+
/* istanbul ignore if */
2543
if (err) {
2644
reject(err);
2745
} else {
@@ -31,15 +49,28 @@ export const getRepo = async (name: string) => {
3149
});
3250
};
3351

34-
3552
export const createRepo = async (repo: Repo) => {
53+
if (isBlank(repo.project)) {
54+
throw new Error('Project name cannot be empty');
55+
}
56+
if (isBlank(repo.name)) {
57+
throw new Error('Repository name cannot be empty');
58+
} else {
59+
repo.name = repo.name.toLowerCase();
60+
}
61+
if (isBlank(repo.url)) {
62+
throw new Error('URL cannot be empty');
63+
}
64+
3665
repo.users = {
3766
canPush: [],
3867
canAuthorise: [],
3968
};
4069

4170
return new Promise<Repo>((resolve, reject) => {
4271
db.insert(repo, (err, doc) => {
72+
// ignore for code coverage as neDB rarely returns errors even for an invalid query
73+
/* istanbul ignore if */
4374
if (err) {
4475
reject(err);
4576
} else {
@@ -50,6 +81,8 @@ export const createRepo = async (repo: Repo) => {
5081
};
5182

5283
export const addUserCanPush = async (name: string, user: string) => {
84+
name = name.toLowerCase();
85+
user = user.toLowerCase();
5386
return new Promise(async (resolve, reject) => {
5487
const repo = await getRepo(name);
5588
if (!repo) {
@@ -65,6 +98,8 @@ export const addUserCanPush = async (name: string, user: string) => {
6598

6699
const options = { multi: false, upsert: false };
67100
db.update({ name: name }, repo, options, (err) => {
101+
// ignore for code coverage as neDB rarely returns errors even for an invalid query
102+
/* istanbul ignore if */
68103
if (err) {
69104
reject(err);
70105
} else {
@@ -75,6 +110,8 @@ export const addUserCanPush = async (name: string, user: string) => {
75110
};
76111

77112
export const addUserCanAuthorise = async (name: string, user: string) => {
113+
name = name.toLowerCase();
114+
user = user.toLowerCase();
78115
return new Promise(async (resolve, reject) => {
79116
const repo = await getRepo(name);
80117
if (!repo) {
@@ -91,6 +128,8 @@ export const addUserCanAuthorise = async (name: string, user: string) => {
91128

92129
const options = { multi: false, upsert: false };
93130
db.update({ name: name }, repo, options, (err) => {
131+
// ignore for code coverage as neDB rarely returns errors even for an invalid query
132+
/* istanbul ignore if */
94133
if (err) {
95134
reject(err);
96135
} else {
@@ -101,6 +140,8 @@ export const addUserCanAuthorise = async (name: string, user: string) => {
101140
};
102141

103142
export const removeUserCanAuthorise = async (name: string, user: string) => {
143+
name = name.toLowerCase();
144+
user = user.toLowerCase();
104145
return new Promise(async (resolve, reject) => {
105146
const repo = await getRepo(name);
106147
if (!repo) {
@@ -112,6 +153,8 @@ export const removeUserCanAuthorise = async (name: string, user: string) => {
112153

113154
const options = { multi: false, upsert: false };
114155
db.update({ name: name }, repo, options, (err) => {
156+
// ignore for code coverage as neDB rarely returns errors even for an invalid query
157+
/* istanbul ignore if */
115158
if (err) {
116159
reject(err);
117160
} else {
@@ -122,6 +165,8 @@ export const removeUserCanAuthorise = async (name: string, user: string) => {
122165
};
123166

124167
export const removeUserCanPush = async (name: string, user: string) => {
168+
name = name.toLowerCase();
169+
user = user.toLowerCase();
125170
return new Promise(async (resolve, reject) => {
126171
const repo = await getRepo(name);
127172
if (!repo) {
@@ -133,6 +178,8 @@ export const removeUserCanPush = async (name: string, user: string) => {
133178

134179
const options = { multi: false, upsert: false };
135180
db.update({ name: name }, repo, options, (err) => {
181+
// ignore for code coverage as neDB rarely returns errors even for an invalid query
182+
/* istanbul ignore if */
136183
if (err) {
137184
reject(err);
138185
} else {
@@ -143,8 +190,11 @@ export const removeUserCanPush = async (name: string, user: string) => {
143190
};
144191

145192
export const deleteRepo = async (name: string) => {
193+
name = name.toLowerCase();
146194
return new Promise<void>((resolve, reject) => {
147195
db.remove({ name: name }, (err) => {
196+
// ignore for code coverage as neDB rarely returns errors even for an invalid query
197+
/* istanbul ignore if */
148198
if (err) {
149199
reject(err);
150200
} else {
@@ -156,6 +206,7 @@ export const deleteRepo = async (name: string) => {
156206

157207
export const isUserPushAllowed = async (name: string, user: string) => {
158208
name = name.toLowerCase();
209+
user = user.toLowerCase();
159210
return new Promise<boolean>(async (resolve) => {
160211
const repo = await getRepo(name);
161212
if (!repo) {
@@ -176,6 +227,7 @@ export const isUserPushAllowed = async (name: string, user: string) => {
176227

177228
export const canUserApproveRejectPushRepo = async (name: string, user: string) => {
178229
name = name.toLowerCase();
230+
user = user.toLowerCase();
179231
console.log(`checking if user ${user} can approve/reject for ${name}`);
180232
return new Promise<boolean>(async (resolve) => {
181233
const repo = await getRepo(name);

0 commit comments

Comments
 (0)