Repository: Redocly/redocly-cli
Pull request #1419 (2024-01-31 15:09:38)

fix: resolve an issue with options in the push command
## What/Why/How?

Fixed an issue with the `push` command, when `destination` option does not work without specifying it in `redocly.yaml`.

## Reference

## Testing

## Screenshots (optional)

## Has code been changed?

- [x] Tested with redoc/reference-docs/workflows (internal)
- [x] All new/updated code is covered with tests

## Security

- [x] Security impact of change has been considered
- [x] Code follows company security practices and guidelines


changeset-bot[bot]: ###  🦋  Changeset detected

Latest commit: fd2638a60851945d12a98dab99ae6d4f8b649629

**The changes in this PR will be included in the next version bump.**

<details><summary>This PR includes changesets to release 2 packages</summary>

  | Name                  | Type  |
| --------------------- | ----- |
| @redocly/cli          | Patch |
| @redocly/openapi-core | Patch |

</details>

Not sure what this means? [Click here  to learn what changesets are](https://github.com/changesets/changesets/blob/main/docs/adding-a-changeset.md).

[Click here if you're a maintainer who wants to add another changeset to this PR](https://github.com/Redocly/redocly-cli/new/fix/push-destination?filename=.changeset/tame-flies-carry.md&value=---%0A%22%40redocly%2Fcli%22%3A%20patch%0A---%0A%0Afix%3A%20resolve%20an%20issue%20with%20options%20in%20the%20push%20command%0A)


github-actions[bot]: | Command | Mean [s] | Min [s] | Max [s] | Relative |
|:---|---:|---:|---:|---:|
| `redocly lint packages/core/src/benchmark/benches/rebilly.yaml` | 1.060 ± 0.011 | 1.040 | 1.075 | 1.00 |
| `redocly-next lint packages/core/src/benchmark/benches/rebilly.yaml` | 1.075 ± 0.019 | 1.054 | 1.117 | 1.01 ± 0.02 |

<!-- thollander/actions-comment-pull-request "hyperfine-comparison" -->
github-actions[bot]: <!-- jest coverage report action for options with hash 3339ae53a38c44382c0fb4491bdb282d -->

## Coverage report 



| <div title="Status of coverage:&#10; 🟢 - ok&#10; 🟡 - slightly more than threshold&#10; 🔴 - under the threshold">St.<sup>:grey_question:</sup></div> | Category   | Percentage | Covered / Total |
| :----------------------------------------------------------------------------------------------------------------------------------------------------: | :--------- | :--------- | :-------------: |
|                                                                           🟡                                                                           | Statements | 76.16%     |    4299/5645    |
|                                                                           🟡                                                                           | Branches   | 65.68%     |    2228/3392    |
|                                                                           🟡                                                                           | Functions  | 68.81%     |     695/1010    |
|                                                                           🟡                                                                           | Lines      | 76.37%     |    4042/5293    |


<details><summary>Show files with reduced coverage 🔻</summary>
<br/>

| <div title="Status of coverage:&#10; 🟢 - ok&#10; 🟡 - slightly more than threshold&#10; 🔴 - under the threshold">St.<sup>:grey_question:</sup></div> | File                                                        | Statements                                                     | Branches                                                       | Functions | Lines                                                          |
| :----------------------------------------------------------------------------------------------------------------------------------------------------: | :---------------------------------------------------------- | :------------------------------------------------------------- | :------------------------------------------------------------- | :-------- | :------------------------------------------------------------- |
|                                                                           🟡                                                                           | <div title="cli/src/commands/push.ts">`...` / push.ts</div> | <div title="Base coverage is: 73.81%">72.67% (-1.14% 🔻)</div> | <div title="Base coverage is: 67.46%">65.25% (-2.21% 🔻)</div> | 54.55%    | <div title="Base coverage is: 76.25%">75.16% (-1.09% 🔻)</div> |

</details>

## Test suite run success
696 tests passing in 100 suites.

<p align="right">Report generated by <a href="https://github.com/ArtiomTr/jest-coverage-report-action">🧪jest coverage report action</a> from fd2638a60851945d12a98dab99ae6d4f8b649629</p>


Patch:
diff --git a/.changeset/thin-badgers-worry.md b/.changeset/thin-badgers-worry.md
new file mode 100644
index 00000000..fce569ce
--- /dev/null
+++ b/.changeset/thin-badgers-worry.md
@@ -0,0 +1,5 @@
+---
+"@redocly/cli": patch
+---
+
+Fixed an issue with the `push` command, when `destination` option does not work without specifying it in `redocly.yaml`.
diff --git a/packages/cli/src/commands/push.ts b/packages/cli/src/commands/push.ts
index 0b9bb5b3..1be48c16 100644
--- a/packages/cli/src/commands/push.ts
+++ b/packages/cli/src/commands/push.ts
@@ -55,7 +55,7 @@ export function commonPushHandler({
   if (project && mountPath) {
     return handleCMSPush;
   }
-  return handlePush;
+  return transformPush(handlePush);
 }
 
 export async function handlePush(argv: PushOptions, config: Config): Promise<void> {
@@ -354,8 +354,7 @@ export function getDestinationProps(
 }
 
 type BarePushArgs = Omit<PushOptions, 'destination' | 'branchName'> & {
-  maybeDestination?: string;
-  maybeBranchName?: string;
+  apis?: string[];
   branch?: string;
   destination?: string;
 };
@@ -364,9 +363,7 @@ export const transformPush =
   (callback: typeof handlePush) =>
   (
     {
-      api: maybeApiOrDestination,
-      maybeDestination,
-      maybeBranchName,
+      apis,
       branch,
       'batch-id': batchId,
       'job-id': jobId,
@@ -374,6 +371,8 @@ export const transformPush =
     }: BarePushArgs & { 'batch-id'?: string },
     config: Config
   ) => {
+    const [maybeApiOrDestination, maybeDestination, maybeBranchName] = apis || [];
+
     if (batchId) {
       process.stderr.write(
         yellow(
@@ -410,12 +409,10 @@ export const transformPush =
       apiFile = maybeApiOrDestination;
     }
 
-    destination = rest.destination || destination;
-
     return callback(
       {
         ...rest,
-        destination,
+        destination: rest.destination ?? destination,
         api: apiFile,
         branchName: branch ?? maybeBranchName,
         'job-id': jobId || batchId,


Test patch:
diff --git a/packages/cli/src/__tests__/commands/push.test.ts b/packages/cli/src/__tests__/commands/push.test.ts
index 8926a771..c4b6bd87 100644
--- a/packages/cli/src/__tests__/commands/push.test.ts
+++ b/packages/cli/src/__tests__/commands/push.test.ts
@@ -283,8 +283,7 @@ describe('transformPush', () => {
     const cb = jest.fn();
     transformPush(cb)(
       {
-        api: 'openapi.yaml',
-        maybeDestination: '@testing_org/main@v1',
+        apis: ['openapi.yaml', '@testing_org/main@v1'],
       },
       {} as any
     );
@@ -300,9 +299,7 @@ describe('transformPush', () => {
     const cb = jest.fn();
     transformPush(cb)(
       {
-        api: 'openapi.yaml',
-        maybeDestination: '@testing_org/main@v1',
-        maybeBranchName: 'other',
+        apis: ['openapi.yaml', '@testing_org/main@v1', 'other'],
       },
       {} as any
     );
@@ -319,9 +316,7 @@ describe('transformPush', () => {
     const cb = jest.fn();
     transformPush(cb)(
       {
-        api: 'openapi.yaml',
-        maybeDestination: '@testing_org/main@v1',
-        maybeBranchName: 'other',
+        apis: ['openapi.yaml', '@testing_org/main@v1', 'other'],
         branch: 'priority-branch',
       },
       {} as any
@@ -339,13 +334,13 @@ describe('transformPush', () => {
     const cb = jest.fn();
     transformPush(cb)(
       {
-        api: '@testing_org/main@v1',
+        apis: ['main@v1'],
       },
       {} as any
     );
     expect(cb).toBeCalledWith(
       {
-        destination: '@testing_org/main@v1',
+        destination: 'main@v1',
       },
       {}
     );
@@ -354,7 +349,7 @@ describe('transformPush', () => {
     const cb = jest.fn();
     transformPush(cb)(
       {
-        api: 'test.yaml',
+        apis: ['test.yaml'],
       },
       {} as any
     );
@@ -365,31 +360,32 @@ describe('transformPush', () => {
       {}
     );
   });
-  it('should accept aliases for the old syntax', () => {
+
+  it('should use destination from option', () => {
     const cb = jest.fn();
     transformPush(cb)(
       {
-        api: 'alias',
-        maybeDestination: '@testing_org/main@v1',
+        apis: ['test.yaml', 'test@v1'],
+        destination: 'main@v1',
       },
       {} as any
     );
     expect(cb).toBeCalledWith(
       {
-        destination: '@testing_org/main@v1',
-        api: 'alias',
+        destination: 'main@v1',
+        api: 'test.yaml',
       },
       {}
     );
   });
+
   it('should use --job-id option firstly', () => {
     const cb = jest.fn();
     transformPush(cb)(
       {
         'batch-id': 'b-123',
         'job-id': 'j-123',
-        api: 'test',
-        maybeDestination: 'main@v1',
+        apis: ['test'],
         branch: 'test',
         destination: 'main@v1',
       },