mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-22 03:07:04 +02:00
fix: more imports 4
This commit is contained in:
1 parent
843992e631
commit
fd2e2f9c7b
11 files changed
+14
-15
No files matched your search
@@ -0,0 +1,64 @@
|
||||
import { assertSecret } from '@blitzkit/core';
|
||||
import { octokit } from '../../../../website/src/core/blitzkit/octokit';
|
||||
import { GithubChangeBlob, createBlob } from './createBlob';
|
||||
|
||||
export class AssetCommit {
|
||||
constructor(public message: string) {}
|
||||
private blobs: GithubChangeBlob[] = [];
|
||||
|
||||
async add(path: string, content: string, encoding: 'utf-8' | 'base64') {
|
||||
console.log(`Blob: adding ${path}...`);
|
||||
|
||||
const blob = await createBlob(
|
||||
'tresabhi',
|
||||
assertSecret(process.env.NEXT_PUBLIC_ASSET_REPO),
|
||||
{
|
||||
content,
|
||||
encoding,
|
||||
path,
|
||||
},
|
||||
);
|
||||
|
||||
this.blobs.push(blob!);
|
||||
}
|
||||
|
||||
async push() {
|
||||
if (this.blobs.length === 0) return;
|
||||
|
||||
const latestCommitSha = (
|
||||
await octokit.git.getRef({
|
||||
owner: 'tresabhi',
|
||||
repo: assertSecret(process.env.NEXT_PUBLIC_ASSET_REPO),
|
||||
ref: `heads/${assertSecret(process.env.NEXT_PUBLIC_ASSET_BRANCH)}`,
|
||||
})
|
||||
).data.object.sha;
|
||||
const treeSha = (
|
||||
await octokit.git.getCommit({
|
||||
owner: 'tresabhi',
|
||||
repo: assertSecret(process.env.NEXT_PUBLIC_ASSET_REPO),
|
||||
commit_sha: latestCommitSha,
|
||||
})
|
||||
).data.tree.sha;
|
||||
const blobs: GithubChangeBlob[] = [];
|
||||
|
||||
const { data: treeData } = await octokit.git.createTree({
|
||||
owner: 'tresabhi',
|
||||
repo: assertSecret(process.env.NEXT_PUBLIC_ASSET_REPO),
|
||||
base_tree: treeSha,
|
||||
tree: blobs,
|
||||
});
|
||||
const { data: newCommitData } = await octokit.git.createCommit({
|
||||
owner: 'tresabhi',
|
||||
repo: assertSecret(process.env.NEXT_PUBLIC_ASSET_REPO),
|
||||
message: this.message,
|
||||
tree: treeData.sha,
|
||||
parents: [latestCommitSha],
|
||||
});
|
||||
await octokit.git.updateRef({
|
||||
owner: 'tresabhi',
|
||||
repo: assertSecret(process.env.NEXT_PUBLIC_ASSET_REPO),
|
||||
ref: `heads/${assertSecret(process.env.NEXT_PUBLIC_ASSET_BRANCH)}`,
|
||||
sha: newCommitData.sha,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,6 @@
|
||||
import ProgressBar from 'progress';
|
||||
import {
|
||||
GithubChangeBlob,
|
||||
createBlob,
|
||||
} from '../../../../website/src/core/blitzkit/createBlob';
|
||||
import { octokit } from '../../../../website/src/core/blitzkit/octokit';
|
||||
import { GithubChangeBlob, createBlob } from './createBlob';
|
||||
|
||||
export interface FileChange {
|
||||
path: string;
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
import { throttle } from 'lodash';
|
||||
import { octokit } from '../../../../website/src/core/blitzkit/octokit';
|
||||
import { FileChange } from './commitMultipleFiles';
|
||||
|
||||
export interface GithubChangeBlob {
|
||||
sha: string;
|
||||
path: string;
|
||||
mode: '100644';
|
||||
type: 'blob';
|
||||
}
|
||||
|
||||
const TIME_PER_BLOB = 2 ** 4 * 1000;
|
||||
|
||||
// github rate limit: 5000 requests per hour
|
||||
export const createBlob = throttle(
|
||||
async (owner: string, repo: string, change: FileChange) => {
|
||||
while (true) {
|
||||
try {
|
||||
const { sha } = (
|
||||
await octokit.git.createBlob({
|
||||
owner,
|
||||
repo,
|
||||
content: change.content,
|
||||
encoding: change.encoding,
|
||||
})
|
||||
).data;
|
||||
|
||||
return {
|
||||
sha,
|
||||
path: change.path,
|
||||
mode: '100644',
|
||||
type: 'blob',
|
||||
} satisfies GithubChangeBlob;
|
||||
} catch (error) {
|
||||
console.warn(
|
||||
`Failed blob ${change.path}; retrying in ${TIME_PER_BLOB}ms...`,
|
||||
// error,
|
||||
);
|
||||
|
||||
await new Promise((resolve) => setTimeout(resolve, TIME_PER_BLOB));
|
||||
}
|
||||
}
|
||||
},
|
||||
(5000 / (60 * 60 * 1000)) * 0.9,
|
||||
{ trailing: true },
|
||||
);
|
||||
Reference in new issue
Block a user