Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(editor): Enable pinning main output with error and always allow unpinning #11452

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions packages/editor-ui/src/components/RunData.vue
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,11 @@ export default defineComponent({
return false;
}

if (this.outputIndex !== 0) {
// Only allow pinning of the main output
return false;
}

const canPinNode = usePinnedData(this.node).canPinNode(false);

return (
Expand Down Expand Up @@ -1209,9 +1214,7 @@ export default defineComponent({
<template>
<div :class="['run-data', $style.container]" @mouseover="activatePane">
<n8n-callout
v-if="
canPinData && pinnedData.hasData.value && !editMode.enabled && !isProductionExecutionPreview
"
v-if="pinnedData.hasData.value && !editMode.enabled && !isProductionExecutionPreview"
theme="secondary"
icon="thumbtack"
:class="$style.pinnedDataCallout"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { setActivePinia, createPinia } from 'pinia';
import { ref } from 'vue';
import { usePinnedData } from '@/composables/usePinnedData';
import type { INodeUi } from '@/Interface';
import { MAX_PINNED_DATA_SIZE } from '@/constants';
import { HTTP_REQUEST_NODE_TYPE, IF_NODE_TYPE, MAX_PINNED_DATA_SIZE } from '@/constants';
import { useWorkflowsStore } from '@/stores/workflows.store';
import { useTelemetry } from '@/composables/useTelemetry';
import { NodeConnectionType, type INodeTypeDescription } from 'n8n-workflow';

vi.mock('@/composables/useToast', () => ({ useToast: vi.fn(() => ({ showError: vi.fn() })) }));
vi.mock('@/composables/useI18n', () => ({
Expand All @@ -17,6 +18,13 @@ vi.mock('@/composables/useExternalHooks', () => ({
})),
}));

const getNodeType = vi.fn();
vi.mock('@/stores/nodeTypes.store', () => ({
useNodeTypesStore: vi.fn(() => ({
getNodeType,
})),
}));

describe('usePinnedData', () => {
beforeEach(() => {
setActivePinia(createPinia());
Expand Down Expand Up @@ -133,4 +141,71 @@ describe('usePinnedData', () => {
expect(spy).toHaveBeenCalled();
});
});

describe('canPinData()', () => {
afterEach(() => {
vi.clearAllMocks();
});

it('allows pin on single output', async () => {
const node = ref({
name: 'single output node',
typeVersion: 1,
type: HTTP_REQUEST_NODE_TYPE,

parameters: {},
onError: 'stopWorkflow',
} as INodeUi);
getNodeType.mockReturnValue(makeNodeType([NodeConnectionType.Main], HTTP_REQUEST_NODE_TYPE));

const { canPinNode } = usePinnedData(node);

expect(canPinNode()).toBe(true);
});

it('allows pin on one main and one error output', async () => {
const node = ref({
name: 'single output node',
typeVersion: 1,
type: HTTP_REQUEST_NODE_TYPE,
parameters: {},
onError: 'continueErrorOutput',
} as INodeUi);
getNodeType.mockReturnValue(makeNodeType([NodeConnectionType.Main], HTTP_REQUEST_NODE_TYPE));

const { canPinNode } = usePinnedData(node);

expect(canPinNode()).toBe(true);
});

it('does not allow pin on two main outputs', async () => {
const node = ref({
name: 'single output node',
typeVersion: 1,
type: IF_NODE_TYPE,
parameters: {},
onError: 'stopWorkflow',
} as INodeUi);
getNodeType.mockReturnValue(
makeNodeType([NodeConnectionType.Main, NodeConnectionType.Main], IF_NODE_TYPE),
);

const { canPinNode } = usePinnedData(node);

expect(canPinNode()).toBe(false);
});
});
});

const makeNodeType = (outputs: NodeConnectionType[], name: string) =>
({
displayName: name,
name,
version: [1],
inputs: [],
outputs,
properties: [],
defaults: { color: '', name: '' },
group: [],
description: '',
}) as INodeTypeDescription;
16 changes: 9 additions & 7 deletions packages/editor-ui/src/composables/usePinnedData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,16 @@ export function usePinnedData(
if (!nodeType || (checkDataEmpty && dataToPin.length === 0)) return false;

const workflow = workflowsStore.getCurrentWorkflow();
const outputs = NodeHelpers.getNodeOutputs(workflow, targetNode, nodeType);
const mainOutputs = outputs.filter((output) =>
typeof output === 'string'
? output === NodeConnectionType.Main
: output.type === NodeConnectionType.Main,
);
const mainOutputs = NodeHelpers.getNodeOutputs(workflow, targetNode, nodeType)
.map((output) => (typeof output === 'string' ? { type: output } : output))
.filter((output) => output.type === NodeConnectionType.Main);

// Outputs are pinnable if there is exactly one main output and optionally one error output
const pinnableMainOutputs =
mainOutputs.length === 1 ||
(mainOutputs.length === 2 && mainOutputs[1]?.category === 'error');

return mainOutputs.length === 1 && !PIN_DATA_NODE_TYPES_DENYLIST.includes(targetNode.type);
return pinnableMainOutputs && !PIN_DATA_NODE_TYPES_DENYLIST.includes(targetNode.type);
}

function isValidJSON(data: string): boolean {
Expand Down