From a13608618ac9aa1f2fc835cb63e01d83f8983d98 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Thu, 25 Jul 2024 17:14:20 -0700 Subject: [PATCH] Fix fetching canary release version name --- src/count.ts | 61 +++++++++++++++++++++++++++++++++------------------ src/index.ts | 6 ++--- src/utils.ts | 35 ++++++++++++++++++----------- tsconfig.json | 1 + 4 files changed, 66 insertions(+), 37 deletions(-) diff --git a/src/count.ts b/src/count.ts index e1b09d3..6c2bd0d 100644 --- a/src/count.ts +++ b/src/count.ts @@ -32,7 +32,7 @@ type GhContentType = Unpacked< GetResponseDataTypeFromEndpointMethod >; -export default async function countDownloads() { +async function countDownloads(): Promise { const results: ResultInfo = { totalString: '', total: 0, @@ -80,7 +80,7 @@ export default async function countDownloads() { const [ghReleases, mgrReleases] = metadata; - function collectGhStats(name: string, release: GhReleaseType): DetailInfo { + function updateGhStats(name: string, release: GhReleaseType): DetailInfo { const info = getInfo(name); let count = 0; release.assets.forEach((asset) => { @@ -100,26 +100,35 @@ export default async function countDownloads() { } // Scan through all releases - ghReleases.forEach((release) => { - const tag = release.tag_name; - let name: string; - if (tag.includes('manager')) { - const ver = tag.replace('manager-v', ''); - name = appVersionMapping[ver]; - } else if (release.prerelease) { - const date = new Date(release.created_at).toISOString().substring(0, 10); - name = `${date} (${tag})`; - } else { - name = tag.replace('v', ''); - } - const info = collectGhStats(name, release); - info.is_canary = release.prerelease; - }); + await Promise.all( + ghReleases.map(async (release) => { + const tag = release.tag_name; + let name: string; + if (tag.includes('manager')) { + const ver = tag.replace('manager-v', ''); + name = appVersionMapping[ver]; + } else if (release.prerelease) { + const date = new Date(release.created_at) + .toISOString() + .substring(0, 10); + // Get sha value from the tag name + const sha = ( + await gh.git.getRef({ ...MAGISK_REPO, ref: `tags/${tag}` }) + ).data.object.sha; + const ver = sha.substring(0, 8); + name = `${date} (${ver})`; + } else { + name = tag.replace('v', ''); + } + const info = updateGhStats(name, release); + info.is_canary = release.prerelease; + }), + ); mgrReleases.forEach((release) => { const ver = release.tag_name.replace('v', ''); const name = appVersionMapping[ver]; - collectGhStats(name, release); + updateGhStats(name, release); }); function versionComparator(a: string, b: string): number { @@ -176,10 +185,11 @@ export default async function countDownloads() { return value; } - const resultStr = `${JSON.stringify(results, trimObject, 2)}\n`; - const resultObj = JSON.parse(resultStr); + return `${JSON.stringify(results, trimObject, 2)}\n`; +} - console.dir(resultObj, { depth: null }); +export default async function updateCountJson() { + const resultStr = await countDownloads(); // Fetch the blob sha of the existing count.json const count_json = ( @@ -201,3 +211,12 @@ export default async function countDownloads() { sha: count_json.sha, }); } + +// For testing only +// Uncomment the last line and call: `npx esrun src/count.ts` +async function localTest() { + const resultStr = await countDownloads(); + const resultObj = JSON.parse(resultStr); + console.dir(resultObj, { depth: null }); +} +// localTest(); diff --git a/src/index.ts b/src/index.ts index e824724..c48eba3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,6 @@ import { blockAllSpam } from './utils.js'; import server from './webhook.js'; -import countDownloads from './count.js'; +import updateCountJson from './count.js'; async function main() { // Scan and block all spam every 8 hours @@ -8,8 +8,8 @@ async function main() { setInterval(blockAllSpam, 8 * 60 * 60 * 1000); // Count downloads every 24 hours - await countDownloads(); - setInterval(countDownloads, 24 * 60 * 60 * 1000); + await updateCountJson(); + setInterval(updateCountJson, 24 * 60 * 60 * 1000); // Start webhook server try { diff --git a/src/utils.ts b/src/utils.ts index 1bec048..7ea6ad7 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,3 +1,4 @@ +import { GetResponseDataTypeFromEndpointMethod } from '@octokit/types'; import { Issue, PullRequest } from '@octokit/webhooks-types'; import { ghOwner as gh, ghBot } from './env.js'; @@ -40,7 +41,7 @@ export async function closePR(repo: GithubRepo, pr: PullRequest) { export async function commentIssue( repo: GithubRepo, issue: Issue, - body: string + body: string, ) { await ghBot.issues.createComment({ ...repo, @@ -49,19 +50,27 @@ export async function commentIssue( }); } +type GhContentType = Unpacked< + GetResponseDataTypeFromEndpointMethod +>; + export async function getVersionCode(): Promise { - const props = (await gh.repos.getContent({ - owner: 'topjohnwu', - repo: 'Magisk', - path: 'gradle.properties', - })) as any; + const props = ( + await ghBot.repos.getContent({ + owner: 'topjohnwu', + repo: 'Magisk', + path: 'gradle.properties', + }) + ).data as GhContentType; - const ver = Buffer.from(props.data.content, props.data.encoding) - .toString() - .split('\n') - .filter((s) => s.startsWith('magisk.versionCode')) - .at(-1) - ?.replace('magisk.versionCode=', '') as string; + if (props.type === 'file' && 'encoding' in props) { + return Buffer.from(props.content, props.encoding as BufferEncoding) + .toString() + .split('\n') + .filter((s) => s.startsWith('magisk.versionCode')) + .at(-1) + ?.replace('magisk.versionCode=', '') as string; + } - return ver; + return ''; } diff --git a/tsconfig.json b/tsconfig.json index 7a580ab..1141384 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,5 +5,6 @@ "types": ["node"], "module": "ES6", "moduleResolution": "node", + "noErrorTruncation": true } }