-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathpushes.ts
More file actions
170 lines (155 loc) · 4.88 KB
/
pushes.ts
File metadata and controls
170 lines (155 loc) · 4.88 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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/**
* Copyright 2026 GitProxy Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import _ from 'lodash';
import Datastore from '@seald-io/nedb';
import { Action } from '../../proxy/actions/Action';
import { toClass, buildSearchFilter, buildSort } from '../helper';
import { paginatedFind } from './helper';
import { PaginatedResult, PaginationOptions, PushQuery } from '../types';
import { CompletedAttestation, Rejection } from '../../proxy/processors/types';
import { handleErrorAndLog } from '../../utils/errors';
const COMPACTION_INTERVAL = 1000 * 60 * 60 * 24; // once per day
// export for testing purposes
export let db: Datastore;
if (process.env.NODE_ENV === 'test') {
db = new Datastore({ inMemoryOnly: true, autoload: true });
} else {
db = new Datastore({ filename: './.data/db/pushes.db', autoload: true });
}
try {
db.ensureIndex({ fieldName: 'id', unique: true });
} catch (error: unknown) {
handleErrorAndLog(
error,
'Failed to build a unique index of push id values. Please check your database file for duplicate entries or delete the duplicate through the UI and restart. ',
);
}
db.setAutocompactionInterval(COMPACTION_INTERVAL);
const defaultPushQuery: Partial<PushQuery> = {
error: false,
blocked: true,
allowPush: false,
authorised: false,
type: 'push',
};
export const getPushes = (
query: Partial<PushQuery>,
pagination?: PaginationOptions,
): Promise<PaginatedResult<Action>> => {
if (!query) query = defaultPushQuery;
const baseQuery = buildSearchFilter(
{ ...query },
['repo', 'branch', 'commitTo', 'user'],
pagination?.search,
);
const sort = buildSort(pagination, 'timestamp', -1, [
'timestamp',
'repo',
'branch',
'commitTo',
'user',
]);
const skip = pagination?.skip ?? 0;
const limit = pagination?.limit ?? 0;
return paginatedFind<Action>(db, baseQuery, sort, skip, limit).then(({ data, total }) => ({
data: _.chain(data)
.map((x) => toClass(x, Action.prototype))
.value(),
total,
}));
};
export const getPush = async (id: string): Promise<Action | null> => {
return new Promise<Action | null>((resolve, reject) => {
db.findOne({ id: id }, (err, doc) => {
// ignore for code coverage as neDB rarely returns errors even for an invalid query
/* istanbul ignore if */
if (err) {
reject(err);
} else {
if (!doc) {
resolve(null);
} else {
resolve(toClass(doc, Action.prototype));
}
}
});
});
};
export const deletePush = async (id: string): Promise<void> => {
return new Promise<void>((resolve, reject) => {
db.remove({ id }, (err) => {
// ignore for code coverage as neDB rarely returns errors even for an invalid query
/* istanbul ignore if */
if (err) {
reject(err);
} else {
resolve();
}
});
});
};
export const writeAudit = async (action: Action): Promise<void> => {
return new Promise((resolve, reject) => {
const options = { multi: false, upsert: true };
db.update({ id: action.id }, action, options, (err) => {
// ignore for code coverage as neDB rarely returns errors even for an invalid query
/* istanbul ignore if */
if (err) {
reject(err);
} else {
resolve();
}
});
});
};
export const authorise = async (
id: string,
attestation?: CompletedAttestation,
): Promise<{ message: string }> => {
const action = await getPush(id);
if (!action) {
throw new Error(`push ${id} not found`);
}
action.authorised = true;
action.canceled = false;
action.rejected = false;
action.attestation = attestation;
await writeAudit(action);
return { message: `authorised ${id}` };
};
export const reject = async (id: string, rejection: Rejection): Promise<{ message: string }> => {
const action = await getPush(id);
if (!action) {
throw new Error(`push ${id} not found`);
}
action.authorised = false;
action.canceled = false;
action.rejected = true;
action.rejection = rejection;
await writeAudit(action);
return { message: `reject ${id}` };
};
export const cancel = async (id: string): Promise<{ message: string }> => {
const action = await getPush(id);
if (!action) {
throw new Error(`push ${id} not found`);
}
action.authorised = false;
action.canceled = true;
action.rejected = false;
await writeAudit(action);
return { message: `canceled ${id}` };
};