Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace ci with interactive and add publishStatusCommentOn* #319

Merged
merged 15 commits into from
Mar 19, 2022
11 changes: 3 additions & 8 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,18 @@
{
"type": "node",
"request": "launch",
"name": "TS-node",
"name": "Run file",
"program": "${workspaceRoot}/node_modules/.bin/ts-node",
"args": ["--transpile-only", "${file}"],
"console": "integratedTerminal"
},
{
"type": "node",
"request": "launch",
"name": "Run backport (ts-node)",
"name": "Run entrypoint.cli.ts",
"program": "${workspaceRoot}/src/entrypoint.cli.ts",
"runtimeArgs": ["-r", "ts-node/register/transpile-only"],
"args": [
"--dry-run",
"--pr=12",
"--branch=7.x",
"--repo=backport-org/repo-with-conflicts"
],
"args": ["--no-fork", "--pr=96", "--repo=backport-org/backport-demo"],
"console": "integratedTerminal"
},
{
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@ See [configuration.md](https://github.com/sqren/backport/blob/main/docs/configur
| --author | | Filter commits by Github username. Opposite of `--all` | _Current user_ |
| --auto-assign | | Assign current user to the target PR | false |
| --branch | -b | Target branch to backport to | |
| --ci | | Disable interactive prompts | false |
| --config-file | | Custom path to project config file (.backportrc.json) | |
| --dir | | Clone repository into custom directory | ~/.backport/repositories/ |
| --dry-run | | Perform backport without pushing to Github | false |
| --editor | | Editor (eg. `code`) to open and resolve conflicts | nano |
| --fork | | Create backports in fork repo | true |
| --git-hostname | | Hostname for Git | github.com |
| --interactive | | Enable interactive prompts | true |
| --mainline | | Parent id of merge commit | 1 |
| --max-number | --number, -n | Number of commits to choose from | 10 |
| --multiple | | Multi-select for commits and branches | false |
Expand Down Expand Up @@ -153,8 +153,8 @@ Filter commits by pull request number
`sha` _string_<br/>
Filter commits by commit sha

`ci` _boolean_<br/>
Enabling this will disable the interactive prompts
`interactive` _boolean_<br/>
Enable interactive prompts

#### Example

Expand All @@ -167,7 +167,7 @@ const result = await backportRun({
repoName: 'kibana',
repoOwner: 'elastic',
pullNumber: 121633,
ci: true,
interactive: false,
},
});

Expand Down
20 changes: 17 additions & 3 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -418,17 +418,31 @@ Config:
}
```

#### `publishStatusComment`
#### `publishStatusCommentOnSuccess`

Whether to publish a status comment to Github with the results of the backport or not
Publish a status comment to the source pull request if all backports succeeded

Default: `true`

Config:

```json
{
"publishStatusComment": false
"publishStatusCommentOnSuccess": false
}
```

#### `publishStatusCommentOnFailure`

Publish a status comment to the source pull request if some backports failed

Default: `true`

Config:

```json
{
"publishStatusCommentOnFailure": false
}
```

Expand Down
2 changes: 1 addition & 1 deletion jest.config.all.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ const config = require('./jest.config');
module.exports = {
...config,

modulePathIgnorePatterns: [],
modulePathIgnorePatterns: ['.*/_tmp_sandbox_/.*$'],
testSequencer: './jest.testSequencer.js',
};
2 changes: 1 addition & 1 deletion jest.config.mutation.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ module.exports = {

// only include "mutation" tests that cannot run on in parallel (like they are on CI) because they mutate shared state
testRegex: ['.*.mutation.test.ts$'],
modulePathIgnorePatterns: [],
modulePathIgnorePatterns: ['.*/_tmp_sandbox_/.*$'],
};
2 changes: 1 addition & 1 deletion jest.config.private.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ module.exports = {

// only include (private) tests that cannot run on CI because they require credentials and thus exclude external contributors
testRegex: ['.*.private.test.ts$'],
modulePathIgnorePatterns: [],
modulePathIgnorePatterns: ['.*/_tmp_sandbox_/.*$'],
};
22 changes: 11 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"lint-and-test": "yarn tsc && yarn lint && yarn test-all",
"lint": "echo \"Running lint\" && eslint './**/*.{ts,js}'",
"start": "ts-node --transpile-only ./src/entrypoint.cli.ts",
"test-all": "jest --config ./jest.config.all.js",
"test-all": "rm -rf src/test/_tmp_sandbox_ && jest --config ./jest.config.all.js",
"test-mutation": "jest --config ./jest.config.mutation.js",
"test-private": "jest --config ./jest.config.private.js",
"test": "jest",
Expand Down Expand Up @@ -65,14 +65,14 @@
},
"dependencies": {
"@octokit/rest": "^18.12.0",
"axios": "^0.26.0",
"axios": "^0.26.1",
"dedent": "^0.7.0",
"del": "^6.0.0",
"dotenv": "^16.0.0",
"find-up": "^5.0.0",
"graphql": "^16.3.0",
"graphql-tag": "^2.12.6",
"inquirer": "^8.2.0",
"inquirer": "^8.2.1",
"lodash": "^4.17.21",
"make-dir": "^3.1.0",
"ora": "^5.4.1",
Expand All @@ -81,22 +81,22 @@
"terminal-link": "^2.1.1",
"utility-types": "^3.10.0",
"winston": "^3.6.0",
"yargs": "^17.3.1",
"yargs": "^17.4.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.179",
"@types/lodash": "^4.14.180",
"@types/node": "^17.0.21",
"@types/safe-json-stringify": "^1.1.2",
"@types/yargs": "^17.0.9",
"@types/yargs": "^17.0.10",
"@types/yargs-parser": "^21.0.0",
"@typescript-eslint/eslint-plugin": "^5.14.0",
"@typescript-eslint/parser": "^5.14.0",
"eslint": "^8.10.0",
"@typescript-eslint/eslint-plugin": "^5.15.0",
"@typescript-eslint/parser": "^5.15.0",
"eslint": "^8.11.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.25.4",
"eslint-plugin-jest": "^26.1.1",
Expand All @@ -105,9 +105,9 @@
"husky": "^7.0.4",
"jest": "^27.5.1",
"jest-snapshot-serializer-ansi": "^1.0.0",
"lint-staged": "^12.3.5",
"lint-staged": "^12.3.7",
"nock": "^13.2.4",
"prettier": "^2.5.1",
"prettier": "^2.6.0",
"strip-ansi": "^6.0.1",
"ts-jest": "^27.1.3",
"ts-node": "^10.7.0",
Expand Down
102 changes: 55 additions & 47 deletions src/backportRun.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
import chalk from 'chalk';
import yargsParser from 'yargs-parser';
import { BackportError } from './lib/BackportError';
import { getLogfilePath } from './lib/env';
import { getCommits } from './lib/getCommits';
import { getGitConfigAuthor } from './lib/getGitConfigAuthor';
import { getTargetBranches } from './lib/getTargetBranches';
import { createStatusComment } from './lib/github/v3/createStatusComment';
import { GithubV4Exception } from './lib/github/v4/apiRequestV4';
import { consoleLog, initLogger } from './lib/logger';
import { ora } from './lib/ora';
import { setupRepo } from './lib/setupRepo';
import { Commit } from './lib/sourceCommit/parseSourceCommit';
import { ConfigFileOptions } from './options/ConfigOptions';
import { CliError } from './options/cliArgs';
import {
getEarlyArguments,
getOptionsFromCliArgs,
OptionsFromCliArgs,
} from './options/cliArgs';
import { getOptions, ValidConfigOptions } from './options/options';
import { runSequentially, Result } from './runSequentially';
import { HandledError } from './services/HandledError';
import { getLogfilePath } from './services/env';
import { createStatusComment } from './services/github/v3/createStatusComment';
import { GithubV4Exception } from './services/github/v4/apiRequestV4';
import { consoleLog, initLogger } from './services/logger';
import { Commit } from './services/sourceCommit/parseSourceCommit';
import { getCommits } from './ui/getCommits';
import { getGitConfigAuthor } from './ui/getGitConfigAuthor';
import { getTargetBranches } from './ui/getTargetBranches';
import { ora } from './ui/ora';
import { setupRepo } from './ui/setupRepo';

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

export type BackportSuccessResponse = {
Expand All @@ -31,7 +35,7 @@ export type BackportSuccessResponse = {
export type BackportFailureResponse = {
status: 'failure';
commits: Commit[];
error: Error | HandledError;
error: Error | BackportError;
errorMessage: string;
};

Expand All @@ -49,39 +53,41 @@ export async function backportRun({
optionsFromModule?: ConfigFileOptions;
exitCodeOnFailure: boolean;
}): Promise<BackportResponse> {
const argv = yargsParser(processArgs) as ConfigFileOptions;
const ci = argv.ci ?? optionsFromModule.ci;
const logFilePath = argv.logFilePath ?? optionsFromModule.logFilePath;
const logger = initLogger({ ci, logFilePath });
const { interactive, logFilePath } = getEarlyArguments(
processArgs,
optionsFromModule
);
const logger = initLogger({ interactive, logFilePath });

let optionsFromCliArgs: OptionsFromCliArgs;
try {
optionsFromCliArgs = getOptionsFromCliArgs(processArgs);
} catch (e) {
if (e instanceof Error) {
consoleLog(e.message);
consoleLog(`Run "backport --help" to see all options`);
return {
status: 'failure',
error: e,
errorMessage: e.message,
commits: [],
} as BackportResponse;
}

throw e;
}

let options: ValidConfigOptions | null = null;
let commits: Commit[] = [];
const spinner = ora(interactive).start('Initializing...');

try {
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;
}
options = await getOptions({
optionsFromCliArgs,
optionsFromModule,
});
logger.info('Backporting options', options);
spinner.stop();

commits = await getCommits(options);
logger.info('Commits', commits);
Expand Down Expand Up @@ -114,19 +120,21 @@ export async function backportRun({
await createStatusComment({ options, backportResponse });
return backportResponse;
} catch (e) {
spinner.stop();
let backportResponse: BackportResponse;

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

// this will catch both HandledError and Error
// this will catch both BackportError and Error
} else if (e instanceof Error) {
backportResponse = {
status: 'failure',
Expand Down Expand Up @@ -159,10 +167,10 @@ function outputError({
e,
logFilePath,
}: {
e: HandledError | GithubV4Exception<any> | Error;
e: BackportError | GithubV4Exception<any> | Error;
logFilePath?: string;
}) {
if (e instanceof HandledError || e instanceof GithubV4Exception) {
if (e instanceof BackportError || e instanceof GithubV4Exception) {
consoleLog(e.message);
return;
}
Expand Down
9 changes: 4 additions & 5 deletions src/entrypoint.cli.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import yargsParser from 'yargs-parser';
import { backportRun } from './backportRun';
import { ConfigFileOptions } from './entrypoint.module';
import { getEarlyArguments } from './options/cliArgs';
const processArgs = process.argv.slice(2);

// this is the entrypoint when running from command line
backportRun({ processArgs, exitCodeOnFailure: true }).then(
(backportResponse) => {
const argv = yargsParser(processArgs) as ConfigFileOptions;
const { ci, ls } = argv;
if (ci || ls) {
const { interactive, ls } = getEarlyArguments(processArgs);

if (!interactive || ls) {
// eslint-disable-next-line no-console
console.log(JSON.stringify(backportResponse));
}
Expand Down
Loading