|
| 1 | +# Plan: Migrate GitHub Contributors to Data Layer |
| 2 | + |
| 3 | +## Summary |
| 4 | + |
| 5 | +Replace the current per-request GitHub API fetching with pre-computed data stored in Netlify Blobs via the existing data-layer infrastructure. This eliminates ~173K-347K API calls per build. |
| 6 | + |
| 7 | +## Files to Delete (Previous Implementation) |
| 8 | + |
| 9 | +- `src/scripts/github/getGitHubContributors.ts` |
| 10 | +- `src/data/github/contributors.json` |
| 11 | +- `src/data/github/app-contributors.json` |
| 12 | +- `.github/workflows/get-github-contributors.yml` |
| 13 | + |
| 14 | +## Files to Create |
| 15 | + |
| 16 | +### 1. `src/data-layer/fetchers/fetchGitHubContributors.ts` |
| 17 | + |
| 18 | +New fetcher that: |
| 19 | +- Fetches contributors for all content files from GitHub API |
| 20 | +- Fetches contributors for all app pages |
| 21 | +- Returns `GitHubContributorsData` type |
| 22 | +- Follows existing fetcher patterns (logging, error handling, rate limiting) |
| 23 | + |
| 24 | +```typescript |
| 25 | +export const FETCH_GITHUB_CONTRIBUTORS_TASK_ID = "fetch-github-contributors" |
| 26 | + |
| 27 | +export async function fetchGitHubContributors(): Promise<GitHubContributorsData> { |
| 28 | + // Fetch all content file contributors |
| 29 | + // Fetch all app page contributors |
| 30 | + // Return combined data |
| 31 | +} |
| 32 | +``` |
| 33 | + |
| 34 | +### 2. `src/data-layer/mocks/fetch-github-contributors.json` |
| 35 | + |
| 36 | +Mock data for local development with `USE_MOCK_DATA=true`. |
| 37 | + |
| 38 | +## Files to Modify |
| 39 | + |
| 40 | +### 1. `src/lib/types.ts` |
| 41 | + |
| 42 | +Add type definition: |
| 43 | +```typescript |
| 44 | +export type GitHubContributorsData = { |
| 45 | + content: Record<string, FileContributor[]> // slug -> contributors |
| 46 | + appPages: Record<string, FileContributor[]> // pagePath -> contributors |
| 47 | + generatedAt: string |
| 48 | +} |
| 49 | +``` |
| 50 | +
|
| 51 | +### 2. `src/data-layer/tasks.ts` |
| 52 | +
|
| 53 | +- Add import for `fetchGitHubContributors` |
| 54 | +- Add key: `GITHUB_CONTRIBUTORS: "fetch-github-contributors"` |
| 55 | +- Add to `DAILY` array: `[KEYS.GITHUB_CONTRIBUTORS, fetchGitHubContributors]` |
| 56 | +
|
| 57 | +### 3. `src/data-layer/index.ts` |
| 58 | +
|
| 59 | +Add getter: |
| 60 | +```typescript |
| 61 | +export const getGitHubContributors = () => |
| 62 | + get<GitHubContributorsData>(KEYS.GITHUB_CONTRIBUTORS) |
| 63 | +``` |
| 64 | + |
| 65 | +### 4. `src/lib/data/index.ts` |
| 66 | + |
| 67 | +Add cached wrapper: |
| 68 | +```typescript |
| 69 | +export const getGitHubContributors = createCachedGetter( |
| 70 | + dataLayer.getGitHubContributors, |
| 71 | + ["github-contributors"], |
| 72 | + CACHE_REVALIDATE_DAY |
| 73 | +) |
| 74 | +``` |
| 75 | + |
| 76 | +### 5. `src/lib/utils/gh.ts` |
| 77 | + |
| 78 | +- Remove the static JSON imports I added earlier |
| 79 | +- Remove `getStaticContentContributors` and `getStaticAppContributors` |
| 80 | +- Keep `fetchAndCacheGitHubContributors` as fallback for dev/new files |
| 81 | + |
| 82 | +### 6. `src/lib/utils/contributors.ts` |
| 83 | + |
| 84 | +Update to use data-layer: |
| 85 | +```typescript |
| 86 | +import { getGitHubContributors } from "@/lib/data" |
| 87 | + |
| 88 | +export const getMarkdownFileContributorInfo = async (...) => { |
| 89 | + const contributorsData = await getGitHubContributors() |
| 90 | + let gitHubContributors = contributorsData?.content[slug] || null |
| 91 | + |
| 92 | + // Fallback to API if not in data layer (new files during dev) |
| 93 | + if (!gitHubContributors) { |
| 94 | + gitHubContributors = await fetchAndCacheGitHubContributors(...) |
| 95 | + } |
| 96 | + // ... rest unchanged |
| 97 | +} |
| 98 | + |
| 99 | +export const getAppPageContributorInfo = async (...) => { |
| 100 | + const contributorsData = await getGitHubContributors() |
| 101 | + let uniqueGitHubContributors = contributorsData?.appPages[pagePath] || null |
| 102 | + |
| 103 | + // Fallback to API if not in data layer |
| 104 | + if (!uniqueGitHubContributors) { |
| 105 | + // ... existing API fetch logic |
| 106 | + } |
| 107 | + // ... rest unchanged |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +## Data Flow |
| 112 | + |
| 113 | +``` |
| 114 | +Trigger.dev (daily) |
| 115 | + ↓ |
| 116 | +fetchGitHubContributors() - fetches from GitHub API |
| 117 | + ↓ |
| 118 | +set(KEYS.GITHUB_CONTRIBUTORS, data) - stores in Netlify Blobs |
| 119 | + ↓ |
| 120 | +Page render calls getGitHubContributors() |
| 121 | + ↓ |
| 122 | +unstable_cache + React cache (request dedup) |
| 123 | + ↓ |
| 124 | +storage.get() - retrieves from Netlify Blobs |
| 125 | + ↓ |
| 126 | +contributors.ts uses data (zero API calls) |
| 127 | +``` |
| 128 | + |
| 129 | +## Implementation Order |
| 130 | + |
| 131 | +1. Delete previous implementation files |
| 132 | +2. Add `GitHubContributorsData` type to `src/lib/types.ts` |
| 133 | +3. Create `src/data-layer/fetchers/fetchGitHubContributors.ts` |
| 134 | +4. Create `src/data-layer/mocks/fetch-github-contributors.json` |
| 135 | +5. Update `src/data-layer/tasks.ts` (key + import + DAILY registration) |
| 136 | +6. Update `src/data-layer/index.ts` (add getter) |
| 137 | +7. Update `src/lib/data/index.ts` (add cached wrapper) |
| 138 | +8. Update `src/lib/utils/gh.ts` (remove static imports/functions) |
| 139 | +9. Update `src/lib/utils/contributors.ts` (use data-layer) |
| 140 | +10. Run `pnpm lint:fix` and `npx tsc --noEmit` |
| 141 | + |
| 142 | +## Notes |
| 143 | + |
| 144 | +- **No filesystem access** in Trigger.dev - use GitHub Contents API to list files |
| 145 | +- Rate limiting: Use delays between requests (100-500ms) |
| 146 | +- App pages list: Predefined static list (changes infrequently) |
| 147 | +- Content files: Use GitHub API `GET /repos/{owner}/{repo}/contents/{path}` to recursively list `public/content/` |
| 148 | + |
| 149 | +## GitHub API for File Discovery |
| 150 | + |
| 151 | +```typescript |
| 152 | +// List directory contents recursively |
| 153 | +async function listContentFiles(path = "public/content"): Promise<string[]> { |
| 154 | + const url = `https://api.github.com/repos/ethereum/ethereum-org-website/contents/${path}` |
| 155 | + const response = await fetch(url, { |
| 156 | + headers: { Authorization: `token ${token}` } |
| 157 | + }) |
| 158 | + const items = await response.json() |
| 159 | + |
| 160 | + const slugs: string[] = [] |
| 161 | + for (const item of items) { |
| 162 | + if (item.type === "dir" && item.name !== "translations") { |
| 163 | + // Recursively list subdirectories |
| 164 | + slugs.push(...await listContentFiles(item.path)) |
| 165 | + } else if (item.name === "index.md") { |
| 166 | + // Found a content file, extract slug |
| 167 | + slugs.push(path.replace("public/content/", "")) |
| 168 | + } |
| 169 | + } |
| 170 | + return slugs |
| 171 | +} |
| 172 | +``` |
0 commit comments