use parent commit details in create commit

This commit is contained in:
Peter Evans 2024-08-23 11:20:57 +00:00
parent 034fcb0bf3
commit 02efff68da
2 changed files with 19 additions and 12 deletions

15
dist/index.js vendored
View file

@ -1325,20 +1325,21 @@ class GitHubHelper {
return __awaiter(this, void 0, void 0, function* () {
let headCommit = {
sha: baseCommit.sha,
tree: baseCommit.tree,
verified: false
};
for (const commit of branchCommits) {
headCommit = yield this.createCommit(commit, [headCommit.sha], repoPath, branchRepository);
headCommit = yield this.createCommit(commit, headCommit, repoPath, branchRepository);
}
yield this.createOrUpdateRef(branchRepository, branch, headCommit.sha);
return headCommit;
});
}
createCommit(commit, parents, repoPath, branchRepository) {
createCommit(commit, parentCommit, repoPath, branchRepository) {
return __awaiter(this, void 0, void 0, function* () {
const repository = this.parseRepository(branchRepository);
// In the case of an empty commit, the tree is the same as the parent
let treeSha = commit.tree;
// In the case of an empty commit, the tree references the parent's tree
let treeSha = parentCommit.tree;
if (commit.changes.length > 0) {
core.info(`Creating tree objects for local commit ${commit.sha}`);
const treeObjects = yield Promise.all(commit.changes.map((_a) => __awaiter(this, [_a], void 0, function* ({ path, mode, status }) {
@ -1362,15 +1363,16 @@ class GitHubHelper {
};
})));
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: parents[0], tree: treeObjects }));
const { data: tree } = yield this.octokit.rest.git.createTree(Object.assign(Object.assign({}, repository), { base_tree: parentCommit.sha, tree: treeObjects }));
treeSha = tree.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: 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: [parentCommit.sha], tree: treeSha, message: `${commit.subject}\n\n${commit.body}` }));
core.info(`Created commit ${remoteCommit.sha} for local commit ${commit.sha}`);
core.info(`Commit verified: ${remoteCommit.verification.verified}; reason: ${remoteCommit.verification.reason}`);
return {
sha: remoteCommit.sha,
tree: remoteCommit.tree.sha,
verified: remoteCommit.verification.verified
};
});
@ -1381,6 +1383,7 @@ class GitHubHelper {
const { data: remoteCommit } = yield this.octokit.rest.git.getCommit(Object.assign(Object.assign({}, repository), { commit_sha: sha }));
return {
sha: remoteCommit.sha,
tree: remoteCommit.tree.sha,
verified: remoteCommit.verification.verified
};
});

View file

@ -27,6 +27,7 @@ interface Pull {
interface CommitResponse {
sha: string
tree: string
verified: boolean
}
@ -227,12 +228,13 @@ export class GitHubHelper {
): Promise<CommitResponse> {
let headCommit: CommitResponse = {
sha: baseCommit.sha,
tree: baseCommit.tree,
verified: false
}
for (const commit of branchCommits) {
headCommit = await this.createCommit(
commit,
[headCommit.sha],
headCommit,
repoPath,
branchRepository
)
@ -243,13 +245,13 @@ export class GitHubHelper {
private async createCommit(
commit: Commit,
parents: string[],
parentCommit: CommitResponse,
repoPath: string,
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
// In the case of an empty commit, the tree references the parent's tree
let treeSha = parentCommit.tree
if (commit.changes.length > 0) {
core.info(`Creating tree objects for local commit ${commit.sha}`)
const treeObjects = await Promise.all(
@ -284,7 +286,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: parents[0],
base_tree: parentCommit.sha, // but this should probably be tree
tree: treeObjects
})
treeSha = tree.sha
@ -293,7 +295,7 @@ export class GitHubHelper {
const {data: remoteCommit} = await this.octokit.rest.git.createCommit({
...repository,
parents: parents,
parents: [parentCommit.sha],
tree: treeSha,
message: `${commit.subject}\n\n${commit.body}`
})
@ -305,6 +307,7 @@ export class GitHubHelper {
)
return {
sha: remoteCommit.sha,
tree: remoteCommit.tree.sha,
verified: remoteCommit.verification.verified
}
}
@ -320,6 +323,7 @@ export class GitHubHelper {
})
return {
sha: remoteCommit.sha,
tree: remoteCommit.tree.sha,
verified: remoteCommit.verification.verified
}
}