@@ -9,9 +9,12 @@ import type { Endpoints } from '@octokit/types';
99
1010import { 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