-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathpushes.ts
More file actions
127 lines (116 loc) · 3.49 KB
/
pushes.ts
File metadata and controls
127 lines (116 loc) · 3.49 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
/**
* 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 { connect, findDocuments, findOneDocument } from './helper';
import { Action } from '../../proxy/actions';
import { toClass } from '../helper';
import { PushQuery } from '../types';
import { CompletedAttestation, Rejection } from '../../proxy/processors/types';
const collectionName = 'pushes';
const defaultPushQuery: Partial<PushQuery> = {
error: false,
blocked: true,
allowPush: false,
authorised: false,
type: 'push',
};
export const getPushes = async (
query: Partial<PushQuery> = defaultPushQuery,
): Promise<Action[]> => {
return findDocuments<Action>(collectionName, query, {
projection: {
_id: 0,
id: 1,
allowPush: 1,
authorised: 1,
blocked: 1,
blockedMessage: 1,
branch: 1,
canceled: 1,
commitData: 1,
commitFrom: 1,
commitTo: 1,
error: 1,
method: 1,
project: 1,
rejected: 1,
repo: 1,
repoName: 1,
tag: 1,
tagData: 1,
timestamp: 1,
type: 1,
url: 1,
user: 1,
},
sort: { timestamp: -1 },
});
};
export const getPush = async (id: string): Promise<Action | null> => {
const doc = await findOneDocument<Action>(collectionName, { id });
return doc ? (toClass(doc, Action.prototype) as Action) : null;
};
export const deletePush = async function (id: string): Promise<void> {
const collection = await connect(collectionName);
await collection.deleteOne({ id });
};
export const writeAudit = async (action: Action): Promise<void> => {
const data = JSON.parse(JSON.stringify(action));
const options = { upsert: true };
const collection = await connect(collectionName);
delete data._id;
if (typeof data.id !== 'string') {
throw new Error('Invalid id');
}
await collection.updateOne({ id: data.id }, { $set: data }, options);
};
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}` };
};