Skip to content

feat: auto-require Postman sandbox globals during import#7878

Open
gopu-bruno wants to merge 3 commits intousebruno:mainfrom
gopu-bruno:feat/postman-native-lib-compatibility
Open

feat: auto-require Postman sandbox globals during import#7878
gopu-bruno wants to merge 3 commits intousebruno:mainfrom
gopu-bruno:feat/postman-native-lib-compatibility

Conversation

@gopu-bruno
Copy link
Copy Markdown
Collaborator

@gopu-bruno gopu-bruno commented Apr 28, 2026

Description

Fixes import failures from Postman to Bruno caused by missing sandbox globals (CryptoJS, _, moment, cheerio, tv4).
Tracked in : JIRA

Change

Adds a final AST-based pass in the Postman→Bruno translator to automatically inject the necessary require() statements. Imported collections run without manual fixes, improving migration reliability.

Contribution Checklist:

  • I've used AI significantly to create this pull request
  • The pull request only addresses one issue or adds one feature.
  • The pull request does not introduce any breaking changes
  • I have added screenshots or gifs to help explain the change if applicable.
  • I have read the contribution guidelines.
  • Create an issue and link to the pull request.

Note: Keeping the PR small and focused helps make it easier to review and merge. If you have multiple changes you want to make, please consider submitting them as separate pull requests.

Publishing to New Package Managers

Please see here for more information.

Summary by CodeRabbit

  • New Features

    • Postman converter now auto-injects imports for common Postman sandbox libraries (CryptoJS, lodash, moment, cheerio, tv4) when their usage is detected, avoids duplicates, respects scope shadowing, and places injections after any leading directive prologue in a consistent alphabetical order.
  • Tests

    • Added tests covering detection, deduplication, ordering, scoping rules, and non-injection cases for the automatic import behavior.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 28, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 88b3a019-bc13-49c9-b679-1bb01703835a

📥 Commits

Reviewing files that changed from the base of the PR and between 3694521 and 77fa647.

📒 Files selected for processing (2)
  • packages/bruno-converters/src/utils/postman-to-bruno-translator.js
  • packages/bruno-converters/tests/postman/postman-translations/transpiler-tests/postman-library-globals.test.js
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/bruno-converters/tests/postman/postman-translations/transpiler-tests/postman-library-globals.test.js

Walkthrough

Adds a translator AST pass that detects Postman sandbox globals (CryptoJS, _, moment, cheerio, tv4) used as member-expression objects or call-expression callees, and injects corresponding const <id> = require('<pkg>') declarations (sorted, deduplicated, after any directive prologue) if not shadowed.

Changes

Cohort / File(s) Summary
Translator: auto-inject requires
packages/bruno-converters/src/utils/postman-to-bruno-translator.js
New AST pass in translateCode() detects usages of Postman library globals (as member-object or callee), ensures they're not shadowed, and prepends alphabetical, deduplicated const <id> = require('<package>') declarations after any directive prologue.
Tests: library globals
packages/bruno-converters/tests/postman/postman-translations/transpiler-tests/postman-library-globals.test.js
New Jest tests validating injection for CryptoJS, moment, computed/member access cases, alphabetical ordering, deduplication when require() already present, scope-shadowing behavior, and insertion after 'use strict' prologue.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested reviewers

  • helloanoop
  • lohit-bruno
  • naman-bruno
  • bijin-bruno

Poem

In Bruno's code the globals wake,
Crypto, moment, lodash make,
Requires appear in tidy line,
Sorted, safe, and just in time —
Translating sandbox into shine ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main change: auto-requiring Postman sandbox globals during the import/translation process.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
Review rate limit: 7/8 reviews remaining, refill in 7 minutes and 30 seconds.

Comment @coderabbitai help to get the list of available commands and usage tips.

@gopu-bruno gopu-bruno marked this pull request as ready for review April 29, 2026 07:04
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🧹 Nitpick comments (1)
packages/bruno-converters/tests/postman/postman-translations/transpiler-tests/postman-library-globals.test.js (1)

16-31: Add a cheerio coverage case to close mapping/order gap.

This suite validates four globals but misses cheerio, which is part of the new auto-require feature. Add it to prevent silent mapping regressions.

Proposed test update
   it('injects multiple requires in alphabetical order', () => {
     const out = translateCode(`
       const h = CryptoJS.MD5('x').toString();
       const c = _.chunk([1,2,3], 2);
+      const $ = cheerio.load('<div/>');
       const t = moment().unix();
       const v = tv4.validate({}, {});
     `);
+    const idxCheerio = out.indexOf(`require("cheerio")`);
     const idxCrypto = out.indexOf(`require("crypto-js")`);
     const idxLodash = out.indexOf(`require("lodash")`);
     const idxMoment = out.indexOf(`require("moment")`);
     const idxTv4 = out.indexOf(`require("tv4")`);
+    expect(idxCheerio).toBeGreaterThan(-1);
     expect(idxCrypto).toBeGreaterThan(-1);
+    expect(idxCheerio).toBeLessThan(idxCrypto);
     expect(idxCrypto).toBeLessThan(idxLodash);
     expect(idxLodash).toBeLessThan(idxMoment);
     expect(idxMoment).toBeLessThan(idxTv4);
   });

