fix(GitHub Node): File Create operation prevent duplicated base64 encoding (#10040)

This commit is contained in:
Marcus 2024-07-12 17:37:00 +02:00 committed by GitHub
parent 46d6edc2a4
commit 9bcc926a91
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 17 additions and 6 deletions

View file

@ -114,3 +114,8 @@ export async function githubApiRequestAllItems(
} while (responseData.headers.link?.includes('next')); } while (responseData.headers.link?.includes('next'));
return returnData; 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);
}

View file

@ -9,7 +9,12 @@ import type {
import { NodeOperationError } from 'n8n-workflow'; import { NodeOperationError } from 'n8n-workflow';
import { snakeCase } from 'change-case'; import { snakeCase } from 'change-case';
import { getFileSha, githubApiRequest, githubApiRequestAllItems } from './GenericFunctions'; import {
getFileSha,
githubApiRequest,
githubApiRequestAllItems,
isBase64,
} from './GenericFunctions';
import { getRepositories, getUsers } from './SearchFunctions'; import { getRepositories, getUsers } from './SearchFunctions';
@ -1981,11 +1986,12 @@ export class Github implements INodeType {
// TODO: Does this work with filesystem mode // TODO: Does this work with filesystem mode
body.content = binaryData.data; body.content = binaryData.data;
} else { } else {
// Is text file const fileContent = this.getNodeParameter('fileContent', i) as string;
// body.content = Buffer.from(this.getNodeParameter('fileContent', i) as string, 'base64'); if (isBase64(fileContent)) {
body.content = Buffer.from( body.content = fileContent;
this.getNodeParameter('fileContent', i) as string, } else {
).toString('base64'); body.content = Buffer.from(fileContent).toString('base64');
}
} }
endpoint = `/repos/${owner}/${repository}/contents/${encodeURI(filePath)}`; endpoint = `/repos/${owner}/${repository}/contents/${encodeURI(filePath)}`;