-
Notifications
You must be signed in to change notification settings - Fork 66.9k
Expand file tree
/
Copy pathdeleted-assets-pr-comment.ts
More file actions
executable file
·76 lines (64 loc) · 2.12 KB
/
deleted-assets-pr-comment.ts
File metadata and controls
executable file
·76 lines (64 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { context as github_context, getOctokit } from '@actions/github'
import { setOutput } from '@actions/core'
const { GITHUB_TOKEN } = process.env
const context = github_context
if (!GITHUB_TOKEN) {
throw new Error(`GITHUB_TOKEN environment variable not set`)
}
// When this file is invoked directly from action as opposed to being imported
if (import.meta.url.endsWith(process.argv[1])) {
const owner = context.repo.owner
const repo = context.payload.repository?.name || ''
const baseSHA = context.payload.pull_request?.base.sha
const headSHA = context.payload.pull_request?.head.sha
const markdown = await main({ owner, repo, baseSHA, headSHA })
setOutput('markdown', markdown)
}
type MainArgs = {
owner: string
repo: string
baseSHA: string
headSHA: string
}
async function main({ owner, repo, baseSHA, headSHA }: MainArgs) {
const octokit = getOctokit(GITHUB_TOKEN as string)
// get the list of file changes from the PR
const response = await octokit.rest.repos.compareCommitsWithBasehead({
owner,
repo,
basehead: `${baseSHA}...${headSHA}`,
})
const { files } = response.data
if (!files) {
throw new Error('No files found in the PR')
}
const oldFilenames = []
for (const file of files) {
const { filename, status } = file
if (!filename.startsWith('assets')) continue
if (status === 'removed') {
// Bad
oldFilenames.push(filename)
} else if (status === 'renamed') {
// Also bad
const previousFilename = file.previous_filename
oldFilenames.push(previousFilename)
}
}
if (!oldFilenames.length) {
return ''
}
let markdown = '**Please restore deleted assets**\n\n'
markdown +=
"Even if you don't reference these assets anymore, as of this branch, please do not delete them.\n"
markdown += 'They might still be referenced in translated content.\n'
markdown += 'The weekly "Delete orphaned assets" workflow will clean those up.\n\n'
markdown += '**To *undo* these removals run this command:**\n\n'
markdown += `
\`\`\`sh
git checkout origin/main -- ${oldFilenames.join(' ')}
\`\`\`
`
return markdown
}
export default main