Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ref_switcher_utils.js « ref_switcher « find_file « projects « pages « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 21a30f1c54b65a2f399cbb91242adf345fb1a479 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { joinPaths } from '~/lib/utils/url_utility';

/**
 * Generates a ref destination url based on the selected ref and current url.
 * @param {string} selectedRef - The selected ref from the ref dropdown.
 * @param {string} namespace - The destination namespace for the path.
 */
export function generateRefDestinationPath(selectedRef, namespace) {
  if (!selectedRef || !namespace) {
    return window.location.href;
  }

  let refType = null;
  const { pathname } = window.location;
  const encodedHash = '%23';

  const [projectRootPath] = pathname.split(namespace);
  let actualRef = selectedRef;

  const matches = selectedRef.match(/^refs\/(heads|tags)\/(.+)/);
  if (matches) {
    [, refType, actualRef] = matches;
  }

  const destinationPath = joinPaths(
    projectRootPath,
    namespace,
    encodeURI(actualRef).replace(/#/g, encodedHash),
  );

  const newURL = new URL(window.location);
  newURL.pathname = destinationPath;

  if (refType) {
    newURL.searchParams.set('ref_type', refType.toLowerCase());
  } else {
    newURL.searchParams.delete('ref_type');
  }

  return newURL.href;
}