Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ import {
addTransientDirectory,
addSaveTransientRequestModal,
updatePathParam,
toggleCollection
migrateCollectionToYmlInPlace
} from './index';

import { each } from 'lodash';
import { closeAllCollectionTabs, closeTabs as _closeTabs, focusTab, restoreTabs, reopenLastClosedTab } from 'providers/ReduxStore/slices/tabs';
import { closeAllCollectionTabs, closeTabs as _closeTabs, focusTab, restoreTabs, reopenLastClosedTab, migrateCollectionTabsToYml } from 'providers/ReduxStore/slices/tabs';
import { clearOpenApiSyncTabState } from 'providers/ReduxStore/slices/openapi-sync';
import { removeCollectionFromWorkspace } from 'providers/ReduxStore/slices/workspaces';
import { resolveRequestFilename } from 'utils/common/platform';
Expand Down Expand Up @@ -3557,54 +3557,18 @@ export const migrateCollectionToYml = (collectionUid) => (dispatch, getState) =>
}

const collectionPathname = collection.pathname;
const uid = collection.uid;
ipcRenderer
.invoke('renderer:migrate-collection-to-yml', collectionPathname, collectionUid)
.then(async (updatedBrunoConfig) => {
// Remove old collection state so we can recreate it with the new yml config.
// openCollectionEvent requires no existing collection with the same pathname.
dispatch(_removeCollection({ collectionUid }));
dispatch(closeAllCollectionTabs({ collectionUid }));

try {
// Reopen the collection with updated config (now yml format)
await dispatch(openCollectionEvent(uid, collectionPathname, updatedBrunoConfig));

// Mount the collection (starts the watcher and loads items)
await dispatch(mountCollection({
collectionUid: uid,
collectionPathname: collectionPathname,
brunoConfig: updatedBrunoConfig
}));
} catch (reopenError) {
// Files on disk are already yml; best-effort recovery so the
// collection doesn't disappear from the UI. openCollectionEvent is
// a no-op if it already succeeded, and mountCollection is what we
// retry when it was the failing step.
try {
await dispatch(openCollectionEvent(uid, collectionPathname, updatedBrunoConfig));
await dispatch(mountCollection({
collectionUid: uid,
collectionPathname: collectionPathname,
brunoConfig: updatedBrunoConfig
}));
} catch (_) { }
throw reopenError;
}

// Expand the collection in the sidebar (only if collapsed)
const reopenedCollection = findCollectionByUid(getState().collections.collections, uid);
if (reopenedCollection?.collapsed) {
dispatch(toggleCollection(uid));
}
.then((updatedBrunoConfig) => {
dispatch(migrateCollectionToYmlInPlace({ collectionUid, brunoConfig: updatedBrunoConfig }));
dispatch(migrateCollectionTabsToYml({ collectionUid }));

// Reopen collection settings on the overview tab
dispatch(addTab({
uid: uid,
collectionUid: uid,
uid: collectionUid,
collectionUid: collectionUid,
type: 'collection-settings'
}));
dispatch(updateSettingsSelectedTab({ collectionUid: uid, tab: 'overview' }));
dispatch(updateSettingsSelectedTab({ collectionUid: collectionUid, tab: 'overview' }));

toast.success('Collection migrated to YML format successfully');
resolve();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,45 @@ export const collectionsSlice = createSlice({
collection.brunoConfig = brunoConfig;
}
},
migrateCollectionToYmlInPlace: (state, action) => {
const { collectionUid, brunoConfig } = action.payload;
const collection = findCollectionByUid(state.collections, collectionUid);
if (!collection) {
return;
}

if (brunoConfig) {
collection.brunoConfig = brunoConfig;
if (collection.draft?.brunoConfig) {
collection.draft.brunoConfig = brunoConfig;
}
}
collection.format = 'yml';
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

const rewriteItemPaths = (items) => {
(items || []).forEach((item) => {
if (item.isTransient) {
return;
}
if (typeof item.pathname === 'string') {
item.pathname = item.pathname.replace(/\.bru$/, '.yml');
}
if (typeof item.filename === 'string') {
item.filename = item.filename.replace(/\.bru$/, '.yml');
}
if (item.items && item.items.length) {
rewriteItemPaths(item.items);
}
});
};
rewriteItemPaths(collection.items);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

(collection.environments || []).forEach((environment) => {
if (typeof environment.pathname === 'string') {
environment.pathname = environment.pathname.replace(/\.bru$/, '.yml');
}
});
},
renameCollection: (state, action) => {
const collection = findCollectionByUid(state.collections, action.payload.collectionUid);

Expand Down Expand Up @@ -3900,6 +3939,7 @@ export const {
setCollectionSecurityConfig,
updateCollectionVersion,
brunoConfigUpdateEvent,
migrateCollectionToYmlInPlace,
renameCollection,
removeCollection,
sortCollections,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import { collectionsSlice } from './index';

const { migrateCollectionToYmlInPlace } = collectionsSlice.actions;
const reducer = collectionsSlice.reducer;

const makeState = () => ({
collections: [
{
uid: 'col1',
format: 'bru',
brunoConfig: { version: '1', name: 'demo' },
environments: [
{ uid: 'e1', name: 'Local', pathname: '/c/environments/Local.bru' }
],
items: [
{
uid: 'r1',
type: 'http-request',
name: 'ping',
pathname: '/c/ping.bru',
filename: 'ping.bru',
seq: 1,
draft: { request: { url: 'https://example.com/ping' } }
},
{
uid: 'f1',
type: 'folder',
name: 'api',
pathname: '/c/api',
filename: 'api',
collapsed: false,
items: [
{
uid: 'r2',
type: 'http-request',
name: 'users',
pathname: '/c/api/users.bru',
filename: 'users.bru',
seq: 1
},
{
uid: 'f2',
type: 'folder',
name: 'v2',
pathname: '/c/api/v2',
filename: 'v2',
collapsed: false,
items: [
{
uid: 'r3',
type: 'http-request',
name: 'deep',
pathname: '/c/api/v2/deep.bru',
filename: 'deep.bru',
seq: 1
}
]
}
]
}
]
}
]
});

describe('migrateCollectionToYmlInPlace', () => {
it('rewrites request pathnames/filenames .bru -> .yml at every nesting depth', () => {
const next = reducer(
makeState(),
migrateCollectionToYmlInPlace({ collectionUid: 'col1', brunoConfig: { opencollection: '1.0.0', version: 'v1' } })
);
const col = next.collections[0];
const r1 = col.items[0];
const f1 = col.items[1];
const r2 = f1.items[0];
const f2 = f1.items[1];
const r3 = f2.items[0];

expect(r1.pathname).toBe('/c/ping.yml');
expect(r1.filename).toBe('ping.yml');
expect(r2.pathname).toBe('/c/api/users.yml');
expect(r2.filename).toBe('users.yml');
expect(r3.pathname).toBe('/c/api/v2/deep.yml');
expect(r3.filename).toBe('deep.yml');
});

it('flips collection.format to yml and updates brunoConfig', () => {
const next = reducer(
makeState(),
migrateCollectionToYmlInPlace({ collectionUid: 'col1', brunoConfig: { opencollection: '1.0.0', version: 'v1' } })
);
expect(next.collections[0].format).toBe('yml');
expect(next.collections[0].brunoConfig).toEqual({ opencollection: '1.0.0', version: 'v1' });
});

it('preserves item uids, seq, draft, and folder collapsed state', () => {
const next = reducer(
makeState(),
migrateCollectionToYmlInPlace({ collectionUid: 'col1', brunoConfig: {} })
);
const col = next.collections[0];
const r1 = col.items[0];
const f1 = col.items[1];
const f2 = f1.items[1];

expect(r1.uid).toBe('r1');
expect(r1.seq).toBe(1);
expect(r1.draft).toEqual({ request: { url: 'https://example.com/ping' } });
expect(f1.uid).toBe('f1');
expect(f1.collapsed).toBe(false);
expect(f2.collapsed).toBe(false);
});

it('leaves folder pathnames/filenames unchanged (only files migrate)', () => {
const next = reducer(
makeState(),
migrateCollectionToYmlInPlace({ collectionUid: 'col1', brunoConfig: {} })
);
const f1 = next.collections[0].items[1];
const f2 = f1.items[1];
expect(f1.pathname).toBe('/c/api');
expect(f1.filename).toBe('api');
expect(f2.pathname).toBe('/c/api/v2');
expect(f2.filename).toBe('v2');
});

it('rewrites environment file pathnames .bru -> .yml', () => {
const next = reducer(
makeState(),
migrateCollectionToYmlInPlace({ collectionUid: 'col1', brunoConfig: {} })
);
expect(next.collections[0].environments[0].pathname).toBe('/c/environments/Local.yml');
expect(next.collections[0].environments[0].uid).toBe('e1');
});

it('is a no-op for an unknown collection', () => {
const state = makeState();
const next = reducer(state, migrateCollectionToYmlInPlace({ collectionUid: 'nope', brunoConfig: {} }));
expect(next.collections[0].items[0].pathname).toBe('/c/ping.bru');
expect(next.collections[0].format).toBe('bru');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { tabsSlice } from './tabs';

const { migrateCollectionTabsToYml } = tabsSlice.actions;
const reducer = tabsSlice.reducer;

const makeState = () => ({
tabs: [
{ uid: 'r1', collectionUid: 'col1', type: 'http-request', pathname: '/c/ping.bru' },
{ uid: 'r2', collectionUid: 'col1', type: 'http-request', pathname: '/c/api/users.bru' },
{ uid: 'fs1', collectionUid: 'col1', type: 'folder-settings', pathname: '/c/api' },
{ uid: 'cs1', collectionUid: 'col1', type: 'collection-settings' },
{ uid: 'other', collectionUid: 'col2', type: 'http-request', pathname: '/c2/req.bru' }
],
activeTabUid: 'r1'
});

describe('migrateCollectionTabsToYml', () => {
it('rewrites .bru -> .yml only for tabs of the given collection', () => {
const next = reducer(makeState(), migrateCollectionTabsToYml({ collectionUid: 'col1' }));
const byUid = (uid) => next.tabs.find((t) => t.uid === uid);

expect(byUid('r1').pathname).toBe('/c/ping.yml');
expect(byUid('r2').pathname).toBe('/c/api/users.yml');
expect(byUid('other').pathname).toBe('/c2/req.bru');
});

it('leaves folder-settings (directory) paths and tabs without a pathname untouched', () => {
const next = reducer(makeState(), migrateCollectionTabsToYml({ collectionUid: 'col1' }));
const byUid = (uid) => next.tabs.find((t) => t.uid === uid);

expect(byUid('fs1').pathname).toBe('/c/api');
expect(byUid('cs1').pathname).toBeUndefined();
});

it('does not change tab uids or the active tab', () => {
const next = reducer(makeState(), migrateCollectionTabsToYml({ collectionUid: 'col1' }));
expect(next.tabs.map((t) => t.uid)).toEqual(['r1', 'r2', 'fs1', 'cs1', 'other']);
expect(next.activeTabUid).toBe('r1');
});
});
9 changes: 9 additions & 0 deletions packages/bruno-app/src/providers/ReduxStore/slices/tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,14 @@ export const tabsSlice = createSlice({
console.error('Tab not found!');
}
},
migrateCollectionTabsToYml: (state, action) => {
const { collectionUid } = action.payload;
state.tabs.forEach((tab) => {
if (tab.collectionUid === collectionUid && typeof tab.pathname === 'string') {
tab.pathname = tab.pathname.replace(/\.bru$/, '.yml');
}
});
},
collapseRequestPane: (state, action) => {
const tab = find(state.tabs, (t) => t.uid === action.payload.uid);
if (tab) {
Expand Down Expand Up @@ -557,6 +565,7 @@ export const {
closeTabs,
closeAllCollectionTabs,
makeTabPermanent,
migrateCollectionTabsToYml,
collapseRequestPane,
collapseResponsePane,
expandRequestPane,
Expand Down
11 changes: 11 additions & 0 deletions packages/bruno-electron/src/ipc/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -2779,6 +2779,17 @@ const registerRendererEventHandlers = (mainWindow, watcher) => {
ymlBrunoConfig.size = size;
ymlBrunoConfig.filesCount = filesCount;

if (watcher) {
try {
watcher.addWatcher(mainWindow, collectionPathname, collectionUid, ymlBrunoConfig, false, undefined, { ignoreInitial: true });
} catch (watcherError) {
console.error('Failed to re-attach watcher after migration:', watcherError);
mainWindow.webContents.send('main:display-error', {
message: `Collection migrated to yml, but live sync could not be re-enabled: ${watcherError.message}. Please reopen the collection.`
});
}
}

Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
return ymlBrunoConfig;
} catch (error) {
for (const ymlFile of writtenYmlFiles) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"version": "1",
"name": "keep-open",
"type": "collection",
"ignore": [
"node_modules",
".git"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
meta {
name: Keep Me
type: http
seq: 1
}

get {
url: https://example.com/keep
body: none
auth: none
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
meta {
name: api
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
meta {
name: users
type: http
seq: 1
}

get {
url: https://example.com/api/users
body: none
auth: none
}
Loading
Loading