2022-09-06 03:51:28 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { Octokit } from '@octokit/rest';
|
|
|
|
import shuffle from 'lodash.shuffle';
|
2023-04-27 22:16:19 +00:00
|
|
|
import { getEnvVar } from '@woocommerce/monorepo-utils/src/core/environment';
|
2022-09-06 03:51:28 +00:00
|
|
|
|
|
|
|
export type ContributorData = {
|
|
|
|
totalCommits: number;
|
|
|
|
contributors: Record< string, unknown >[];
|
|
|
|
org: string;
|
|
|
|
repo: string;
|
|
|
|
baseRef: string;
|
|
|
|
headRef: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
const filterUniqBy = ( arr: Record< string, unknown >[], key: string ) => {
|
|
|
|
const seen = new Set();
|
|
|
|
return arr.filter( ( item ) => {
|
|
|
|
const k = item[ key ];
|
|
|
|
if ( seen.has( k ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
seen.add( k );
|
|
|
|
return true;
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
const PAGE_SIZE = 100;
|
|
|
|
|
|
|
|
export const getContributorData = async (
|
|
|
|
orgName: string,
|
|
|
|
repoName: string,
|
|
|
|
baseRef: string,
|
|
|
|
headRef: string
|
|
|
|
) => {
|
|
|
|
const octokit = new Octokit( {
|
|
|
|
auth: getEnvVar( 'GITHUB_ACCESS_TOKEN', true ),
|
|
|
|
} );
|
|
|
|
|
|
|
|
const {
|
|
|
|
data: { total_commits, commits },
|
|
|
|
} = await octokit.repos.compareCommitsWithBasehead( {
|
|
|
|
owner: orgName,
|
|
|
|
repo: repoName,
|
|
|
|
basehead: `${ baseRef }...${ headRef }`,
|
|
|
|
per_page: PAGE_SIZE,
|
|
|
|
} );
|
|
|
|
|
|
|
|
const pages = Math.ceil( total_commits / PAGE_SIZE );
|
|
|
|
const allAuthors = [];
|
|
|
|
|
|
|
|
// add page 1 commits
|
|
|
|
allAuthors.push(
|
|
|
|
...commits
|
|
|
|
.filter( ( commit ) => {
|
|
|
|
return (
|
|
|
|
!! commit.author && ! commit.author.login.includes( 'bot' )
|
|
|
|
);
|
|
|
|
} )
|
|
|
|
.map( ( commit ) => commit.author )
|
|
|
|
);
|
|
|
|
|
|
|
|
for ( let i = 2; i <= pages; i++ ) {
|
|
|
|
const {
|
|
|
|
data: { commits: pageCommits },
|
|
|
|
} = await octokit.repos.compareCommitsWithBasehead( {
|
|
|
|
owner: orgName,
|
|
|
|
repo: repoName,
|
|
|
|
basehead: `${ baseRef }...${ headRef }`,
|
|
|
|
per_page: PAGE_SIZE,
|
|
|
|
page: i,
|
|
|
|
} );
|
|
|
|
|
|
|
|
allAuthors.push(
|
|
|
|
...pageCommits
|
|
|
|
.filter( ( commit ) => {
|
|
|
|
return (
|
|
|
|
!! commit.author &&
|
|
|
|
! commit.author.login.includes( 'bot' )
|
|
|
|
);
|
|
|
|
} )
|
|
|
|
.map( ( commit ) => commit.author )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
totalCommits: total_commits,
|
|
|
|
contributors: shuffle(
|
|
|
|
filterUniqBy(
|
|
|
|
allAuthors as Array< Record< string, unknown > >,
|
|
|
|
'login'
|
|
|
|
)
|
|
|
|
),
|
|
|
|
org: orgName,
|
|
|
|
repo: repoName,
|
|
|
|
baseRef,
|
|
|
|
headRef,
|
|
|
|
} as ContributorData;
|
|
|
|
};
|
2023-03-16 19:37:53 +00:00
|
|
|
|
|
|
|
export const getMostRecentFinal = async () => {
|
|
|
|
const octokit = new Octokit( {
|
|
|
|
auth: getEnvVar( 'GITHUB_ACCESS_TOKEN', true ),
|
|
|
|
} );
|
|
|
|
|
|
|
|
const release = await octokit.repos.getLatestRelease( {
|
|
|
|
owner: 'woocommerce',
|
|
|
|
repo: 'woocommerce',
|
|
|
|
} );
|
|
|
|
|
|
|
|
return release.data;
|
|
|
|
};
|
2023-04-03 03:19:39 +00:00
|
|
|
|
|
|
|
export const getMostRecentBeta = async () => {
|
|
|
|
const octokit = new Octokit( {
|
|
|
|
auth: getEnvVar( 'GITHUB_ACCESS_TOKEN', true ),
|
|
|
|
} );
|
|
|
|
|
|
|
|
const { data: releases } = await octokit.repos.listReleases( {
|
|
|
|
owner: 'woocommerce',
|
|
|
|
repo: 'woocommerce',
|
|
|
|
} );
|
|
|
|
|
|
|
|
const betaReleases = releases.filter(
|
|
|
|
( release ) =>
|
|
|
|
release?.name && release.name.toLowerCase().includes( 'beta' )
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( betaReleases.length === 0 ) {
|
|
|
|
throw new Error( 'No beta releases found' );
|
|
|
|
}
|
|
|
|
|
|
|
|
const latestBetaRelease = betaReleases.reduce( ( latest, current ) => {
|
|
|
|
const latestDate = new Date( latest.created_at );
|
|
|
|
const currentDate = new Date( current.created_at );
|
|
|
|
return currentDate > latestDate ? current : latest;
|
|
|
|
} );
|
|
|
|
|
|
|
|
return latestBetaRelease;
|
|
|
|
};
|