From 245bcc7bbfb152c6ecfea26f295ae6b64f19033f Mon Sep 17 00:00:00 2001 From: Samantha Ming Date: Mon, 22 Mar 2021 20:25:06 -0700 Subject: [PATCH 1/3] Update currentRef find to match exact ref Issue: https://gitlab.com/gitlab-org/gitlab/-/issues/322792 --- app/assets/javascripts/pages/projects/project.js | 7 ++++--- .../unreleased/322792-switch-branch-of-shorter-name.yml | 5 +++++ 2 files changed, 9 insertions(+), 3 deletions(-) create mode 100644 changelogs/unreleased/322792-switch-branch-of-shorter-name.yml diff --git a/app/assets/javascripts/pages/projects/project.js b/app/assets/javascripts/pages/projects/project.js index da8dc527d79f72..86582a9e492559 100644 --- a/app/assets/javascripts/pages/projects/project.js +++ b/app/assets/javascripts/pages/projects/project.js @@ -123,9 +123,10 @@ export default class Project { const loc = window.location.href; if (loc.includes('/-/')) { - const refs = this.fullData.Branches.concat(this.fullData.Tags); - const currentRef = refs.find((ref) => loc.indexOf(ref) > -1); - if (currentRef) { + const currentRef = $dropdown.data('ref'); + const doesPathContainRef = loc.indexOf(currentRef) > -1; + + if (doesPathContainRef) { const targetPath = loc.split(currentRef)[1].slice(1).split('#')[0]; selectedUrl.searchParams.set('path', targetPath); selectedUrl.hash = window.location.hash; diff --git a/changelogs/unreleased/322792-switch-branch-of-shorter-name.yml b/changelogs/unreleased/322792-switch-branch-of-shorter-name.yml new file mode 100644 index 00000000000000..f9eddc86b8c836 --- /dev/null +++ b/changelogs/unreleased/322792-switch-branch-of-shorter-name.yml @@ -0,0 +1,5 @@ +--- +title: Fix branch switch to be exact instead of partial match +merge_request: 57197 +author: +type: fixed -- GitLab From 6d6b64217722b38b255632be4b0576f3d357ebd9 Mon Sep 17 00:00:00 2001 From: Samantha Ming Date: Wed, 7 Apr 2021 02:34:33 -0700 Subject: [PATCH 2/3] Adjust logic for exact ref match --- app/assets/javascripts/pages/projects/project.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/assets/javascripts/pages/projects/project.js b/app/assets/javascripts/pages/projects/project.js index 86582a9e492559..0692154d784d2f 100644 --- a/app/assets/javascripts/pages/projects/project.js +++ b/app/assets/javascripts/pages/projects/project.js @@ -124,10 +124,11 @@ export default class Project { if (loc.includes('/-/')) { const currentRef = $dropdown.data('ref'); - const doesPathContainRef = loc.indexOf(currentRef) > -1; + const splitPathAfterRefPortion = loc.split(currentRef)[1]; + const doesPathContainRef = splitPathAfterRefPortion?.startsWith('/'); if (doesPathContainRef) { - const targetPath = loc.split(currentRef)[1].slice(1).split('#')[0]; + const targetPath = splitPathAfterRefPortion?.slice(1).split('#')[0]; selectedUrl.searchParams.set('path', targetPath); selectedUrl.hash = window.location.hash; } -- GitLab From dfa940b7929d6b52ea67d556a1b2b2ff6d27f7a8 Mon Sep 17 00:00:00 2001 From: Samantha Ming Date: Mon, 12 Apr 2021 18:52:11 -0700 Subject: [PATCH 3/3] Add comments to clarify code Address reviewer comment --- app/assets/javascripts/pages/projects/project.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/app/assets/javascripts/pages/projects/project.js b/app/assets/javascripts/pages/projects/project.js index 0692154d784d2f..91f376060f882d 100644 --- a/app/assets/javascripts/pages/projects/project.js +++ b/app/assets/javascripts/pages/projects/project.js @@ -123,11 +123,18 @@ export default class Project { const loc = window.location.href; if (loc.includes('/-/')) { + // Since the current ref in renderRow is outdated on page changes + // (To be addressed in: https://gitlab.com/gitlab-org/gitlab/-/issues/327085) + // We are deciphering the current ref from the dropdown data instead const currentRef = $dropdown.data('ref'); + // The split and startWith is to ensure an exact word match + // and avoid partial match ie. currentRef is "dev" and loc is "development" const splitPathAfterRefPortion = loc.split(currentRef)[1]; const doesPathContainRef = splitPathAfterRefPortion?.startsWith('/'); if (doesPathContainRef) { + // We are ignoring the url containing the ref portion + // and plucking the thereafter portion to reconstructure the url that is correct const targetPath = splitPathAfterRefPortion?.slice(1).split('#')[0]; selectedUrl.searchParams.set('path', targetPath); selectedUrl.hash = window.location.hash; -- GitLab