pass base commit to push signed commits

This commit is contained in:
Peter Evans 2024-08-23 11:10:56 +00:00
parent 42beb85339
commit 034fcb0bf3
4 changed files with 56 additions and 48 deletions

View file

@ -135,7 +135,7 @@ interface CreateOrUpdateBranchResult {
action: string
base: string
hasDiffWithBase: boolean
baseSha: string
baseCommit: Commit
headSha: string
branchCommits: Commit[]
}
@ -162,16 +162,6 @@ export async function createOrUpdateBranch(
base = base ? base : workingBase
const baseRemote = 'origin'
// Set the default return values
const result: CreateOrUpdateBranchResult = {
action: 'none',
base: base,
hasDiffWithBase: false,
baseSha: '',
headSha: '',
branchCommits: []
}
// Save the working base changes to a temporary branch
const tempBranch = uuidv4()
await git.checkout(tempBranch, 'HEAD')
@ -251,6 +241,9 @@ export async function createOrUpdateBranch(
? tempBranchCommitsAhead + FETCH_DEPTH_MARGIN
: FETCH_DEPTH_MARGIN
let action = 'none'
let hasDiffWithBase = false
// Try to fetch the pull request branch
if (!(await tryFetch(git, branchRemoteName, branch, fetchDepth))) {
// The pull request branch does not exist
@ -258,9 +251,9 @@ export async function createOrUpdateBranch(
// Create the pull request branch
await git.checkout(branch, tempBranch)
// Check if the pull request branch is ahead of the base
result.hasDiffWithBase = await isAhead(git, base, branch)
if (result.hasDiffWithBase) {
result.action = 'created'
hasDiffWithBase = await isAhead(git, base, branch)
if (hasDiffWithBase) {
action = 'created'
core.info(`Created branch '${branch}'`)
} else {
core.info(
@ -301,26 +294,28 @@ export async function createOrUpdateBranch(
// If the branch was reset or updated it will be ahead
// It may be behind if a reset now results in no diff with the base
if (!(await isEven(git, `${branchRemoteName}/${branch}`, branch))) {
result.action = 'updated'
action = 'updated'
core.info(`Updated branch '${branch}'`)
} else {
result.action = 'not-updated'
action = 'not-updated'
core.info(
`Branch '${branch}' is even with its remote and will not be updated`
)
}
// Check if the pull request branch is ahead of the base
result.hasDiffWithBase = await isAhead(git, base, branch)
hasDiffWithBase = await isAhead(git, base, branch)
}
// Get the base and head SHAs
result.baseSha = await git.revParse(base)
result.headSha = await git.revParse(branch)
const baseSha = await git.revParse(base)
const baseCommit = await git.getCommit(baseSha)
const headSha = await git.revParse(branch)
if (result.hasDiffWithBase) {
let branchCommits: Commit[] = []
if (hasDiffWithBase) {
// Build the branch commits
result.branchCommits = await buildBranchCommits(git, base, branch)
branchCommits = await buildBranchCommits(git, base, branch)
}
// Delete the temporary branch
@ -334,5 +329,12 @@ export async function createOrUpdateBranch(
await git.stashPop()
}
return result
return {
action: action,
base: base,
hasDiffWithBase: hasDiffWithBase,
baseCommit: baseCommit,
headSha: headSha,
branchCommits: branchCommits
}
}

View file

@ -212,7 +212,7 @@ export async function createPullRequest(inputs: Inputs): Promise<void> {
await git.checkout(inputs.branch)
const pushSignedCommitsResult = await ghBranch.pushSignedCommits(
result.branchCommits,
result.baseSha,
result.baseCommit,
repoPath,
branchRepository,
inputs.branch
@ -284,6 +284,7 @@ export async function createPullRequest(inputs: Inputs): Promise<void> {
result.branchCommits.length > 0 &&
result.branchCommits[result.branchCommits.length - 1].signed
) {
// Using the local head commit SHA because in this case commits have not been pushed via the API.
core.info(`Checking verification status of head commit ${result.headSha}`)
try {
const headCommit = await ghBranch.getCommit(

View file

@ -220,13 +220,13 @@ export class GitHubHelper {
async pushSignedCommits(
branchCommits: Commit[],
baseSha: string,
baseCommit: Commit,
repoPath: string,
branchRepository: string,
branch: string
): Promise<CommitResponse> {
let headCommit: CommitResponse = {
sha: baseSha,
sha: baseCommit.sha,
verified: false
}
for (const commit of branchCommits) {
@ -248,6 +248,7 @@ export class GitHubHelper {
branchRepository: string
): Promise<CommitResponse> {
const repository = this.parseRepository(branchRepository)
// In the case of an empty commit, the tree is the same as the parent
let treeSha = commit.tree
if (commit.changes.length > 0) {
core.info(`Creating tree objects for local commit ${commit.sha}`)