Skip to content

Commit

Permalink
Fix formatting of github comment (#317)
Browse files Browse the repository at this point in the history
  • Loading branch information
sorenlouv authored Mar 10, 2022
1 parent a64407f commit 7b8045f
Show file tree
Hide file tree
Showing 47 changed files with 2,675 additions and 2,487 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ module.exports = {
es6: true,
node: true,
},
globals: {
NodeJS: true,
},
ignorePatterns: ['dist/', 'node_modules/'],
plugins: ['@typescript-eslint', 'jest', 'import'],
parser: '@typescript-eslint/parser', // Specifies the ESLint parser
Expand Down
15 changes: 12 additions & 3 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,25 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "TS-node",
"program": "${workspaceRoot}/node_modules/.bin/ts-node",
"args": ["--transpile-only", "${file}"],
"console": "integratedTerminal"
},
{
"type": "node",
"request": "launch",
"name": "Run backport (ts-node)",
"program": "${workspaceRoot}/src/entrypoint.cli.ts",
"runtimeArgs": ["-r", "ts-node/register/transpile-only"],
"args": [
"--branch=6.3",
"--repo-owner=backport-org",
"--repo-name=backport-demo"
"--dry-run",
"--pr=12",
"--branch=7.x",
"--repo=backport-org/repo-with-conflicts"
],
"console": "integratedTerminal"
},
Expand Down
25 changes: 12 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ The remaining documentation focuses on the Backport CLI tool (not the Github Act
npm install -g backport
```

After installation you should add an access token to the [global config](https://github.com/sqren/backport/blob/master/docs/configuration.md#global-config-backportconfigjson) in `~/.backport/config.json`. See the [documentation](https://github.com/sqren/backport/blob/master/docs/configuration.md#accesstoken-required) for how the access token is generated.
After installation you should add an access token to the [global config](https://github.com/sqren/backport/blob/main/docs/configuration.md#global-config-backportconfigjson) in `~/.backport/config.json`. See the [documentation](https://github.com/sqren/backport/blob/main/docs/configuration.md#accesstoken-required) for how the access token is generated.

## Quick start

```
npm install backport
```

Add a [project config](https://github.com/sqren/backport/blob/master/docs/configuration.md#project-config-backportrcjson) to the root of your repository:
Add a [project config](https://github.com/sqren/backport/blob/main/docs/configuration.md#project-config-backportrcjson) to the root of your repository:

```js
// .backportrc.json
Expand All @@ -60,7 +60,7 @@ Add a [project config](https://github.com/sqren/backport/blob/master/docs/config
}
```

Add personal access token to [global config](https://github.com/sqren/backport/blob/master/docs/configuration.md#global-config-backportconfigjson):
Add personal access token to [global config](https://github.com/sqren/backport/blob/main/docs/configuration.md#global-config-backportconfigjson):

```js
// ~/.backport/config.json
Expand All @@ -79,7 +79,7 @@ _This will start an interactive prompt. You can use your keyboards arrow keys to

### Config options

See [configuration.md](https://github.com/sqren/backport/blob/master/docs/configuration.md)
See [configuration.md](https://github.com/sqren/backport/blob/main/docs/configuration.md)

### CLI options

Expand Down Expand Up @@ -124,7 +124,7 @@ See [configuration.md](https://github.com/sqren/backport/blob/master/docs/config
| --help | | Show help | |
| -v, --version | | Show version number | |

The CLI options will override the [configuration options](https://github.com/sqren/backport/blob/master/docs/configuration.md).
The CLI options will override the [configuration options](https://github.com/sqren/backport/blob/main/docs/configuration.md).

## Backport Module API

Expand Down Expand Up @@ -162,14 +162,13 @@ Enabling this will disable the interactive prompts
import { backportRun } from 'backport';

const result = await backportRun({
accessToken: 'abc',
repoName: 'kibana',
repoOwner: 'elastic',
pullNumber: 121633,
ci: true,
targetPRLabels: ['backport'],
autoMerge: true,
autoMergeMethod: 'squash',
options: {
accessToken: 'very secret',
repoName: 'kibana',
repoOwner: 'elastic',
pullNumber: 121633,
ci: true,
},
});

console.log(result);
Expand Down
1 change: 1 addition & 0 deletions foo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo
24 changes: 12 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
},
"dependencies": {
"@octokit/rest": "^18.12.0",
"axios": "^0.25.0",
"axios": "^0.26.0",
"dedent": "^0.7.0",
"del": "^6.0.0",
"dotenv": "^16.0.0",
Expand All @@ -82,35 +82,35 @@
"utility-types": "^3.10.0",
"winston": "^3.6.0",
"yargs": "^17.3.1",
"yargs-parser": "^21.0.0"
"yargs-parser": "^21.0.1"
},
"devDependencies": {
"@types/core-js": "^2.5.5",
"@types/dedent": "^0.7.0",
"@types/inquirer": "^8.2.0",
"@types/jest": "^27.4.1",
"@types/lodash": "^4.14.178",
"@types/lodash": "^4.14.179",
"@types/node": "^17.0.21",
"@types/safe-json-stringify": "^1.1.2",
"@types/yargs": "^17.0.8",
"@types/yargs-parser": "^20.2.1",
"@typescript-eslint/eslint-plugin": "^5.12.1",
"@typescript-eslint/parser": "^5.12.1",
"eslint": "^8.9.0",
"eslint-config-prettier": "^8.4.0",
"@types/yargs": "^17.0.9",
"@types/yargs-parser": "^21.0.0",
"@typescript-eslint/eslint-plugin": "^5.14.0",
"@typescript-eslint/parser": "^5.14.0",
"eslint": "^8.10.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.25.4",
"eslint-plugin-jest": "^26.1.1",
"eslint-plugin-prettier": "^4.0.0",
"graphql-config": "^4.1.0",
"husky": "^7.0.4",
"jest": "^27.5.1",
"jest-snapshot-serializer-ansi": "^1.0.0",
"lint-staged": "^12.3.4",
"lint-staged": "^12.3.5",
"nock": "^13.2.4",
"prettier": "^2.5.1",
"strip-ansi": "^6.0.1",
"ts-jest": "^27.1.3",
"ts-node": "^10.5.0",
"typescript": "^4.5.5"
"ts-node": "^10.7.0",
"typescript": "^4.6.2"
}
}
119 changes: 66 additions & 53 deletions src/backportRun.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import chalk from 'chalk';
import yargsParser from 'yargs-parser';
import { ConfigFileOptions } from './options/ConfigOptions';
import { CliError } from './options/cliArgs';
import { getOptions, ValidConfigOptions } from './options/options';
import { runSequentially, Result } from './runSequentially';
import { HandledError } from './services/HandledError';
Expand All @@ -15,6 +16,12 @@ import { getTargetBranches } from './ui/getTargetBranches';
import { ora } from './ui/ora';
import { setupRepo } from './ui/setupRepo';

export type BackportAbortResponse = {
status: 'aborted';
commits: Commit[];
error: HandledError;
};

export type BackportSuccessResponse = {
status: 'success';
commits: Commit[];
Expand All @@ -30,48 +37,57 @@ export type BackportFailureResponse = {

export type BackportResponse =
| BackportSuccessResponse
| BackportFailureResponse;
| BackportFailureResponse
| BackportAbortResponse;

export async function backportRun({
processArgs,
optionsFromModule = {},
isCliMode,
exitCodeOnFailure,
}: {
processArgs: string[];
optionsFromModule?: ConfigFileOptions;
isCliMode: boolean;
exitCodeOnFailure: boolean;
}): Promise<BackportResponse> {
const argv = yargsParser(processArgs) as ConfigFileOptions;
const ci = argv.ci ?? optionsFromModule.ci;
const ls = argv.ls ?? optionsFromModule.ls;
const logFilePath = argv.logFilePath ?? optionsFromModule.logFilePath;
const logger = initLogger({ ci, logFilePath });

const spinner = ora(ci);

// don't show spinner for yargs commands that exit the process without stopping the spinner first
if (!argv.help && !argv.version && !argv.v) {
spinner.start('Initializing...');
}

let options: ValidConfigOptions | null = null;
let commits: Commit[] = [];

try {
options = await getOptions(processArgs, optionsFromModule);
logger.info('Backporting options', options);

spinner.stop();
const spinner = ora(ci);
try {
// don't show spinner for yargs commands that exit the process without stopping the spinner first
if (!argv.help && !argv.version && !argv.v) {
spinner.start('Initializing...');
}

options = await getOptions(processArgs, optionsFromModule);
logger.info('Backporting options', options);
spinner.stop();
} catch (e) {
spinner.stop();
if (e instanceof CliError) {
consoleLog(e.message);
consoleLog(`Run "backport --help" to see all options`);
return {
status: 'failure',
error: e,
errorMessage: e.message,
commits: [],
} as BackportResponse;
}
throw e;
}

commits = await getCommits(options);
logger.info('Commits', commits);

if (options.ls) {
return {
status: 'success',
commits,
results: [],
};
return { status: 'success', commits, results: [] } as BackportResponse;
}

const targetBranches = await getTargetBranches(options, commits);
Expand All @@ -95,38 +111,46 @@ export async function backportRun({
commits,
results,
};

await createStatusComment({
options,
backportResponse,
});

await createStatusComment({ options, backportResponse });
return backportResponse;
} catch (e) {
spinner.stop();
const backportResponse: BackportResponse = {
status: 'failure',
commits,
error: e,
errorMessage: e.message,
};
let backportResponse: BackportResponse;

if (
e instanceof HandledError &&
e.errorContext.code === 'no-branches-exception'
) {
backportResponse = {
status: 'aborted',
commits,
error: e,
};

if (options) {
await createStatusComment({
options,
backportResponse,
});
// this will catch both HandledError and Error
} else if (e instanceof Error) {
backportResponse = {
status: 'failure',
commits,
error: e,
errorMessage: e.message,
};
} else {
throw e;
}

if (!ls) {
outputError({ e, logFilePath });
if (options) {
await createStatusComment({ options, backportResponse });
}

logger.error('Unhandled exception', e);
if (isCliMode && isCriticalError(e)) {
outputError({ e, logFilePath });

// only change exit code for failures while in cli mode
if (exitCodeOnFailure && backportResponse.status === 'failure') {
process.exitCode = 1;
}

logger.error('Unhandled exception', e);

return backportResponse;
}
}
Expand Down Expand Up @@ -161,14 +185,3 @@ function outputError({
);
}
}

function isCriticalError(e: Error | HandledError) {
if (
e instanceof HandledError &&
e.errorContext.code === 'no-branches-exception'
) {
return false;
}

return true;
}
16 changes: 9 additions & 7 deletions src/entrypoint.cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import { ConfigFileOptions } from './entrypoint.module';
const processArgs = process.argv.slice(2);

// this is the entrypoint when running from command line
backportRun({ processArgs, isCliMode: true }).then((backportResponse) => {
const argv = yargsParser(processArgs) as ConfigFileOptions;
const { ci, ls } = argv;
if (ci || ls) {
// eslint-disable-next-line no-console
console.log(JSON.stringify(backportResponse));
backportRun({ processArgs, exitCodeOnFailure: true }).then(
(backportResponse) => {
const argv = yargsParser(processArgs) as ConfigFileOptions;
const { ci, ls } = argv;
if (ci || ls) {
// eslint-disable-next-line no-console
console.log(JSON.stringify(backportResponse));
}
}
});
);
Loading

0 comments on commit 7b8045f

Please sign in to comment.