1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-08-02 20:35:25 +02:00

refactor(stacks): extract auto update logic [EE-4945] (#8545)

This commit is contained in:
Chaim Lev-Ari 2023-03-02 17:07:50 +02:00 committed by GitHub
parent 085381e6fc
commit 6918da2414
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
42 changed files with 410 additions and 166 deletions

View file

@ -14,6 +14,8 @@ export function AutoUpdateFieldset({
isForcePullVisible = true,
errors,
baseWebhookUrl,
webhookId,
webhooksDocs,
}: {
value: AutoUpdateModel;
onChange: (value: AutoUpdateModel) => void;
@ -21,6 +23,8 @@ export function AutoUpdateFieldset({
isForcePullVisible?: boolean;
errors?: FormikErrors<AutoUpdateModel>;
baseWebhookUrl: string;
webhookId: string;
webhooksDocs?: string;
}) {
return (
<>
@ -45,12 +49,14 @@ export function AutoUpdateFieldset({
{value.RepositoryAutomaticUpdates && (
<AutoUpdateSettings
webhookId={webhookId}
baseWebhookUrl={baseWebhookUrl}
value={value}
onChange={handleChange}
environmentType={environmentType}
showForcePullImage={isForcePullVisible}
errors={errors}
webhookDocs={webhooksDocs}
/>
)}
</>

View file

@ -19,6 +19,8 @@ export function AutoUpdateSettings({
showForcePullImage,
errors,
baseWebhookUrl,
webhookId,
webhookDocs,
}: {
value: AutoUpdateModel;
onChange: (value: Partial<AutoUpdateModel>) => void;
@ -26,6 +28,8 @@ export function AutoUpdateSettings({
showForcePullImage: boolean;
errors?: FormikErrors<AutoUpdateModel>;
baseWebhookUrl: string;
webhookId: string;
webhookDocs?: string;
}) {
return (
<>
@ -50,12 +54,8 @@ export function AutoUpdateSettings({
{value.RepositoryMechanism === 'Webhook' && (
<WebhookSettings
baseUrl={baseWebhookUrl}
value={value.RepositoryWebhookId || ''}
docsLink={
environmentType === 'KUBERNETES'
? 'https://docs.portainer.io/user/kubernetes/applications/webhooks'
: 'https://docs.portainer.io/user/docker/stacks/webhooks'
}
value={webhookId}
docsLink={webhookDocs}
/>
)}

View file

@ -8,7 +8,7 @@ export function WebhookSettings({
baseUrl,
docsLink,
}: {
docsLink: string;
docsLink?: string;
value: string;
baseUrl: string;
}) {
@ -18,13 +18,15 @@ export function WebhookSettings({
<FormControl
label="Webhook"
tooltip={
<>
See{' '}
<a href={docsLink} target="_blank" rel="noreferrer">
Portainer documentation on webhook usage
</a>
.
</>
!!docsLink && (
<>
See{' '}
<a href={docsLink} target="_blank" rel="noreferrer">
Portainer documentation on webhook usage
</a>
.
</>
)
}
>
<div className="flex items-center gap-2">

View file

@ -1,5 +1,3 @@
import { v4 as uuid } from 'uuid';
import { AutoUpdateResponse, AutoUpdateModel } from '../types';
export function parseAutoUpdateResponse(
@ -11,7 +9,6 @@ export function parseAutoUpdateResponse(
RepositoryAutomaticUpdatesForce: false,
RepositoryMechanism: 'Interval',
RepositoryFetchInterval: '5m',
RepositoryWebhookId: uuid(),
ForcePullImage: false,
};
}
@ -20,28 +17,30 @@ export function parseAutoUpdateResponse(
RepositoryAutomaticUpdates: true,
RepositoryMechanism: response.Interval ? 'Interval' : 'Webhook',
RepositoryFetchInterval: response.Interval || '',
RepositoryWebhookId: response.Webhook || uuid(),
RepositoryAutomaticUpdatesForce: response.ForceUpdate,
ForcePullImage: response.ForcePullImage,
};
}
export function transformAutoUpdateViewModel(
viewModel?: AutoUpdateModel
viewModel?: AutoUpdateModel,
webhookId?: string
): AutoUpdateResponse | null {
if (!viewModel || !viewModel.RepositoryAutomaticUpdates) {
return null;
}
if (viewModel.RepositoryMechanism === 'Webhook' && !webhookId) {
throw new Error('Webhook ID is required');
}
return {
Interval:
viewModel.RepositoryMechanism === 'Interval'
? viewModel.RepositoryFetchInterval
: '',
Webhook:
viewModel.RepositoryMechanism === 'Webhook'
? viewModel.RepositoryWebhookId
: '',
viewModel.RepositoryMechanism === 'Webhook' && webhookId ? webhookId : '',
ForceUpdate: viewModel.RepositoryAutomaticUpdatesForce,
ForcePullImage: viewModel.ForcePullImage,
};

View file

@ -90,6 +90,7 @@ export function Primary({
isForcePullVisible={isForcePullVisible}
deployMethod={deployMethod}
baseWebhookUrl="ws://localhost:9000"
webhookId="1234"
/>
</Form>
)}

View file

@ -28,6 +28,8 @@ interface Props {
isAuthExplanationVisible?: boolean;
errors: FormikErrors<GitFormModel>;
baseWebhookUrl: string;
webhookId: string;
webhooksDocs?: string;
}
export function GitForm({
@ -40,6 +42,8 @@ export function GitForm({
isAuthExplanationVisible,
errors = {},
baseWebhookUrl,
webhookId,
webhooksDocs,
}: Props) {
return (
<FormSection title="Git repository">
@ -89,11 +93,13 @@ export function GitForm({
{value.AutoUpdate && (
<AutoUpdateFieldset
webhookId={webhookId}
baseWebhookUrl={baseWebhookUrl}
value={value.AutoUpdate}
onChange={(value) => handleChange({ AutoUpdate: value })}
isForcePullVisible={isForcePullVisible}
errors={errors.AutoUpdate as FormikErrors<GitFormModel['AutoUpdate']>}
webhooksDocs={webhooksDocs}
/>
)}

View file

@ -31,7 +31,6 @@ export interface RepoConfigResponse {
export type AutoUpdateModel = {
RepositoryAutomaticUpdates: boolean;
RepositoryMechanism: AutoUpdateMechanism;
RepositoryWebhookId: string;
RepositoryFetchInterval: string;
ForcePullImage: boolean;
RepositoryAutomaticUpdatesForce: boolean;