From 6c4c6cfe815aaa487d90e42ab73cc53bb4cb97ae Mon Sep 17 00:00:00 2001
From: Peter Evans <18365890+peter-evans@users.noreply.github.com>
Date: Fri, 23 Aug 2024 13:01:41 +0000
Subject: [PATCH] multipart tree creation

---
 dist/index.js        | 11 +++++++++--
 src/github-helper.ts | 26 ++++++++++++++++++++------
 2 files changed, 29 insertions(+), 8 deletions(-)

diff --git a/dist/index.js b/dist/index.js
index b63b5fd..893aae4 100644
--- a/dist/index.js
+++ b/dist/index.js
@@ -1362,9 +1362,16 @@ class GitHubHelper {
                         type: 'blob'
                     };
                 })));
+                const chunkSize = 100;
+                const chunkedTreeObjects = Array.from({ length: Math.ceil(treeObjects.length / chunkSize) }, (_, i) => treeObjects.slice(i * chunkSize, i * chunkSize + chunkSize));
                 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: parentCommit.tree, tree: treeObjects }));
-                treeSha = tree.sha;
+                for (let i = 0; i < chunkedTreeObjects.length; i++) {
+                    const { data: tree } = yield this.octokit.rest.git.createTree(Object.assign(Object.assign({}, repository), { base_tree: treeSha, tree: chunkedTreeObjects[i] }));
+                    treeSha = tree.sha;
+                    if (chunkedTreeObjects.length > 1) {
+                        core.info(`Created tree ${treeSha} of multipart tree (${i + 1} of ${chunkedTreeObjects.length})`);
+                    }
+                }
                 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: [parentCommit.sha], tree: treeSha, message: `${commit.subject}\n\n${commit.body}` }));
diff --git a/src/github-helper.ts b/src/github-helper.ts
index 08aede0..ff549db 100644
--- a/src/github-helper.ts
+++ b/src/github-helper.ts
@@ -283,13 +283,27 @@ export class GitHubHelper {
           }
         })
       )
+
+      const chunkSize = 100
+      const chunkedTreeObjects: TreeObject[][] = Array.from(
+        {length: Math.ceil(treeObjects.length / chunkSize)},
+        (_, i) => treeObjects.slice(i * chunkSize, i * chunkSize + chunkSize)
+      )
+
       core.info(`Creating tree for local commit ${commit.sha}`)
-      const {data: tree} = await this.octokit.rest.git.createTree({
-        ...repository,
-        base_tree: parentCommit.tree,
-        tree: treeObjects
-      })
-      treeSha = tree.sha
+      for (let i = 0; i < chunkedTreeObjects.length; i++) {
+        const {data: tree} = await this.octokit.rest.git.createTree({
+          ...repository,
+          base_tree: treeSha,
+          tree: chunkedTreeObjects[i]
+        })
+        treeSha = tree.sha
+        if (chunkedTreeObjects.length > 1) {
+          core.info(
+            `Created tree ${treeSha} of multipart tree (${i + 1} of ${chunkedTreeObjects.length})`
+          )
+        }
+      }
       core.info(`Created tree ${treeSha} for local commit ${commit.sha}`)
     }