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): Prevent workflow breaking when credential type is unknown #6923

Merged
merged 1 commit into from
Aug 14, 2023
Merged
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
2 changes: 1 addition & 1 deletion packages/editor-ui/src/components/CredentialCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export default defineComponent({
currentUser(): IUser | null {
return this.usersStore.currentUser;
},
credentialType(): ICredentialType {
credentialType(): ICredentialType | undefined {
return this.credentialsStore.getCredentialTypeByName(this.data.type);
},
credentialPermissions(): IPermissions | null {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ export default defineComponent({

// If there is already selected type, use it
if (this.selectedCredential !== '') {
return this.credentialsStore.getCredentialTypeByName(this.selectedCredential);
return this.credentialsStore.getCredentialTypeByName(this.selectedCredential) ?? null;
} else if (this.requiredCredentials) {
// Otherwise, use credential type that corresponds to the first auth option in the node definition
const nodeAuthOptions = getNodeAuthOptions(this.activeNodeType);
Expand Down
1 change: 1 addition & 0 deletions packages/editor-ui/src/components/CredentialsSelect.vue
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ export default defineComponent({
const supported = this.getSupportedSets(this.parameter.credentialTypes);

const checkedCredType = this.credentialsStore.getCredentialTypeByName(name);
if (!checkedCredType) return false;

for (const property of supported.has) {
if (checkedCredType[property as keyof ICredentialType] !== undefined) {
Expand Down
30 changes: 13 additions & 17 deletions packages/editor-ui/src/components/NodeCredentials.vue
Original file line number Diff line number Diff line change
Expand Up @@ -291,14 +291,14 @@ export default defineComponent({
});
},
credentialTypesNodeDescription(): INodeCredentialDescription[] {
const node = this.node as INodeUi;
const node = this.node;

const credType = this.credentialsStore.getCredentialTypeByName(this.overrideCredType);

if (credType) return [credType];

const activeNodeType = this.nodeTypesStore.getNodeType(node.type, node.typeVersion);
if (activeNodeType && activeNodeType.credentials) {
if (activeNodeType?.credentials) {
return activeNodeType.credentials;
}

Expand All @@ -308,11 +308,12 @@ export default defineComponent({
const returnData: {
[key: string]: string;
} = {};
let credentialType: ICredentialType | null;
let credentialType: ICredentialType | undefined;
for (const credentialTypeName of this.credentialTypesNode) {
credentialType = this.credentialsStore.getCredentialTypeByName(credentialTypeName);
returnData[credentialTypeName] =
credentialType !== null ? credentialType.displayName : credentialTypeName;
returnData[credentialTypeName] = credentialType
? credentialType.displayName
: credentialTypeName;
}
return returnData;
},
Expand Down Expand Up @@ -347,7 +348,7 @@ export default defineComponent({
options = options.concat(
this.credentialsStore.allUsableCredentialsByType[type].map((option: any) => ({
...option,
typeDisplayName: this.credentialsStore.getCredentialTypeByName(type).displayName,
typeDisplayName: this.credentialsStore.getCredentialTypeByName(type)?.displayName,
})),
);
});
Expand Down Expand Up @@ -436,10 +437,9 @@ export default defineComponent({

const selectedCredentials = this.credentialsStore.getCredentialById(credentialId);
const selectedCredentialsType = this.showAll ? selectedCredentials.type : credentialType;
const oldCredentials =
this.node.credentials && this.node.credentials[selectedCredentialsType]
? this.node.credentials[selectedCredentialsType]
: {};
const oldCredentials = this.node.credentials?.[selectedCredentialsType]
? this.node.credentials[selectedCredentialsType]
: {};

const selected = { id: selectedCredentials.id, name: selectedCredentials.name };

Expand Down Expand Up @@ -513,9 +513,9 @@ export default defineComponent({
},

getIssues(credentialTypeName: string): string[] {
const node = this.node as INodeUi;
const node = this.node;

if (node.issues === undefined || node.issues.credentials === undefined) {
if (node.issues?.credentials === undefined) {
return [];
}

Expand All @@ -526,11 +526,7 @@ export default defineComponent({
},

isCredentialExisting(credentialType: string): boolean {
if (
!this.node.credentials ||
!this.node.credentials[credentialType] ||
!this.node.credentials[credentialType].id
) {
if (!this.node.credentials?.[credentialType]?.id) {
return false;
}
const { id } = this.node.credentials[credentialType];
Expand Down
10 changes: 6 additions & 4 deletions packages/editor-ui/src/components/ScopesNotice.vue
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ export default defineComponent({
const oauth1Api = this.$locale.baseText('generic.oauth1Api');
const oauth2Api = this.$locale.baseText('generic.oauth2Api');

return this.credentialsStore
.getCredentialTypeByName(this.activeCredentialType)
.displayName.replace(new RegExp(`${oauth1Api}|${oauth2Api}`), '')
.trim();
return (
this.credentialsStore
.getCredentialTypeByName(this.activeCredentialType)
?.displayName.replace(new RegExp(`${oauth1Api}|${oauth2Api}`), '')
.trim() || ''
);
},
},
});
Expand Down
29 changes: 10 additions & 19 deletions packages/editor-ui/src/mixins/nodeHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export const nodeHelpers = defineComponent({
}
}

if (this.hasNodeExecutionIssues(node) === true && !ignoreIssues.includes('execution')) {
if (this.hasNodeExecutionIssues(node) && !ignoreIssues.includes('execution')) {
if (nodeIssues === null) {
nodeIssues = {};
}
Expand Down Expand Up @@ -245,7 +245,7 @@ export const nodeHelpers = defineComponent({
const foundIssues: INodeIssueObjectProperty = {};

let userCredentials: ICredentialsResponse[] | null;
let credentialType: ICredentialType | null;
let credentialType: ICredentialType | undefined;
let credentialDisplayName: string;
let selectedCredentials: INodeCredentialsDetails;

Expand All @@ -258,7 +258,7 @@ export const nodeHelpers = defineComponent({
selectedCredsAreUnusable(node, genericAuthType)
) {
const credential = this.credentialsStore.getCredentialTypeByName(genericAuthType);
return this.reportUnsetCredential(credential);
return credential ? this.reportUnsetCredential(credential) : null;
}

if (
Expand All @@ -271,7 +271,7 @@ export const nodeHelpers = defineComponent({

if (selectedCredsDoNotExist(node, nodeCredentialType, stored)) {
const credential = this.credentialsStore.getCredentialTypeByName(nodeCredentialType);
return this.reportUnsetCredential(credential);
return credential ? this.reportUnsetCredential(credential) : null;
}
}

Expand All @@ -282,7 +282,7 @@ export const nodeHelpers = defineComponent({
selectedCredsAreUnusable(node, nodeCredentialType)
) {
const credential = this.credentialsStore.getCredentialTypeByName(nodeCredentialType);
return this.reportUnsetCredential(credential);
return credential ? this.reportUnsetCredential(credential) : null;
}

for (const credentialTypeDescription of nodeType.credentials) {
Expand All @@ -301,7 +301,7 @@ export const nodeHelpers = defineComponent({
credentialDisplayName = credentialType.displayName;
}

if (!node.credentials || !node.credentials?.[credentialTypeDescription.name]) {
if (!node.credentials?.[credentialTypeDescription.name]) {
// Credentials are not set
if (credentialTypeDescription.required) {
foundIssues[credentialTypeDescription.name] = [
Expand All @@ -312,9 +312,7 @@ export const nodeHelpers = defineComponent({
}
} else {
// If they are set check if the value is valid
selectedCredentials = node.credentials[
credentialTypeDescription.name
] as INodeCredentialsDetails;
selectedCredentials = node.credentials[credentialTypeDescription.name];
if (typeof selectedCredentials === 'string') {
selectedCredentials = {
id: null,
Expand Down Expand Up @@ -409,16 +407,14 @@ export const nodeHelpers = defineComponent({
return [];
}
const executionData = this.workflowsStore.getWorkflowExecution.data;
if (!executionData || !executionData.resultData) {
if (!executionData?.resultData) {
// unknown status
return [];
}
const runData = executionData.resultData.runData;

if (
runData === null ||
runData[node.name] === undefined ||
!runData[node.name][runIndex].data ||
!runData?.[node.name]?.[runIndex].data ||
runData[node.name][runIndex].data === undefined
) {
return [];
Expand Down Expand Up @@ -457,12 +453,7 @@ export const nodeHelpers = defineComponent({

const runData: IRunData | null = workflowRunData;

if (
runData === null ||
!runData[node] ||
!runData[node][runIndex] ||
!runData[node][runIndex].data
) {
if (!runData?.[node]?.[runIndex]?.data) {
return [];
}

Expand Down
35 changes: 18 additions & 17 deletions packages/editor-ui/src/stores/credentials.store.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import type { INodeUi, IUsedCredential } from '../Interface';
import type {
INodeUi,
IUsedCredential,
ICredentialMap,
ICredentialsDecryptedResponse,
ICredentialsResponse,
ICredentialsState,
ICredentialTypeMap,
} from '../Interface';
import {
createNewCredential,
deleteCredential,
Expand All @@ -15,19 +23,11 @@ import { setCredentialSharedWith } from '@/api/credentials.ee';
import { makeRestApiRequest } from '@/utils/apiUtils';
import { getAppNameFromCredType } from '@/utils/nodeTypesUtils';
import { EnterpriseEditionFeature, STORES } from '@/constants';
import type {
ICredentialMap,
ICredentialsDecryptedResponse,
ICredentialsResponse,
ICredentialsState,
ICredentialTypeMap,
} from '@/Interface';
import { i18n } from '@/plugins/i18n';
import type {
ICredentialsDecrypted,
ICredentialType,
INodeCredentialTestResult,
INodeProperties,
INodeTypeDescription,
IUser,
} from 'n8n-workflow';
Expand Down Expand Up @@ -77,7 +77,7 @@ export const useCredentialsStore = defineStore(STORES.CREDENTIALS, {
return (node: INodeUi): ICredentialsResponse[] => {
let credentials: ICredentialsResponse[] = [];
const nodeType = useNodeTypesStore().getNodeType(node.type, node.typeVersion);
if (nodeType && nodeType.credentials) {
if (nodeType?.credentials) {
nodeType.credentials.forEach((cred) => {
credentials = credentials.concat(this.allUsableCredentialsByType[cred.name]);
});
Expand Down Expand Up @@ -106,7 +106,7 @@ export const useCredentialsStore = defineStore(STORES.CREDENTIALS, {
);
},
getCredentialTypeByName() {
return (type: string): ICredentialType => this.credentialTypes[type];
return (type: string): ICredentialType | undefined => this.credentialTypes[type];
},
getCredentialById() {
return (id: string): ICredentialsResponse => this.credentials[id];
Expand Down Expand Up @@ -149,9 +149,10 @@ export const useCredentialsStore = defineStore(STORES.CREDENTIALS, {
},
getScopesByCredentialType() {
return (credentialTypeName: string) => {
const credentialType = this.getCredentialTypeByName(credentialTypeName) as {
properties: INodeProperties[];
};
const credentialType = this.getCredentialTypeByName(credentialTypeName);
if (!credentialType) {
return [];
}

const scopeProperty = credentialType.properties.find((p) => p.name === 'scope');

Expand Down Expand Up @@ -232,7 +233,7 @@ export const useCredentialsStore = defineStore(STORES.CREDENTIALS, {
// enable oauth event to track change between modals
},
async fetchCredentialTypes(forceFetch: boolean): Promise<void> {
if (this.allCredentialTypes.length > 0 && forceFetch !== true) {
if (this.allCredentialTypes.length > 0 && !forceFetch) {
return;
}
const rootStore = useRootStore();
Expand Down Expand Up @@ -337,8 +338,8 @@ export const useCredentialsStore = defineStore(STORES.CREDENTIALS, {
const { credentialTypeName } = params;
let newName = DEFAULT_CREDENTIAL_NAME;
if (!TYPES_WITH_DEFAULT_NAME.includes(credentialTypeName)) {
const { displayName } = this.getCredentialTypeByName(credentialTypeName);
newName = getAppNameFromCredType(displayName);
const cred = this.getCredentialTypeByName(credentialTypeName);
newName = cred ? getAppNameFromCredType(cred.displayName) : '';
newName =
newName.length > 0
? `${newName} ${DEFAULT_CREDENTIAL_POSTFIX}`
Expand Down
2 changes: 1 addition & 1 deletion packages/editor-ui/src/utils/nodeTypesUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ export const getNodeAuthOptions = (
const cred = getNodeCredentialForSelectedAuthType(nodeType, option.value);
if (cred) {
hasOverrides =
useCredentialsStore().getCredentialTypeByName(cred.name).__overwrittenProperties !==
useCredentialsStore().getCredentialTypeByName(cred.name)?.__overwrittenProperties !==
undefined;
}
return {
Expand Down
Loading