2
0
mirror of https://github.com/tenrok/axios.git synced 2026-06-17 19:21:29 +03:00

chore(ci): improved contributors & PRs sections generator; (#5453)

This commit is contained in:
Dmitriy Mozgovoy
2023-01-09 15:24:02 +02:00
committed by GitHub
parent 18772ed8fd
commit e2a1e280f6
7 changed files with 279 additions and 139 deletions
+110 -20
View File
@@ -3,6 +3,7 @@ import util from "util";
import cp from "child_process";
import Handlebars from "handlebars";
import fs from "fs/promises";
import {colorize} from "./helpers/colorize.js";
const exec = util.promisify(cp.exec);
@@ -20,6 +21,8 @@ const getUserFromCommit = ((commitCache) => async (sha) => {
return commitCache[sha];
}
console.log(colorize()`fetch github commit info (${sha})`);
const {data} = await axios.get(`https://api.github.com/repos/axios/axios/commits/${sha}`);
return commitCache[sha] = {
@@ -32,6 +35,20 @@ const getUserFromCommit = ((commitCache) => async (sha) => {
}
})({});
const getIssueById = ((cache) => async (id) => {
if(cache[id] !== undefined) {
return cache[id];
}
try {
const {data} = await axios.get(`https://api.github.com/repos/axios/axios/issues/${id}`);
return cache[id] = data;
} catch (err) {
return null;
}
})({});
const getUserInfo = ((userCache) => async (userEntry) => {
const {email, commits} = userEntry;
@@ -39,11 +56,11 @@ const getUserInfo = ((userCache) => async (userEntry) => {
return userCache[email];
}
console.log(`fetch github user info [${userEntry.name}]`);
console.log(colorize()`fetch github user info [${userEntry.name}]`);
return userCache[email] = {
...userEntry,
...await getUserFromCommit(commits[0])
...await getUserFromCommit(commits[0].hash)
}
})({});
@@ -76,16 +93,24 @@ const deduplicate = (authors) => {
return combined;
}
const getReleaseInfo = async (version, useGithub) => {
version = 'v' + version.replace(/^v/, '');
const getReleaseInfo = ((releaseCache) => async (tag) => {
if(releaseCache[tag] !== undefined) {
return releaseCache[tag];
}
const releases = JSON.parse((await exec(
const isUnreleasedTag = !tag;
const version = 'v' + tag.replace(/^v/, '');
const command = isUnreleasedTag ?
`npx auto-changelog --unreleased-only --stdout --commit-limit false --template json` :
`npx auto-changelog ${
version ? '--starting-version ' + version + ' --ending-version ' + version: ''
} --stdout --commit-limit false --template json`)).stdout
);
version ? '--starting-version ' + version + ' --ending-version ' + version : ''
} --stdout --commit-limit false --template json`;
for(const release of releases) {
const release = JSON.parse((await exec(command)).stdout)[0];
if(release) {
const authors = {};
const commits = [
@@ -94,15 +119,30 @@ const getReleaseInfo = async (version, useGithub) => {
...release.merges.map(fix => fix.commit)
].filter(Boolean);
for(const {hash, author, email, insertions, deletions} of commits) {
const commitMergeMap = {};
for(const merge of release.merges) {
commitMergeMap[merge.commit.hash] = merge.id;
}
for (const {hash, author, email, insertions, deletions} of commits) {
const entry = authors[email] = (authors[email] || {
name: author,
prs: [],
email,
commits: [],
insertions: 0, deletions: 0
});
entry.commits.push(hash);
entry.commits.push({hash});
let pr;
if((pr = commitMergeMap[hash])) {
entry.prs.push(pr);
}
console.log(colorize()`Found commit [${hash}]`);
entry.displayName = entry.name || author || entry.login;
@@ -111,22 +151,27 @@ const getReleaseInfo = async (version, useGithub) => {
entry.insertions += insertions;
entry.deletions += deletions;
entry.points = entry.insertions + entry.deletions;
entry.isBot = entry.type === "Bot"
}
for(const [email, author] of Object.entries(authors)) {
authors[email] = await getUserInfo(author);
for (const [email, author] of Object.entries(authors)) {
const entry = authors[email] = await getUserInfo(author);
entry.isBot = entry.type === "Bot";
}
release.authors = Object.values(deduplicate(authors)).sort((a, b) => b.points - a.points);
release.authors = Object.values(deduplicate(authors))
.sort((a, b) => b.points - a.points);
release.allCommits = commits;
}
return releases;
}
releaseCache[tag] = release;
const renderContributorsList = async (version, useGithub = false, template) => {
const release = (await getReleaseInfo(version, useGithub))[0];
return release;
})({});
const renderContributorsList = async (tag, template) => {
const release = await getReleaseInfo(tag);
const compile = Handlebars.compile(String(await fs.readFile(template)))
@@ -135,6 +180,51 @@ const renderContributorsList = async (version, useGithub = false, template) => {
return removeExtraLineBreaks(cleanTemplate(content));
}
const renderPRsList = async (tag, template, {comments_threshold= 5, awesome_threshold= 5, label = 'add_to_changelog'} = {}) => {
const release = await getReleaseInfo(tag);
const prs = {};
for(const merge of release.merges) {
const pr = await getIssueById(merge.id);
if (pr && pr.labels.find(({name})=> name === label)) {
const {reactions, body} = pr;
prs[pr.number] = pr;
pr.isHot = pr.comments > comments_threshold;
const points = reactions['+1'] +
reactions['hooray'] + reactions['rocket'] + reactions['heart'] + reactions['laugh'] - reactions['-1'];
pr.isAwesome = points > awesome_threshold;
let match;
pr.messages = [];
if (body && (match = /```+changelog(.+)?```/gms.exec(body)) && match[1]) {
pr.messages.push(match[1]);
}
}
}
release.prs = Object.values(prs);
const compile = Handlebars.compile(String(await fs.readFile(template)))
const content = compile(release);
return removeExtraLineBreaks(cleanTemplate(content));
}
const getTagRef = async (tag) => {
try {
return (await exec(`git show-ref --tags "refs/tags/${tag}"`)).stdout.split(' ')[0];
} catch(e) {
}
}
export {
renderContributorsList
renderContributorsList,
renderPRsList,
getTagRef
}