From 9bcc926a91d7afab0c2ef6eb57e818ef79e3a8f7 Mon Sep 17 00:00:00 2001 From: Marcus <56945030+maspio@users.noreply.github.com> Date: Fri, 12 Jul 2024 17:37:00 +0200 Subject: [PATCH] fix(GitHub Node): File Create operation prevent duplicated base64 encoding (#10040) --- .../nodes/Github/GenericFunctions.ts | 5 +++++ .../nodes-base/nodes/Github/Github.node.ts | 18 ++++++++++++------ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/packages/nodes-base/nodes/Github/GenericFunctions.ts b/packages/nodes-base/nodes/Github/GenericFunctions.ts index a27618b3db..901d080343 100644 --- a/packages/nodes-base/nodes/Github/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Github/GenericFunctions.ts @@ -114,3 +114,8 @@ export async function githubApiRequestAllItems( } while (responseData.headers.link?.includes('next')); return returnData; } + +export function isBase64(content: string) { + const base64regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/; + return base64regex.test(content); +} diff --git a/packages/nodes-base/nodes/Github/Github.node.ts b/packages/nodes-base/nodes/Github/Github.node.ts index fed69c4c3c..964c08756b 100644 --- a/packages/nodes-base/nodes/Github/Github.node.ts +++ b/packages/nodes-base/nodes/Github/Github.node.ts @@ -9,7 +9,12 @@ import type { import { NodeOperationError } from 'n8n-workflow'; import { snakeCase } from 'change-case'; -import { getFileSha, githubApiRequest, githubApiRequestAllItems } from './GenericFunctions'; +import { + getFileSha, + githubApiRequest, + githubApiRequestAllItems, + isBase64, +} from './GenericFunctions'; import { getRepositories, getUsers } from './SearchFunctions'; @@ -1981,11 +1986,12 @@ export class Github implements INodeType { // TODO: Does this work with filesystem mode body.content = binaryData.data; } else { - // Is text file - // body.content = Buffer.from(this.getNodeParameter('fileContent', i) as string, 'base64'); - body.content = Buffer.from( - this.getNodeParameter('fileContent', i) as string, - ).toString('base64'); + const fileContent = this.getNodeParameter('fileContent', i) as string; + if (isBase64(fileContent)) { + body.content = fileContent; + } else { + body.content = Buffer.from(fileContent).toString('base64'); + } } endpoint = `/repos/${owner}/${repository}/contents/${encodeURI(filePath)}`;