Try refactor of file changes
This commit is contained in:
parent
743dcd81f7
commit
3d409de49f
4 changed files with 144 additions and 53 deletions
89
dist/index.js
vendored
89
dist/index.js
vendored
|
@ -45,6 +45,7 @@ exports.tryFetch = tryFetch;
|
||||||
exports.createOrUpdateBranch = createOrUpdateBranch;
|
exports.createOrUpdateBranch = createOrUpdateBranch;
|
||||||
const core = __importStar(__nccwpck_require__(2186));
|
const core = __importStar(__nccwpck_require__(2186));
|
||||||
const uuid_1 = __nccwpck_require__(5840);
|
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 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 NOTHING_TO_COMMIT = 'nothing to commit, working tree clean';
|
||||||
const FETCH_DEPTH_MARGIN = 10;
|
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
|
// Check if the pull request branch is ahead of the base
|
||||||
result.hasDiffWithBase = yield isAhead(git, base, branch);
|
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
|
// Get the pull request branch SHA
|
||||||
result.headSha = yield git.revParse('HEAD');
|
result.headSha = yield git.revParse('HEAD');
|
||||||
// Delete the temporary branch
|
// 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)}'`);
|
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
|
// // switch to input-branch for reading updated file contents
|
||||||
yield git.checkout(inputs.branch);
|
// await git.checkout(inputs.branch)
|
||||||
const changedFiles = yield git.getChangedFiles(branchRef.repository.ref.target.oid, ['--diff-filter=M']);
|
// const changedFiles = await git.getChangedFiles(
|
||||||
const deletedFiles = yield git.getChangedFiles(branchRef.repository.ref.target.oid, ['--diff-filter=D']);
|
// branchRef.repository.ref!.target!.oid,
|
||||||
const fileChanges = { additions: [], deletions: [] };
|
// ['--diff-filter=M']
|
||||||
core.debug(`Changed files: '${JSON.stringify(changedFiles)}'`);
|
// )
|
||||||
core.debug(`Deleted files: '${JSON.stringify(deletedFiles)}'`);
|
// const deletedFiles = await git.getChangedFiles(
|
||||||
for (const file of changedFiles) {
|
// branchRef.repository.ref!.target!.oid,
|
||||||
core.debug(`Reading contents of file: '${file}'`);
|
// ['--diff-filter=D']
|
||||||
fileChanges.additions.push({
|
// )
|
||||||
path: file,
|
// const fileChanges = <FileChanges>{additions: [], deletions: []}
|
||||||
contents: utils.readFileBase64([repoPath, file])
|
// core.debug(`Changed files: '${JSON.stringify(changedFiles)}'`)
|
||||||
});
|
// core.debug(`Deleted files: '${JSON.stringify(deletedFiles)}'`)
|
||||||
}
|
// for (const file of changedFiles) {
|
||||||
for (const file of deletedFiles) {
|
// core.debug(`Reading contents of file: '${file}'`)
|
||||||
core.debug(`Marking file as deleted: '${file}'`);
|
// fileChanges.additions!.push({
|
||||||
fileChanges.deletions.push({
|
// path: file,
|
||||||
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 = `
|
const pushCommitMutation = `
|
||||||
mutation PushCommit(
|
mutation PushCommit(
|
||||||
$repoNameWithOwner: String!,
|
$repoNameWithOwner: String!,
|
||||||
|
@ -587,8 +629,8 @@ function createPullRequest(inputs) {
|
||||||
const commit = yield graphqlWithAuth(pushCommitMutation, pushCommitVars);
|
const commit = yield graphqlWithAuth(pushCommitMutation, pushCommitVars);
|
||||||
core.debug(`Pushed commit - '${JSON.stringify(commit)}'`);
|
core.debug(`Pushed commit - '${JSON.stringify(commit)}'`);
|
||||||
core.info(`Pushed commit with hash - '${commit.createCommitOnBranch.commit.oid}' on branch - '${commit.createCommitOnBranch.ref.name}'`);
|
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
|
// // switch back to previous branch/state since we are done with reading the changed file contents
|
||||||
yield git.checkout('-');
|
// await git.checkout('-')
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
yield git.push([
|
yield git.push([
|
||||||
|
@ -831,13 +873,12 @@ class GitCommandManager {
|
||||||
return output.exitCode === 1;
|
return output.exitCode === 1;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
getChangedFiles(ref, options) {
|
getChangedFiles(options) {
|
||||||
return __awaiter(this, void 0, void 0, function* () {
|
return __awaiter(this, void 0, void 0, function* () {
|
||||||
const args = ['diff', '--name-only'];
|
const args = ['diff', '--name-only'];
|
||||||
if (options) {
|
if (options) {
|
||||||
args.push(...options);
|
args.push(...options);
|
||||||
}
|
}
|
||||||
args.push(ref);
|
|
||||||
const output = yield this.exec(args);
|
const output = yield this.exec(args);
|
||||||
return output.stdout.split('\n').filter(filename => filename != '');
|
return output.stdout.split('\n').filter(filename => filename != '');
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import * as core from '@actions/core'
|
import * as core from '@actions/core'
|
||||||
import {GitCommandManager} from './git-command-manager'
|
import {GitCommandManager} from './git-command-manager'
|
||||||
import {v4 as uuidv4} from 'uuid'
|
import {v4 as uuidv4} from 'uuid'
|
||||||
|
import * as utils from './utils'
|
||||||
|
|
||||||
const CHERRYPICK_EMPTY =
|
const CHERRYPICK_EMPTY =
|
||||||
'The previous cherry-pick is now empty, possibly due to conflict resolution.'
|
'The previous cherry-pick is now empty, possibly due to conflict resolution.'
|
||||||
|
@ -115,6 +116,15 @@ interface CreateOrUpdateBranchResult {
|
||||||
base: string
|
base: string
|
||||||
hasDiffWithBase: boolean
|
hasDiffWithBase: boolean
|
||||||
headSha: string
|
headSha: string
|
||||||
|
fileChanges?: {
|
||||||
|
additions: {
|
||||||
|
path: string
|
||||||
|
contents: string
|
||||||
|
}[]
|
||||||
|
deletions: {
|
||||||
|
path: string
|
||||||
|
}[]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function createOrUpdateBranch(
|
export async function createOrUpdateBranch(
|
||||||
|
@ -289,6 +299,42 @@ export async function createOrUpdateBranch(
|
||||||
result.hasDiffWithBase = await isAhead(git, base, branch)
|
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
|
// Get the pull request branch SHA
|
||||||
result.headSha = await git.revParse('HEAD')
|
result.headSha = await git.revParse('HEAD')
|
||||||
|
|
||||||
|
|
|
@ -296,35 +296,40 @@ export async function createPullRequest(inputs: Inputs): Promise<void> {
|
||||||
`Hash ref of branch '${inputs.branch}' is '${JSON.stringify(branchRef.repository.ref!.target!.oid)}'`
|
`Hash ref of branch '${inputs.branch}' is '${JSON.stringify(branchRef.repository.ref!.target!.oid)}'`
|
||||||
)
|
)
|
||||||
|
|
||||||
// switch to input-branch for reading updated file contents
|
// // switch to input-branch for reading updated file contents
|
||||||
await git.checkout(inputs.branch)
|
// await git.checkout(inputs.branch)
|
||||||
|
|
||||||
const changedFiles = await git.getChangedFiles(
|
// const changedFiles = await git.getChangedFiles(
|
||||||
branchRef.repository.ref!.target!.oid,
|
// branchRef.repository.ref!.target!.oid,
|
||||||
['--diff-filter=M']
|
// ['--diff-filter=M']
|
||||||
)
|
// )
|
||||||
const deletedFiles = await git.getChangedFiles(
|
// const deletedFiles = await git.getChangedFiles(
|
||||||
branchRef.repository.ref!.target!.oid,
|
// branchRef.repository.ref!.target!.oid,
|
||||||
['--diff-filter=D']
|
// ['--diff-filter=D']
|
||||||
)
|
// )
|
||||||
const fileChanges = <FileChanges>{additions: [], deletions: []}
|
// const fileChanges = <FileChanges>{additions: [], deletions: []}
|
||||||
|
|
||||||
core.debug(`Changed files: '${JSON.stringify(changedFiles)}'`)
|
// core.debug(`Changed files: '${JSON.stringify(changedFiles)}'`)
|
||||||
core.debug(`Deleted files: '${JSON.stringify(deletedFiles)}'`)
|
// core.debug(`Deleted files: '${JSON.stringify(deletedFiles)}'`)
|
||||||
|
|
||||||
for (const file of changedFiles) {
|
// for (const file of changedFiles) {
|
||||||
core.debug(`Reading contents of file: '${file}'`)
|
// core.debug(`Reading contents of file: '${file}'`)
|
||||||
fileChanges.additions!.push({
|
// fileChanges.additions!.push({
|
||||||
path: file,
|
// path: file,
|
||||||
contents: utils.readFileBase64([repoPath, file])
|
// contents: utils.readFileBase64([repoPath, file])
|
||||||
})
|
// })
|
||||||
}
|
// }
|
||||||
|
|
||||||
for (const file of deletedFiles) {
|
// for (const file of deletedFiles) {
|
||||||
core.debug(`Marking file as deleted: '${file}'`)
|
// core.debug(`Marking file as deleted: '${file}'`)
|
||||||
fileChanges.deletions!.push({
|
// fileChanges.deletions!.push({
|
||||||
path: file
|
// path: file
|
||||||
})
|
// })
|
||||||
|
// }
|
||||||
|
|
||||||
|
const fileChanges = <FileChanges>{
|
||||||
|
additions: result.fileChanges!.additions,
|
||||||
|
deletions: result.fileChanges!.deletions
|
||||||
}
|
}
|
||||||
|
|
||||||
const pushCommitMutation = `
|
const pushCommitMutation = `
|
||||||
|
@ -393,8 +398,8 @@ export async function createPullRequest(inputs: Inputs): Promise<void> {
|
||||||
`Pushed commit with hash - '${commit.createCommitOnBranch.commit.oid}' on branch - '${commit.createCommitOnBranch.ref.name}'`
|
`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
|
// // switch back to previous branch/state since we are done with reading the changed file contents
|
||||||
await git.checkout('-')
|
// await git.checkout('-')
|
||||||
} else {
|
} else {
|
||||||
await git.push([
|
await git.push([
|
||||||
'--force-with-lease',
|
'--force-with-lease',
|
||||||
|
|
|
@ -166,12 +166,11 @@ export class GitCommandManager {
|
||||||
return output.exitCode === 1
|
return output.exitCode === 1
|
||||||
}
|
}
|
||||||
|
|
||||||
async getChangedFiles(ref: string, options?: string[]): Promise<string[]> {
|
async getChangedFiles(options?: string[]): Promise<string[]> {
|
||||||
const args = ['diff', '--name-only']
|
const args = ['diff', '--name-only']
|
||||||
if (options) {
|
if (options) {
|
||||||
args.push(...options)
|
args.push(...options)
|
||||||
}
|
}
|
||||||
args.push(ref)
|
|
||||||
const output = await this.exec(args)
|
const output = await this.exec(args)
|
||||||
return output.stdout.split('\n').filter(filename => filename != '')
|
return output.stdout.split('\n').filter(filename => filename != '')
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue