Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/bruno-app/src/utils/collections/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1148,6 +1148,7 @@ export const getGlobalEnvironmentVariables = ({ globalEnvironments, activeGlobal
}
});
}
variables.__name__ = environment?.name;
return variables;
};

Expand Down
48 changes: 31 additions & 17 deletions packages/bruno-converters/src/postman/postman-translations.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,35 @@ import translateCode from '../utils/postman-to-bruno-translator';
// Currently these APIs only work within the request lifecycle but fail to update the UI tables.
// e.g., setCollectionVar only sets the variable in the request lifecycle, fails to update the table in the UI.
const replacements = {
'pm\\.environment\\.get\\(': 'bru.getEnvVar(',
'pm\\.environment\\.set\\(': 'bru.setEnvVar(',
'pm\\.variables\\.get\\(': 'bru.getVar(',
'pm\\.variables\\.set\\(': 'bru.setVar(',
// Environment variables
'pm\\.environment\\.get\\(': 'bru.environment.get(',
'pm\\.environment\\.set\\(': 'bru.environment.set(',
'pm\\.environment\\.has\\(': 'bru.environment.has(',
'pm\\.environment\\.unset\\(': 'bru.environment.unset(',
'pm\\.environment\\.replaceIn\\(': 'bru.interpolate(',
'pm\\.environment\\.toObject\\(': 'bru.environment.toObject(',
'pm\\.environment\\.clear\\(': 'bru.environment.clear(',
'pm\\.environment\\.name': 'bru.environment.name',

// Runtime variables
'pm\\.variables\\.get\\(': 'bru.variables.get(',
'pm\\.variables\\.set\\(': 'bru.variables.set(',
'pm\\.variables\\.has\\(': 'bru.variables.has(',
'pm\\.variables\\.unset\\(': 'bru.variables.unset(',
'pm\\.variables\\.replaceIn\\(': 'bru.interpolate(',
'pm\\.variables\\.toObject\\(': 'bru.variables.toObject(',
'pm\\.variables\\.clear\\(': 'bru.variables.clear(',

// Global variables
'pm\\.globals\\.get\\(': 'bru.globals.get(',
'pm\\.globals\\.set\\(': 'bru.globals.set(',
'pm\\.globals\\.has\\(': 'bru.globals.has(',
// 'pm\\.globals\\.unset\\(': 'bru.globals.unset(', // TODO: Re-enable once UI sync issue is resolved
'pm\\.globals\\.replaceIn\\(': 'bru.interpolate(',
'pm\\.globals\\.toObject\\(': 'bru.globals.toObject(',
// 'pm\\.globals\\.clear\\(': 'bru.globals.clear(', // TODO: Re-enable once UI sync issue is resolved

// Collection variables
'pm\\.collectionVariables\\.get\\(': 'bru.getCollectionVar(',
// 'pm\\.collectionVariables\\.set\\(': 'bru.setCollectionVar(',
'pm\\.collectionVariables\\.has\\(': 'bru.hasCollectionVar(',
Expand All @@ -21,19 +45,10 @@ const replacements = {
'pm\\.response\\.to\\.have\\.status\\(': 'expect(res.getStatus()).to.equal(',
'pm\\.response\\.json\\(': 'res.getBody(',
'pm\\.expect\\(': 'expect(',
'pm\\.environment\\.has\\(([^)]+)\\)': 'bru.getEnvVar($1) !== undefined && bru.getEnvVar($1) !== null',
'pm\\.response\\.code': 'res.getStatus()',
'pm\\.response\\.text\\(\\)': 'JSON.stringify(res.getBody())',
'pm\\.expect\\.fail\\(': 'expect.fail(',
'pm\\.response\\.responseTime': 'res.getResponseTime()',
'pm\\.globals\\.set\\(': 'bru.setGlobalEnvVar(',
'pm\\.globals\\.get\\(': 'bru.getGlobalEnvVar(',
// 'pm\\.globals\\.unset\\(': 'bru.deleteGlobalEnvVar(',
'pm\\.globals\\.toObject\\(': 'bru.getAllGlobalEnvVars(',
// 'pm\\.globals\\.clear\\(': 'bru.deleteAllGlobalEnvVars(',
'pm\\.environment\\.toObject\\(': 'bru.getAllEnvVars(',
'pm\\.environment\\.clear\\(': 'bru.deleteAllEnvVars(',
'pm\\.variables\\.toObject\\(': 'bru.getAllVars(',
'pm\\.request\\.headers\\.remove\\(': 'req.deleteHeader(',
'pm\\.response\\.headers\\.get\\(': 'res.getHeader(',
'pm\\.response\\.to\\.have\\.jsonSchema\\(': 'expect(res.getBody()).to.have.jsonSchema(',
Expand All @@ -51,7 +66,6 @@ const replacements = {
'pm\\.response\\.responseSize': 'res.getSize().body',
'pm\\.response\\.size\\(\\)\\.header': 'res.getSize().header',
'pm\\.response\\.size\\(\\)\\.total': 'res.getSize().total',
'pm\\.environment\\.name': 'bru.getEnvName()',
'pm\\.response\\.status': 'res.statusText',
'pm\\.response\\.headers': 'res.getHeaders()',
'tests\\[\'([^\']+)\'\\]\\s*=\\s*([^;]+);': 'test("$1", function() { expect(Boolean($2)).to.be.true; });',
Expand Down Expand Up @@ -81,9 +95,9 @@ const replacements = {
'request\\.body': 'req.getBody()',
'request\\.name': 'req.getName()',
// deprecated translations
'postman\\.setEnvironmentVariable\\(': 'bru.setEnvVar(',
'postman\\.getEnvironmentVariable\\(': 'bru.getEnvVar(',
'postman\\.clearEnvironmentVariable\\(': 'bru.deleteEnvVar(',
'postman\\.setEnvironmentVariable\\(': 'bru.environment.set(',
'postman\\.getEnvironmentVariable\\(': 'bru.environment.get(',
'postman\\.clearEnvironmentVariable\\(': 'bru.environment.unset(',
'pm\\.execution\\.skipRequest\\(\\)': 'bru.runner.skipRequest()',
'pm\\.execution\\.skipRequest': 'bru.runner.skipRequest',
'pm\\.execution\\.setNextRequest\\(null\\)': 'bru.runner.stopExecution()',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,32 @@ const cloneDeep = require('lodash/cloneDeep');
// e.g., setCollectionVar only sets the variable in the request lifecycle, fails to update the table in the UI.
const simpleTranslations = {
// Global Variables
'pm.globals.get': 'bru.getGlobalEnvVar',
'pm.globals.set': 'bru.setGlobalEnvVar',
'pm.globals.get': 'bru.globals.get',
'pm.globals.set': 'bru.globals.set',
'pm.globals.has': 'bru.globals.has',
// 'pm.globals.unset': 'bru.globals.unset', // TODO: Re-enable once UI sync issue is resolved
'pm.globals.replaceIn': 'bru.interpolate',
// 'pm.globals.unset': 'bru.deleteGlobalEnvVar',
'pm.globals.toObject': 'bru.getAllGlobalEnvVars',
// 'pm.globals.clear': 'bru.deleteAllGlobalEnvVars',
'pm.globals.toObject': 'bru.globals.toObject',
// 'pm.globals.clear': 'bru.globals.clear', // TODO: Re-enable once UI sync issue is resolved

// Environment variables
'pm.environment.get': 'bru.getEnvVar',
'pm.environment.set': 'bru.setEnvVar',
'pm.environment.name': 'bru.getEnvName()',
'pm.environment.unset': 'bru.deleteEnvVar',
'pm.environment.get': 'bru.environment.get',
'pm.environment.set': 'bru.environment.set',
'pm.environment.has': 'bru.environment.has',
'pm.environment.name': 'bru.environment.name',
'pm.environment.unset': 'bru.environment.unset',
'pm.environment.replaceIn': 'bru.interpolate',
'pm.environment.toObject': 'bru.getAllEnvVars',
'pm.environment.clear': 'bru.deleteAllEnvVars',
'pm.environment.toObject': 'bru.environment.toObject',
'pm.environment.clear': 'bru.environment.clear',

// Variables
'pm.variables.get': 'bru.getVar',
'pm.variables.set': 'bru.setVar',
'pm.variables.has': 'bru.hasVar',
'pm.variables.toObject': 'bru.getAllVars',
'pm.variables.get': 'bru.variables.get',
'pm.variables.set': 'bru.variables.set',
'pm.variables.has': 'bru.variables.has',
'pm.variables.unset': 'bru.variables.unset',
'pm.variables.toObject': 'bru.variables.toObject',
'pm.variables.replaceIn': 'bru.interpolate',
'pm.variables.clear': 'bru.variables.clear',
// Collection variables
'pm.collectionVariables.get': 'bru.getCollectionVar',
// 'pm.collectionVariables.set': 'bru.setCollectionVar',
Expand Down Expand Up @@ -121,9 +125,9 @@ const simpleTranslations = {
'pm.execution.skipRequest': 'bru.runner.skipRequest',

// Legacy Postman API (deprecated) (we can use pm instead of postman, as we are converting all postman references to pm in the code as the part of pre-processing)
'pm.setEnvironmentVariable': 'bru.setEnvVar',
'pm.getEnvironmentVariable': 'bru.getEnvVar',
'pm.clearEnvironmentVariable': 'bru.deleteEnvVar',
'pm.setEnvironmentVariable': 'bru.environment.set',
'pm.getEnvironmentVariable': 'bru.environment.get',
'pm.clearEnvironmentVariable': 'bru.environment.unset',

// Legacy response properties
'responseCode.code': 'res.getStatus()',
Expand All @@ -144,31 +148,6 @@ const complexTransformations = [
transform: sendRequestTransformer
},

// pm.environment.has requires special handling
{
pattern: 'pm.environment.has',
transform: (path, j) => {
const callExpr = path.parent.value;

const args = callExpr.arguments;

// Create: bru.getEnvVar(arg) !== undefined && bru.getEnvVar(arg) !== null
return j.logicalExpression(
'&&',
j.binaryExpression(
'!==',
j.callExpression(j.identifier('bru.getEnvVar'), args),
j.identifier('undefined')
),
j.binaryExpression(
'!==',
j.callExpression(j.identifier('bru.getEnvVar'), args),
j.identifier('null')
)
);
}
},

{
pattern: 'pm.response.text',
transform: (_, j) => {
Expand Down Expand Up @@ -289,30 +268,6 @@ const complexTransformations = [
}
},

// pm.globals.has requires special handling
{
pattern: 'pm.globals.has',
transform: (path, j) => {
const callExpr = path.parent.value;
const args = callExpr.arguments;

// Create: bru.getGlobalEnvVar(arg) !== undefined && bru.getGlobalEnvVar(arg) !== null
return j.logicalExpression(
'&&',
j.binaryExpression(
'!==',
j.callExpression(j.identifier('bru.getGlobalEnvVar'), args),
j.identifier('undefined')
),
j.binaryExpression(
'!==',
j.callExpression(j.identifier('bru.getGlobalEnvVar'), args),
j.identifier('null')
)
);
}
},

// pm.request.headers.add({key, value}) -> req.setHeader(key, value)
{
pattern: 'pm.request.headers.add',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('postmanTranslations - comment handling', () => {
`;
const result = postmanTranslation(inputScript);
expect(result).toContain('console.log(\'This script does not contain pm commands.\');');
expect(result).toContain('const data = bru.getEnvVar(\'key\');');
expect(result).toContain('const data = bru.environment.get(\'key\');');
});

// TODO: Restore once UI update fixes are live for setCollectionVar
Expand All @@ -27,7 +27,7 @@ describe('postmanTranslations - comment handling', () => {

test('should handle multiple pm commands on the same line', () => {
const inputScript = 'pm.environment.get(\'key\'); pm.environment.set(\'key\', \'value\');';
const expectedOutput = 'bru.getEnvVar(\'key\'); bru.setEnvVar(\'key\', \'value\');';
const expectedOutput = 'bru.environment.get(\'key\'); bru.environment.set(\'key\', \'value\');';
expect(postmanTranslation(inputScript)).toBe(expectedOutput);
});

Expand All @@ -45,11 +45,11 @@ describe('postmanTranslations - comment handling', () => {
const expectedOutput = `
// This is a comment
const value = 'test';
bru.setEnvVar('key', value);
bru.environment.set('key', value);
/*
Multi-line comment
*/
const result = bru.getEnvVar('key');
const result = bru.environment.get('key');
console.log('Result:', result);
`;
expect(postmanTranslation(inputScript)).toBe(expectedOutput);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ describe('postmanTranslations - edge cases', () => {
const expectedOutput = `
const sampleObjects = [
{
key: bru.getEnvVar('key'),
value: bru.getVar('value')
key: bru.environment.get('key'),
value: bru.variables.get('value')
},
{
key: bru.getCollectionVar('key'),
Expand All @@ -35,11 +35,11 @@ describe('postmanTranslations - edge cases', () => {
];
const dataTesting = Object.entries(sampleObjects || {}).reduce((acc, [key, value]) => {
// this is a comment
acc[key] = bru.getCollectionVar(bru.getEnvVar(value));
acc[key] = bru.getCollectionVar(bru.environment.get(value));
return acc; // Return the accumulator
}, {});
Object.values(dataTesting).forEach((data) => {
bru.setEnvVar(data.key, bru.getVar(data.value));
bru.environment.set(data.key, bru.variables.get(data.value));
});
`;
expect(postmanTranslation(inputScript)).toBe(expectedOutput);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ describe('postmanTranslations - variables commands', () => {
pm.environment.set('key', 'value');
`;
const result = postmanTranslation(inputScript);
expect(result).toContain('bru.getEnvVar(\'key\')');
expect(result).toContain('bru.setEnvVar(\'key\', \'value\')');
expect(result).toContain('bru.environment.get(\'key\')');
expect(result).toContain('bru.environment.set(\'key\', \'value\')');
});

test('should translate runtime variable commands', () => {
Expand All @@ -17,8 +17,8 @@ describe('postmanTranslations - variables commands', () => {
pm.variables.set('key', 'value');
`;
const result = postmanTranslation(inputScript);
expect(result).toContain('bru.getVar(\'key\')');
expect(result).toContain('bru.setVar(\'key\', \'value\')');
expect(result).toContain('bru.variables.get(\'key\')');
expect(result).toContain('bru.variables.set(\'key\', \'value\')');
});

test('should translate pm.collectionVariables.get', () => {
Expand All @@ -30,7 +30,7 @@ describe('postmanTranslations - variables commands', () => {
test('should translate pm.expect with pm.environment.has', () => {
const inputScript = 'pm.expect(pm.environment.has(\'key\')).to.be.true;';
const result = postmanTranslation(inputScript);
expect(result).toContain('bru.getEnvVar(\'key\') !== undefined && bru.getEnvVar(\'key\') !== null');
expect(result).toContain('bru.environment.has(\'key\')');
expect(result).toContain('.to.be.true');
});

Expand Down
Loading
Loading