|
| 1 | +// @ts-check |
| 2 | +import * as process from 'node:process'; |
| 3 | +import { Octokit } from '@octokit/core'; |
| 4 | +import { output } from './util.mjs'; |
| 5 | + |
| 6 | +const { |
| 7 | + GITHUB_TOKEN = '', |
| 8 | + PR_LIST = '', |
| 9 | + owner = 'mongodb', |
| 10 | + repo = 'node-mongodb-native' |
| 11 | +} = process.env; |
| 12 | +if (GITHUB_TOKEN === '') throw new Error('GITHUB_TOKEN cannot be empty'); |
| 13 | + |
| 14 | +const octokit = new Octokit({ |
| 15 | + auth: GITHUB_TOKEN, |
| 16 | + log: { |
| 17 | + debug: msg => console.error('Octokit.debug', msg), |
| 18 | + info: msg => console.error('Octokit.info', msg), |
| 19 | + warn: msg => console.error('Octokit.warn', msg), |
| 20 | + error: msg => console.error('Octokit.error', msg) |
| 21 | + } |
| 22 | +}); |
| 23 | + |
| 24 | +const prs = PR_LIST.split(',').map(pr => { |
| 25 | + const prNum = Number(pr); |
| 26 | + if (Number.isNaN(prNum)) |
| 27 | + throw Error(`expected PR number list: ${PR_LIST}, offending entry: ${pr}`); |
| 28 | + return prNum; |
| 29 | +}); |
| 30 | + |
| 31 | +/** @param {number} pull_number */ |
| 32 | +async function getPullRequestContent(pull_number) { |
| 33 | + const startIndicator = 'RELEASE_HIGHLIGHT_START -->'; |
| 34 | + const endIndicator = '<!-- RELEASE_HIGHLIGHT_END'; |
| 35 | + |
| 36 | + let body; |
| 37 | + try { |
| 38 | + const res = await octokit.request('GET /repos/{owner}/{repo}/pulls/{pull_number}', { |
| 39 | + owner, |
| 40 | + repo, |
| 41 | + pull_number, |
| 42 | + headers: { 'X-GitHub-Api-Version': '2022-11-28' } |
| 43 | + }); |
| 44 | + body = res.data.body; |
| 45 | + } catch (error) { |
| 46 | + console.log(`Could not get PR ${pull_number}, skipping. ${error.status}`); |
| 47 | + return ''; |
| 48 | + } |
| 49 | + |
| 50 | + if (body == null || !(body.includes(startIndicator) && body.includes(endIndicator))) { |
| 51 | + console.log(`PR #${pull_number} has no highlight`); |
| 52 | + return ''; |
| 53 | + } |
| 54 | + |
| 55 | + const start = body.indexOf('## ', body.indexOf(startIndicator)); |
| 56 | + const end = body.indexOf(endIndicator); |
| 57 | + const highlightSection = body.slice(start, end).trim(); |
| 58 | + |
| 59 | + console.log(`PR #${pull_number} has a highlight ${highlightSection.length} characters long`); |
| 60 | + return highlightSection; |
| 61 | +} |
| 62 | + |
| 63 | +/** @param {number[]} prs */ |
| 64 | +async function pullRequestHighlights(prs) { |
| 65 | + const highlights = []; |
| 66 | + for (const pr of prs) { |
| 67 | + const content = await getPullRequestContent(pr); |
| 68 | + highlights.push(content); |
| 69 | + } |
| 70 | + return highlights.join(''); |
| 71 | +} |
| 72 | + |
| 73 | +console.log('List of PRs to collect highlights from:', prs); |
| 74 | +const highlights = await pullRequestHighlights(prs); |
| 75 | + |
| 76 | +await output('highlights', JSON.stringify({ highlights })); |
0 commit comments