Skip to content

Commit 56e493c

Browse files
Copilotqoomon
andauthored
fix: use parent commit's tree SHA as base_tree for GitHub API createTree call
Agent-Logs-Url: https://github.com/qoomon/actions--create-commit/sessions/e7d2a904-30c4-4099-ac7e-a162a020bdca Co-authored-by: qoomon <3963394+qoomon@users.noreply.github.com>
1 parent e5a26c8 commit 56e493c

3 files changed

Lines changed: 18 additions & 5 deletions

File tree

dist/index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38248,7 +38248,7 @@ async function createCommit(octokit, repository, args) {
3824838248
const chunk = commitTreeBlobs.slice(i, i + chunkSize);
3824938249
commitTreeSha = await octokit.rest.git.createTree({
3825038250
...repository,
38251-
base_tree: commitTreeSha,
38251+
...(commitTreeSha !== undefined ? { base_tree: commitTreeSha } : {}),
3825238252
tree: chunk,
3825338253
}).then(({ data }) => data.sha).finally(() => {
3825438254
progress++;
@@ -38257,6 +38257,9 @@ async function createCommit(octokit, repository, args) {
3825738257
});
3825838258
}
3825938259
}
38260+
if (commitTreeSha === undefined) {
38261+
throw new Error('Could not determine commit tree SHA: no parent tree SHA was provided and no files were given to create a new tree');
38262+
}
3826038263
console.log('Creating commit...');
3826138264
const commit = await octokit.rest.git.createCommit({
3826238265
...repository,
@@ -38381,10 +38384,13 @@ const action = () => run(async () => {
3838138384
const headCommit = await getCommitDetails('HEAD');
3838238385
const repositoryRemoteUrl = await getRemoteUrl(input.remoteName);
3838338386
const repository = parseRepositoryFromUrl(repositoryRemoteUrl);
38387+
const parentCommit = headCommit.parents.length > 0
38388+
? await getCommitDetails(headCommit.parents[0])
38389+
: null;
3838438390
const githubCommit = await createCommit(octokit, repository, {
3838538391
subject: headCommit.subject,
3838638392
body: headCommit.body,
38387-
tree: headCommit.tree,
38393+
tree: parentCommit?.tree,
3838838394
parents: headCommit.parents,
3838938395
files: headCommit.files.map((file) => ({
3839038396
path: file.path,

index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,13 @@ export const action = () => run(async () => {
9393
const headCommit = await getCommitDetails('HEAD')
9494
const repositoryRemoteUrl = await getRemoteUrl(input.remoteName)
9595
const repository = parseRepositoryFromUrl(repositoryRemoteUrl)
96+
const parentCommit = headCommit.parents.length > 0
97+
? await getCommitDetails(headCommit.parents[0])
98+
: null
9699
const githubCommit = await createCommit(octokit, repository, {
97100
subject: headCommit.subject,
98101
body: headCommit.body,
99-
tree: headCommit.tree,
102+
tree: parentCommit?.tree,
100103
parents: headCommit.parents,
101104
files: headCommit.files.map((file) => ({
102105
path: file.path,

lib/github.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export async function createCommit(
6363
const chunk = commitTreeBlobs.slice(i, i + chunkSize)
6464
commitTreeSha = await octokit.rest.git.createTree({
6565
...repository,
66-
base_tree: commitTreeSha,
66+
...(commitTreeSha !== undefined ? {base_tree: commitTreeSha} : {}),
6767
tree: chunk,
6868
}).then(({data}) => data.sha).finally(() => {
6969
progress++;
@@ -73,6 +73,10 @@ export async function createCommit(
7373
}
7474
}
7575

76+
if (commitTreeSha === undefined) {
77+
throw new Error('Could not determine commit tree SHA: no parent tree SHA was provided and no files were given to create a new tree')
78+
}
79+
7680
console.log('Creating commit...')
7781
const commit = await octokit.rest.git.createCommit({
7882
...repository,
@@ -119,7 +123,7 @@ export type CreateCommitArgs = {
119123
subject: string
120124
body: string
121125
parents: string[]
122-
tree: string,
126+
tree: string | undefined,
123127
files: {
124128
path: string
125129
mode: '100644' | '100755' | '040000' | '160000' | '120000' | string

0 commit comments

Comments
 (0)