Skip to content

Commit 0d4d874

Browse files
committed
refactor(ts): refactor remaining proxy files, fix imports and type issues
1 parent 3d0a486 commit 0d4d874

File tree

10 files changed

+207
-68
lines changed

10 files changed

+207
-68
lines changed

package-lock.json

Lines changed: 109 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@
8282
"@babel/preset-react": "^7.22.5",
8383
"@commitlint/cli": "^19.0.0",
8484
"@commitlint/config-conventional": "^19.0.0",
85+
"@types/express": "^5.0.1",
86+
"@types/express-http-proxy": "^1.6.6",
8587
"@types/lodash": "^4.17.15",
8688
"@types/mocha": "^10.0.10",
8789
"@types/node": "^22.13.5",

src/config/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ let _api: Record<string, unknown> = defaultSettings.api;
1818
let _cookieSecret: string = defaultSettings.cookieSecret;
1919
let _sessionMaxAgeHours: number = defaultSettings.sessionMaxAgeHours;
2020
let _plugins: any[] = defaultSettings.plugins;
21-
let _commitConfig: Record<string, unknown> = defaultSettings.commitConfig;
21+
let _commitConfig: Record<string, any> = defaultSettings.commitConfig;
2222
let _attestationConfig: Record<string, unknown> = defaultSettings.attestationConfig;
2323
let _privateOrganizations: string[] = defaultSettings.privateOrganizations;
2424
let _urlShortener: string = defaultSettings.urlShortener;

