From bf28f8270d9e5d9ab3906afec9e1e06cab906e5c Mon Sep 17 00:00:00 2001 From: Graham Christensen Date: Thu, 20 Feb 2025 15:03:50 -0500 Subject: [PATCH 1/7] GitCommandManager: add a function to get a file's contents at a specific revision --- src/git-command-manager.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/git-command-manager.ts b/src/git-command-manager.ts index 9be35e4144..303c4b5360 100644 --- a/src/git-command-manager.ts +++ b/src/git-command-manager.ts @@ -278,6 +278,12 @@ export class GitCommandManager { return output.stdout.trim() } + async showFileAtRef(ref: string, path: string): Promise { + const args = ['show', `${ref}:${path}`] + const output = await this.exec(args) + return output.stdout.trim() + } + async stashPush(options?: string[]): Promise { const args = ['stash', 'push'] if (options) { From c2125cf01833d9ec201a12c202e73c0edc3bba77 Mon Sep 17 00:00:00 2001 From: Graham Christensen Date: Thu, 20 Feb 2025 15:08:16 -0500 Subject: [PATCH 2/7] use showFileAtRef instead of readFileBase64 --- src/create-pull-request.ts | 1 + src/github-helper.ts | 7 +++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/create-pull-request.ts b/src/create-pull-request.ts index 9076b7df75..0ab4f8cc17 100644 --- a/src/create-pull-request.ts +++ b/src/create-pull-request.ts @@ -211,6 +211,7 @@ export async function createPullRequest(inputs: Inputs): Promise { const stashed = await git.stashPush(['--include-untracked']) await git.checkout(inputs.branch) const pushSignedCommitsResult = await ghBranch.pushSignedCommits( + git, result.branchCommits, result.baseCommit, repoPath, diff --git a/src/github-helper.ts b/src/github-helper.ts index 82a9296a21..0aeb4e9eea 100644 --- a/src/github-helper.ts +++ b/src/github-helper.ts @@ -1,6 +1,6 @@ import * as core from '@actions/core' import {Inputs} from './create-pull-request' -import {Commit} from './git-command-manager' +import {Commit, GitCommandManager} from './git-command-manager' import {Octokit, OctokitOptions, throttleOptions} from './octokit-client' import pLimit from 'p-limit' import * as utils from './utils' @@ -220,6 +220,7 @@ export class GitHubHelper { } async pushSignedCommits( + git: GitCommandManager, branchCommits: Commit[], baseCommit: Commit, repoPath: string, @@ -233,6 +234,7 @@ export class GitHubHelper { } for (const commit of branchCommits) { headCommit = await this.createCommit( + git, commit, headCommit, repoPath, @@ -244,6 +246,7 @@ export class GitHubHelper { } private async createCommit( + git: GitCommandManager, commit: Commit, parentCommit: CommitResponse, repoPath: string, @@ -272,7 +275,7 @@ export class GitHubHelper { const {data: blob} = await blobCreationLimit(() => this.octokit.rest.git.createBlob({ ...repository, - content: utils.readFileBase64([repoPath, path]), + content: git.showFileAtRef(commit.sha, path), encoding: 'base64' }) ) From 65c46285dc50099d7f301843124d0748f8adfe73 Mon Sep 17 00:00:00 2001 From: Graham Christensen Date: Thu, 20 Feb 2025 15:25:24 -0500 Subject: [PATCH 3/7] Teach GitCommandManager.exec about an object of exec parameters so we can add more --- src/create-or-update-branch.ts | 4 ++-- src/git-command-manager.ts | 20 ++++++++++++-------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/create-or-update-branch.ts b/src/create-or-update-branch.ts index 2eecd45d13..746a3a0381 100644 --- a/src/create-or-update-branch.ts +++ b/src/create-or-update-branch.ts @@ -19,7 +19,7 @@ export async function getWorkingBaseAndType( ): Promise<[string, WorkingBaseType]> { const symbolicRefResult = await git.exec( ['symbolic-ref', 'HEAD', '--short'], - true + {allowAllExitCodes: true} ) if (symbolicRefResult.exitCode == 0) { // A ref is checked out @@ -200,7 +200,7 @@ export async function createOrUpdateBranch( } else { aopts.push('-A') } - await git.exec(aopts, true) + await git.exec(aopts, {allowAllExitCodes: true}) const popts = ['-m', commitMessage] if (signoff) { popts.push('--signoff') diff --git a/src/git-command-manager.ts b/src/git-command-manager.ts index 303c4b5360..b5cc5721a0 100644 --- a/src/git-command-manager.ts +++ b/src/git-command-manager.ts @@ -21,6 +21,10 @@ export type Commit = { unparsedChanges: string[] } +export type ExecOpts = { + allowAllExitCodes?: boolean, +} + export class GitCommandManager { private gitPath: string private workingDirectory: string @@ -66,7 +70,7 @@ export class GitCommandManager { args.push(...options) } - return await this.exec(args, allowAllExitCodes) + return await this.exec(args, {allowAllExitCodes: allowAllExitCodes}) } async commit( @@ -82,7 +86,7 @@ export class GitCommandManager { args.push(...options) } - return await this.exec(args, allowAllExitCodes) + return await this.exec(args, {allowAllExitCodes: allowAllExitCodes}) } async config( @@ -113,7 +117,7 @@ export class GitCommandManager { configKey, configValue ], - true + {allowAllExitCodes: true} ) return output.exitCode === 0 } @@ -222,7 +226,7 @@ export class GitCommandManager { if (options) { args.push(...options) } - const output = await this.exec(args, true) + const output = await this.exec(args, {allowAllExitCodes: true}) return output.exitCode === 1 } @@ -332,7 +336,7 @@ export class GitCommandManager { configKey, configValue ], - true + {allowAllExitCodes: true} ) return output.exitCode === 0 } @@ -340,7 +344,7 @@ export class GitCommandManager { async tryGetRemoteUrl(): Promise { const output = await this.exec( ['config', '--local', '--get', 'remote.origin.url'], - true + {allowAllExitCodes: true} ) if (output.exitCode !== 0) { @@ -355,7 +359,7 @@ export class GitCommandManager { return stdout } - async exec(args: string[], allowAllExitCodes = false): Promise { + async exec(args: string[], opts: ExecOpts = {}): Promise { const result = new GitOutput() const env = {} @@ -369,7 +373,7 @@ export class GitCommandManager { const options = { cwd: this.workingDirectory, env, - ignoreReturnCode: allowAllExitCodes, + ignoreReturnCode: opts.allowAllExitCodes ?? false, listeners: { stdout: (data: Buffer) => { stdout.push(data.toString()) From 4e9b4181aa7d89674098826beec42dbcb6bb719a Mon Sep 17 00:00:00 2001 From: Graham Christensen Date: Thu, 20 Feb 2025 15:27:30 -0500 Subject: [PATCH 4/7] Encode the showFiletRef output as base64 out of the gate --- src/git-command-manager.ts | 11 ++++++----- src/github-helper.ts | 2 +- src/utils.ts | 10 ---------- 3 files changed, 7 insertions(+), 16 deletions(-) diff --git a/src/git-command-manager.ts b/src/git-command-manager.ts index b5cc5721a0..47e3937f99 100644 --- a/src/git-command-manager.ts +++ b/src/git-command-manager.ts @@ -22,7 +22,8 @@ export type Commit = { } export type ExecOpts = { - allowAllExitCodes?: boolean, + allowAllExitCodes?: boolean + encoding?: 'utf8' | 'base64' } export class GitCommandManager { @@ -282,9 +283,9 @@ export class GitCommandManager { return output.stdout.trim() } - async showFileAtRef(ref: string, path: string): Promise { + async showFileAtRefBase64(ref: string, path: string): Promise { const args = ['show', `${ref}:${path}`] - const output = await this.exec(args) + const output = await this.exec(args, {encoding: 'base64'}) return output.stdout.trim() } @@ -376,10 +377,10 @@ export class GitCommandManager { ignoreReturnCode: opts.allowAllExitCodes ?? false, listeners: { stdout: (data: Buffer) => { - stdout.push(data.toString()) + stdout.push(data.toString(opts.encoding ?? 'utf8')) }, stderr: (data: Buffer) => { - stderr.push(data.toString()) + stderr.push(data.toString(opts.encoding ?? 'utf8')) } } } diff --git a/src/github-helper.ts b/src/github-helper.ts index 0aeb4e9eea..fb541b4088 100644 --- a/src/github-helper.ts +++ b/src/github-helper.ts @@ -275,7 +275,7 @@ export class GitHubHelper { const {data: blob} = await blobCreationLimit(() => this.octokit.rest.git.createBlob({ ...repository, - content: git.showFileAtRef(commit.sha, path), + content: await git.showFileAtRefBase64(commit.sha, path), encoding: 'base64' }) ) diff --git a/src/utils.ts b/src/utils.ts index ced3cbdfb6..b501dd4841 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -126,16 +126,6 @@ export function readFile(path: string): string { return fs.readFileSync(path, 'utf-8') } -export function readFileBase64(pathParts: string[]): string { - const resolvedPath = path.resolve(...pathParts) - if (fs.lstatSync(resolvedPath).isSymbolicLink()) { - return fs - .readlinkSync(resolvedPath, {encoding: 'buffer'}) - .toString('base64') - } - return fs.readFileSync(resolvedPath).toString('base64') -} - /* eslint-disable @typescript-eslint/no-explicit-any */ function hasErrorCode(error: any): error is {code: string} { return typeof (error && error.code) === 'string' From af869eb4a040e66494708a476e577a5c8befee99 Mon Sep 17 00:00:00 2001 From: gustavderdrache Date: Thu, 20 Feb 2025 15:43:47 -0500 Subject: [PATCH 5/7] Fix missing async for function --- dist/index.js | 56 +++++++++++++++++++++++--------------------- src/github-helper.ts | 2 +- 2 files changed, 30 insertions(+), 28 deletions(-) diff --git a/dist/index.js b/dist/index.js index 1f1f6090f7..993ad462ef 100644 --- a/dist/index.js +++ b/dist/index.js @@ -67,7 +67,7 @@ var WorkingBaseType; })(WorkingBaseType || (exports.WorkingBaseType = WorkingBaseType = {})); function getWorkingBaseAndType(git) { return __awaiter(this, void 0, void 0, function* () { - const symbolicRefResult = yield git.exec(['symbolic-ref', 'HEAD', '--short'], true); + const symbolicRefResult = yield git.exec(['symbolic-ref', 'HEAD', '--short'], { allowAllExitCodes: true }); if (symbolicRefResult.exitCode == 0) { // A ref is checked out return [symbolicRefResult.stdout.trim(), WorkingBaseType.Branch]; @@ -194,7 +194,7 @@ function createOrUpdateBranch(git, commitMessage, base, branch, branchRemoteName else { aopts.push('-A'); } - yield git.exec(aopts, true); + yield git.exec(aopts, { allowAllExitCodes: true }); const popts = ['-m', commitMessage]; if (signoff) { popts.push('--signoff'); @@ -517,7 +517,7 @@ function createPullRequest(inputs) { // Create signed commits via the GitHub API const stashed = yield git.stashPush(['--include-untracked']); yield git.checkout(inputs.branch); - const pushSignedCommitsResult = yield ghBranch.pushSignedCommits(result.branchCommits, result.baseCommit, repoPath, branchRepository, inputs.branch); + const pushSignedCommitsResult = yield ghBranch.pushSignedCommits(git, result.branchCommits, result.baseCommit, repoPath, branchRepository, inputs.branch); outputs.set('pull-request-head-sha', pushSignedCommitsResult.sha); outputs.set('pull-request-commits-verified', pushSignedCommitsResult.verified.toString()); yield git.checkout('-'); @@ -704,7 +704,7 @@ class GitCommandManager { if (options) { args.push(...options); } - return yield this.exec(args, allowAllExitCodes); + return yield this.exec(args, { allowAllExitCodes: allowAllExitCodes }); }); } commit(options_1) { @@ -716,7 +716,7 @@ class GitCommandManager { if (options) { args.push(...options); } - return yield this.exec(args, allowAllExitCodes); + return yield this.exec(args, { allowAllExitCodes: allowAllExitCodes }); }); } config(configKey, configValue, globalConfig, add) { @@ -738,7 +738,7 @@ class GitCommandManager { '--get-regexp', configKey, configValue - ], true); + ], { allowAllExitCodes: true }); return output.exitCode === 0; }); } @@ -835,7 +835,7 @@ class GitCommandManager { if (options) { args.push(...options); } - const output = yield this.exec(args, true); + const output = yield this.exec(args, { allowAllExitCodes: true }); return output.exitCode === 1; }); } @@ -892,6 +892,13 @@ class GitCommandManager { return output.stdout.trim(); }); } + showFileAtRefBase64(ref, path) { + return __awaiter(this, void 0, void 0, function* () { + const args = ['show', `${ref}:${path}`]; + const output = yield this.exec(args, { encoding: 'base64' }); + return output.stdout.trim(); + }); + } stashPush(options) { return __awaiter(this, void 0, void 0, function* () { const args = ['stash', 'push']; @@ -939,13 +946,13 @@ class GitCommandManager { '--unset', configKey, configValue - ], true); + ], { allowAllExitCodes: true }); return output.exitCode === 0; }); } tryGetRemoteUrl() { return __awaiter(this, void 0, void 0, function* () { - const output = yield this.exec(['config', '--local', '--get', 'remote.origin.url'], true); + const output = yield this.exec(['config', '--local', '--get', 'remote.origin.url'], { allowAllExitCodes: true }); if (output.exitCode !== 0) { return ''; } @@ -957,7 +964,8 @@ class GitCommandManager { }); } exec(args_1) { - return __awaiter(this, arguments, void 0, function* (args, allowAllExitCodes = false) { + return __awaiter(this, arguments, void 0, function* (args, opts = {}) { + var _a; const result = new GitOutput(); const env = {}; for (const key of Object.keys(process.env)) { @@ -968,13 +976,15 @@ class GitCommandManager { const options = { cwd: this.workingDirectory, env, - ignoreReturnCode: allowAllExitCodes, + ignoreReturnCode: (_a = opts.allowAllExitCodes) !== null && _a !== void 0 ? _a : false, listeners: { stdout: (data) => { - stdout.push(data.toString()); + var _a; + stdout.push(data.toString((_a = opts.encoding) !== null && _a !== void 0 ? _a : 'utf8')); }, stderr: (data) => { - stderr.push(data.toString()); + var _a; + stderr.push(data.toString((_a = opts.encoding) !== null && _a !== void 0 ? _a : 'utf8')); } } }; @@ -1400,7 +1410,7 @@ class GitHubHelper { return pull; }); } - pushSignedCommits(branchCommits, baseCommit, repoPath, branchRepository, branch) { + pushSignedCommits(git, branchCommits, baseCommit, repoPath, branchRepository, branch) { return __awaiter(this, void 0, void 0, function* () { let headCommit = { sha: baseCommit.sha, @@ -1408,13 +1418,13 @@ class GitHubHelper { verified: false }; for (const commit of branchCommits) { - headCommit = yield this.createCommit(commit, headCommit, repoPath, branchRepository); + headCommit = yield this.createCommit(git, commit, headCommit, repoPath, branchRepository); } yield this.createOrUpdateRef(branchRepository, branch, headCommit.sha); return headCommit; }); } - createCommit(commit, parentCommit, repoPath, branchRepository) { + createCommit(git, commit, parentCommit, repoPath, branchRepository) { return __awaiter(this, void 0, void 0, function* () { const repository = this.parseRepository(branchRepository); // In the case of an empty commit, the tree references the parent's tree @@ -1436,7 +1446,9 @@ class GitHubHelper { let sha = null; if (status === 'A' || status === 'M') { try { - const { data: blob } = yield blobCreationLimit(() => this.octokit.rest.git.createBlob(Object.assign(Object.assign({}, repository), { content: utils.readFileBase64([repoPath, path]), encoding: 'base64' }))); + const { data: blob } = yield blobCreationLimit(() => __awaiter(this, void 0, void 0, function* () { + return this.octokit.rest.git.createBlob(Object.assign(Object.assign({}, repository), { content: yield git.showFileAtRefBase64(commit.sha, path), encoding: 'base64' })); + })); sha = blob.sha; } catch (error) { @@ -1763,7 +1775,6 @@ exports.randomString = randomString; exports.parseDisplayNameEmail = parseDisplayNameEmail; exports.fileExistsSync = fileExistsSync; exports.readFile = readFile; -exports.readFileBase64 = readFileBase64; exports.getErrorMessage = getErrorMessage; const core = __importStar(__nccwpck_require__(7484)); const fs = __importStar(__nccwpck_require__(9896)); @@ -1853,15 +1864,6 @@ function fileExistsSync(path) { function readFile(path) { return fs.readFileSync(path, 'utf-8'); } -function readFileBase64(pathParts) { - const resolvedPath = path.resolve(...pathParts); - if (fs.lstatSync(resolvedPath).isSymbolicLink()) { - return fs - .readlinkSync(resolvedPath, { encoding: 'buffer' }) - .toString('base64'); - } - return fs.readFileSync(resolvedPath).toString('base64'); -} /* eslint-disable @typescript-eslint/no-explicit-any */ function hasErrorCode(error) { return typeof (error && error.code) === 'string'; diff --git a/src/github-helper.ts b/src/github-helper.ts index fb541b4088..6d41e06d72 100644 --- a/src/github-helper.ts +++ b/src/github-helper.ts @@ -272,7 +272,7 @@ export class GitHubHelper { let sha: string | null = null if (status === 'A' || status === 'M') { try { - const {data: blob} = await blobCreationLimit(() => + const {data: blob} = await blobCreationLimit(async () => this.octokit.rest.git.createBlob({ ...repository, content: await git.showFileAtRefBase64(commit.sha, path), From 805b174a6e04e51fbbf8a1ba5d85079b5913737c Mon Sep 17 00:00:00 2001 From: gustavderdrache Date: Thu, 20 Feb 2025 15:58:56 -0500 Subject: [PATCH 6/7] Use Buffer.concat to avoid issues with partial data streams --- dist/index.js | 19 ++++++++++--------- src/git-command-manager.ts | 20 ++++++++++++-------- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/dist/index.js b/dist/index.js index 993ad462ef..0ce0204bb4 100644 --- a/dist/index.js +++ b/dist/index.js @@ -964,33 +964,34 @@ class GitCommandManager { }); } exec(args_1) { - return __awaiter(this, arguments, void 0, function* (args, opts = {}) { - var _a; + return __awaiter(this, arguments, void 0, function* (args, { encoding = 'utf8', allowAllExitCodes = false } = {}) { const result = new GitOutput(); const env = {}; for (const key of Object.keys(process.env)) { env[key] = process.env[key]; } const stdout = []; + let stdoutLength = 0; const stderr = []; + let stderrLength = 0; const options = { cwd: this.workingDirectory, env, - ignoreReturnCode: (_a = opts.allowAllExitCodes) !== null && _a !== void 0 ? _a : false, + ignoreReturnCode: allowAllExitCodes, listeners: { stdout: (data) => { - var _a; - stdout.push(data.toString((_a = opts.encoding) !== null && _a !== void 0 ? _a : 'utf8')); + stdout.push(data); + stdoutLength += data.length; }, stderr: (data) => { - var _a; - stderr.push(data.toString((_a = opts.encoding) !== null && _a !== void 0 ? _a : 'utf8')); + stderr.push(data); + stderrLength += data.length; } } }; result.exitCode = yield exec.exec(`"${this.gitPath}"`, args, options); - result.stdout = stdout.join(''); - result.stderr = stderr.join(''); + result.stdout = Buffer.concat(stdout, stdoutLength).toString(encoding); + result.stderr = Buffer.concat(stderr, stderrLength).toString(encoding); return result; }); } diff --git a/src/git-command-manager.ts b/src/git-command-manager.ts index 47e3937f99..1649c3f05c 100644 --- a/src/git-command-manager.ts +++ b/src/git-command-manager.ts @@ -360,7 +360,7 @@ export class GitCommandManager { return stdout } - async exec(args: string[], opts: ExecOpts = {}): Promise { + async exec(args: string[], { encoding = 'utf8', allowAllExitCodes = false }: ExecOpts = {}): Promise { const result = new GitOutput() const env = {} @@ -368,26 +368,30 @@ export class GitCommandManager { env[key] = process.env[key] } - const stdout: string[] = [] - const stderr: string[] = [] + const stdout: Buffer[] = [] + let stdoutLength = 0 + const stderr: Buffer[] = [] + let stderrLength = 0 const options = { cwd: this.workingDirectory, env, - ignoreReturnCode: opts.allowAllExitCodes ?? false, + ignoreReturnCode: allowAllExitCodes, listeners: { stdout: (data: Buffer) => { - stdout.push(data.toString(opts.encoding ?? 'utf8')) + stdout.push(data) + stdoutLength += data.length }, stderr: (data: Buffer) => { - stderr.push(data.toString(opts.encoding ?? 'utf8')) + stderr.push(data) + stderrLength += data.length } } } result.exitCode = await exec.exec(`"${this.gitPath}"`, args, options) - result.stdout = stdout.join('') - result.stderr = stderr.join('') + result.stdout = Buffer.concat(stdout, stdoutLength).toString(encoding) + result.stderr = Buffer.concat(stderr, stderrLength).toString(encoding) return result } } From 4ae5e43b1aba98af0a3f69c98da66b63fbeff9f3 Mon Sep 17 00:00:00 2001 From: gustavderdrache Date: Fri, 21 Feb 2025 09:17:17 -0500 Subject: [PATCH 7/7] formatting --- src/git-command-manager.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/git-command-manager.ts b/src/git-command-manager.ts index 1649c3f05c..54cad08948 100644 --- a/src/git-command-manager.ts +++ b/src/git-command-manager.ts @@ -360,7 +360,10 @@ export class GitCommandManager { return stdout } - async exec(args: string[], { encoding = 'utf8', allowAllExitCodes = false }: ExecOpts = {}): Promise { + async exec( + args: string[], + {encoding = 'utf8', allowAllExitCodes = false}: ExecOpts = {} + ): Promise { const result = new GitOutput() const env = {}