Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 57 additions & 50 deletions packages/git-proxy-cli/index.js → packages/git-proxy-cli/index.ts
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#!/usr/bin/env node
const axios = require('axios');
const yargs = require('yargs/yargs');
const { hideBin } = require('yargs/helpers');
const fs = require('fs');
const util = require('util');
import axios from 'axios';
import yargs from 'yargs/yargs';
import { hideBin } from 'yargs/helpers';
import fs from 'fs';
import util from 'util';

import { CommitData, PushData, PushFilters } from './types';

const GIT_PROXY_COOKIE_FILE = 'git-proxy-cookie';
// GitProxy UI HOST and PORT (configurable via environment variable)
Expand All @@ -19,7 +21,7 @@ axios.defaults.timeout = 30000;
* @param {string} username The user name to login with
* @param {string} password The password to use for the login
*/
async function login(username, password) {
async function login(username: string, password: string) {
try {
let response = await axios.post(
`${baseUrl}/api/auth/login`,
Expand All @@ -44,7 +46,7 @@ async function login(username, password) {
const user = `"${response.data.username}" <${response.data.email}>`;
const isAdmin = response.data.admin ? ' (admin)' : '';
console.log(`Login ${user}${isAdmin}: OK`);
} catch (error) {
} catch (error: any) {
if (error.response) {
console.error(`Error: Login '${username}': '${error.response.status}'`);
process.exitCode = 1;
Expand Down Expand Up @@ -76,7 +78,7 @@ async function login(username, password) {
* @param {boolean} filters.rejected - If not null, filters for pushes with
* given attribute and status.
*/
async function getGitPushes(filters) {
async function getGitPushes(filters: PushFilters) {
if (!fs.existsSync(GIT_PROXY_COOKIE_FILE)) {
console.error('Error: List: Authentication required');
process.exitCode = 1;
Expand All @@ -91,40 +93,46 @@ async function getGitPushes(filters) {
params: filters,
});

const records = [];
response.data?.forEach((push) => {
const record = {};
record.id = push.id;
record.timestamp = push.timestamp;
record.url = push.url;
record.allowPush = push.allowPush;
record.authorised = push.authorised;
record.blocked = push.blocked;
record.canceled = push.canceled;
record.error = push.error;
record.rejected = push.rejected;

record.lastStep = {
stepName: push.lastStep?.stepName,
error: push.lastStep?.error,
errorMessage: push.lastStep?.errorMessage,
blocked: push.lastStep?.blocked,
blockedMessage: push.lastStep?.blockedMessage,
const records: PushData[] = [];
response.data.forEach((push: PushData) => {
const record: PushData = {
id: push.id,
timestamp: push.timestamp,
url: push.url,
allowPush: push.allowPush,
authorised: push.authorised,
blocked: push.blocked,
canceled: push.canceled,
error: push.error,
rejected: push.rejected,
};

record.commitData = [];
push.commitData?.forEach((pushCommitDataRecord) => {
record.commitData.push({
message: pushCommitDataRecord.message,
committer: pushCommitDataRecord.committer,
if (push.lastStep) {
record.lastStep = {
stepName: push.lastStep?.stepName,
error: push.lastStep?.error,
errorMessage: push.lastStep?.errorMessage,
blocked: push.lastStep?.blocked,
blockedMessage: push.lastStep?.blockedMessage,
};
}

if (push.commitData) {
const commitData: CommitData[] = [];
push.commitData.forEach((pushCommitDataRecord: CommitData) => {
commitData.push({
message: pushCommitDataRecord.message,
committer: pushCommitDataRecord.committer,
});
});
});
record.commitData = commitData;
}

records.push(record);
});

console.log(`${util.inspect(records, false, null, false)}`);
} catch (error) {
} catch (error: any) {
// default error
const errorMessage = `Error: List: '${error.message}'`;
process.exitCode = 2;
Expand All @@ -136,7 +144,7 @@ async function getGitPushes(filters) {
* Authorise git push by ID
* @param {string} id The ID of the git push to authorise
*/
async function authoriseGitPush(id) {
async function authoriseGitPush(id: string) {
if (!fs.existsSync(GIT_PROXY_COOKIE_FILE)) {
console.error('Error: Authorise: Authentication required');
process.exitCode = 1;
Expand Down Expand Up @@ -168,16 +176,15 @@ async function authoriseGitPush(id) {
);

console.log(`Authorise: ID: '${id}': OK`);
} catch (error) {
} catch (error: any) {
// default error
let errorMessage = `Error: Authorise: '${error.message}'`;
process.exitCode = 2;

if (error.response) {
switch (error.response.status) {
case 401:
errorMessage =
'Error: Authorise: Authentication required (401): ' + error?.response?.data?.message;
errorMessage = 'Error: Authorise: Authentication required';
process.exitCode = 3;
break;
case 404:
Expand All @@ -193,7 +200,7 @@ async function authoriseGitPush(id) {
* Reject git push by ID
* @param {string} id The ID of the git push to reject
*/
async function rejectGitPush(id) {
async function rejectGitPush(id: string) {
if (!fs.existsSync(GIT_PROXY_COOKIE_FILE)) {
console.error('Error: Reject: Authentication required');
process.exitCode = 1;
Expand All @@ -216,16 +223,15 @@ async function rejectGitPush(id) {
);

console.log(`Reject: ID: '${id}': OK`);
} catch (error) {
} catch (error: any) {
// default error
let errorMessage = `Error: Reject: '${error.message}'`;
process.exitCode = 2;

if (error.response) {
switch (error.response.status) {
case 401:
errorMessage =
'Error: Reject: Authentication required (401): ' + error?.response?.data?.message;
errorMessage = 'Error: Reject: Authentication required';
process.exitCode = 3;
break;
case 404:
Expand All @@ -241,7 +247,7 @@ async function rejectGitPush(id) {
* Cancel git push by ID
* @param {string} id The ID of the git push to cancel
*/
async function cancelGitPush(id) {
async function cancelGitPush(id: string) {
if (!fs.existsSync(GIT_PROXY_COOKIE_FILE)) {
console.error('Error: Cancel: Authentication required');
process.exitCode = 1;
Expand All @@ -264,16 +270,15 @@ async function cancelGitPush(id) {
);

console.log(`Cancel: ID: '${id}': OK`);
} catch (error) {
} catch (error: any) {
// default error
let errorMessage = `Error: Cancel: '${error.message}'`;
process.exitCode = 2;

if (error.response) {
switch (error.response.status) {
case 401:
errorMessage =
'Error: Cancel: Authentication required (401): ' + error?.response?.data?.message;
errorMessage = 'Error: Cancel: Authentication required';
process.exitCode = 3;
break;
case 404:
Expand Down Expand Up @@ -302,7 +307,7 @@ async function logout() {
headers: { Cookie: cookies },
},
);
} catch (error) {
} catch (error: any) {
console.log(`Warning: Logout: '${error.message}'`);
}
}
Expand All @@ -326,7 +331,7 @@ async function reloadConfig() {
await axios.post(`${baseUrl}/api/v1/admin/reload-config`, {}, { headers: { Cookie: cookies } });

console.log('Configuration reloaded successfully');
} catch (error) {
} catch (error: any) {
const errorMessage = `Error: Reload config: '${error.message}'`;
process.exitCode = 2;
console.error(errorMessage);
Expand Down Expand Up @@ -465,8 +470,10 @@ yargs(hideBin(process.argv)) // eslint-disable-line @typescript-eslint/no-unused
})
.command({
command: 'reload-config',
description: 'Reload GitProxy configuration without restarting',
action: reloadConfig,
describe: 'Reload GitProxy configuration without restarting',
handler() {
reloadConfig();
},
})
.demandCommand(1, 'You need at least one command before moving on')
.strict()
Expand Down
10 changes: 7 additions & 3 deletions packages/git-proxy-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
"name": "@finos/git-proxy-cli",
"version": "0.1.0",
"description": "Command line interface tool for FINOS GitProxy.",
"bin": "./index.js",
"bin": {
"git-proxy-cli": "./dist/index.js"
},
"dependencies": {
"axios": "^1.11.0",
"yargs": "^17.7.2",
Expand All @@ -12,8 +14,10 @@
"chai": "^4.5.0"
},
"scripts": {
"lint": "eslint --fix . --ext .js,.jsx",
"test": "NODE_ENV=test ts-mocha --exit --timeout 10000",
"build": "tsc",
"lint": "eslint \"./*.ts\" --fix",
"test:dev": "NODE_ENV=test ts-mocha test/*.ts --exit --timeout 10000",
"test": "npm run build && NODE_ENV=test ts-mocha test/*.ts --exit --timeout 10000",
"test-coverage": "nyc npm run test",
"test-coverage-ci": "nyc --reporter=lcovonly --reporter=text --reporter=html npm run test"
},
Expand Down
Loading
Loading