mirror of
https://github.com/portainer/portainer.git
synced 2025-07-25 08:19:40 +02:00
refactor(ui/image-config): create react component [EE-5342] (#8856)
This commit is contained in:
parent
bf51f1b6c9
commit
10014ae171
34 changed files with 1464 additions and 84 deletions
101
app/react/docker/images/utils.ts
Normal file
101
app/react/docker/images/utils.ts
Normal file
|
@ -0,0 +1,101 @@
|
|||
import _ from 'lodash';
|
||||
|
||||
import { trimSHA } from '@/docker/filters/utils';
|
||||
import {
|
||||
Registry,
|
||||
RegistryTypes,
|
||||
} from '@/react/portainer/registries/types/registry';
|
||||
|
||||
import { DockerImage } from './types';
|
||||
import { DockerImageResponse } from './types/response';
|
||||
|
||||
export function parseViewModel(response: DockerImageResponse): DockerImage {
|
||||
return {
|
||||
...response,
|
||||
Used: false,
|
||||
RepoTags:
|
||||
response.RepoTags ??
|
||||
response.RepoDigests.map((digest) => `${trimSHA(digest)}:<none>`),
|
||||
};
|
||||
}
|
||||
|
||||
export function getUniqueTagListFromImages(
|
||||
images: Array<{ RepoTags?: string[] }>
|
||||
) {
|
||||
return _.uniq(
|
||||
images.flatMap((image) =>
|
||||
image.RepoTags
|
||||
? image.RepoTags.filter((item) => !item.includes('<none>'))
|
||||
: []
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
export function imageContainsURL(image: string) {
|
||||
const split = image.split('/');
|
||||
const url = split[0];
|
||||
if (split.length > 1) {
|
||||
return url.includes('.') || url.includes(':');
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* builds the complete uri for an image based on its registry
|
||||
* @param {PorImageRegistryModel} imageModel
|
||||
*/
|
||||
export function buildImageFullURI(image: string, registry?: Registry) {
|
||||
if (!registry) {
|
||||
return ensureTag(image);
|
||||
}
|
||||
|
||||
const imageName = buildImageFullURIWithRegistry(image, registry);
|
||||
|
||||
return ensureTag(imageName);
|
||||
|
||||
function ensureTag(image: string, defaultTag = 'latest') {
|
||||
return image.includes(':') ? image : `${image}:${defaultTag}`;
|
||||
}
|
||||
}
|
||||
|
||||
function buildImageFullURIWithRegistry(image: string, registry: Registry) {
|
||||
switch (registry.Type) {
|
||||
case RegistryTypes.GITHUB:
|
||||
return buildImageURIForGithub(image, registry);
|
||||
case RegistryTypes.GITLAB:
|
||||
return buildImageURIForGitLab(image, registry);
|
||||
case RegistryTypes.QUAY:
|
||||
return buildImageURIForQuay(image, registry);
|
||||
case RegistryTypes.ANONYMOUS:
|
||||
return image;
|
||||
default:
|
||||
return buildImageURIForOtherRegistry(image, registry);
|
||||
}
|
||||
|
||||
function buildImageURIForGithub(image: string, registry: Registry) {
|
||||
const imageName = image.split('/').pop();
|
||||
|
||||
const namespace = registry.Github.UseOrganisation
|
||||
? registry.Github.OrganisationName
|
||||
: registry.Username;
|
||||
return `${registry.URL}/${namespace}/${imageName}`;
|
||||
}
|
||||
|
||||
function buildImageURIForGitLab(image: string, registry: Registry) {
|
||||
const slash = image.startsWith(':') ? '' : '/';
|
||||
return `${registry.URL}/${registry.Gitlab.ProjectPath}${slash}${image}`;
|
||||
}
|
||||
|
||||
function buildImageURIForQuay(image: string, registry: Registry) {
|
||||
const name = registry.Quay.UseOrganisation
|
||||
? registry.Quay.OrganisationName
|
||||
: registry.Username;
|
||||
const url = registry.URL ? `${registry.URL}/` : '';
|
||||
return `${url}${name}/${image}`;
|
||||
}
|
||||
|
||||
function buildImageURIForOtherRegistry(image: string, registry: Registry) {
|
||||
const url = registry.URL ? `${registry.URL}/` : '';
|
||||
return url + image;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue