|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Create one GitHub Discussion per semantic anchor in the "Anchor Feedback" category. |
| 4 | + * |
| 5 | + * Prerequisites: |
| 6 | + * - gh CLI authenticated with discussions:write scope |
| 7 | + * - "Anchor Feedback" discussion category exists |
| 8 | + * |
| 9 | + * Usage: node scripts/create-anchor-discussions.js [--dry-run] |
| 10 | + */ |
| 11 | + |
| 12 | +const { execSync } = require('child_process') |
| 13 | +const fs = require('fs') |
| 14 | +const path = require('path') |
| 15 | + |
| 16 | +const REPO_ID = 'R_kgDOQTHmRw' |
| 17 | +const CATEGORY_ID = 'DIC_kwDOQTHmR84C4oz7' |
| 18 | +const WEBSITE_URL = 'https://llm-coding.github.io/Semantic-Anchors' |
| 19 | +const DRY_RUN = process.argv.includes('--dry-run') |
| 20 | + |
| 21 | +const anchorsPath = path.join(__dirname, '..', 'website', 'public', 'data', 'anchors.json') |
| 22 | +const anchors = JSON.parse(fs.readFileSync(anchorsPath, 'utf-8')) |
| 23 | + |
| 24 | +function sleep(ms) { |
| 25 | + return new Promise((resolve) => setTimeout(resolve, ms)) |
| 26 | +} |
| 27 | + |
| 28 | +function ghGraphql(query) { |
| 29 | + const payload = JSON.stringify({ query }) |
| 30 | + const result = execSync('gh api graphql --input -', { |
| 31 | + input: payload, |
| 32 | + encoding: 'utf-8', |
| 33 | + }) |
| 34 | + return JSON.parse(result) |
| 35 | +} |
| 36 | + |
| 37 | +function fetchExistingDiscussions() { |
| 38 | + const discussions = [] |
| 39 | + let cursor = null |
| 40 | + let hasNext = true |
| 41 | + |
| 42 | + while (hasNext) { |
| 43 | + const afterClause = cursor ? `, after: "${cursor}"` : '' |
| 44 | + const result = ghGraphql(`{ |
| 45 | + repository(owner: "LLM-Coding", name: "Semantic-Anchors") { |
| 46 | + discussions(first: 100, categoryId: "${CATEGORY_ID}"${afterClause}) { |
| 47 | + nodes { title body } |
| 48 | + pageInfo { hasNextPage endCursor } |
| 49 | + } |
| 50 | + } |
| 51 | + }`) |
| 52 | + |
| 53 | + const data = result.data.repository.discussions |
| 54 | + discussions.push(...data.nodes) |
| 55 | + hasNext = data.pageInfo.hasNextPage |
| 56 | + cursor = data.pageInfo.endCursor |
| 57 | + } |
| 58 | + |
| 59 | + return discussions |
| 60 | +} |
| 61 | + |
| 62 | +function extractAnchorId(body) { |
| 63 | + const match = body.match(/<!-- anchor-id: ([a-z0-9-]+) -->/) |
| 64 | + return match ? match[1] : null |
| 65 | +} |
| 66 | + |
| 67 | +async function main() { |
| 68 | + console.log(`Found ${anchors.length} anchors`) |
| 69 | + |
| 70 | + // Fetch existing discussions to avoid duplicates |
| 71 | + console.log('Fetching existing discussions...') |
| 72 | + const existing = fetchExistingDiscussions() |
| 73 | + const existingIds = new Set(existing.map((d) => extractAnchorId(d.body)).filter(Boolean)) |
| 74 | + console.log(`Found ${existing.length} existing discussions (${existingIds.size} with anchor IDs)`) |
| 75 | + |
| 76 | + const toCreate = anchors.filter((a) => !existingIds.has(a.id)) |
| 77 | + console.log(`${toCreate.length} discussions to create`) |
| 78 | + |
| 79 | + if (DRY_RUN) { |
| 80 | + toCreate.forEach((a) => console.log(` [dry-run] Would create: ⚓ ${a.title}`)) |
| 81 | + return |
| 82 | + } |
| 83 | + |
| 84 | + let created = 0 |
| 85 | + let failed = 0 |
| 86 | + let index = 0 |
| 87 | + const total = toCreate.length |
| 88 | + for (const anchor of toCreate) { |
| 89 | + index++ |
| 90 | + const title = `⚓ ${anchor.title}` |
| 91 | + const body = [ |
| 92 | + `<!-- anchor-id: ${anchor.id} -->`, |
| 93 | + '', |
| 94 | + `**${anchor.title}** — vote and discuss this semantic anchor.`, |
| 95 | + '', |
| 96 | + `👉 [View on Semantic Anchors website](${WEBSITE_URL}/#/anchor/${anchor.id})`, |
| 97 | + '', |
| 98 | + '---', |
| 99 | + '_Upvote this discussion if you find this anchor useful. Leave a comment to suggest improvements._', |
| 100 | + ].join('\n') |
| 101 | + |
| 102 | + try { |
| 103 | + const result = ghGraphql(`mutation { |
| 104 | + createDiscussion(input: { |
| 105 | + repositoryId: "${REPO_ID}", |
| 106 | + categoryId: "${CATEGORY_ID}", |
| 107 | + title: ${JSON.stringify(title)}, |
| 108 | + body: ${JSON.stringify(body)} |
| 109 | + }) { |
| 110 | + discussion { url } |
| 111 | + } |
| 112 | + }`) |
| 113 | + const url = result.data.createDiscussion.discussion.url |
| 114 | + created++ |
| 115 | + console.log(` [${created}/${toCreate.length}] Created: ${title} → ${url}`) |
| 116 | + } catch (err) { |
| 117 | + failed++ |
| 118 | + console.error(` ✗ Failed: ${title} — ${err.stderr || err.message}`) |
| 119 | + } |
| 120 | + |
| 121 | + // Delay to avoid secondary rate limits |
| 122 | + if (index < total) { |
| 123 | + await sleep(2000) |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + console.log(`\nDone. Created ${created} discussions.`) |
| 128 | + if (failed > 0) { |
| 129 | + console.error(`Failed to create ${failed} discussions.`) |
| 130 | + process.exitCode = 1 |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +main().catch(console.error) |
0 commit comments