Skip to content

Commit

Permalink
fix(GitHub Node): File Create operation prevent duplicated base64 enc…
Browse files Browse the repository at this point in the history
…oding (#10040)
  • Loading branch information
maspio authored Jul 12, 2024
1 parent 46d6edc commit 9bcc926
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
5 changes: 5 additions & 0 deletions packages/nodes-base/nodes/Github/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
18 changes: 12 additions & 6 deletions packages/nodes-base/nodes/Github/Github.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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)}`;
Expand Down

0 comments on commit 9bcc926

Please sign in to comment.