From ee32e06f1f117e748963a8badf6abfd5b57f339b Mon Sep 17 00:00:00 2001 From: Paul Slaughter Date: Tue, 30 Apr 2019 11:35:12 -0500 Subject: [PATCH] Fix IDE get file data with '/' as relative root https://gitlab.com/gitlab-org/gitlab-ce/issues/60932 --- .../javascripts/ide/stores/actions/file.js | 7 ++++--- .../unreleased/fix-ide-relative-url-bug.yml | 5 +++++ .../ide/stores/actions/file_spec.js | 19 +++++++++++++++---- 3 files changed, 24 insertions(+), 7 deletions(-) create mode 100644 changelogs/unreleased/fix-ide-relative-url-bug.yml diff --git a/app/assets/javascripts/ide/stores/actions/file.js b/app/assets/javascripts/ide/stores/actions/file.js index e74b880e02c..e7e8ac6d80b 100644 --- a/app/assets/javascripts/ide/stores/actions/file.js +++ b/app/assets/javascripts/ide/stores/actions/file.js @@ -1,5 +1,6 @@ -import { __ } from '../../../locale'; -import { normalizeHeaders } from '../../../lib/utils/common_utils'; +import { joinPaths } from '~/lib/utils/url_utility'; +import { normalizeHeaders } from '~/lib/utils/common_utils'; +import { __ } from '~/locale'; import eventHub from '../../eventhub'; import service from '../../services'; import * as types from '../mutation_types'; @@ -69,7 +70,7 @@ export const getFileData = ( const url = file.prevPath ? file.url.replace(file.path, file.prevPath) : file.url; return service - .getFileData(`${gon.relative_url_root ? gon.relative_url_root : ''}${url.replace('/-/', '/')}`) + .getFileData(joinPaths(gon.relative_url_root || '', url.replace('/-/', '/'))) .then(({ data, headers }) => { const normalizedHeaders = normalizeHeaders(headers); setPageTitle(decodeURI(normalizedHeaders['PAGE-TITLE'])); diff --git a/changelogs/unreleased/fix-ide-relative-url-bug.yml b/changelogs/unreleased/fix-ide-relative-url-bug.yml new file mode 100644 index 00000000000..183af722657 --- /dev/null +++ b/changelogs/unreleased/fix-ide-relative-url-bug.yml @@ -0,0 +1,5 @@ +--- +title: Fix IDE get file data with '/' as relative root +merge_request: 27911 +author: +type: fixed diff --git a/spec/javascripts/ide/stores/actions/file_spec.js b/spec/javascripts/ide/stores/actions/file_spec.js index 1e5b55af4ba..e6fb08bcc49 100644 --- a/spec/javascripts/ide/stores/actions/file_spec.js +++ b/spec/javascripts/ide/stores/actions/file_spec.js @@ -10,11 +10,19 @@ import eventHub from '~/ide/eventhub'; import { file, resetStore } from '../../helpers'; import testAction from '../../../helpers/vuex_action_helper'; +const RELATIVE_URL_ROOT = '/gitlab'; + describe('IDE store file actions', () => { let mock; + let originalGon; beforeEach(() => { mock = new MockAdapter(axios); + originalGon = window.gon; + window.gon = { + ...window.gon, + relative_url_root: RELATIVE_URL_ROOT, + }; spyOn(router, 'push'); }); @@ -22,6 +30,7 @@ describe('IDE store file actions', () => { afterEach(() => { mock.restore(); resetStore(store); + window.gon = originalGon; }); describe('closeFile', () => { @@ -173,13 +182,13 @@ describe('IDE store file actions', () => { spyOn(service, 'getFileData').and.callThrough(); localFile = file(`newCreate-${Math.random()}`); - localFile.url = `${gl.TEST_HOST}/getFileDataURL`; + localFile.url = `project/getFileDataURL`; store.state.entries[localFile.path] = localFile; }); describe('success', () => { beforeEach(() => { - mock.onGet(`${gl.TEST_HOST}/getFileDataURL`).replyOnce( + mock.onGet(`${RELATIVE_URL_ROOT}/project/getFileDataURL`).replyOnce( 200, { blame_path: 'blame_path', @@ -200,7 +209,9 @@ describe('IDE store file actions', () => { store .dispatch('getFileData', { path: localFile.path }) .then(() => { - expect(service.getFileData).toHaveBeenCalledWith(`${gl.TEST_HOST}/getFileDataURL`); + expect(service.getFileData).toHaveBeenCalledWith( + `${RELATIVE_URL_ROOT}/project/getFileDataURL`, + ); done(); }) @@ -266,7 +277,7 @@ describe('IDE store file actions', () => { describe('error', () => { beforeEach(() => { - mock.onGet(`${gl.TEST_HOST}/getFileDataURL`).networkError(); + mock.onGet(`project/getFileDataURL`).networkError(); }); it('dispatches error action', done => {