forked from finos/git-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpushes.ts
More file actions
285 lines (262 loc) · 8.97 KB
/
Copy pathpushes.ts
File metadata and controls
285 lines (262 loc) · 8.97 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/**
* 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 { activityPrimaryStatusFromFlags } from '../../activity/activityPrimaryStatus';
import { canonicalRemoteUrl } from '../../activity/canonicalRemoteUrl';
import { Action } from '../../proxy/actions';
import { CompletedAttestation, Rejection } from '../../proxy/processors/types';
import { toClass } from '../helper';
import {
emptyRepoActivityTabCounts,
PushQuery,
RepoActivityTabCounts,
RepoPushRollupsByCanonicalUrl,
} from '../types';
import { query } from './helper';
const defaultPushQuery: Partial<PushQuery> = {
error: false,
blocked: true,
allowPush: false,
authorised: false,
type: 'push',
};
// Columns that mirror Action fields used to filter `getPushes` results.
// Anything not in this map is ignored — the API only filters by these.
const FILTER_COLUMNS: Record<string, string> = {
error: 'error',
blocked: 'blocked',
allowPush: 'allow_push',
authorised: 'authorised',
canceled: 'canceled',
rejected: 'rejected',
type: 'type',
};
const rowToAction = (row: { data: unknown }): Action =>
toClass(row.data, Action.prototype) as Action;
function bumpCount(
m: Map<string, RepoActivityTabCounts>,
canonicalKey: string,
tab: keyof RepoActivityTabCounts,
): void {
if (!canonicalKey) {
return;
}
let row = m.get(canonicalKey);
if (!row) {
row = emptyRepoActivityTabCounts();
m.set(canonicalKey, row);
}
row[tab] += 1;
}
function bumpMaxTimestampMs(
m: Map<string, number>,
canonicalKey: string,
timestamp: unknown,
): void {
if (!canonicalKey) {
return;
}
// `timestamp` is a BIGINT column, which node-postgres returns as a string.
// Anything else (null, undefined, empty) is not a usable timestamp.
const ts =
typeof timestamp === 'number'
? timestamp
: typeof timestamp === 'string' && timestamp.trim() !== ''
? Number(timestamp)
: NaN;
if (!Number.isFinite(ts)) {
return;
}
const prev = m.get(canonicalKey);
if (prev === undefined || ts > prev) {
m.set(canonicalKey, ts);
}
}
/**
* Scan all push rows: tab counts and max timestamps per canonical remote URL
* (matches the Activity UI). The URL lives inside the `data` JSONB payload, and
* canonicalization happens in Node so the result matches the mongo and fs
* backends exactly.
*/
export const getRepoPushRollupsByCanonicalUrl =
async (): Promise<RepoPushRollupsByCanonicalUrl> => {
const result = await query<{
url: string | null;
error: boolean;
rejected: boolean;
canceled: boolean;
authorised: boolean;
blocked: boolean;
allow_push: boolean;
timestamp: string | number | null;
}>(
`SELECT data->>'url' AS url, error, rejected, canceled, authorised, blocked,
allow_push, timestamp
FROM pushes
WHERE type = 'push'`,
);
const tabCounts = new Map<string, RepoActivityTabCounts>();
const latestPendingReviewAtMs = new Map<string, number>();
const latestPushAtMs = new Map<string, number>();
for (const row of result.rows) {
const url = typeof row.url === 'string' ? row.url : '';
const key = canonicalRemoteUrl(url);
if (!key) {
continue;
}
const tab = activityPrimaryStatusFromFlags({
error: row.error === true,
rejected: row.rejected === true,
canceled: row.canceled === true,
authorised: row.authorised === true,
blocked: row.blocked === true,
allowPush: row.allow_push === true,
});
bumpCount(tabCounts, key, tab);
bumpMaxTimestampMs(latestPushAtMs, key, row.timestamp);
if (tab === 'pending') {
bumpMaxTimestampMs(latestPendingReviewAtMs, key, row.timestamp);
}
}
return { tabCounts, latestPendingReviewAtMs, latestPushAtMs };
};
/**
* Pushes shown on a user profile: those the user made (any known email variant)
* plus those they reviewed. Mirrors `buildUserProfilePushFilter`, which the
* mongo and fs backends feed to their query engines; the reviewer match is
* case-insensitive on the exact username.
*/
export const getPushesForUserProfile = async (
emailVariants: string[],
profileUsername: string,
): Promise<Action[]> => {
const reviewerClause = `lower(data->'attestation'->'reviewer'->>'username') = lower($1)`;
const values: unknown[] = [profileUsername];
let predicate = reviewerClause;
if (emailVariants.length > 0) {
values.push(emailVariants);
predicate = `((data->>'userEmail') = ANY($${values.length}::text[]) OR ${reviewerClause})`;
}
const result = await query<{ data: unknown }>(
`SELECT data - 'steps' AS data FROM pushes WHERE type = 'push' AND ${predicate} ORDER BY timestamp DESC`,
values,
);
return result.rows.map(rowToAction);
};
// List queries drop `steps` from the returned document: it holds the full diff
// (largest part of a push row) and the mongo backend's list projection excludes
// it as well. The push-detail path (`getPush`) still returns the whole document.
export const getPushes = async (q: Partial<PushQuery> = defaultPushQuery): Promise<Action[]> => {
const clauses: string[] = [];
const values: unknown[] = [];
for (const [key, value] of Object.entries(q)) {
const column = FILTER_COLUMNS[key];
if (!column || value === undefined) continue;
values.push(value);
clauses.push(`${column} = $${values.length}`);
}
const where = clauses.length > 0 ? `WHERE ${clauses.join(' AND ')}` : '';
const result = await query<{ data: unknown }>(
`SELECT data - 'steps' AS data FROM pushes ${where} ORDER BY timestamp DESC`,
values,
);
return result.rows.map(rowToAction);
};
export const getPush = async (id: string): Promise<Action | null> => {
const result = await query<{ data: unknown }>(`SELECT data FROM pushes WHERE id = $1`, [id]);
if (result.rowCount === 0) return null;
return rowToAction(result.rows[0]);
};
export const deletePush = async (id: string): Promise<void> => {
await query(`DELETE FROM pushes WHERE id = $1`, [id]);
};
export const writeAudit = async (action: Action): Promise<void> => {
if (typeof action.id !== 'string') {
throw new Error('Invalid id');
}
// Round-trip through JSON to drop class identity / mongo-specific _id fields
// before persisting (mirrors mongo's `JSON.parse(JSON.stringify(action))`).
const data = JSON.parse(JSON.stringify(action));
delete data._id;
await query(
`INSERT INTO pushes (
id, timestamp, type, error, blocked, allow_push,
authorised, canceled, rejected, data
)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10::jsonb)
ON CONFLICT (id) DO UPDATE SET
timestamp = EXCLUDED.timestamp,
type = EXCLUDED.type,
error = EXCLUDED.error,
blocked = EXCLUDED.blocked,
allow_push = EXCLUDED.allow_push,
authorised = EXCLUDED.authorised,
canceled = EXCLUDED.canceled,
rejected = EXCLUDED.rejected,
data = EXCLUDED.data`,
[
action.id,
action.timestamp ?? Date.now(),
action.type ?? null,
action.error ?? false,
action.blocked ?? false,
action.allowPush ?? false,
action.authorised ?? false,
action.canceled ?? false,
action.rejected ?? false,
JSON.stringify(data),
],
);
};
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;
// Preserve the existing rejection-payload shape used by the fs/mongo
// backends — the issue calls this out explicitly as a must-fix.
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}` };
};