Commit a98b4258ac
Unsigned
Layout: unified · split
web/src/components/things/ItemGitHubMetrics.astro added +156
| @@ -0,0 +1,156 @@ | ||
| 1 | --- | |
| 2 | import FontAwesome from "@components/form/FontAwesome.svelte"; | |
| 3 | const { github } = Astro.props; | |
| 4 | ||
| 5 | ||
| 6 | const [user, repo] = github.split("/"); | |
| 7 | ||
| 8 | ||
| 9 | /** | |
| 10 | * For a given `user/repo` fetch repository stats from the GitHub API data | |
| 11 | * If API key is available through env var, use it to increase rate limit | |
| 12 | * Returns the response data and status code (200 == success) | |
| 13 | * @param repo | |
| 14 | */ | |
| 15 | const fetchGitHubData = async (repo: string) => { | |
| 16 | const apiKey = import.meta.env.GITHUB_API_KEY; | |
| 17 | const headers = new Headers(); | |
| 18 | if (apiKey) { | |
| 19 | headers.append("Authorization", `token ${apiKey}`); | |
| 20 | } | |
| 21 | ||
| 22 | let data = {}; | |
| 23 | let statusCode = 0; | |
| 24 | ||
| 25 | const response = await fetch(`https://api.github.com/repos/${repo}`, { | |
| 26 | headers: headers, | |
| 27 | }).catch((e) => { | |
| 28 | console.error(`Network error: ${e.message}`); | |
| 29 | // Return a placeholder response to handle this gracefully | |
| 30 | return null; | |
| 31 | }); | |
| 32 | ||
| 33 | // Check if fetch was successful but caught by the catch block | |
| 34 | if (!response) { | |
| 35 | return { data, statusCode: 503 }; // Use an appropriate error status code | |
| 36 | } | |
| 37 | ||
| 38 | statusCode = response.status; | |
| 39 | ||
| 40 | // Only attempt to parse JSON if the response was OK | |
| 41 | if (response.ok) { | |
| 42 | data = await response.json(); | |
| 43 | } else { | |
| 44 | console.error(`HTTP error: Received status code ${response.status}`); | |
| 45 | } | |
| 46 | ||
| 47 | return { data, statusCode }; | |
| 48 | }; | |
| 49 | ||
| 50 | ||
| 51 | /** | |
| 52 | * Given a license object, return SPDX ID, or a formatted name | |
| 53 | * @param license | |
| 54 | */ | |
| 55 | const formatLicense = (license: { spdx_id?: string, name?: string }) => { | |
| 56 | if (!license) { | |
| 57 | return "Unknown"; | |
| 58 | } | |
| 59 | if (license.spdx_id === "NOASSERTION") { | |
| 60 | return "No License"; | |
| 61 | } | |
| 62 | return license.spdx_id; | |
| 63 | } | |
| 64 | ||
| 65 | /** | |
| 66 | * Given a (possibly large) number, return a formatted string | |
| 67 | * E.g. If greater than thousand, then return in k format | |
| 68 | * @param num | |
| 69 | */ | |
| 70 | const formatBigNumber = (num: number) => { | |
| 71 | if (num > 1000) { | |
| 72 | return `${(num / 1000).toFixed(1)}k`; | |
| 73 | } | |
| 74 | return num; | |
| 75 | } | |
| 76 | ||
| 77 | // Initiate GitHub fetch, and make available to the component | |
| 78 | const stats = await fetchGitHubData(github); | |
| 79 | ||
| 80 | const { | |
| 81 | stargazers_count, forks_count, open_issues_count, language, license, owner, | |
| 82 | } = stats.data; | |
| 83 | ||
| 84 | ||
| 85 | --- | |
| 86 | ||
| 87 | <div class="github-metrics"> | |
| 88 | <!-- <span title={`Built / owned by @${user}`}> | |
| 89 | <img src={`https://github.com/${user}.png`} alt={user || owner?.login} width="20" height="20" /> | |
| 90 | </span> --> | |
| 91 | <!-- <span title={`${repo} is open source, with code published on GitHub`}> | |
| 92 | <FontAwesome iconName="openSource" /> Open Source | |
| 93 | </span> --> | |
| 94 | <span title={`GitHub repository: ${github}`}> | |
| 95 | <a href={`https://github.com/${github}`}><FontAwesome iconName="github" /> {github}</a> | |
| 96 | </span> | |
| 97 | { stats.statusCode === 200 && stats.data && ( | |
| 98 | <span title={`GitHub Star Count: ${stargazers_count}`}> | |
| 99 | <FontAwesome iconName="stars" /> {formatBigNumber(stargazers_count)} | |
| 100 | </span> | |
| 101 | <span title={`GitHub Fork Count: ${forks_count}`}> | |
| 102 | <FontAwesome iconName="forks" /> {formatBigNumber(forks_count)} | |
| 103 | </span> | |
| 104 | <span title={`Open Issues / Bugs: ${open_issues_count}`}> | |
| 105 | <FontAwesome iconName="issues" /> {formatBigNumber(open_issues_count)} | |
| 106 | </span> | |
| 107 | <span title={`Code licensed under ${license?.name}`}> | |
| 108 | <FontAwesome iconName="license" /> {formatLicense(license)} | |
| 109 | </span> | |
| 110 | <span title={`Primary programming language ${language}`}> | |
| 111 | <FontAwesome iconName="language" /> {language} | |
| 112 | </span> | |
| 113 | )} | |
| 114 | </div> | |
| 115 | ||
| 116 | ||
| 117 | <style lang="scss"> | |
| 118 | .github-metrics { | |
| 119 | display: flex; | |
| 120 | align-items: center; | |
| 121 | gap: 1rem; | |
| 122 | font-size: 0.9rem; | |
| 123 | opacity: 0.6; | |
| 124 | span { | |
| 125 | display: flex; | |
| 126 | align-items: center; | |
| 127 | justify-content: center; | |
| 128 | color: var(--foreground); | |
| 129 | gap: 0.25rem; | |
| 130 | padding: 0.5rem 0; | |
| 131 | ||
| 132 | a { | |
| 133 | color: var(--foreground); | |
| 134 | text-decoration: none; | |
| 135 | align-items: center; | |
| 136 | display: flex; | |
| 137 | gap: 0.25rem; | |
| 138 | transition: all 0.2s ease-in-out; | |
| 139 | color: var(--accent-3); | |
| 140 | &:hover { | |
| 141 | color: var(--accent-3); | |
| 142 | font-weight: 500; | |
| 143 | } | |
| 144 | } | |
| 145 | ||
| 146 | img { | |
| 147 | border-radius: var(--curve-md); | |
| 148 | } | |
| 149 | } | |
| 150 | :global(svg) { | |
| 151 | color: var(--foregroung); | |
| 152 | width: 1.1rem; | |
| 153 | height: 1.1rem; | |
| 154 | } | |
| 155 | } | |
| 156 | </style> | |