Skip to content

Commit

Permalink
Add status comment (#281)
Browse files Browse the repository at this point in the history
  • Loading branch information
sorenlouv authored Dec 27, 2021
1 parent d3c9fea commit 601d9df
Show file tree
Hide file tree
Showing 24 changed files with 1,042 additions and 1,063 deletions.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
"strip-json-comments": "^3.1.1",
"terminal-link": "^2.1.1",
"winston": "^3.3.3",
"yargs": "^17.3.0"
"yargs": "^17.3.1"
},
"devDependencies": {
"@types/core-js": "^2.5.5",
Expand All @@ -81,10 +81,10 @@
"@types/lodash": "^4.14.178",
"@types/node": "^16.11.13",
"@types/safe-json-stringify": "^1.1.2",
"@types/yargs": "^17.0.7",
"@types/yargs": "^17.0.8",
"@types/yargs-parser": "^20.2.1",
"@typescript-eslint/eslint-plugin": "^5.7.0",
"@typescript-eslint/parser": "^5.7.0",
"@typescript-eslint/eslint-plugin": "^5.8.0",
"@typescript-eslint/parser": "^5.8.0",
"eslint": "^8.5.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-import": "^2.25.3",
Expand All @@ -96,7 +96,7 @@
"husky": "^7.0.4",
"jest": "^27.4.5",
"jest-snapshot-serializer-ansi": "^1.0.0",
"lint-staged": "^12.1.3",
"lint-staged": "^12.1.4",
"nock": "^13.2.1",
"prettier": "^2.5.1",
"strip-ansi": "^6.0.1",
Expand Down
85 changes: 51 additions & 34 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,74 @@
import chalk from 'chalk';
import { ConfigOptions } from './options/ConfigOptions';
import { getOptions } from './options/options';
import { runWithOptions, Result } from './runWithOptions';
import { getOptions, ValidConfigOptions } from './options/options';
import { runSequentially, Result } from './runSequentially';
import { HandledError } from './services/HandledError';
import { getLogfilePath } from './services/env';
import { initLogger, consoleLog, redact } from './services/logger';
import { createStatusComment } from './services/github/v3/createStatusComment';
import { initLogger, consoleLog } from './services/logger';
import { Commit } from './services/sourceCommit/parseSourceCommit';
import { getCommits } from './ui/getCommits';
import { getTargetBranches } from './ui/getTargetBranches';

export type BackportResponse = {
success: boolean;
results: Result[];
errorMessage?: string;
error?: Error;
};
export type BackportResponse =
| {
status: 'success';
commits: Commit[];
results: Result[];
}
| {
status: 'failure';
commits: Commit[];
errorMessage: string;
error: Error;
};

export async function main(
argv: string[],
optionsFromModule?: ConfigOptions
): Promise<BackportResponse> {
const logger = initLogger();
let options: ValidConfigOptions | null = null;
let commits: Commit[] = [];

try {
const options = await getOptions(argv, optionsFromModule);
const results = await runWithOptions(options);
return {
success: results.every((res) => res.success),
options = await getOptions(argv, optionsFromModule);
commits = await getCommits(options);
const targetBranches = await getTargetBranches(options, commits);
const results = await runSequentially({ options, commits, targetBranches });
const backportResponse: BackportResponse = {
status: 'success',
commits,
results,
};

await createStatusComment({
options,
backportResponse,
});

return backportResponse;
} catch (e) {
const backportResponse: BackportResponse = {
status: 'failure',
commits,
errorMessage: e.message,
error: e,
};

if (options?.ci) {
await createStatusComment({
options,
backportResponse,
});
}

if (e instanceof HandledError) {
consoleLog(e.message);

return {
success: false,
results: [],
errorMessage: redact(e.message),
error: e,
};
} else if (e instanceof Error) {
// output
consoleLog('\n');
consoleLog(chalk.bold('⚠️ Ouch! An unknown error occured 😿'));
consoleLog(chalk.bold('⚠️ Ouch! An unhandled error occured 😿'));
consoleLog(`Error message: ${e.message}`);

consoleLog(
Expand All @@ -52,20 +81,8 @@ export async function main(

// log file
logger.info('Unknown error:', e);

return {
success: false,
results: [],
errorMessage: `An unhandled error occurred: ${redact(e.message)}`,
error: e,
};
}

return {
success: false,
results: [],
errorMessage: 'Unknown error',
error: new Error('Unknown error'),
};
return backportResponse;
}
}
14 changes: 12 additions & 2 deletions src/options/cliArgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export function getOptionsFromCliArgs(
.option('cherrypickRef', {
description: 'Append commit message with "(cherry picked from commit...)',
type: 'boolean',
conflicts: ['noCherrypickRef'],
})

.option('details', {
Expand All @@ -88,6 +89,7 @@ export function getOptionsFromCliArgs(
.option('fork', {
description: 'Create backports in fork or origin repo',
type: 'boolean',
conflicts: ['noFork'],
})

.option('gitHostname', {
Expand Down Expand Up @@ -160,13 +162,20 @@ export function getOptionsFromCliArgs(
description:
'Do not append commit message with "(cherry picked from commit...)"',
type: 'boolean',
conflicts: ['cherrypickRef'],
})

.option('noVerify', {
description: 'Bypass the pre-commit and commit-msg hooks',
type: 'boolean',
})

.option('noFork', {
description: 'Create backports in the origin repo',
type: 'boolean',
conflicts: ['fork'],
})

.option('path', {
description: 'Only list commits touching files under the specified path',
alias: 'p',
Expand Down Expand Up @@ -304,8 +313,8 @@ export function getOptionsFromCliArgs(
// negations
verify,
noVerify,
cherrypickRef,
noCherrypickRef,
noFork,

// array types (should be renamed to plural form)
assignee,
Expand Down Expand Up @@ -336,7 +345,8 @@ export function getOptionsFromCliArgs(
targetPRLabels: targetPRLabel,

// negations (cli-only flags)
cherrypickRef: noCherrypickRef ? false : cherrypickRef,
cherrypickRef: noCherrypickRef === true ? false : restOptions.cherrypickRef,
fork: noFork === true ? false : restOptions.fork,
noVerify: verify ?? noVerify,
});
}
Expand Down
Loading

0 comments on commit 601d9df

Please sign in to comment.