Skip to content

Commit

Permalink
clean up workflow actions (#2136)
Browse files Browse the repository at this point in the history
* clean up workflow actions

* better i18n string name

* fix batch action checks

* fix tests

* add integration tests
  • Loading branch information
rossedfort authored Jun 4, 2024
1 parent ce6e2b7 commit 9616d0e
Show file tree
Hide file tree
Showing 13 changed files with 535 additions and 209 deletions.
134 changes: 79 additions & 55 deletions src/lib/components/workflow-actions.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@
import { translate } from '$lib/i18n/translate';
import { coreUserStore } from '$lib/stores/core-user';
import { resetEvents } from '$lib/stores/events';
import { settings } from '$lib/stores/settings';
import { refresh } from '$lib/stores/workflow-run';
import type { WorkflowExecution } from '$lib/types/workflows';
import { workflowCancelEnabled } from '$lib/utilities/workflow-cancel-enabled';
import { workflowResetEnabled } from '$lib/utilities/workflow-reset-enabled';
import { workflowSignalEnabled } from '$lib/utilities/workflow-signal-enabled';
import { workflowTerminateEnabled } from '$lib/utilities/workflow-terminate-enabled';
import { writeActionsAreAllowed } from '$lib/utilities/write-actions-are-allowed';
export let workflow: WorkflowExecution;
export let namespace: string;
Expand All @@ -31,28 +29,55 @@
let resetConfirmationModalOpen = false;
let signalConfirmationModalOpen = false;
let resetTooltipText: string;
let coreUser = coreUserStore();
$: cancelEnabled = workflowCancelEnabled(
$page.data.settings,
$coreUser,
namespace,
);
$: signalEnabled = workflowSignalEnabled(
$page.data.settings,
$coreUser,
namespace,
);
$: terminateEnabled = workflowTerminateEnabled(
$page.data.settings,
$coreUser,
namespace,
);
$: resetAuthorized = workflowResetEnabled(
$page.data.settings,
$coreUser,
namespace,
);
$: cancelEnabled = workflowCancelEnabled($page.data.settings);
$: signalEnabled = workflowSignalEnabled($page.data.settings);
$: terminateEnabled = workflowTerminateEnabled($page.data.settings);
$: resetEnabled = workflowResetEnabled($page.data.settings);
$: resetEnabled =
resetAuthorized &&
workflow.pendingChildren.length === 0 &&
$resetEvents.length > 0;
$: actionsDisabled = !resetEnabled && !signalEnabled && !terminateEnabled;
let workflowActions: {
label: string;
onClick: () => void;
allowed: boolean;
enabled: boolean;
testId: string;
destructive?: boolean;
tooltip?: string;
}[];
$: {
if (!resetEnabled) {
resetTooltipText = translate('workflows.reset-disabled');
} else if (resetEnabled && workflow?.pendingChildren?.length > 0) {
if (!resetAuthorized) {
resetTooltipText = translate('workflows.reset-disabled-unauthorized');
} else if (resetAuthorized && workflow?.pendingChildren?.length > 0) {
resetTooltipText = translate('workflows.reset-disabled-pending-children');
} else if (
resetEnabled &&
resetAuthorized &&
workflow?.pendingChildren?.length === 0 &&
$resetEvents.length === 0
) {
Expand All @@ -65,39 +90,27 @@
label: translate('workflows.reset'),
onClick: () => (resetConfirmationModalOpen = true),
testId: 'reset-button',
allowed: resetAllowed,
tooltip: resetAllowed ? '' : resetTooltipText,
enabled: resetEnabled,
tooltip: resetEnabled ? '' : resetTooltipText,
},
{
label: translate('workflows.signal'),
onClick: () => (signalConfirmationModalOpen = true),
testId: 'signal-button',
allowed: signalEnabled,
enabled: signalEnabled,
tooltip: signalEnabled ? '' : translate('workflows.signal-disabled'),
},
{
label: translate('workflows.terminate'),
onClick: () => (terminateConfirmationModalOpen = true),
testId: 'terminate-button',
allowed: terminateEnabled,
enabled: terminateEnabled,
destructive: true,
tooltip: terminateEnabled
? ''
: translate('workflows.terminate-disabled'),
},
];
let coreUser = coreUserStore();
$: actionsDisabled =
$coreUser.namespaceWriteDisabled(namespace) ||
!writeActionsAreAllowed(settings);
$: resetAllowed =
resetEnabled &&
workflow?.pendingChildren?.length === 0 &&
$resetEvents.length > 0 &&
!actionsDisabled;
</script>

{#if isRunning}
Expand All @@ -110,15 +123,15 @@
label={translate('workflows.request-cancellation')}
menuLabel={translate('workflows.workflow-actions')}
>
{#each workflowActions as { onClick, destructive, label, allowed, testId, tooltip }}
{#each workflowActions as { onClick, destructive, label, enabled, testId, tooltip }}
{#if destructive}
<MenuDivider />
{/if}
<Tooltip text={tooltip} hide={!tooltip} width={200} left>
<MenuItem
on:click={onClick}
{destructive}
disabled={!allowed}
disabled={!enabled}
data-testid={testId}
>
{label}
Expand All @@ -127,10 +140,10 @@
{/each}
</SplitButton>
{:else}
<Tooltip bottomRight width={200} text={resetTooltipText} hide={resetAllowed}>
<Tooltip bottomRight width={200} text={resetTooltipText} hide={resetEnabled}>
<Button
aria-label={translate('workflows.reset')}
disabled={!resetAllowed}
disabled={!resetEnabled}
variant="primary"
on:click={() => (resetConfirmationModalOpen = true)}
data-testid="workflow-reset-button"
Expand All @@ -140,27 +153,38 @@
</Tooltip>
{/if}

<ResetConfirmationModal
{refresh}
{workflow}
{namespace}
bind:open={resetConfirmationModalOpen}
/>
<SignalConfirmationModal
{refresh}
{workflow}
{namespace}
bind:open={signalConfirmationModalOpen}
/>
<CancelConfirmationModal
{refresh}
{workflow}
{namespace}
bind:open={cancelConfirmationModalOpen}
/>
<TerminateConfirmationModal
{refresh}
{workflow}
{namespace}
bind:open={terminateConfirmationModalOpen}
/>
{#if resetEnabled}
<ResetConfirmationModal
{refresh}
{workflow}
{namespace}
bind:open={resetConfirmationModalOpen}
/>
{/if}

{#if signalEnabled}
<SignalConfirmationModal
{refresh}
{workflow}
{namespace}
bind:open={signalConfirmationModalOpen}
/>
{/if}

{#if cancelEnabled}
<CancelConfirmationModal
{refresh}
{workflow}
{namespace}
bind:open={cancelConfirmationModalOpen}
/>
{/if}

{#if terminateEnabled}
<TerminateConfirmationModal
{refresh}
{workflow}
{namespace}
bind:open={terminateConfirmationModalOpen}
/>
{/if}
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,26 @@
selectedWorkflowsCount = $selectedWorkflows?.length ?? 0;
}
$: terminateEnabled = workflowTerminateEnabled($page.data.settings);
$: cancelEnabled = workflowCancelEnabled($page.data.settings);
$: terminateEnabled = workflowTerminateEnabled(
$page.data.settings,
$coreUser,
$page.params.namespace,
);
$: cancelEnabled = workflowCancelEnabled(
$page.data.settings,
$coreUser,
$page.params.namespace,
);
$: resetEnabled =
workflowResetEnabled($page.data.settings) && $isCloud
workflowResetEnabled(
$page.data.settings,
$coreUser,
$page.params.namespace,
) && $isCloud
? true
: minimumVersionRequired('1.23.0', $temporalVersion);
$: namespaceWriteDisabled = $coreUser.namespaceWriteDisabled(
$page.params.namespace,
);
</script>

{#if $allSelected}
Expand Down Expand Up @@ -84,7 +95,7 @@
variant="ghost"
class="text-off-white focus-visible:border-table"
data-testid="bulk-cancel-button"
disabled={namespaceWriteDisabled || !$cancelableWorkflows.length}
disabled={!$cancelableWorkflows.length}
on:click={openBatchCancelConfirmationModal}
>{translate('workflows.request-cancellation')}</Button
>
Expand All @@ -95,7 +106,6 @@
variant="ghost"
class="text-off-white focus-visible:border-table"
data-testid="bulk-reset-button"
disabled={namespaceWriteDisabled}
on:click={openBatchResetConfirmationModal}
>{translate('workflows.reset')}</Button
>
Expand All @@ -106,7 +116,6 @@
variant="destructive"
class="focus-visible:border-table"
data-testid="bulk-terminate-button"
disabled={namespaceWriteDisabled}
on:click={openBatchTerminateConfirmationModal}
>{translate('workflows.terminate')}</Button
>
Expand Down
2 changes: 1 addition & 1 deletion src/lib/i18n/locales/en/workflows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export const Strings = {
json: 'JSON',
download: 'Download',
'workflow-actions': 'Workflow Actions',
'reset-disabled':
'reset-disabled-unauthorized':
'Resetting workflows is not enabled, please contact your administrator for assistance.',
'reset-disabled-pending-children':
'Cannot reset workflows with pending children.',
Expand Down
69 changes: 50 additions & 19 deletions src/lib/utilities/workflow-cancel-enabled.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,73 @@ import { describe, expect, test } from 'vitest';
import { workflowCancelEnabled } from './workflow-cancel-enabled';

describe('workflowCancelEnabled', () => {
test('returns true when write actions are enabled, and when cancel is enabled', () => {
const coreUser = {
namespaceWriteDisabled: (ns: string) => ns === 'ns-write-disabled',
};

test('returns true when global write actions, cancel, and namespace write are all enabled', () => {
expect(
workflowCancelEnabled({
disableWriteActions: false,
worklowCancelDisabled: false,
}),
workflowCancelEnabled(
{
disableWriteActions: false,
worklowCancelDisabled: false,
},
coreUser,
'ns-write-enabled',
),
).toBe(true);
});

describe('returns false', () => {
test('when write actions are disabled', () => {
expect(
workflowCancelEnabled({
disableWriteActions: true,
workflowCancelDisabled: false,
}),
workflowCancelEnabled(
{
disableWriteActions: true,
workflowCancelDisabled: false,
},
coreUser,
'ns-write-enabled',
),
).toBe(false);
});

test('when cancel is disabled', () => {
expect(
workflowCancelEnabled({
runtimeEnvironment: { isCloud: false },
disableWriteActions: false,
workflowCancelDisabled: true,
}),
workflowCancelEnabled(
{
disableWriteActions: false,
workflowCancelDisabled: true,
},
coreUser,
'ns-write-enabled',
),
).toBe(false);
});

test('when write actions and cancel are both disabled', () => {
expect(
workflowCancelEnabled({
runtimeEnvironment: { isCloud: false },
disableWriteActions: true,
workflowCancelDisabled: true,
}),
workflowCancelEnabled(
{
disableWriteActions: true,
workflowCancelDisabled: true,
},
coreUser,
'ns-write-enabled',
),
).toBe(false);
});

test('when write actions are disabled on the namespace', () => {
expect(
workflowCancelEnabled(
{
disableWriteActions: false,
workflowCancelDisabled: false,
},
coreUser,
'ns-write-disabled',
),
).toBe(false);
});
});
Expand Down
13 changes: 11 additions & 2 deletions src/lib/utilities/workflow-cancel-enabled.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import type { CoreUser } from '$lib/models/core-user';
import type { Settings } from '$lib/types/global';

export const workflowCancelEnabled = (settings: Settings): boolean => {
return !settings.disableWriteActions && !settings.workflowCancelDisabled;
export const workflowCancelEnabled = (
settings: Settings,
coreUser: CoreUser,
namespace: string,
): boolean => {
return (
!settings.disableWriteActions &&
!settings.workflowCancelDisabled &&
!coreUser.namespaceWriteDisabled(namespace)
);
};
Loading

0 comments on commit 9616d0e

Please sign in to comment.