-
Notifications
You must be signed in to change notification settings - Fork 61.6k
/
Copy pathunallowed-contributions.ts
executable file
·69 lines (55 loc) · 2.86 KB
/
unallowed-contributions.ts
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
import coreLib from '@actions/core'
import { readFileSync } from 'fs'
import yaml from 'js-yaml'
import { difference } from 'lodash-es'
import { checkContentType } from '@/workflows/fm-utils'
import github from '@/workflows/github'
const core = coreLib
const octokit = github()
const {
PR_NUMBER,
REPO_OWNER_AND_NAME,
FILE_PATHS_NOT_ALLOWED,
CHANGED_FILE_PATHS,
ADDED_CONTENT_FILES,
} = process.env
const [owner, repo] = (REPO_OWNER_AND_NAME || '').split('/') || []
const filters = yaml.load(
readFileSync('src/workflows/unallowed-contribution-filters.yml', 'utf8'),
) as Record<string, any>
main()
async function main() {
// Files in the diff that match specific paths we don't allow
const unallowedChangedFiles = [...JSON.parse(FILE_PATHS_NOT_ALLOWED || '')]
// Content files that are added in a forked repo won't be in the
// `github/docs` repo, so we don't need to check them. They will be
// reviewed manually by a content writer.
const contentFilesToCheck: string[] = difference(
JSON.parse(CHANGED_FILE_PATHS || ''),
JSON.parse(ADDED_CONTENT_FILES || ''),
)
// Any modifications or deletions to a file in the content directory
// could potentially have `type: rai` so each changed content file's
// frontmatter needs to be checked.
unallowedChangedFiles.push(...checkContentType(contentFilesToCheck, 'rai'))
if (unallowedChangedFiles.length === 0) return
// Format into Markdown bulleted list to use in the PR comment
const listUnallowedChangedFiles = unallowedChangedFiles.map((file) => `\n - ${file}`).join('')
const listUnallowedFiles = filters.notAllowed.map((file: string) => `\n - ${file}`).join('')
const reviewMessage = `👋 Hey there spelunker. It looks like you've modified some files that we can't accept as contributions:${listUnallowedChangedFiles}\n\nYou'll need to revert all of the files you changed that match that list using [GitHub Desktop](https://docs.github.com/en/free-pro-team@latest/desktop/contributing-and-collaborating-using-github-desktop/managing-commits/reverting-a-commit-in-github-desktop) or \`git checkout origin/main <file name>\`. Once you get those files reverted, we can continue with the review process. :octocat:\n\nThe complete list of files we can't accept are:${listUnallowedFiles}\n\nWe also can't accept contributions to files in the content directory with frontmatter \`type: rai\`.`
let workflowFailMessage =
"It looks like you've modified some files that we can't accept as contributions."
let createdComment
try {
createdComment = await octokit.rest.issues.createComment({
owner,
repo,
issue_number: Number(PR_NUMBER || ''),
body: reviewMessage,
})
workflowFailMessage = `${workflowFailMessage} Please see ${createdComment.data.html_url} for details.`
} catch (err) {
console.log('Error creating comment.', err)
}
core.setFailed(workflowFailMessage)
}