Skip to content
Snippets Groups Projects
Verified Commit cb7591cf authored by Phil Hughes's avatar Phil Hughes
Browse files

Fixes merge request URL growing unexpectedly

This fixes a bug where whena user clicks the file name in a project
with a specific name it would cause the URL to change incorrectly.

#408402
parent 8e054514
1 merge request!142035Fixes merge request URL growing unexpectedly
......@@ -171,11 +171,11 @@ function toggleLoader(state) {
$('.mr-loading-status .loading').toggleClass('hide', !state);
}
function getActionFromHref(href) {
let action = new URL(href).pathname.match(/\/(commits|diffs|pipelines).*$/);
export function getActionFromHref(pathName) {
let action = pathName.match(/\/(\d+)\/(commits|diffs|pipelines).*$/);
if (action) {
action = action[0].replace(/(^\/|\.html)/g, '');
action = action.at(-1).replace(/(^\/|\.html)/g, '');
} else {
action = 'show';
}
......@@ -239,7 +239,7 @@ export default class MergeRequestTabs {
$('.merge-request-tabs a[data-toggle="tabvue"]').on('click', this.clickTab);
window.addEventListener('popstate', (event) => {
if (event?.state?.skipScrolling) return;
const action = getActionFromHref(location.href);
const action = getActionFromHref(window.location.pathname);
this.tabShown(action, location.href);
this.eventHub.$emit('MergeRequestTabChange', action);
......
......@@ -5,7 +5,7 @@ import { setHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
import initMrPage from 'helpers/init_vue_mr_page_helper';
import { stubPerformanceWebAPI } from 'helpers/performance';
import axios from '~/lib/utils/axios_utils';
import MergeRequestTabs from '~/merge_request_tabs';
import MergeRequestTabs, { getActionFromHref } from '~/merge_request_tabs';
import Diff from '~/diff';
import '~/lib/utils/common_utils';
import '~/lib/utils/url_utility';
......@@ -478,4 +478,17 @@ describe('MergeRequestTabs', () => {
});
});
});
describe('getActionFromHref', () => {
it.each`
pathName | action
${'/user/pipelines/-/merge_requests/1/diffs'} | ${'diffs'}
${'/user/diffs/-/merge_requests/1/pipelines'} | ${'pipelines'}
${'/user/pipelines/-/merge_requests/1/commits'} | ${'commits'}
${'/user/pipelines/1/-/merge_requests/1/diffs'} | ${'diffs'}
${'/user/pipelines/-/merge_requests/1'} | ${'show'}
`('returns $action for $location', ({ pathName, action }) => {
expect(getActionFromHref(pathName)).toBe(action);
});
});
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment