|
| 1 | +/** |
| 2 | + * This script is supposed to be used in Actions. When it's run in Actions |
| 3 | + * there will be an env var called GITHUB_REPOSITORY. If it's not there, |
| 4 | + * you can use this script as a CLI tool. For example: |
| 5 | + * |
| 6 | + * export GITHUB_TOKEN=github_pat_blablabla |
| 7 | + * npm run deleted-features-pr-comment -- github docs-internal main 2ba53b6a |
| 8 | + * |
| 9 | + */ |
| 10 | + |
| 11 | +import * as github from '@actions/github' |
| 12 | +import core from '@actions/core' |
| 13 | +import { program } from 'commander' |
| 14 | + |
| 15 | +const { GITHUB_TOKEN, GITHUB_REPOSITORY } = process.env |
| 16 | + |
| 17 | +if (!GITHUB_TOKEN) { |
| 18 | + throw new Error(`GITHUB_TOKEN environment variable not set`) |
| 19 | +} |
| 20 | + |
| 21 | +if (GITHUB_REPOSITORY) { |
| 22 | + const context = github.context |
| 23 | + |
| 24 | + const owner = context.repo.owner |
| 25 | + const repo = context.payload.repository!.name |
| 26 | + const baseSHA = process.env.BASE_SHA || context.payload.pull_request!.base.sha |
| 27 | + const headSHA = process.env.HEAD_SHA || context.payload.pull_request!.head.sha |
| 28 | + |
| 29 | + const markdown = await main(owner, repo, baseSHA, headSHA) |
| 30 | + core.setOutput('markdown', markdown) |
| 31 | +} else { |
| 32 | + program |
| 33 | + .description('Print a nice Markdown comment if there were features deleted in a PR.') |
| 34 | + .arguments('owner repo bash_sha head_sha') |
| 35 | + .parse(process.argv) |
| 36 | + |
| 37 | + const args = program.args |
| 38 | + const [owner, repo, baseSHA, headSHA] = args |
| 39 | + console.log(await main(owner, repo, baseSHA, headSHA)) |
| 40 | +} |
| 41 | + |
| 42 | +async function main(owner: string, repo: string, baseSHA: string, headSHA: string) { |
| 43 | + if (!GITHUB_TOKEN) { |
| 44 | + throw new Error(`GITHUB_TOKEN environment variable not set`) |
| 45 | + } |
| 46 | + const octokit = github.getOctokit(GITHUB_TOKEN) |
| 47 | + // get the list of file changes from the PR |
| 48 | + const response = await octokit.rest.repos.compareCommitsWithBasehead({ |
| 49 | + owner, |
| 50 | + repo, |
| 51 | + basehead: `${baseSHA}...${headSHA}`, |
| 52 | + }) |
| 53 | + |
| 54 | + const { files } = response.data |
| 55 | + |
| 56 | + if (!files) return '' |
| 57 | + |
| 58 | + const oldFilenames = [] |
| 59 | + for (const file of files) { |
| 60 | + const { filename, status } = file |
| 61 | + if (!filename.startsWith('data/features')) continue |
| 62 | + |
| 63 | + console.warn(`Feature involved in this PR: ${filename}; Status: ${status}`) |
| 64 | + if (status === 'removed') { |
| 65 | + // Bad |
| 66 | + oldFilenames.push(filename) |
| 67 | + } else if (status === 'renamed') { |
| 68 | + // Also bad |
| 69 | + const previousFilename = file.previous_filename |
| 70 | + oldFilenames.push(previousFilename) |
| 71 | + } else { |
| 72 | + console.warn(`${filename} was not removed or renamed. Skipping.`) |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + if (!oldFilenames.length) { |
| 77 | + console.warn("No old file names in this PR. And that's perfectly cool.") |
| 78 | + return '' |
| 79 | + } |
| 80 | + |
| 81 | + let markdown = '⚠️ 🙀 **You deleted some features** 🙀 ⚠️\n\n' |
| 82 | + markdown += |
| 83 | + "Even if you don't reference these features anymore, as of this branch, you should not delete them.\n" |
| 84 | + markdown += 'They might still be referenced in translated content.\n' |
| 85 | + markdown += |
| 86 | + 'The weekly "Delete orphaned features" workflow will worry about cleaning those up.\n\n' |
| 87 | + markdown += '**To *undo* these removals run this command:**\n\n' |
| 88 | + markdown += ` |
| 89 | +\`\`\`sh |
| 90 | +git checkout origin/main -- ${oldFilenames.join(' ')} |
| 91 | +\`\`\` |
| 92 | +` |
| 93 | + |
| 94 | + return markdown |
| 95 | +} |
| 96 | + |
| 97 | +export default main |
0 commit comments