try fix base tree

This commit is contained in:
Peter Evans 2024-08-08 18:36:07 +00:00
parent 90b04fe25b
commit b0303827bb
4 changed files with 42 additions and 15 deletions

View file

@ -142,6 +142,7 @@ interface CreateOrUpdateBranchResult {
action: string
base: string
hasDiffWithBase: boolean
baseSha: string
headSha: string
branchCommits: Commit[]
}
@ -173,6 +174,7 @@ export async function createOrUpdateBranch(
action: 'none',
base: base,
hasDiffWithBase: false,
baseSha: '',
headSha: '',
branchCommits: []
}
@ -322,8 +324,9 @@ export async function createOrUpdateBranch(
// Build the branch commits
result.branchCommits = await buildBranchCommits(git, base, branch)
// Get the pull request branch SHA
result.headSha = await git.revParse('HEAD')
// Get the base and head SHAs
result.baseSha = await git.revParse(base)
result.headSha = await git.revParse(branch)
// Delete the temporary branch
await git.exec(['branch', '--delete', '--force', tempBranch])

View file

@ -201,6 +201,7 @@ export async function createPullRequest(inputs: Inputs): Promise<void> {
await git.checkout(inputs.branch)
await githubHelper.pushSignedCommits(
result.branchCommits,
result.baseSha,
repoPath,
branchRepository,
inputs.branch

View file

@ -195,19 +195,35 @@ export class GitHubHelper {
async pushSignedCommits(
branchCommits: Commit[],
baseSha: string,
repoPath: string,
branchRepository: string,
branch: string
): Promise<void> {
let headSha = ''
let headSha = baseSha
// testing
if (branchCommits.length > 0 && branchCommits[0].parents[0] !== baseSha) {
throw new Error(
`The base commit ${baseSha} does not match the first commit's parent ${branchCommits[0].parents[0]}`
)
}
for (const commit of branchCommits) {
headSha = await this.createCommit(commit, repoPath, branchRepository)
// TODO: The headSha of the previous commit should be passed and used as the parent.
headSha = await this.createCommit(
commit,
[headSha],
repoPath,
branchRepository
)
}
await this.createOrUpdateRef(branchRepository, branch, headSha)
}
private async createCommit(
commit: Commit,
parents: string[],
repoPath: string,
branchRepository: string
): Promise<string> {
@ -238,7 +254,7 @@ export class GitHubHelper {
core.info(`Creating tree for local commit ${commit.sha}`)
const {data: tree} = await this.octokit.rest.git.createTree({
...repository,
base_tree: commit.parents[0],
base_tree: parents[0],
tree: treeObjects
})
treeSha = tree.sha
@ -247,7 +263,7 @@ export class GitHubHelper {
const {data: remoteCommit} = await this.octokit.rest.git.createCommit({
...repository,
parents: commit.parents,
parents: parents,
tree: treeSha,
message: `${commit.subject}\n\n${commit.body}`
})