-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgithubRoadmap.ts
More file actions
397 lines (344 loc) · 11.8 KB
/
Copy pathgithubRoadmap.ts
File metadata and controls
397 lines (344 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
// src/libs/githubRoadmap.ts
/**
* GitHub Milestones roadmap fetcher.
*
* Fetches milestones from the datum-cloud/enhancements repo via the GraphQL
* API to drive the /roadmap Upcoming/Shipped tabs. Cover images are resolved
* separately from static repo assets (see `getRoadmapCoverImage`).
*
* Cache strategy:
* - Redis (when REDIS_URL is set): key `${redisKeyPrefix}github:roadmaps`, TTL 30 min
* - In-memory Map: fallback for local dev (single-instance, no persistence)
*
* Auth: uses existing APP_ID / APP_PRIVATE_KEY / APP_INSTALLATION_ID env vars
* via the `graph()` helper in `src/libs/github.ts`.
*/
import { graph } from './github';
import { redisClient, redisKeyPrefix } from './strapi/_runtime';
export interface RoadmapMilestone {
number: number;
title: string;
slug: string;
summary?: string;
releaseDate: string;
githubUrl: string;
shipped: boolean;
}
const GITHUB_ORG = 'datum-cloud';
const GITHUB_REPO = 'enhancements';
const CACHE_TTL_SECONDS = 30 * 60; // 30 minutes
const REDIS_CACHE_KEY = `${redisKeyPrefix}github:roadmaps`;
/** Logical cache name for admin force-regen (`POST /api/cache/strapi`). */
export const GITHUB_ROADMAPS_CACHE_KEY = 'github-roadmaps';
/** Module-level in-memory cache for local dev (no Redis). */
const memCache = {
data: null as RoadmapMilestone[] | null,
expiresAt: 0,
};
const ROADMAP_MILESTONES_QUERY = `
query GetRoadmapMilestones($owner: String!, $name: String!, $cursor: String) {
repository(owner: $owner, name: $name) {
milestones(first: 100, after: $cursor, states: [OPEN, CLOSED]) {
nodes {
number
title
description
dueOn
state
url
issues(first: 100) {
nodes {
number
title
url
}
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
}
`;
interface GitHubMilestoneIssueNode {
number?: number;
title?: string;
url?: string;
}
interface GitHubMilestoneNode {
number?: number;
title?: string;
description?: string | null;
dueOn?: string | null;
state?: string;
url?: string;
issues?: { nodes: GitHubMilestoneIssueNode[] };
}
interface GitHubMilestonesPage {
nodes: GitHubMilestoneNode[];
pageInfo: { hasNextPage: boolean; endCursor: string | null };
}
interface GitHubMilestonesResponse {
repository: {
milestones: GitHubMilestonesPage;
};
}
function isValidCachedMilestone(item: unknown): item is RoadmapMilestone {
if (!item || typeof item !== 'object') return false;
const record = item as Record<string, unknown>;
return (
typeof record.number === 'number' &&
typeof record.title === 'string' &&
typeof record.slug === 'string' &&
typeof record.releaseDate === 'string' &&
typeof record.githubUrl === 'string' &&
typeof record.shipped === 'boolean'
);
}
function isValidCachedMilestones(items: unknown): items is RoadmapMilestone[] {
return Array.isArray(items) && items.every(isValidCachedMilestone);
}
function sortMilestones(items: RoadmapMilestone[]): RoadmapMilestone[] {
return [...items].sort(
(a, b) => new Date(a.releaseDate).getTime() - new Date(b.releaseDate).getTime()
);
}
/** Read cached roadmap milestones from Redis. Returns null on miss or error. */
async function readFromRedis(): Promise<RoadmapMilestone[] | null> {
if (!redisClient) return null;
try {
const raw = await redisClient.get(REDIS_CACHE_KEY);
if (!raw) return null;
const parsed = JSON.parse(raw) as unknown;
if (!isValidCachedMilestones(parsed)) {
console.warn('[githubRoadmap] Invalid cached roadmaps data detected, refetching');
return null;
}
return parsed;
} catch (err) {
console.warn('[githubRoadmap] Redis read error:', err);
return null;
}
}
/** Write roadmap milestones to Redis with TTL. Silently logs on error. */
async function writeToRedis(items: RoadmapMilestone[]): Promise<void> {
if (!redisClient) return;
try {
await redisClient.set(REDIS_CACHE_KEY, JSON.stringify(items), 'EX', CACHE_TTL_SECONDS);
} catch (err) {
console.warn('[githubRoadmap] Redis write error:', err);
}
}
/** Escape HTML special characters so issue titles can't inject markup into the rendered summary. */
function escapeHtml(value: string): string {
return value
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
/** Build a markdown list of issues linked to a milestone, for appending to its description. */
function buildIssuesListMarkdown(issues: GitHubMilestoneIssueNode[]): string {
const validIssues = issues.filter(
(issue): issue is Required<GitHubMilestoneIssueNode> =>
typeof issue.number === 'number' &&
typeof issue.title === 'string' &&
typeof issue.url === 'string'
);
if (validIssues.length === 0) return '';
const items = validIssues
.map(
(issue) =>
`<li>${escapeHtml(issue.title)} <a href="${issue.url}" target="_blank" rel="noopener noreferrer">#${issue.number}</a></li>`
)
.join('');
return `<div><p style="margin-bottom: 2rem;"><strong>What's included in this milestone:</strong></p><ul style="margin-top: 0;">${items}</ul></div>`;
}
/** Fetch all milestone pages from the GitHub GraphQL API. */
async function fetchFromGitHub(): Promise<RoadmapMilestone[]> {
const items: RoadmapMilestone[] = [];
let cursor: string | null = null;
while (true) {
const variables: Record<string, unknown> = {
owner: GITHUB_ORG,
name: GITHUB_REPO,
};
if (cursor) variables.cursor = cursor;
let data: unknown;
try {
data = await graph(ROADMAP_MILESTONES_QUERY, variables);
} catch (err) {
console.error('[githubRoadmap] GraphQL fetch error:', err);
break;
}
const response = data as GitHubMilestonesResponse | null;
const page = response?.repository?.milestones;
if (!page) break;
for (const node of page.nodes) {
if (typeof node.number !== 'number' || !node.title || !node.dueOn) continue;
const issuesMarkdown = buildIssuesListMarkdown(node.issues?.nodes ?? []);
const summaryParts = [node.description, issuesMarkdown].filter((part): part is string =>
Boolean(part && part.trim())
);
items.push({
number: node.number,
title: node.title,
slug: getRoadmapSlug({ title: node.title }),
summary: summaryParts.length > 0 ? summaryParts.join('\n\n') : undefined,
releaseDate: node.dueOn,
githubUrl: node.url ?? `https://github.com/${GITHUB_ORG}/${GITHUB_REPO}`,
shipped: node.state === 'CLOSED',
});
}
if (!page.pageInfo.hasNextPage || !page.pageInfo.endCursor) break;
cursor = page.pageInfo.endCursor;
}
return sortMilestones(items);
}
/** Returns true when a non-expired roadmap milestones cache entry exists. */
export async function isGitHubRoadmapsCached(): Promise<boolean> {
if (redisClient) {
try {
return (await redisClient.get(REDIS_CACHE_KEY)) !== null;
} catch {
return false;
}
}
return memCache.data !== null && Date.now() < memCache.expiresAt;
}
/** Clear Redis and in-memory roadmap milestones cache without refetching. */
export async function clearGitHubRoadmapsCache(): Promise<void> {
if (redisClient) {
try {
await redisClient.del(REDIS_CACHE_KEY);
} catch (err) {
console.warn('[githubRoadmap] Redis delete error:', err);
}
}
memCache.data = null;
memCache.expiresAt = 0;
}
/**
* Clear roadmap milestones cache and fetch fresh data from GitHub.
* @throws When the GitHub fetch fails after cache was cleared
*/
export async function forceRegenerateGitHubRoadmaps(): Promise<RoadmapMilestone[]> {
await clearGitHubRoadmapsCache();
const items = await fetchFromGitHub();
await writeToRedis(items);
if (!redisClient) {
memCache.data = items;
memCache.expiresAt = Date.now() + CACHE_TTL_SECONDS * 1000;
}
return items;
}
/**
* Fetch roadmap milestones from the datum-cloud/enhancements GitHub repo.
*
* Cache read order:
* 1. Redis (if configured) — 30-min TTL
* 2. In-memory map (local dev, single-instance)
* 3. Live fetch from GitHub API → write result back to cache
*
* Never throws — returns empty array on complete failure.
*/
export async function fetchGitHubRoadmaps(): Promise<RoadmapMilestone[]> {
// 1. Try Redis
const cached = await readFromRedis();
if (cached) return cached;
// 2. Try in-memory (local dev / no Redis)
if (
!redisClient &&
memCache.data &&
Date.now() < memCache.expiresAt &&
isValidCachedMilestones(memCache.data)
) {
return memCache.data;
}
// 3. Fetch live
try {
const items = await fetchFromGitHub();
await writeToRedis(items);
if (!redisClient) {
memCache.data = items;
memCache.expiresAt = Date.now() + CACHE_TTL_SECONDS * 1000;
}
return items;
} catch (err) {
console.error('[githubRoadmap] Failed to fetch roadmap milestones:', err);
return [];
}
}
export interface GroupedRoadmaps {
upcoming: RoadmapMilestone[];
previous: RoadmapMilestone[];
}
export function isRoadmapShipped(milestone: Pick<RoadmapMilestone, 'shipped'>): boolean {
return milestone.shipped === true;
}
/**
* Group roadmaps into upcoming and previous based on releaseDate.
* Milestones with releaseDate >= today are "upcoming".
*/
export function groupRoadmapsByDate(roadmaps: RoadmapMilestone[]): GroupedRoadmaps {
const today = new Date();
today.setHours(0, 0, 0, 0);
const upcoming: RoadmapMilestone[] = [];
const previous: RoadmapMilestone[] = [];
for (const roadmap of roadmaps) {
const releaseDate = new Date(roadmap.releaseDate);
releaseDate.setHours(0, 0, 0, 0);
(releaseDate >= today ? upcoming : previous).push(roadmap);
}
upcoming.sort((a, b) => new Date(a.releaseDate).getTime() - new Date(b.releaseDate).getTime());
previous.sort((a, b) => new Date(b.releaseDate).getTime() - new Date(a.releaseDate).getTime());
return { upcoming, previous };
}
/** Three-letter uppercase month from an ISO date string (e.g. `"2025-03-01"` → `"MAR"`). */
export function getMonthAbbreviation(dateString: string): string {
const date = new Date(dateString);
return date.toLocaleString('en-US', { month: 'short' }).toUpperCase();
}
/**
* Month key for aligning roadmap entries to calendar grid slots.
* @example getRoadmapMonthKey("2026-01-01") → "2026-01"
*/
export function getRoadmapMonthKey(releaseDate: string): string {
return releaseDate.slice(0, 7);
}
/** URL slug for roadmap detail pages, derived from the milestone title. */
export function getRoadmapSlug(milestone: Pick<RoadmapMilestone, 'title'>): string {
return milestone.title
.toLowerCase()
.trim()
.replace(/[^a-z0-9]+/g, '-')
.replace(/^-+|-+$/g, '');
}
/**
* Days between today and the release date (releases are always on the 1st of the month).
* Returns a positive number for future dates, negative for past dates.
*/
export function getDaysUntilRelease(releaseDate: string): number {
const release = new Date(releaseDate);
const today = new Date();
today.setHours(0, 0, 0, 0);
return Math.ceil((release.getTime() - today.getTime()) / 86_400_000);
}
/** Build-time manifest of static roadmap cover images, keyed by milestone number. */
const coverImageModules = import.meta.glob<{ default: ImageMetadata }>(
'/src/assets/roadmap/*.{png,jpg,jpeg,webp,avif}',
{ eager: true }
);
const coverImagesByNumber = new Map<number, ImageMetadata>();
for (const [path, mod] of Object.entries(coverImageModules)) {
const match = path.match(/\/(\d+)\.[a-z]+$/i);
if (!match) continue;
coverImagesByNumber.set(Number(match[1]), mod.default);
}
/** Resolve the static cover image asset for a milestone, keyed by milestone number. */
export function getRoadmapCoverImage(number: number): ImageMetadata | undefined {
return coverImagesByNumber.get(number);
}