Fix fetching canary release version name

This commit is contained in:
topjohnwu 2024-07-25 17:14:20 -07:00
parent 249f22fd5a
commit a13608618a
4 changed files with 66 additions and 37 deletions

View File

@ -32,7 +32,7 @@ type GhContentType = Unpacked<
GetResponseDataTypeFromEndpointMethod<typeof gh.repos.getContent>
>;
export default async function countDownloads() {
async function countDownloads(): Promise<string> {
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();

View File

@ -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 {

View File

@ -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<typeof ghBot.repos.getContent>
>;
export async function getVersionCode(): Promise<string> {
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 '';
}

View File

@ -5,5 +5,6 @@
"types": ["node"],
"module": "ES6",
"moduleResolution": "node",
"noErrorTruncation": true
}
}