Skip to content

Commit

Permalink
[Fix] Updated readCiCdExec error response
Browse files Browse the repository at this point in the history
  • Loading branch information
mist8kengas committed Jul 24, 2022
1 parent 03b916e commit d3c7657
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "helmsman",
"version": "1.1.0",
"version": "1.1.1",
"description": "CI/CD from GitHub to your own servers",
"main": "dist/index.js",
"scripts": {
Expand Down
9 changes: 6 additions & 3 deletions src/server/routes/webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ const router = Router();
import 'colors';
import { exec } from 'child_process';
import { createHmac, timingSafeEqual } from 'crypto';
import readCiCdExec from '../../utils/readCiCdExec.js';
import readCiCdExec, {
EXEC_RESPONSE,
EXEC_RESPONSE_MAP,
} from '../../utils/readCiCdExec.js';
import logger from '../../utils/logger.js';

// import .env
Expand Down Expand Up @@ -57,8 +60,8 @@ router.post('/', (req, _, next) => {
);
const repositoryBranchStr = `${payloadBody.repository.full_name}: ${payloadBody.ref}`;
if (ciCdExec) {
if (ciCdExec == 'bad_config') {
const logMessage = `An error occurred while executing the ${githubEvent} command for "${repositoryBranchStr}" (${githubDelivery}): ${ciCdExec}`;
if (typeof ciCdExec != 'string') {
const logMessage = `An error occurred while executing the ${githubEvent} command for "${repositoryBranchStr}" (${githubDelivery}): ${EXEC_RESPONSE_MAP[ciCdExec]}`;
console.error('[server:post]'.red, '/webhook'.gray, logMessage);
logger(logMessage, 'error');
return next(500);
Expand Down
15 changes: 13 additions & 2 deletions src/utils/readCiCdExec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import { readFileSync } from 'fs';
import logger from './logger.js';

enum EXEC_RESPONSE {
BAD_CONFIG = 1 << 0,
}
const EXEC_RESPONSE_MAP: { [index: number]: string } = {
1: 'bad_config',
};
type execResponse = EXEC_RESPONSE;

export default function readCiCdExec(
repository: string,
ref: string,
type: string
): string | undefined {
): string | EXEC_RESPONSE | undefined {
const jsonPath = './var/config/ci_cd.json';
let ciCd = undefined;

Expand All @@ -16,7 +24,7 @@ export default function readCiCdExec(
const message =
'An error occured while trying to parse "./var/config/ci_cd.json", check for invalid syntax and run the program again. (Check error logs)';
console.log('[helmsman]'.red, message, error);
return 'bad_config';
return EXEC_RESPONSE.BAD_CONFIG;
}
const [, branch] = /refs\/heads\/(.*)$/.exec(ref) || [];
const repositoryBranch = `${repository}/${branch}`;
Expand All @@ -29,3 +37,6 @@ export default function readCiCdExec(
? ciCd[repositoryBranch][type]
: undefined;
}

export { EXEC_RESPONSE, EXEC_RESPONSE_MAP };
export type { execResponse };

0 comments on commit d3c7657

Please sign in to comment.