diff --git a/dist/index.js b/dist/index.js index 096fdd9..8ba67d2 100644 --- a/dist/index.js +++ b/dist/index.js @@ -45,6 +45,7 @@ exports.tryFetch = tryFetch; exports.createOrUpdateBranch = createOrUpdateBranch; const core = __importStar(__nccwpck_require__(2186)); const uuid_1 = __nccwpck_require__(5840); +const utils = __importStar(__nccwpck_require__(918)); const CHERRYPICK_EMPTY = 'The previous cherry-pick is now empty, possibly due to conflict resolution.'; const NOTHING_TO_COMMIT = 'nothing to commit, working tree clean'; const FETCH_DEPTH_MARGIN = 10; @@ -259,6 +260,37 @@ function createOrUpdateBranch(git, commitMessage, base, branch, branchRemoteName // Check if the pull request branch is ahead of the base result.hasDiffWithBase = yield isAhead(git, base, branch); } + if (result.hasDiffWithBase) { + // Build file changes + result.fileChanges = { + additions: [], + deletions: [] + }; + const changedFiles = yield git.getChangedFiles([ + '--diff-filter=M', + `${base}..${branch}` + ]); + const deletedFiles = yield git.getChangedFiles([ + '--diff-filter=D', + `${base}..${branch}` + ]); + core.debug(`Changed files: '${JSON.stringify(changedFiles)}'`); + core.debug(`Deleted files: '${JSON.stringify(deletedFiles)}'`); + const repoPath = git.getWorkingDirectory(); + for (const file of changedFiles) { + core.debug(`Reading contents of file: '${file}'`); + result.fileChanges.additions.push({ + path: file, + contents: utils.readFileBase64([repoPath, file]) + }); + } + for (const file of deletedFiles) { + core.debug(`Marking file as deleted: '${file}'`); + result.fileChanges.deletions.push({ + path: file + }); + } + } // Get the pull request branch SHA result.headSha = yield git.revParse('HEAD'); // Delete the temporary branch @@ -518,26 +550,36 @@ function createPullRequest(inputs) { } } core.info(`Hash ref of branch '${inputs.branch}' is '${JSON.stringify(branchRef.repository.ref.target.oid)}'`); - // switch to input-branch for reading updated file contents - yield git.checkout(inputs.branch); - const changedFiles = yield git.getChangedFiles(branchRef.repository.ref.target.oid, ['--diff-filter=M']); - const deletedFiles = yield git.getChangedFiles(branchRef.repository.ref.target.oid, ['--diff-filter=D']); - const fileChanges = { additions: [], deletions: [] }; - core.debug(`Changed files: '${JSON.stringify(changedFiles)}'`); - core.debug(`Deleted files: '${JSON.stringify(deletedFiles)}'`); - for (const file of changedFiles) { - core.debug(`Reading contents of file: '${file}'`); - fileChanges.additions.push({ - path: file, - contents: utils.readFileBase64([repoPath, file]) - }); - } - for (const file of deletedFiles) { - core.debug(`Marking file as deleted: '${file}'`); - fileChanges.deletions.push({ - path: file - }); - } + // // switch to input-branch for reading updated file contents + // await git.checkout(inputs.branch) + // const changedFiles = await git.getChangedFiles( + // branchRef.repository.ref!.target!.oid, + // ['--diff-filter=M'] + // ) + // const deletedFiles = await git.getChangedFiles( + // branchRef.repository.ref!.target!.oid, + // ['--diff-filter=D'] + // ) + // const fileChanges = {additions: [], deletions: []} + // core.debug(`Changed files: '${JSON.stringify(changedFiles)}'`) + // core.debug(`Deleted files: '${JSON.stringify(deletedFiles)}'`) + // for (const file of changedFiles) { + // core.debug(`Reading contents of file: '${file}'`) + // fileChanges.additions!.push({ + // path: file, + // contents: utils.readFileBase64([repoPath, file]) + // }) + // } + // for (const file of deletedFiles) { + // core.debug(`Marking file as deleted: '${file}'`) + // fileChanges.deletions!.push({ + // path: file + // }) + // } + const fileChanges = { + additions: result.fileChanges.additions, + deletions: result.fileChanges.deletions + }; const pushCommitMutation = ` mutation PushCommit( $repoNameWithOwner: String!, @@ -587,8 +629,8 @@ function createPullRequest(inputs) { const commit = yield graphqlWithAuth(pushCommitMutation, pushCommitVars); core.debug(`Pushed commit - '${JSON.stringify(commit)}'`); core.info(`Pushed commit with hash - '${commit.createCommitOnBranch.commit.oid}' on branch - '${commit.createCommitOnBranch.ref.name}'`); - // switch back to previous branch/state since we are done with reading the changed file contents - yield git.checkout('-'); + // // switch back to previous branch/state since we are done with reading the changed file contents + // await git.checkout('-') } else { yield git.push([ @@ -831,13 +873,12 @@ class GitCommandManager { return output.exitCode === 1; }); } - getChangedFiles(ref, options) { + getChangedFiles(options) { return __awaiter(this, void 0, void 0, function* () { const args = ['diff', '--name-only']; if (options) { args.push(...options); } - args.push(ref); const output = yield this.exec(args); return output.stdout.split('\n').filter(filename => filename != ''); }); diff --git a/src/create-or-update-branch.ts b/src/create-or-update-branch.ts index 7b584e8..9ee8829 100644 --- a/src/create-or-update-branch.ts +++ b/src/create-or-update-branch.ts @@ -1,6 +1,7 @@ import * as core from '@actions/core' import {GitCommandManager} from './git-command-manager' import {v4 as uuidv4} from 'uuid' +import * as utils from './utils' const CHERRYPICK_EMPTY = 'The previous cherry-pick is now empty, possibly due to conflict resolution.' @@ -115,6 +116,15 @@ interface CreateOrUpdateBranchResult { base: string hasDiffWithBase: boolean headSha: string + fileChanges?: { + additions: { + path: string + contents: string + }[] + deletions: { + path: string + }[] + } } export async function createOrUpdateBranch( @@ -289,6 +299,42 @@ export async function createOrUpdateBranch( result.hasDiffWithBase = await isAhead(git, base, branch) } + if (result.hasDiffWithBase) { + // Build file changes + result.fileChanges = { + additions: [], + deletions: [] + } + + const changedFiles = await git.getChangedFiles([ + '--diff-filter=M', + `${base}..${branch}` + ]) + const deletedFiles = await git.getChangedFiles([ + '--diff-filter=D', + `${base}..${branch}` + ]) + + core.debug(`Changed files: '${JSON.stringify(changedFiles)}'`) + core.debug(`Deleted files: '${JSON.stringify(deletedFiles)}'`) + + const repoPath = git.getWorkingDirectory() + for (const file of changedFiles) { + core.debug(`Reading contents of file: '${file}'`) + result.fileChanges.additions!.push({ + path: file, + contents: utils.readFileBase64([repoPath, file]) + }) + } + + for (const file of deletedFiles) { + core.debug(`Marking file as deleted: '${file}'`) + result.fileChanges.deletions!.push({ + path: file + }) + } + } + // Get the pull request branch SHA result.headSha = await git.revParse('HEAD') diff --git a/src/create-pull-request.ts b/src/create-pull-request.ts index 7abffd7..abf9463 100644 --- a/src/create-pull-request.ts +++ b/src/create-pull-request.ts @@ -296,35 +296,40 @@ export async function createPullRequest(inputs: Inputs): Promise { `Hash ref of branch '${inputs.branch}' is '${JSON.stringify(branchRef.repository.ref!.target!.oid)}'` ) - // switch to input-branch for reading updated file contents - await git.checkout(inputs.branch) + // // switch to input-branch for reading updated file contents + // await git.checkout(inputs.branch) - const changedFiles = await git.getChangedFiles( - branchRef.repository.ref!.target!.oid, - ['--diff-filter=M'] - ) - const deletedFiles = await git.getChangedFiles( - branchRef.repository.ref!.target!.oid, - ['--diff-filter=D'] - ) - const fileChanges = {additions: [], deletions: []} + // const changedFiles = await git.getChangedFiles( + // branchRef.repository.ref!.target!.oid, + // ['--diff-filter=M'] + // ) + // const deletedFiles = await git.getChangedFiles( + // branchRef.repository.ref!.target!.oid, + // ['--diff-filter=D'] + // ) + // const fileChanges = {additions: [], deletions: []} - core.debug(`Changed files: '${JSON.stringify(changedFiles)}'`) - core.debug(`Deleted files: '${JSON.stringify(deletedFiles)}'`) + // core.debug(`Changed files: '${JSON.stringify(changedFiles)}'`) + // core.debug(`Deleted files: '${JSON.stringify(deletedFiles)}'`) - for (const file of changedFiles) { - core.debug(`Reading contents of file: '${file}'`) - fileChanges.additions!.push({ - path: file, - contents: utils.readFileBase64([repoPath, file]) - }) - } + // for (const file of changedFiles) { + // core.debug(`Reading contents of file: '${file}'`) + // fileChanges.additions!.push({ + // path: file, + // contents: utils.readFileBase64([repoPath, file]) + // }) + // } - for (const file of deletedFiles) { - core.debug(`Marking file as deleted: '${file}'`) - fileChanges.deletions!.push({ - path: file - }) + // for (const file of deletedFiles) { + // core.debug(`Marking file as deleted: '${file}'`) + // fileChanges.deletions!.push({ + // path: file + // }) + // } + + const fileChanges = { + additions: result.fileChanges!.additions, + deletions: result.fileChanges!.deletions } const pushCommitMutation = ` @@ -393,8 +398,8 @@ export async function createPullRequest(inputs: Inputs): Promise { `Pushed commit with hash - '${commit.createCommitOnBranch.commit.oid}' on branch - '${commit.createCommitOnBranch.ref.name}'` ) - // switch back to previous branch/state since we are done with reading the changed file contents - await git.checkout('-') + // // switch back to previous branch/state since we are done with reading the changed file contents + // await git.checkout('-') } else { await git.push([ '--force-with-lease', diff --git a/src/git-command-manager.ts b/src/git-command-manager.ts index 65762c1..36380a3 100644 --- a/src/git-command-manager.ts +++ b/src/git-command-manager.ts @@ -166,12 +166,11 @@ export class GitCommandManager { return output.exitCode === 1 } - async getChangedFiles(ref: string, options?: string[]): Promise { + async getChangedFiles(options?: string[]): Promise { const args = ['diff', '--name-only'] if (options) { args.push(...options) } - args.push(ref) const output = await this.exec(args) return output.stdout.split('\n').filter(filename => filename != '') }