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

25
dist/index.js vendored
View file

@ -154,6 +154,7 @@ function createOrUpdateBranch(git, commitMessage, base, branch, branchRemoteName
action: 'none', action: 'none',
base: base, base: base,
hasDiffWithBase: false, hasDiffWithBase: false,
baseSha: '',
headSha: '', headSha: '',
branchCommits: [] branchCommits: []
}; };
@ -278,8 +279,9 @@ function createOrUpdateBranch(git, commitMessage, base, branch, branchRemoteName
} }
// Build the branch commits // Build the branch commits
result.branchCommits = yield buildBranchCommits(git, base, branch); result.branchCommits = yield buildBranchCommits(git, base, branch);
// Get the pull request branch SHA // Get the base and head SHAs
result.headSha = yield git.revParse('HEAD'); result.baseSha = yield git.revParse(base);
result.headSha = yield git.revParse(branch);
// Delete the temporary branch // Delete the temporary branch
yield git.exec(['branch', '--delete', '--force', tempBranch]); yield git.exec(['branch', '--delete', '--force', tempBranch]);
// Checkout the working base to leave the local repository as it was found // Checkout the working base to leave the local repository as it was found
@ -458,7 +460,7 @@ function createPullRequest(inputs) {
// Stash any uncommitted tracked and untracked changes // Stash any uncommitted tracked and untracked changes
const stashed = yield git.stashPush(['--include-untracked']); const stashed = yield git.stashPush(['--include-untracked']);
yield git.checkout(inputs.branch); yield git.checkout(inputs.branch);
yield githubHelper.pushSignedCommits(result.branchCommits, repoPath, branchRepository, inputs.branch); yield githubHelper.pushSignedCommits(result.branchCommits, result.baseSha, repoPath, branchRepository, inputs.branch);
yield git.checkout('-'); yield git.checkout('-');
if (stashed) { if (stashed) {
yield git.stashPop(); yield git.stashPop();
@ -1274,16 +1276,21 @@ class GitHubHelper {
return pull; return pull;
}); });
} }
pushSignedCommits(branchCommits, repoPath, branchRepository, branch) { pushSignedCommits(branchCommits, baseSha, repoPath, branchRepository, branch) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
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) { for (const commit of branchCommits) {
headSha = yield this.createCommit(commit, repoPath, branchRepository); // TODO: The headSha of the previous commit should be passed and used as the parent.
headSha = yield this.createCommit(commit, [headSha], repoPath, branchRepository);
} }
yield this.createOrUpdateRef(branchRepository, branch, headSha); yield this.createOrUpdateRef(branchRepository, branch, headSha);
}); });
} }
createCommit(commit, repoPath, branchRepository) { createCommit(commit, parents, repoPath, branchRepository) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
const repository = this.parseRepository(branchRepository); const repository = this.parseRepository(branchRepository);
let treeSha = commit.tree; let treeSha = commit.tree;
@ -1304,11 +1311,11 @@ class GitHubHelper {
}; };
}))); })));
core.info(`Creating tree for local commit ${commit.sha}`); core.info(`Creating tree for local commit ${commit.sha}`);
const { data: tree } = yield this.octokit.rest.git.createTree(Object.assign(Object.assign({}, repository), { base_tree: commit.parents[0], tree: treeObjects })); const { data: tree } = yield this.octokit.rest.git.createTree(Object.assign(Object.assign({}, repository), { base_tree: parents[0], tree: treeObjects }));
treeSha = tree.sha; treeSha = tree.sha;
core.info(`Created tree ${treeSha} for local commit ${commit.sha}`); core.info(`Created tree ${treeSha} for local commit ${commit.sha}`);
} }
const { data: remoteCommit } = yield this.octokit.rest.git.createCommit(Object.assign(Object.assign({}, repository), { parents: commit.parents, tree: treeSha, message: `${commit.subject}\n\n${commit.body}` })); const { data: remoteCommit } = yield this.octokit.rest.git.createCommit(Object.assign(Object.assign({}, repository), { parents: parents, tree: treeSha, message: `${commit.subject}\n\n${commit.body}` }));
core.info(`Created commit ${remoteCommit.sha} for local commit ${commit.sha}`); core.info(`Created commit ${remoteCommit.sha} for local commit ${commit.sha}`);
return remoteCommit.sha; return remoteCommit.sha;
}); });

View file

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

View file

@ -195,19 +195,35 @@ export class GitHubHelper {
async pushSignedCommits( async pushSignedCommits(
branchCommits: Commit[], branchCommits: Commit[],
baseSha: string,
repoPath: string, repoPath: string,
branchRepository: string, branchRepository: string,
branch: string branch: string
): Promise<void> { ): 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) { 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) await this.createOrUpdateRef(branchRepository, branch, headSha)
} }
private async createCommit( private async createCommit(
commit: Commit, commit: Commit,
parents: string[],
repoPath: string, repoPath: string,
branchRepository: string branchRepository: string
): Promise<string> { ): Promise<string> {
@ -238,7 +254,7 @@ export class GitHubHelper {
core.info(`Creating tree for local commit ${commit.sha}`) core.info(`Creating tree for local commit ${commit.sha}`)
const {data: tree} = await this.octokit.rest.git.createTree({ const {data: tree} = await this.octokit.rest.git.createTree({
...repository, ...repository,
base_tree: commit.parents[0], base_tree: parents[0],
tree: treeObjects tree: treeObjects
}) })
treeSha = tree.sha treeSha = tree.sha
@ -247,7 +263,7 @@ export class GitHubHelper {
const {data: remoteCommit} = await this.octokit.rest.git.createCommit({ const {data: remoteCommit} = await this.octokit.rest.git.createCommit({
...repository, ...repository,
parents: commit.parents, parents: parents,
tree: treeSha, tree: treeSha,
message: `${commit.subject}\n\n${commit.body}` message: `${commit.subject}\n\n${commit.body}`
}) })