Fix/script changelog creation (#496)

* [Tooling] Select git commit author correctly

Caused the credit for a commit without a pr id to show as "@[object Object]"

* [Tooling] Fix typo in file name

* [Tooling] Use total commit count from webUI repos master branch
This commit is contained in:
schroda
2023-12-13 23:48:35 +01:00
committed by GitHub
parent 1dc60af67c
commit 214043fe9d
2 changed files with 30 additions and 11 deletions

View File

@@ -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<GithubCommit[]> => {
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],
}));