Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update script to paginate request + init stars #436

Merged
merged 7 commits into from
Aug 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 76 additions & 17 deletions .github/actions/update-stars/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ function gatherUrls() {

// Github

/**
* Get all GitHub repositories
* @returns {Array<{owner: string, repo: string}>}
*/
function getAllGHRepos() {
return gatherUrls()
.filter((url) => url !== false && githubNameRegexp.test(url))
Expand All @@ -55,27 +59,65 @@ function ghRepoGraphQl({ owner, repo }) {
return `${identifier}: repository(name: "${repo}", owner: "${owner}"){nameWithOwner stargazerCount}`;
}

/**
* Divide an array into multiple smaller array
* @param {Array} input
* @param {number} size
* @return {Array<Array>}
*/
function chunk(input, size) {
size = size < 1 ? 10 : size;
const pages = Math.ceil(input.length / size);
const final = [];
for (let index = 0; index < pages; index++) {
final.push(input.slice(index * size, (index + 1) * size));
}
return final;
}

/**
* Get the number of stars for all GitHub repositories.
* The result is a Map where the key the repo name and the value is the number of stars.
* @returns {Promise<Record<string, number>>}
*/
async function getGHStars() {
const repoData = getAllGHRepos();
let body =
'query{' + '\n' + repoData.map((repoInfo) => ghRepoGraphQl(repoInfo)).join('\n') + '\n' + '}';
let lines = await doGraphQlQuery(ghGraphQlUrl, body, {
authorization:
'Bearer ' +
core.getInput('token', {
// required: true,
trimWhitespace: true
})
});
core.info('Found ' + repoData.length + ' repositories');
const pagedRepoData = chunk(repoData, 100);
const pageCount = pagedRepoData.length;
core.debug('Divide the repositories into ' + pageCount + ' pages (of 100 repositories)');
let lines = [];
for (let index = 0; index < pageCount; index++) {
const page = pagedRepoData[index];
core.debug('Running GraphQL for page ' + (index + 1) + '/' + pageCount);
let body =
'query{' + '\n' + page.map((repoInfo) => ghRepoGraphQl(repoInfo)).join('\n') + '\n' + '}';
lines = [
...lines,
...(await doGraphQlQuery(ghGraphQlUrl, body, {
authorization:
'Bearer ' +
core.getInput('token', {
// required: true,
trimWhitespace: true
})
}))
];
}
return Object.fromEntries(
lines
.filter((line) => line?.nameWithOwner)
.map((line) => [line.nameWithOwner.toLowerCase(), line.stargazerCount])
.sort()
);
}

// Gitlab

/**
* Get all GitLab repositories path (relative to GitLab root)
* @returns {Array<string>}
*/
function getAllGitlabRepos() {
return gatherUrls()
.filter((url) => url !== false && gitlabNameRegExp.test(url))
Expand All @@ -89,25 +131,42 @@ function gitlabRepoGraphQl(name) {
return `${identifier}: project(fullPath: "${name}"){starCount fullPath}`;
}

/**
* Get the number of stars for all Gitlab repositories.
* The result is a Map where the key the repo name and the value is the number of stars.
* @returns {Promise<Record<string, number>>}
*/
async function getGitlabStars() {
const repoData = getAllGitlabRepos();
let body =
'query{' +
'\n' +
repoData.map((repoInfo) => gitlabRepoGraphQl(repoInfo)).join('\n') +
'\n' +
'}';
let lines = await doGraphQlQuery(gitlabGraphQlUrl, body);
core.info('Found ' + repoData.length + ' repositories');
const pagedRepoData = chunk(repoData, 100);
const pageCount = pagedRepoData.length;
core.debug('Divide the repositories into ' + pageCount + ' pages (of 100 repositories)');
let lines = [];
for (let index = 0; index < pageCount; index++) {
const page = pagedRepoData[index];
core.debug('Running GraphQL for page ' + (index + 1) + '/' + pageCount);
const body =
'query{' + '\n' + page.map((repoInfo) => gitlabRepoGraphQl(repoInfo)).join('\n') + '\n' + '}';
lines = [...lines, ...(await doGraphQlQuery(gitlabGraphQlUrl, body))];
}
return Object.fromEntries(
lines
.filter((line) => line?.fullPath)
.map((line) => [line.fullPath.toLowerCase(), line.starCount])
.sort()
);
}

async function main() {
core.startGroup('GitHub');
const github = await getGHStars();
core.endGroup();

core.startGroup('GitLab');
const gitlab = await getGitlabStars();
core.endGroup();

core.info(
`\tGithub: ${Object.keys(github).length} repositories (${Object.values(github).reduce(
(count, item) => count + item,
Expand Down
Loading