From 11bbcc7993daa5a6769bdec4e694c8bb378828bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tr=C3=A8sAbhi?= <43380238+tresabhi@users.noreply.github.com> Date: Sun, 18 Feb 2024 19:11:41 -0600 Subject: [PATCH] feat: async commit --- scripts/buildAssets/tankModels.ts | 21 ++++++----- src/core/blitzkrieg/createBlob.ts | 2 +- src/core/github/assetCommit.ts | 60 +++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 12 deletions(-) create mode 100644 src/core/github/assetCommit.ts diff --git a/scripts/buildAssets/tankModels.ts b/scripts/buildAssets/tankModels.ts index 02b37b258..1943befc8 100644 --- a/scripts/buildAssets/tankModels.ts +++ b/scripts/buildAssets/tankModels.ts @@ -5,8 +5,7 @@ import { extractModel } from '../../src/core/blitz/extractModel'; import { readXMLDVPL } from '../../src/core/blitz/readXMLDVPL'; import { readYAMLDVPL } from '../../src/core/blitz/readYAMLDVPL'; import { toUniqueId } from '../../src/core/blitz/toUniqueId'; -import { commitAssets } from '../../src/core/blitzkrieg/commitAssets'; -import { FileChange } from '../../src/core/blitzkrieg/commitMultipleFiles'; +import { AssetCommit } from '../../src/core/github/assetCommit'; import { DATA, POI } from './constants'; import { VehicleDefinitionList } from './definitions'; import { TankParameters } from './tankIcons'; @@ -24,8 +23,8 @@ export async function tankModels(production: boolean) { ); for (const nationIndex in nations) { - const changes: FileChange[] = []; const nation = nations[nationIndex]; + const assetCommit = new AssetCommit(`tank models ${nation}`, production); const tanks = await readXMLDVPL<{ root: VehicleDefinitionList }>( `${DATA}/${POI.vehicleDefinitions}/${nation}/list.xml.dvpl`, ); @@ -38,6 +37,8 @@ export async function tankModels(production: boolean) { .map(Number) .map((channel) => channel / 255) as Vector3Tuple; + if (nation !== 'other') continue; + await Promise.all( Object.entries(tanks.root).map(async ([tankKey, tank]) => { if (tankKey.includes('tutorial_bot')) return; @@ -55,16 +56,14 @@ export async function tankModels(production: boolean) { baseColor, ); - changes.push({ - path: `3d/tanks/models/${id}.glb`, - encoding: 'base64', - content: Buffer.from(await nodeIO.writeBinary(model)).toString( - 'base64', - ), - }); + assetCommit.add( + `3d/tanks/models/${id}.glb`, + Buffer.from(await nodeIO.writeBinary(model)).toString('base64'), + 'base64', + ); }), ); - await commitAssets(`tank models ${nation}`, changes, production); + assetCommit.push(); } } diff --git a/src/core/blitzkrieg/createBlob.ts b/src/core/blitzkrieg/createBlob.ts index 743820083..227cd2987 100644 --- a/src/core/blitzkrieg/createBlob.ts +++ b/src/core/blitzkrieg/createBlob.ts @@ -40,6 +40,6 @@ export const createBlob = throttle( } } }, - (5000 / (60 * 60 * 1000)) * 0.95, // 5% less than max rate + 1000, { trailing: true }, ); diff --git a/src/core/github/assetCommit.ts b/src/core/github/assetCommit.ts new file mode 100644 index 000000000..e1efe963e --- /dev/null +++ b/src/core/github/assetCommit.ts @@ -0,0 +1,60 @@ +import { GithubChangeBlob, createBlob } from '../blitzkrieg/createBlob'; +import { octokit } from '../blitzkrieg/octokit'; + +export class AssetCommit { + constructor( + public message: string, + public production: boolean, + ) {} + private blobs: GithubChangeBlob[] = []; + + async add(path: string, content: string, encoding: 'utf-8' | 'base64') { + console.log(`Blob: adding ${path}...`); + + const blob = await createBlob('tresabhi', 'blitzkrieg-assets', { + content, + encoding, + path, + }); + + this.blobs.push(blob!); + } + + async push() { + const latestCommitSha = ( + await octokit.git.getRef({ + owner: 'tresabhi', + repo: 'blitzkrieg-assets', + ref: `heads/${this.production ? 'main' : 'dev'}`, + }) + ).data.object.sha; + const treeSha = ( + await octokit.git.getCommit({ + owner: 'tresabhi', + repo: 'blitzkrieg-assets', + commit_sha: latestCommitSha, + }) + ).data.tree.sha; + const blobs: GithubChangeBlob[] = []; + + const { data: treeData } = await octokit.git.createTree({ + owner: 'tresabhi', + repo: 'blitzkrieg-assets', + base_tree: treeSha, + tree: blobs, + }); + const { data: newCommitData } = await octokit.git.createCommit({ + owner: 'tresabhi', + repo: 'blitzkrieg-assets', + message: this.message, + tree: treeData.sha, + parents: [latestCommitSha], + }); + await octokit.git.updateRef({ + owner: 'tresabhi', + repo: 'blitzkrieg-assets', + ref: `heads/${this.production ? 'main' : 'dev'}`, + sha: newCommitData.sha, + }); + } +}