Skip to content

Commit

Permalink
[affected][bugfix]: maxBuffer for execSync set to 50MB for large git …
Browse files Browse the repository at this point in the history
…repos
  • Loading branch information
leblancmeneses committed Dec 17, 2024
1 parent 04b8f52 commit c4aaa40
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 40 deletions.
3 changes: 2 additions & 1 deletion apps/affected/src/changedFiles.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { execSync } from 'child_process';
import * as github from '@actions/github';
import { EXEC_SYNC_MAX_BUFFER } from './constants';

export enum ChangeStatus {
Added = 'added',
Expand Down Expand Up @@ -57,7 +58,7 @@ export const getChangedFiles = async (): Promise<ChangedFile[]> => {
baseDiffCommand = 'git diff --name-status HEAD~1 HEAD';
}

const output = execSync(baseDiffCommand, { encoding: 'utf-8' }).trim();
const output = execSync(baseDiffCommand, { encoding: 'utf-8', maxBuffer: EXEC_SYNC_MAX_BUFFER }).trim();
if (output) {
// Each line of output is formatted like: "<STATUS>\t<FILE_PATH>"
changedFiles = output
Expand Down
1 change: 1 addition & 0 deletions apps/affected/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const EXEC_SYNC_MAX_BUFFER = 50 * 1024 * 1024; // 50 MB buffer
3 changes: 2 additions & 1 deletion apps/affected/src/evaluateStatementsForHashes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import { execSync } from 'child_process';
import picomatch from 'picomatch';
import crypto from 'crypto';
import { AST, Expression } from './parser.types';
import { EXEC_SYNC_MAX_BUFFER } from './constants';

interface EvaluationResult {
matchedFiles: string[];
excludedFiles: string[];
}

export function allGitFiles() {
return execSync('git ls-files', { encoding: 'utf-8' }).split('\n').filter(Boolean);
return execSync('git ls-files', { encoding: 'utf-8', maxBuffer: EXEC_SYNC_MAX_BUFFER }).split('\n').filter(Boolean);
};

export async function evaluateStatementsForHashes(statements: AST, allFiles: string[]): Promise<Record<string, string>> {
Expand Down
9 changes: 5 additions & 4 deletions apps/e2e/src/affected/changed-files.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { execSync } from 'child_process';
import * as github from '@actions/github';
import { ChangeStatus, mapGitStatusCode, getChangedFiles } from "../../../affected/src/changedFiles";
import { EXEC_SYNC_MAX_BUFFER } from '../../../affected/src/constants';

jest.mock('child_process');
jest.mock('@actions/github', () => {
Expand Down Expand Up @@ -69,7 +70,7 @@ describe('changed-files.spec', () => {
mockExecSync.mockReturnValueOnce('');
const files = await getChangedFiles();
expect(files).toEqual([]);
expect(mockExecSync).toHaveBeenCalledWith('git diff --name-status base-sha head-sha', { encoding: 'utf-8' });
expect(mockExecSync).toHaveBeenCalledWith('git diff --name-status base-sha head-sha', { encoding: 'utf-8', maxBuffer: EXEC_SYNC_MAX_BUFFER });
});

it('parses changed files for a pull_request event', async () => {
Expand Down Expand Up @@ -99,23 +100,23 @@ describe('changed-files.spec', () => {
mockExecSync.mockReturnValueOnce('');

await getChangedFiles();
expect(mockExecSync).toHaveBeenCalledWith('git diff --name-status env-base-sha env-head-sha', { encoding: 'utf-8' });
expect(mockExecSync).toHaveBeenCalledWith('git diff --name-status env-base-sha env-head-sha', { encoding: 'utf-8', maxBuffer: EXEC_SYNC_MAX_BUFFER });
});

it('handles push event by comparing HEAD~1 and HEAD', async () => {
(github.context as any).eventName = 'push';
mockExecSync.mockReturnValueOnce('');

await getChangedFiles();
expect(mockExecSync).toHaveBeenCalledWith('git diff --name-status HEAD~1 HEAD', { encoding: 'utf-8' });
expect(mockExecSync).toHaveBeenCalledWith('git diff --name-status HEAD~1 HEAD', { encoding: 'utf-8', maxBuffer: EXEC_SYNC_MAX_BUFFER });
});

it('handles unknown events by falling back to HEAD~1 and HEAD', async () => {
(github.context as any).eventName = 'unknown_event';
mockExecSync.mockReturnValueOnce('');

await getChangedFiles();
expect(mockExecSync).toHaveBeenCalledWith('git diff --name-status HEAD~1 HEAD', { encoding: 'utf-8' });
expect(mockExecSync).toHaveBeenCalledWith('git diff --name-status HEAD~1 HEAD', { encoding: 'utf-8', maxBuffer: EXEC_SYNC_MAX_BUFFER });
});

it('handles filenames with tabs (unlikely, but safe)', async () => {
Expand Down
68 changes: 34 additions & 34 deletions dist/apps/affected/main.js

Large diffs are not rendered by default.

0 comments on commit c4aaa40

Please sign in to comment.