mirror of
https://forgejo.stefka.eu/jiriks74/create-pull-request.git
synced 2025-01-18 16:01:06 +01:00
Try refactor of file changes
This commit is contained in:
parent
6284ea5854
commit
36e042a736
4 changed files with 144 additions and 53 deletions
|
@ -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')
|
||||
|
||||
|
|
|
@ -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)}'`
|
||||
)
|
||||
|
||||
// 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 = <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 = <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 = <FileChanges>{
|
||||
additions: result.fileChanges!.additions,
|
||||
deletions: result.fileChanges!.deletions
|
||||
}
|
||||
|
||||
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}'`
|
||||
)
|
||||
|
||||
// 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',
|
||||
|
|
|
@ -166,12 +166,11 @@ export class GitCommandManager {
|
|||
return output.exitCode === 1
|
||||
}
|
||||
|
||||
async getChangedFiles(ref: string, options?: string[]): Promise<string[]> {
|
||||
async getChangedFiles(options?: string[]): Promise<string[]> {
|
||||
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 != '')
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue