|
| 1 | +const ORG_NAME = "recodehive"; |
| 2 | +const DISCUSSIONS_REPO = "recode-website"; |
| 3 | +const DEFAULT_LIMIT = 20; |
| 4 | +const UNAVAILABLE_MESSAGE = |
| 5 | + "GitHub Discussions are available only when a server-side GITHUB_TOKEN or DOCUSAURUS_GIT_TOKEN is configured."; |
| 6 | + |
| 7 | +function getToken() { |
| 8 | + return process.env.GITHUB_TOKEN?.trim() || process.env.DOCUSAURUS_GIT_TOKEN?.trim() || ""; |
| 9 | +} |
| 10 | + |
| 11 | +function parseLimit(limitParam) { |
| 12 | + const parsed = Number.parseInt(limitParam, 10); |
| 13 | + if (!Number.isFinite(parsed) || parsed <= 0) { |
| 14 | + return DEFAULT_LIMIT; |
| 15 | + } |
| 16 | + |
| 17 | + return Math.min(parsed, 50); |
| 18 | +} |
| 19 | + |
| 20 | +function mapDiscussion(discussion) { |
| 21 | + return { |
| 22 | + id: discussion.id, |
| 23 | + title: discussion.title || "Untitled discussion", |
| 24 | + body: discussion.body || "", |
| 25 | + author: { |
| 26 | + login: discussion.author?.login || "Unknown", |
| 27 | + avatar_url: discussion.author?.avatarUrl || "", |
| 28 | + html_url: discussion.author?.url || "", |
| 29 | + }, |
| 30 | + category: { |
| 31 | + name: discussion.category?.name || "General", |
| 32 | + emoji: discussion.category?.emoji || "", |
| 33 | + }, |
| 34 | + created_at: discussion.createdAt, |
| 35 | + updated_at: discussion.updatedAt, |
| 36 | + comments: discussion.comments?.totalCount || 0, |
| 37 | + reactions: { |
| 38 | + total_count: discussion.reactions?.totalCount || 0, |
| 39 | + }, |
| 40 | + html_url: discussion.url, |
| 41 | + labels: |
| 42 | + discussion.labels?.nodes?.map((label) => ({ |
| 43 | + name: label.name, |
| 44 | + color: label.color, |
| 45 | + })) || [], |
| 46 | + }; |
| 47 | +} |
| 48 | + |
| 49 | +async function fetchGitHubDiscussions(token, limit) { |
| 50 | + const query = ` |
| 51 | + query GetDiscussions($owner: String!, $name: String!, $first: Int!) { |
| 52 | + repository(owner: $owner, name: $name) { |
| 53 | + discussions(first: $first, orderBy: { field: UPDATED_AT, direction: DESC }) { |
| 54 | + totalCount |
| 55 | + nodes { |
| 56 | + id |
| 57 | + title |
| 58 | + body |
| 59 | + createdAt |
| 60 | + updatedAt |
| 61 | + url |
| 62 | + author { |
| 63 | + login |
| 64 | + avatarUrl |
| 65 | + url |
| 66 | + } |
| 67 | + category { |
| 68 | + name |
| 69 | + emoji |
| 70 | + } |
| 71 | + comments { |
| 72 | + totalCount |
| 73 | + } |
| 74 | + reactions { |
| 75 | + totalCount |
| 76 | + } |
| 77 | + labels(first: 10) { |
| 78 | + nodes { |
| 79 | + name |
| 80 | + color |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + `; |
| 88 | + |
| 89 | + const response = await fetch("https://api.github.com/graphql", { |
| 90 | + method: "POST", |
| 91 | + headers: { |
| 92 | + Accept: "application/vnd.github.v3+json", |
| 93 | + Authorization: `Bearer ${token}`, |
| 94 | + "Content-Type": "application/json", |
| 95 | + }, |
| 96 | + body: JSON.stringify({ |
| 97 | + query, |
| 98 | + variables: { |
| 99 | + owner: ORG_NAME, |
| 100 | + name: DISCUSSIONS_REPO, |
| 101 | + first: limit, |
| 102 | + }, |
| 103 | + }), |
| 104 | + }); |
| 105 | + |
| 106 | + if (!response.ok) { |
| 107 | + throw new Error(`GitHub discussions request failed: ${response.status}`); |
| 108 | + } |
| 109 | + |
| 110 | + const payload = await response.json(); |
| 111 | + |
| 112 | + if (payload.errors?.length) { |
| 113 | + const message = payload.errors.map((error) => error.message).join(", "); |
| 114 | + throw new Error(message || "GitHub discussions GraphQL query failed"); |
| 115 | + } |
| 116 | + |
| 117 | + const discussions = payload.data?.repository?.discussions; |
| 118 | + |
| 119 | + return { |
| 120 | + available: true, |
| 121 | + message: null, |
| 122 | + totalCount: discussions?.totalCount ?? 0, |
| 123 | + discussions: (discussions?.nodes || []).map(mapDiscussion), |
| 124 | + fetchedAt: new Date().toISOString(), |
| 125 | + }; |
| 126 | +} |
| 127 | + |
| 128 | +export default async function handler(req, res) { |
| 129 | + const token = getToken(); |
| 130 | + |
| 131 | + res.setHeader( |
| 132 | + "Cache-Control", |
| 133 | + "public, s-maxage=300, stale-while-revalidate=600", |
| 134 | + ); |
| 135 | + |
| 136 | + if (!token) { |
| 137 | + res.status(503).json({ |
| 138 | + available: false, |
| 139 | + message: UNAVAILABLE_MESSAGE, |
| 140 | + totalCount: null, |
| 141 | + discussions: [], |
| 142 | + fetchedAt: null, |
| 143 | + }); |
| 144 | + return; |
| 145 | + } |
| 146 | + |
| 147 | + try { |
| 148 | + const limit = parseLimit(req.query.limit); |
| 149 | + const data = await fetchGitHubDiscussions(token, limit); |
| 150 | + res.status(200).json(data); |
| 151 | + } catch (error) { |
| 152 | + res.status(502).json({ |
| 153 | + available: false, |
| 154 | + message: |
| 155 | + error instanceof Error |
| 156 | + ? error.message |
| 157 | + : "Failed to fetch GitHub discussions.", |
| 158 | + totalCount: null, |
| 159 | + discussions: [], |
| 160 | + fetchedAt: null, |
| 161 | + }); |
| 162 | + } |
| 163 | +} |
0 commit comments