src/db/file/repo.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,12 @@ export const deleteRepo = async (name: string) => {
157157
export const isUserPushAllowed = async (name: string, user: string) => {
158158
name = name.toLowerCase();
159159
return new Promise<boolean>(async (resolve) => {
160-
const repo = await exports.getRepo(name);
160+
const repo = await getRepo(name);
161+
if (!repo) {
162+
resolve(false);
163+
return;
164+
}
165+
161166
console.log(repo.users.canPush);
162167
console.log(repo.users.canAuthorise);
163168

@@ -173,7 +178,12 @@ export const canUserApproveRejectPushRepo = async (name: string, user: string) =
173178
name = name.toLowerCase();
174179
console.log(`checking if user ${user} can approve/reject for ${name}`);
175180
return new Promise<boolean>(async (resolve) => {
176-
const repo = await exports.getRepo(name);
181+
const repo = await getRepo(name);
182+
if (!repo) {
183+
resolve(false);
184+
return;
185+
}
186+
177187
if (repo.users.canAuthorise.includes(user)) {
178188
console.log(`user ${user} can approve/reject to repo ${name}`);
179189
resolve(true);

src/proxy/chain.js renamed to src/proxy/chain.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { PluginLoader } from '../plugin';
2+
import { Action } from './actions';
13
import * as proc from './processors';
24

35
const pushActionChain = [
@@ -20,14 +22,14 @@ const pullActionChain = [proc.push.checkRepoInAuthorisedList];
2022

2123
let pluginsInserted = false;
2224

23-
const executeChain = async (req) => {
24-
let action;
25+
export const executeChain = async (req: any, res: any): Promise<Action> => {
26+
let action: Action;
2527
try {
2628
action = await proc.pre.parseAction(req);
2729
const actions = await getChain(action);
2830
for (const i in actions) {
2931
if (!i) continue;
30-
const fn = actions[i];
32+
const fn = actions[i as any];
3133

3234
action = await fn(req, action);
3335
if (!action.continue()) {
@@ -39,7 +41,7 @@ const executeChain = async (req) => {
3941
}
4042
}
4143
} finally {
42-
await proc.push.audit(req, action);
44+
await proc.push.audit(req, action!!);
4345
}
4446

4547
return action;
@@ -49,9 +51,9 @@ const executeChain = async (req) => {
4951
* The plugin loader used for the GitProxy chain.
5052
* @type {import('../plugin').PluginLoader}
5153
*/
52-
let chainPluginLoader;
54+
let chainPluginLoader: PluginLoader;
5355

54-
const getChain = async (action) => {
56+
const getChain = async (action: Action) => {
5557
if (chainPluginLoader === undefined) {
5658
console.error(
5759
'Plugin loader was not initialized! This is an application error. Please report it to the GitProxy maintainers. Skipping plugins...',
@@ -65,12 +67,12 @@ const getChain = async (action) => {
6567
for (const pluginObj of chainPluginLoader.pushPlugins) {
6668
console.log(`Inserting push plugin ${pluginObj.constructor.name} into chain`);
6769
// insert custom functions after parsePush but before other actions
68-
pushActionChain.splice(1, 0, pluginObj.exec);
70+
pushActionChain.splice(1, 0, pluginObj.exec as any);
6971
}
7072
for (const pluginObj of chainPluginLoader.pullPlugins) {
7173
console.log(`Inserting pull plugin ${pluginObj.constructor.name} into chain`);
7274
// insert custom functions before other pull actions
73-
pullActionChain.splice(0, 0, pluginObj.exec);
75+
pullActionChain.splice(0, 0, pluginObj.exec as any);
7476
}
7577
// This is set to true so that we don't re-insert the plugins into the chain
7678
pluginsInserted = true;
@@ -84,7 +86,7 @@ const getChain = async (action) => {
8486
if (action.type === 'default') return [];
8587
};
8688

87-
module.exports = {
89+
export default {
8890
set chainPluginLoader(loader) {
8991
chainPluginLoader = loader;
9092
},
Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,52 @@
1-
const express = require('express');
2-
const bodyParser = require('body-parser');
3-
const http = require('http');
4-
const https = require('https');
5-
const fs = require('fs');
6-
const path = require('path');
7-
const router = require('./routes').router;
8-
const config = require('../config');
9-
const db = require('../db');
10-
const { PluginLoader } = require('../plugin');
11-
const chain = require('./chain');
1+
import express from 'express';
2+
import bodyParser from 'body-parser';
3+
import http from 'http';
4+
import https from 'https';
5+
import fs from 'fs';
6+
import path from 'path';
7+
import { router } from './routes';
8+
import {
9+
getAuthorisedList,
10+
getPlugins,
11+
getSSLCertPath,
12+
getSSLKeyPath
13+
} from '../config';
14+
import {
15+
addUserCanAuthorise,
16+
addUserCanPush,
17+
createRepo,
18+
getRepos
19+
} from '../db';
20+
import { PluginLoader } from '../plugin';
21+
import chain from './chain';
22+
import { Repo } from '../db/types';
23+
1224
const { GIT_PROXY_SERVER_PORT: proxyHttpPort, GIT_PROXY_HTTPS_SERVER_PORT: proxyHttpsPort } =
1325
require('../config/env').serverConfig;
1426

1527
const options = {
1628
inflate: true,
1729
limit: '100000kb',
1830
type: '*/*',
19-
key: fs.readFileSync(path.join(__dirname, config.getSSLKeyPath())),
20-
cert: fs.readFileSync(path.join(__dirname, config.getSSLCertPath())),
31+
key: fs.readFileSync(path.join(__dirname, getSSLKeyPath())),
32+
cert: fs.readFileSync(path.join(__dirname, getSSLCertPath())),
2133
};
2234

2335
const proxyPreparations = async () => {
24-
const plugins = config.getPlugins();
36+
const plugins = getPlugins();
2537
const pluginLoader = new PluginLoader(plugins);
2638
await pluginLoader.load();
2739
chain.chainPluginLoader = pluginLoader;
2840
// Check to see if the default repos are in the repo list
29-
const defaultAuthorisedRepoList = config.getAuthorisedList();
30-
const allowedList = await db.getRepos();
41+
const defaultAuthorisedRepoList = getAuthorisedList();
42+
const allowedList: Repo[] = await getRepos();
3143

3244
defaultAuthorisedRepoList.forEach(async (x) => {
3345
const found = allowedList.find((y) => y.project === x.project && x.name === y.name);
3446
if (!found) {
35-
await db.createRepo(x);
36-
await db.addUserCanPush(x.name, 'admin');
37-
await db.addUserCanAuthorise(x.name, 'admin');
47+
await createRepo(x);
48+
await addUserCanPush(x.name, 'admin');
49+
await addUserCanAuthorise(x.name, 'admin');
3850
}
3951
});
4052
};
@@ -51,7 +63,7 @@ const createApp = async () => {
5163
const start = async () => {
5264
const app = await createApp();
5365
await proxyPreparations();
54-
http.createServer(options, app).listen(proxyHttpPort, () => {
66+
http.createServer(options as any, app).listen(proxyHttpPort, () => {
5567
console.log(`HTTP Proxy Listening on ${proxyHttpPort}`);
5668
});
5769
https.createServer(options, app).listen(proxyHttpsPort, () => {
@@ -61,6 +73,8 @@ const start = async () => {
6173
return app;
6274
};
6375

64-
module.exports.proxyPreparations = proxyPreparations;
65-
module.exports.createApp = createApp;
66-
module.exports.start = start;
76+
export {
77+
proxyPreparations,
78+
createApp,
79+
start
80+
};

src/proxy/processors/push-action/index.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
import { exec as parsePush } from './parsePush.js';
2-
import { exec as preReceive } from './preReceive.js';
3-
import { exec as checkRepoInAuthorisedList } from './checkRepoInAuthorisedList.js';
4-
import { exec as audit } from './audit.js';
5-
import { exec as pullRemote } from './pullRemote.js';
6-
import { exec as writePack } from './writePack.js';
7-
import { exec as getDiff } from './getDiff.js';
8-
import { exec as scanDiff } from './scanDiff.js';
9-
import { exec as blockForAuth } from './blockForAuth.js';
10-
import { exec as checkIfWaitingAuth } from './checkIfWaitingAuth.js';
11-
import { exec as checkCommitMessages } from './checkCommitMessages.js';
12-
import { exec as checkAuthorEmails } from './checkAuthorEmails.js';
13-
import { exec as checkUserPushPermission } from './checkUserPushPermission.js';
14-
import { exec as clearBareClone } from './clearBareClone.js';
1+
import { exec as parsePush } from './parsePush';
2+
import { exec as preReceive } from './preReceive';
3+
import { exec as checkRepoInAuthorisedList } from './checkRepoInAuthorisedList';
4+
import { exec as audit } from './audit';
5+
import { exec as pullRemote } from './pullRemote';
6+
import { exec as writePack } from './writePack';
7+
import { exec as getDiff } from './getDiff';
8+
import { exec as scanDiff } from './scanDiff';
9+
import { exec as blockForAuth } from './blockForAuth';
10+
import { exec as checkIfWaitingAuth } from './checkIfWaitingAuth';
11+
import { exec as checkCommitMessages } from './checkCommitMessages';
12+
import { exec as checkAuthorEmails } from './checkAuthorEmails';
13+
import { exec as checkUserPushPermission } from './checkUserPushPermission';
14+
import { exec as clearBareClone } from './clearBareClone';
1515

1616
export {
1717
parsePush,

0 commit comments

Comments
 (0)