|
| 1 | +/* |
| 2 | + * Keyman is copyright (C) SIL Global. MIT License. |
| 3 | + * |
| 4 | + * Created by pbdurdin on 2026-01-18 |
| 5 | + * |
| 6 | + * Generate github section of contributors.md |
| 7 | + */ |
| 8 | +import { Octokit } from "octokit"; |
| 9 | +import { paginateRest } from "@octokit/plugin-paginate-rest"; |
| 10 | + |
| 11 | +// Initiatlize an empty Octokit variable, to be set later. |
| 12 | +const OctokitInit = Octokit.plugin(paginateRest); |
| 13 | +let octokit = null; |
| 14 | +const githubOrgName = "keymanapp"; |
| 15 | + |
| 16 | +export async function getGithub(githubKey) { |
| 17 | + // Set the aforementioned Octokit variable, with the provided key. |
| 18 | + octokit = new OctokitInit({ |
| 19 | + auth: githubKey, |
| 20 | + }); |
| 21 | + |
| 22 | + // Get a list of every repo in the keymanapp github organization |
| 23 | + let allRepos = await getReposByOrg(githubOrgName); |
| 24 | + |
| 25 | + // Get the data of each of those repositories |
| 26 | + let allRepoData = await getAllRepos(allRepos); |
| 27 | + return allRepoData; |
| 28 | +} |
| 29 | + |
| 30 | +export function genGithub(repo) { |
| 31 | + const MAJOR_CONTRIBUTION_THRESHOLD = 100; |
| 32 | + |
| 33 | + // Initialize empty arrays, to be added to later |
| 34 | + let major = []; |
| 35 | + let minor = []; |
| 36 | + let all = []; |
| 37 | + |
| 38 | + // For each item in the provided data, |
| 39 | + repo.forEach((user) => { |
| 40 | + // ... check for bots, and skip over if one is found, |
| 41 | + if (/^(keyman-server|keyman-status|.+\[bot\])$/.test(user.login)) { |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + // ... if we have not already added this user to the user list, add them, |
| 46 | + if (!all.map(user => user.login).includes(user.login)) { |
| 47 | + all.push(user); |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + // ... Otherwise, increase their contributions score. |
| 52 | + let i = all.findIndex(oldUser => oldUser.login == user.login); |
| 53 | + all[i].contributions += user.contributions; |
| 54 | + }); |
| 55 | + |
| 56 | + // Sort into the major and minor categories |
| 57 | + all.forEach((user) => { |
| 58 | + if (user.contributions > MAJOR_CONTRIBUTION_THRESHOLD) { |
| 59 | + major.push(user); |
| 60 | + } |
| 61 | + else { |
| 62 | + minor.push(user); |
| 63 | + }; |
| 64 | + }); |
| 65 | + |
| 66 | + // Sort the major and minor categories by contributions |
| 67 | + major.sort((a, b) => a.login.localeCompare(b.login)); |
| 68 | + minor.sort((a, b) => a.login.localeCompare(b.login)); |
| 69 | + return [major, minor]; |
| 70 | +} |
| 71 | + |
| 72 | +async function getReposByOrg(org) { |
| 73 | + // Fetch a list of all repositories belonging to this organization. |
| 74 | + |
| 75 | + console.log("Retrieving list of Repositories"); |
| 76 | + let repos = await octokit.paginate(`GET /orgs/${org}/repos`, { |
| 77 | + org, |
| 78 | + per_page: 100, |
| 79 | + type: "sources", |
| 80 | + headers: { |
| 81 | + "X-GitHub-Api-Version": "2022-11-28", |
| 82 | + }, |
| 83 | + }); |
| 84 | + |
| 85 | + // Map the repository data to make mearly a list of names. |
| 86 | + return repos.map(repo => repo.name); |
| 87 | +} |
| 88 | + |
| 89 | +async function getAllRepos(allRepos) { |
| 90 | + // Fetch the data for the given list of repositories. |
| 91 | + |
| 92 | + let allRepoData = []; |
| 93 | + |
| 94 | + // Using for instead of foreach, as foreach does not support asynchrony. |
| 95 | + for (let repo of allRepos) { |
| 96 | + // Get data, then concat it to all previously fetched data. |
| 97 | + console.log(`Fetching repository contributors: ${repo}`); |
| 98 | + let repoData = await getRepo(repo); |
| 99 | + allRepoData = allRepoData.concat(repoData); |
| 100 | + }; |
| 101 | + return allRepoData; |
| 102 | +} |
| 103 | + |
| 104 | +async function getRepo(repoName) { |
| 105 | + // Fetch data for repo of the inputted name. |
| 106 | + let repo = await octokit.paginate(`GET /repos/${githubOrgName}/${repoName}/contributors`, { |
| 107 | + owner: "keymanapp", |
| 108 | + repo: repoName, |
| 109 | + per_page: 100, |
| 110 | + headers: { |
| 111 | + "X-GitHub-Api-Version": "2022-11-28", |
| 112 | + }, |
| 113 | + }); |
| 114 | + return repo; |
| 115 | +} |
0 commit comments