Skip to content

Commit 5c7daab

Browse files
fix: add support for binary files (#22)
* ci: add a PNG to the integration test * fix: add support for binary files --------- Co-authored-by: Marcus Aspin <maspin@unilink.com>
1 parent abeded3 commit 5c7daab

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

.github/workflows/integration-tests.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,14 @@ jobs:
4242
run: |
4343
date > current-date.txt
4444
echo 'foobar' > README.md
45+
curl -o git-logo.png https://git-scm.com/images/logos/downloads/Git-Icon-1788C.png
46+
GIT_LOGO_SHA=$(git hash-object -t blob git-logo.png)
47+
mv git-logo.png "git-logo-${GIT_LOGO_SHA}.png"
4548
git add current-date.txt
4649
git add README.md
4750
git rm LICENSE
4851
git add --chmod=+x src/lib.ts
52+
git add "git-logo-${GIT_LOGO_SHA}.png"
4953
5054
- name: Commit to new ref
5155
uses: ./
@@ -79,6 +83,18 @@ jobs:
7983
assert.strictEqual(data.sha, '${{ steps.commit-new-ref.outputs.sha }}', 'Expected sha for commit to match');
8084
assert.strictEqual(data.commit.message, message, 'Expected commit message to match');
8185
assert.strictEqual(data.commit.verification.verified, true, 'Expected commit to be verified');
86+
87+
const { data: { tree } } = await github.rest.git.getTree({
88+
owner: context.repo.owner,
89+
repo: context.repo.repo,
90+
tree_sha: ref,
91+
});
92+
93+
const filenameRegex = /git-logo-(\S+).png/;
94+
const { path, sha } = tree.find(({ path }) => filenameRegex.test(path));
95+
const [, expectedSha] = path.match(filenameRegex);
96+
97+
assert.strictEqual(sha, expectedSha, 'Expected SHA for Git logo PNG to match');
8298
}
8399
84100
- name: Update checkout

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.ts

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@ import type { Endpoints } from '@octokit/types';
99

1010
import { getHeadRef, getHeadSha, getHeadTreeHash, getStagedFiles } from './lib';
1111

12-
export async function populateTree(): Promise<
13-
Endpoints['POST /repos/{owner}/{repo}/git/trees']['parameters']['tree']
14-
> {
12+
type GitHubGitTreeType =
13+
Endpoints['POST /repos/{owner}/{repo}/git/trees']['parameters']['tree'];
14+
type GitTreeItem = Omit<GitHubGitTreeType[0], 'content'> & { content?: Buffer };
15+
type GitTree = GitTreeItem[];
16+
17+
export async function populateTree(): Promise<GitTree> {
1518
return (await getStagedFiles()).map(
1619
({ oldMode, mode, oldSha, sha, change, filename }) => {
1720
if (change === 'D') {
@@ -47,7 +50,7 @@ export async function populateTree(): Promise<
4750
path: filename,
4851
mode,
4952
type: 'blob',
50-
content: fs.readFileSync(path.join(process.cwd(), filename), 'utf-8')
53+
content: fs.readFileSync(path.join(process.cwd(), filename))
5154
};
5255
}
5356
}
@@ -84,10 +87,26 @@ export async function run(): Promise<void> {
8487
const repo = github.context.repo.repo;
8588
const octokit = github.getOctokit(token, { log: console });
8689

90+
// Upload file contents as blobs and update the tree with the returned SHAs
91+
for (const item of tree) {
92+
if (item.content) {
93+
const blob = await octokit.rest.git.createBlob({
94+
owner,
95+
repo,
96+
content: item.content.toString('base64'),
97+
encoding: 'base64'
98+
});
99+
100+
item.content = undefined;
101+
item.sha = blob.data.sha;
102+
core.debug(`File SHA: ${item.path} ${blob.data.sha}`);
103+
}
104+
}
105+
87106
const newTree = await octokit.rest.git.createTree({
88107
owner,
89108
repo,
90-
tree,
109+
tree: tree as GitHubGitTreeType,
91110
base_tree: await getHeadTreeHash()
92111
});
93112
core.debug(`New tree SHA: ${newTree.data.sha}`);

0 commit comments

Comments
 (0)