summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/packages_and_registries/harbor_registry/utils.js
blob: 13df303cffe7383f40c8080051f2eb8fce72bd84 (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { isFinite } from 'lodash';
import {
  SORT_FIELD_MAPPING,
  TOKEN_TYPE_TAG_NAME,
} from '~/packages_and_registries/harbor_registry/constants';
import { FILTERED_SEARCH_TERM } from '~/packages_and_registries/shared/constants';
import { normalizeHeaders, parseIntPagination } from '~/lib/utils/common_utils';

export const extractSortingDetail = (parsedSorting = '') => {
  const [orderBy, sortOrder] = parsedSorting.split('_');
  if (orderBy && sortOrder) {
    return {
      orderBy: SORT_FIELD_MAPPING[orderBy],
      sort: sortOrder.toLowerCase(),
    };
  }

  return {
    orderBy: '',
    sort: '',
  };
};

export const parseFilter = (filters = [], defaultPrefix = '') => {
  /* eslint-disable @gitlab/require-i18n-strings */
  const prefixMap = {
    [FILTERED_SEARCH_TERM]: `${defaultPrefix}=`,
    [TOKEN_TYPE_TAG_NAME]: 'tags=',
  };
  /* eslint-enable @gitlab/require-i18n-strings */
  const filterList = [];
  filters.forEach((i) => {
    if (i.value?.data) {
      const filterVal = i.value?.data;
      const prefix = prefixMap[i.type];
      const filterString = `${prefix}${filterVal}`;

      filterList.push(filterString);
    }
  });

  return filterList.join(',');
};

export const getNameFromParams = (fullName) => {
  const names = fullName.split('/');
  return {
    projectName: names[0] || '',
    imageName: names[1] || '',
  };
};

export const formatPagination = (headers) => {
  const pagination = parseIntPagination(normalizeHeaders(headers)) || {};

  if (pagination.nextPage || pagination.previousPage) {
    pagination.hasNextPage = isFinite(pagination.nextPage);
    pagination.hasPreviousPage = isFinite(pagination.previousPage);
  }

  return pagination;
};

/* eslint-disable @gitlab/require-i18n-strings */
export const dockerBuildCommand = ({ repositoryUrl, harborProjectName, projectName = '' }) => {
  return `docker build -t ${repositoryUrl}/${harborProjectName}/${projectName} .`;
};

export const dockerPushCommand = ({ repositoryUrl, harborProjectName, projectName = '' }) => {
  return `docker push ${repositoryUrl}/${harborProjectName}/${projectName}`;
};

export const dockerLoginCommand = (repositoryUrl) => {
  return `docker login ${repositoryUrl}`;
};

export const artifactPullCommand = ({ repositoryUrl, harborProjectName, imageName, digest }) => {
  return `docker pull ${repositoryUrl}/${harborProjectName}/${imageName}@${digest}`;
};

export const tagPullCommand = ({ repositoryUrl, harborProjectName, imageName, tag }) => {
  return `docker pull ${repositoryUrl}/${harborProjectName}/${imageName}:${tag}`;
};
/* eslint-enable @gitlab/require-i18n-strings */