|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import React, {useEffect, useState} from 'react'; |
| 4 | +import organizations from "./data/organizations.json"; |
| 5 | +import {Octokit} from "@octokit/core"; |
| 6 | +import {paginateRest} from "@octokit/plugin-paginate-rest"; |
| 7 | +import {retry} from "@octokit/plugin-retry"; |
| 8 | + |
| 9 | +export interface OrganizationsJson { |
| 10 | + id: number; |
| 11 | + name: string; |
| 12 | + url: string; |
| 13 | + owner: string; |
| 14 | +} |
| 15 | + |
| 16 | +type OrganizationsWithRepos = { |
| 17 | + id: number; |
| 18 | + name: string; |
| 19 | + url: string; |
| 20 | + owner: string; |
| 21 | + repos: number |
| 22 | +} |
| 23 | + |
| 24 | +export const OrganizationsOnGithub = () => { |
| 25 | + const organizationsOnGithub: OrganizationsJson[] = organizations |
| 26 | + const [organizationsWithRepos, setOrganizationsWithRepos] = useState<OrganizationsWithRepos[]>([]); |
| 27 | + |
| 28 | + useEffect(() => { |
| 29 | + organizationsOnGithub.forEach(organization => ( |
| 30 | + getNumberOfPublicRepos(organization.owner) |
| 31 | + .then((numberOfRepos) => { |
| 32 | + setOrganizationsWithRepos(organizationsWithRepos => { |
| 33 | + return [{ |
| 34 | + id: organization.id, |
| 35 | + name: organization.name, |
| 36 | + url: organization.url, |
| 37 | + owner: organization.owner, |
| 38 | + repos: numberOfRepos |
| 39 | + }, ...organizationsWithRepos] |
| 40 | + }); |
| 41 | + |
| 42 | + }))); |
| 43 | + }, []); |
| 44 | + |
| 45 | + const organizationsWithReposByRepoNumber: OrganizationsWithRepos[] = organizationsWithRepos.sort((a, b) => b.repos - a.repos) |
| 46 | + |
| 47 | + |
| 48 | + return ( |
| 49 | + <div className="flex min-h-screen flex-col p-16 md:items-center md:p-24"> |
| 50 | + <h1 className="text-5xl font-extrabold mb-4">Norwegian public organizations on GitHub</h1> |
| 51 | + <div className="md:flex md:min-h-screen md:flex-col"> |
| 52 | + <div className="mt-4 md:mt-10"> |
| 53 | + {organizationsWithReposByRepoNumber && |
| 54 | + <table className="w-full text-sm text-left rtl:text-right"> |
| 55 | + <thead className="text-m uppercase"> |
| 56 | + <tr> |
| 57 | + <th scope="col" className="px-6 py-3">Name</th> |
| 58 | + <th scope="col" className="px-6 py-3">Url</th> |
| 59 | + <th scope="col" className="px-6 py-3">Number of repos</th> |
| 60 | + </tr> |
| 61 | + </thead> |
| 62 | + <tbody> |
| 63 | + {organizationsWithReposByRepoNumber.map(organization => |
| 64 | + <tr className="border-b dark:border-gray-700" key={organization.id}> |
| 65 | + <td className="px-6 py-4"> |
| 66 | + {organization.name} |
| 67 | + </td> |
| 68 | + <td className="px-6 py-4"> |
| 69 | + <a className="text-blue-600 dark:text-blue-500 hover:underline" |
| 70 | + href={organization.url}>{organization.url} </a> |
| 71 | + </td> |
| 72 | + <td className="px-6 py-4"> |
| 73 | + {organization.repos} |
| 74 | + </td> |
| 75 | + </tr> |
| 76 | + )} |
| 77 | + </tbody> |
| 78 | + </table> |
| 79 | + } |
| 80 | + </div> |
| 81 | + </div> |
| 82 | + </div> |
| 83 | + ); |
| 84 | +} |
| 85 | + |
| 86 | + |
| 87 | +async function getNumberOfPublicRepos(owner: string): Promise<number> { |
| 88 | + const octokitplugin = Octokit.plugin(paginateRest, retry).defaults({ |
| 89 | + userAgent: "norwegian-public-organizations", |
| 90 | + /// auth: 'mysupersecrettoken' |
| 91 | + }); |
| 92 | + |
| 93 | + const myOctokit = new octokitplugin() |
| 94 | + |
| 95 | + try { |
| 96 | + const repos = await myOctokit.request(`GET /orgs/${owner}`, { |
| 97 | + headers: { |
| 98 | + 'X-GitHub-Api-Version': '2022-11-28' |
| 99 | + } |
| 100 | + }) |
| 101 | + |
| 102 | + return repos.data.public_repos |
| 103 | + } catch (e) { |
| 104 | + console.log(e) |
| 105 | + return 0 |
| 106 | + } |
| 107 | +} |
| 108 | + |
0 commit comments