diff --git a/package.json b/package.json index 7c809e24..647141b5 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "build-md5": "find build -type f | sort | xargs md5sum | awk '{ print $1 }' | tr -d '\n' | md5sum| awk '{ print $1 }' > buildZip/md5sum ", "build-zip": "cd build && rev=$(git rev-list HEAD --count) && echo r$rev > revision && zip -9 -r ../buildZip/Tachidesk-WebUI-r$rev *", "lint": "eslint src --ext .ts,.tsx,.js,.jsx", - "createChangelog": "ts-node tools/scripts/createReleaseChanglog.ts", + "createChangelog": "ts-node tools/scripts/createReleaseChangelog.ts", "updateDeps": "yarn upgrade && yarn syncyarnlock -s -k && yarn && git add package.json yarn.lock && git commit -m \"Update dependencies\" && echo Updated non breaking dependencies. Check potential breaking changes below... && yarn outdated", "gql:codegen-base": "graphql-codegen --config gql_codegen.ts", "gql:codegen-formatter": "ts-node tools/scripts/codegenFormatter.ts", diff --git a/tools/scripts/createReleaseChanglog.ts b/tools/scripts/createReleaseChangelog.ts similarity index 74% rename from tools/scripts/createReleaseChanglog.ts rename to tools/scripts/createReleaseChangelog.ts index 7ca528c0..2b905574 100644 --- a/tools/scripts/createReleaseChanglog.ts +++ b/tools/scripts/createReleaseChangelog.ts @@ -6,7 +6,6 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -import { execSync } from 'child_process'; import yargs from 'yargs'; import githubToken from './github_token.json'; @@ -15,7 +14,9 @@ type GithubCommit = { html_url: string; commit: { message: string; - author: string; + author: { + name: string; + }; }; author: { login: string } | null; }; @@ -39,6 +40,29 @@ const { sha } = yargs }) .parseSync(); +const getHeaderWithAuth = () => ({ + headers: { + // token is optional, might be needed in case of getting rate-limited - to provide a token create a "github_token.json" (see "github_token.template.json") and add your token in there + Authorization: githubToken.token ? `token ${githubToken.token}` : '', + }, +}); + +const fetchTotalCommitCount = async (owner: string, repo: string, branch: string = 'master') => { + const response = await fetch( + `https://api.github.com/repos/${owner}/${repo}/commits?sha=${branch}&per_page=1&page=1`, + getHeaderWithAuth(), + ); + + const linkHeader = response.headers.get('Link'); + const pageCountMatch = linkHeader?.match(/page=(\d+)>; rel="last"/); + + if (!pageCountMatch) { + throw new Error('Page count not found in Link header'); + } + + return parseInt(pageCountMatch[1], 10); +}; + /** * Fetches and returns all commits after the provided commit hash */ @@ -49,12 +73,7 @@ const fetchCommits = async ( page: number = 1, ): Promise => { const commitList = (await ( - await fetch(`https://api.github.com/repos/${owner}/${repo}/commits?page=${page}`, { - headers: { - // token is optional, might be needed in case of getting rate-limited - to provide a token create a "github_token.json" (see "github_token.template.json") and add your token in there - Authorization: githubToken.token ? `token ${githubToken.token}` : '', - }, - }) + await fetch(`https://api.github.com/repos/${owner}/${repo}/commits?page=${page}`, getHeaderWithAuth()) ).json()) as GithubCommit[]; const indexOfOldestCommitToLoad = commitList.findIndex((commit) => commit.sha === loadUntilSha); @@ -90,7 +109,7 @@ const createChangelog = async (prevReleaseLastCommitSha: string) => { const owner = 'Suwayomi'; const repo = 'Tachidesk-WebUI'; - const numberOfCommits = Number(execSync('git rev-list origin/master --count').toString()); + const numberOfCommits = await fetchTotalCommitCount(owner, repo); const githubCommits = await fetchCommits(owner, repo, prevReleaseLastCommitSha); const commits: Commit[] = githubCommits.map((githubCommit, index) => ({ @@ -98,7 +117,7 @@ const createChangelog = async (prevReleaseLastCommitSha: string) => { revision: numberOfCommits - index, url: githubCommit.html_url, githubUser: githubCommit.author?.login, - author: githubCommit.commit.author, + author: githubCommit.commit.author.name, title: githubCommit.commit.message.split('\n')[0], }));