|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | +const { buildFileName, buildSidebarLabel, transformBody } = require('./release-utils'); |
| 4 | + |
| 5 | +const RELEASES_DIR = path.join(__dirname, '..', 'docs', 'releases'); |
| 6 | +const BASE_API_URL = 'https://api.github.com/repos/reportportal/reportportal/releases/tags'; |
| 7 | + |
| 8 | +async function main() { |
| 9 | + const releaseName = process.env.RELEASE_NAME?.trim(); |
| 10 | + const releaseTag = process.env.RELEASE_TAG?.trim(); |
| 11 | + |
| 12 | + if (!releaseName || !releaseTag) { |
| 13 | + throw new Error('RELEASE_NAME and RELEASE_TAG env vars are required'); |
| 14 | + } |
| 15 | + |
| 16 | + const release = await fetchReleaseByTag(releaseTag); |
| 17 | + |
| 18 | + const fileName = buildFileName(releaseName); |
| 19 | + const filePath = path.join(RELEASES_DIR, fileName); |
| 20 | + |
| 21 | + const sidebarPosition = readExistingSidebarPosition(filePath); |
| 22 | + const sidebarLabel = buildSidebarLabel(releaseName); |
| 23 | + const body = transformBody(release.body || ''); |
| 24 | + |
| 25 | + const content = [ |
| 26 | + '---', |
| 27 | + `sidebar_position: ${sidebarPosition}`, |
| 28 | + `sidebar_label: ${sidebarLabel}`, |
| 29 | + '---', |
| 30 | + '', |
| 31 | + `# ${sidebarLabel}`, |
| 32 | + '', |
| 33 | + body, |
| 34 | + '', |
| 35 | + ].join('\n'); |
| 36 | + |
| 37 | + fs.mkdirSync(RELEASES_DIR, { recursive: true }); |
| 38 | + fs.writeFileSync(filePath, content, 'utf-8'); |
| 39 | + console.log(`Updated: ${fileName}`); |
| 40 | +} |
| 41 | + |
| 42 | +function readExistingSidebarPosition(filePath) { |
| 43 | + if (!fs.existsSync(filePath)) return 1; |
| 44 | + const text = fs.readFileSync(filePath, 'utf-8'); |
| 45 | + const match = text.match(/^sidebar_position:\s*(\d+)/m); |
| 46 | + return match ? parseInt(match[1], 10) : 1; |
| 47 | +} |
| 48 | + |
| 49 | +async function fetchReleaseByTag(tag) { |
| 50 | + const url = `${BASE_API_URL}/${encodeURIComponent(tag)}`; |
| 51 | + const headers = { |
| 52 | + Accept: 'application/vnd.github+json', |
| 53 | + 'X-GitHub-Api-Version': '2022-11-28', |
| 54 | + 'User-Agent': 'reportportal-docs-sync', |
| 55 | + }; |
| 56 | + |
| 57 | + if (process.env.GH_TOKEN) { |
| 58 | + headers.Authorization = `Bearer ${process.env.GH_TOKEN}`; |
| 59 | + } else { |
| 60 | + console.warn('GH_TOKEN not set - using unauthenticated API (60 req/hr limit)'); |
| 61 | + } |
| 62 | + |
| 63 | + const res = await fetch(url, { headers }); |
| 64 | + if (!res.ok) { |
| 65 | + throw new Error(`GitHub API ${res.status}: ${res.statusText}`); |
| 66 | + } |
| 67 | + |
| 68 | + return res.json(); |
| 69 | +} |
| 70 | + |
| 71 | +main().catch((err) => { |
| 72 | + console.error(err); |
| 73 | + process.exit(1); |
| 74 | +}); |
0 commit comments