As per coding guidelines "Add tests for any new functionality or meaningful changes. If code is added, removed, or significantly modified, corresponding tests should be updated or created."

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In
`@packages/bruno-converters/tests/postman/postman-translations/transpiler-tests/postman-library-globals.test.js`
around lines 16 - 31, The test "injects multiple requires in alphabetical order"
is missing coverage for cheerio; update the test that calls translateCode to
include a cheerio usage (e.g., referencing cheerio.load or similar) and add an
index check variable (like idxCheerio) using out.indexOf(`require("cheerio")`),
then assert idxLodash < idxCheerio < idxMoment (or the correct alphabetical
order among idxCrypto, idxLodash, idxCheerio, idxMoment, idxTv4) so the
expectations verify cheerio is injected and sits in the proper alphabetical
position; update the variable names and comparisons in the existing test (which
uses translateCode and idxCrypto/idxLodash/idxMoment/idxTv4) to include
idxCheerio accordingly.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@packages/bruno-converters/src/utils/postman-to-bruno-translator.js`:
- Around line 699-703: The current isLibraryUsage check rejects computed
MemberExpression access (parent.computed), so patterns like CryptoJS['MD5'] or
_['chunk'] are ignored; update the logic in the isLibraryUsage assignment to
also accept computed member accesses when the property is a literal string
(e.g., parent.property.type === 'Literal' or 'StringLiteral' depending on the
parser), e.g., treat MemberExpression as library usage if parent.object ===
path.value and (not computed OR computed with a string/constant property). Keep
the existing CallExpression check unchanged (parent.type === 'CallExpression' &&
parent.callee === path.value).
- Line 722: The current transformation uses
ast.get().value.program.body.unshift(...declarations) which prepends
declarations before any existing directive prologue (e.g. 'use strict') and thus
loses strict mode; change the insertion to detect the directive prologue and
insert declarations after it — for example, inspect
ast.get().value.program.directives (or count leading ExpressionStatement string
literals in ast.get().value.program.body), compute the insert index as the
number of directives, and use Array.prototype.splice to insert declarations at
that index instead of unshift so directives remain at the top.
- Around line 713-720: The current generation of declarations sorts
usedLibraries by identifier instead of the actual package name, causing
non-deterministic require order; update the sort to compare
POSTMAN_LIBRARY_GLOBALS entries (e.g., [...usedLibraries].sort((a,b) =>
POSTMAN_LIBRARY_GLOBALS[a].localeCompare(POSTMAN_LIBRARY_GLOBALS[b]))) so the
variable declarations array (`declarations`) is built in deterministic
package-name order; adjust the sort call where `declarations` is created
(references: declarations, usedLibraries, POSTMAN_LIBRARY_GLOBALS).

---

Nitpick comments:
In
`@packages/bruno-converters/tests/postman/postman-translations/transpiler-tests/postman-library-globals.test.js`:
- Around line 16-31: The test "injects multiple requires in alphabetical order"
is missing coverage for cheerio; update the test that calls translateCode to
include a cheerio usage (e.g., referencing cheerio.load or similar) and add an
index check variable (like idxCheerio) using out.indexOf(`require("cheerio")`),
then assert idxLodash < idxCheerio < idxMoment (or the correct alphabetical
order among idxCrypto, idxLodash, idxCheerio, idxMoment, idxTv4) so the
expectations verify cheerio is injected and sits in the proper alphabetical
position; update the variable names and comparisons in the existing test (which
uses translateCode and idxCrypto/idxLodash/idxMoment/idxTv4) to include
idxCheerio accordingly.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 6cef1592-7568-42c4-86e0-cc45b14942c0

📥 Commits

Reviewing files that changed from the base of the PR and between a688eff and 3694521.

📒 Files selected for processing (2)
  • packages/bruno-converters/src/utils/postman-to-bruno-translator.js
  • packages/bruno-converters/tests/postman/postman-translations/transpiler-tests/postman-library-globals.test.js

Comment thread packages/bruno-converters/src/utils/postman-to-bruno-translator.js Outdated
Comment thread packages/bruno-converters/src/utils/postman-to-bruno-translator.js
Comment thread packages/bruno-converters/src/utils/postman-to-bruno-translator.js Outdated
@gopu-bruno gopu-bruno marked this pull request as draft April 29, 2026 07:57
@gopu-bruno gopu-bruno marked this pull request as ready for review April 30, 2026 06:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant