|
| 1 | +/** |
| 2 | + * Check GitHub issues and pull requests referenced by reports and create |
| 3 | + * a pull request to drop reports that have been addressed. |
| 4 | + */ |
| 5 | + |
| 6 | +const core = require('@actions/core'); |
| 7 | +const octokit = require('../lib/octokit'); |
| 8 | +const fs = require('fs'); |
| 9 | +const path = require('path'); |
| 10 | +const matter = require('gray-matter'); |
| 11 | + |
| 12 | +const issueReportDir = "issue-reports"; |
| 13 | + |
| 14 | +/** |
| 15 | + * Check GitHub issues and PR referenced by patch files and drop patch files |
| 16 | + * that only reference closed issues and PR. |
| 17 | + * |
| 18 | + * @function |
| 19 | + * @return {String} A GitHub flavored markdown string that describes what |
| 20 | + * patches got dropped and why. To be used in a possible PR. Returns an |
| 21 | + * empty string when there are no patches to drop. |
| 22 | + */ |
| 23 | +async function dropReportsWhenPossible () { |
| 24 | + const rootDir = path.join(__dirname, '../../issues'); |
| 25 | + |
| 26 | + console.log('Gather reports files'); |
| 27 | + let reports = []; |
| 28 | + const files = fs.readdirSync(rootDir); |
| 29 | + for (const file of files) { |
| 30 | + if (file.endsWith('.md')) { |
| 31 | + const report = path.join(rootDir, file); |
| 32 | + console.log(`- add "${report}"`); |
| 33 | + reports.push({ name: report }); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + console.log(); |
| 38 | + console.log('Extract list of issues'); |
| 39 | + const issueUrl = /^https:\/\/github\.com\/([^/]+)\/([^/]+)\/(issues|pull)\/(\d+)$/; |
| 40 | + for (const report of reports) { |
| 41 | + let contents; |
| 42 | + try { |
| 43 | + contents = matter(await fs.promises.readFile(report.name, 'utf-8')); |
| 44 | + } catch (e) { |
| 45 | + console.error(`- "${report.name}" has invalid YAML`); |
| 46 | + continue; |
| 47 | + } |
| 48 | + const tracked = contents.data.Tracked; |
| 49 | + const m = tracked.match(issueUrl); |
| 50 | + if (m) { |
| 51 | + report.issue = { |
| 52 | + owner: m[1], |
| 53 | + repo: m[2], |
| 54 | + number: parseInt(m[4], 10), |
| 55 | + url: tracked |
| 56 | + }; |
| 57 | + console.log(`- "${report.name}" linked to ${report.issue.url}`); |
| 58 | + } else { |
| 59 | + console.log(`- "${report.name}" not linked to any issue`); |
| 60 | + } |
| 61 | + } |
| 62 | + reports = reports.filter(report => report.issue); |
| 63 | + |
| 64 | + console.log(); |
| 65 | + console.log('Check status of GitHub issues/PR'); |
| 66 | + for (const { issue } of reports) { |
| 67 | + const response = await octokit.issues.get({ |
| 68 | + owner: issue.owner, |
| 69 | + repo: issue.repo, |
| 70 | + issue_number: issue.number |
| 71 | + }); |
| 72 | + issue.state = response?.data?.state ?? 'unknown'; |
| 73 | + console.log(`- [${issue.state}] ${issue.url}`); |
| 74 | + } |
| 75 | + |
| 76 | + console.log(); |
| 77 | + console.log('Drop reports when possible'); |
| 78 | + reports = reports.filter(report => report.issue.state === 'closed'); |
| 79 | + if (reports.length > 0) { |
| 80 | + const res = []; |
| 81 | + for (const report of reports) { |
| 82 | + console.log(`- drop "${report.name}"`); |
| 83 | + fs.unlinkSync(report.name); |
| 84 | + res.push(`- \`${report.name}\` was linked to now closed: [${report.issue.owner}/${report.issue.repo}#${report.issue.number}](${report.issue.url})`); |
| 85 | + } |
| 86 | + return res.join('\n'); |
| 87 | + } else { |
| 88 | + console.log('- No report to drop at this time'); |
| 89 | + return ''; |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | + |
| 94 | +dropReportsWhenPossible() |
| 95 | + .then(res => { |
| 96 | + core.exportVariable('dropped_reports', res); |
| 97 | + console.log(); |
| 98 | + console.log('Set dropped_reports env variable'); |
| 99 | + console.log(res); |
| 100 | + console.log('== The end =='); |
| 101 | + }) |
| 102 | + .catch(err => { |
| 103 | + console.error(err); |
| 104 | + process.exit(1); |
| 105 | + }); |
0 commit comments