mirror of
https://github.com/portainer/portainer.git
synced 2025-08-09 07:45:22 +02:00
fix(edge/templates): fix issues with git templates [EE-6357] (#10679)
This commit is contained in:
parent
974378c9b5
commit
2a18c9f215
16 changed files with 201 additions and 116 deletions
|
@ -6,25 +6,7 @@ import { UserId } from '@/portainer/users/types';
|
|||
|
||||
import { isBE } from '../../feature-flags/feature-flags.service';
|
||||
|
||||
import {
|
||||
CreateGitCredentialPayload,
|
||||
GitCredential,
|
||||
UpdateGitCredentialPayload,
|
||||
} from './types';
|
||||
|
||||
export async function createGitCredential(
|
||||
gitCredential: CreateGitCredentialPayload
|
||||
) {
|
||||
try {
|
||||
const { data } = await axios.post<{ gitCredential: GitCredential }>(
|
||||
buildGitUrl(gitCredential.userId),
|
||||
gitCredential
|
||||
);
|
||||
return data.gitCredential;
|
||||
} catch (e) {
|
||||
throw parseAxiosError(e as Error, 'Unable to create git credential');
|
||||
}
|
||||
}
|
||||
import { GitCredential, UpdateGitCredentialPayload } from './types';
|
||||
|
||||
export async function getGitCredentials(userId: number) {
|
||||
try {
|
||||
|
@ -141,24 +123,7 @@ export function useGitCredential(userId: number, id: number) {
|
|||
});
|
||||
}
|
||||
|
||||
export function useCreateGitCredentialMutation() {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation(createGitCredential, {
|
||||
onSuccess: (_, payload) => {
|
||||
notifySuccess('Credentials created successfully', payload.name);
|
||||
return queryClient.invalidateQueries(['gitcredentials']);
|
||||
},
|
||||
meta: {
|
||||
error: {
|
||||
title: 'Failure',
|
||||
message: 'Unable to create credential',
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
function buildGitUrl(userId: number, credentialId?: number) {
|
||||
export function buildGitUrl(userId: number, credentialId?: number) {
|
||||
return credentialId
|
||||
? `/users/${userId}/gitcredentials/${credentialId}`
|
||||
: `/users/${userId}/gitcredentials`;
|
||||
|
|
|
@ -0,0 +1,82 @@
|
|||
import { useQueryClient, useMutation } from 'react-query';
|
||||
|
||||
import axios, { parseAxiosError } from '@/portainer/services/axios';
|
||||
import { notifyError, notifySuccess } from '@/portainer/services/notifications';
|
||||
import { GitAuthModel } from '@/react/portainer/gitops/types';
|
||||
import { useCurrentUser } from '@/react/hooks/useUser';
|
||||
|
||||
import { GitCredential } from '../types';
|
||||
import { buildGitUrl } from '../git-credentials.service';
|
||||
|
||||
export interface CreateGitCredentialPayload {
|
||||
userId: number;
|
||||
name: string;
|
||||
username?: string;
|
||||
password: string;
|
||||
}
|
||||
|
||||
export function useCreateGitCredentialMutation() {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation(createGitCredential, {
|
||||
onSuccess: (_, payload) => {
|
||||
notifySuccess('Credentials created successfully', payload.name);
|
||||
return queryClient.invalidateQueries(['gitcredentials']);
|
||||
},
|
||||
meta: {
|
||||
error: {
|
||||
title: 'Failure',
|
||||
message: 'Unable to create credential',
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async function createGitCredential(gitCredential: CreateGitCredentialPayload) {
|
||||
try {
|
||||
const { data } = await axios.post<{ gitCredential: GitCredential }>(
|
||||
buildGitUrl(gitCredential.userId),
|
||||
gitCredential
|
||||
);
|
||||
return data.gitCredential;
|
||||
} catch (e) {
|
||||
throw parseAxiosError(e as Error, 'Unable to create git credential');
|
||||
}
|
||||
}
|
||||
|
||||
export function useSaveCredentialsIfRequired() {
|
||||
const saveCredentialsMutation = useCreateGitCredentialMutation();
|
||||
const { user } = useCurrentUser();
|
||||
|
||||
return {
|
||||
saveCredentials: saveCredentialsIfRequired,
|
||||
isLoading: saveCredentialsMutation.isLoading,
|
||||
};
|
||||
|
||||
async function saveCredentialsIfRequired(authentication?: GitAuthModel) {
|
||||
if (!authentication) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (
|
||||
!authentication.SaveCredential ||
|
||||
!authentication.RepositoryPassword ||
|
||||
!authentication.NewCredentialName
|
||||
) {
|
||||
return authentication.RepositoryGitCredentialID;
|
||||
}
|
||||
|
||||
try {
|
||||
const credential = await saveCredentialsMutation.mutateAsync({
|
||||
userId: user.Id,
|
||||
username: authentication.RepositoryUsername,
|
||||
password: authentication.RepositoryPassword,
|
||||
name: authentication.NewCredentialName,
|
||||
});
|
||||
return credential.id;
|
||||
} catch (err) {
|
||||
notifyError('Error', err as Error, 'Unable to save credentials');
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -13,13 +13,6 @@ export interface GitCredentialFormValues {
|
|||
password: string;
|
||||
}
|
||||
|
||||
export interface CreateGitCredentialPayload {
|
||||
userId: number;
|
||||
name: string;
|
||||
username?: string;
|
||||
password: string;
|
||||
}
|
||||
|
||||
export interface UpdateGitCredentialPayload {
|
||||
name: string;
|
||||
username?: string;
|
||||
|
|
|
@ -35,7 +35,7 @@ export function RelativePathFieldset({
|
|||
|
||||
const { errors } = useValidation(value);
|
||||
|
||||
const { enableFsPath0, enableFsPath1, toggleFsPath } = useEnableFsPath();
|
||||
const { enableFsPath0, enableFsPath1, toggleFsPath } = useEnableFsPath(value);
|
||||
|
||||
const pathTip0 =
|
||||
'For relative path volumes use with Docker Swarm, you must have a network filesystem which all of your nodes can access.';
|
||||
|
|
|
@ -1,7 +1,11 @@
|
|||
import { useState } from 'react';
|
||||
|
||||
export function useEnableFsPath() {
|
||||
const [state, setState] = useState<number[]>([]);
|
||||
import { RelativePathModel } from './types';
|
||||
|
||||
export function useEnableFsPath(initialValue: RelativePathModel) {
|
||||
const [state, setState] = useState<number[]>(() =>
|
||||
initialValue.SupportPerDeviceConfigs ? [1] : []
|
||||
);
|
||||
|
||||
const enableFsPath0 = state.length && state[0] === 0;
|
||||
const enableFsPath1 = state.length && state[0] === 1;
|
||||
|
|
|
@ -9,11 +9,7 @@ import { usePublicSettings } from '../../settings/queries';
|
|||
import { queryKeys } from './query-keys';
|
||||
|
||||
export function useRegistries<T = Registry[]>(
|
||||
queryOptions: {
|
||||
enabled?: boolean;
|
||||
select?: (registries: Registry[]) => T;
|
||||
onSuccess?: (data: T) => void;
|
||||
} = {}
|
||||
queryOptions: GenericRegistriesQueryOptions<T> = {}
|
||||
) {
|
||||
return useGenericRegistriesQuery(
|
||||
queryKeys.base(),
|
||||
|
@ -22,13 +18,11 @@ export function useRegistries<T = Registry[]>(
|
|||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @field hideDefault - is used to hide the default registry from the list of registries, regardless of the user's settings. Kubernetes views use this.
|
||||
*/
|
||||
export type GenericRegistriesQueryOptions<T> = {
|
||||
enabled?: boolean;
|
||||
select?: (registries: Registry[]) => T;
|
||||
onSuccess?: (data: T) => void;
|
||||
/** is used to hide the default registry from the list of registries, regardless of the user's settings. Kubernetes views use this. */
|
||||
hideDefault?: boolean;
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue