-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathindex.js
103 lines (87 loc) · 3.74 KB
/
index.js
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import walkSync from "walk-sync";
import {
isFileInCorrectFolder,
isFilepathDateValid,
getNoticeCategory,
} from "./utils/validators.js";
import _ from "lodash";
const { partition } = _;
import { setOutput } from "@actions/core";
const { CHANGED_FILES } = process.env;
const dmcaFilesChangedInPr = CHANGED_FILES.split("\n").filter(
isFileInsideAYearFolder
);
const [prChangedFilesWithValidDates, prChangedFilesWithInvalidDates] =
partition(dmcaFilesChangedInPr, isFilepathDateValid);
const [prChangedFilesinCorrectFolder, prChangedFilesNotInCorrectFolder] =
partition(prChangedFilesWithValidDates, isFileInCorrectFolder);
let commentBody = `Thanks for the PR!
# Files changed in this PR
`;
let commentCopyForFilesChangedInThisPr =
prChangedFilesWithInvalidDates.length ||
prChangedFilesNotInCorrectFolder.length
? ` I've scanned the changed files and found some issues to double check:`
: ` All DMCA notices changed in this PR appear to have valid YYYY-MM-DD dates and seem to be in the correct folder.`;
commentCopyForFilesChangedInThisPr += prChangedFilesWithInvalidDates.length
? `\n\n**Notices that don't contain a date that conforms to the YYYY-MM-DD format:**\n\n${prChangedFilesWithInvalidDates
.map((file) => `- ${file}`)
.join("\n")}`
: "";
commentCopyForFilesChangedInThisPr += prChangedFilesNotInCorrectFolder.length
? `\n\n**Notices that might need to be moved to the correct year/month folder:**\n\n${prChangedFilesNotInCorrectFolder
.map((file) => `- ${file}`)
.join("\n")}`
: "";
let commentCopyForCategoriesOfFilesChangedInThisPr = `\n\n**Notices appear to fall into the following categories:**
| Filepath | Category |
| --- | --- |
${dmcaFilesChangedInPr
.map((filepath) => {
const noticeCategory = getNoticeCategory(filepath);
return "| " + filepath + " | " + noticeCategory + " |";
})
.join("\n")}
`;
commentBody += commentCopyForFilesChangedInThisPr;
commentBody += commentCopyForCategoriesOfFilesChangedInThisPr;
const filesInDmcaNoticeFolders = walkSync(".", { directories: false })
.filter(isFileInsideAYearFolder)
.filter((file) => {
const isFileAmongChangedFiles = dmcaFilesChangedInPr.includes(file);
return !isFileAmongChangedFiles; // filter out files that are in the changed files list
});
const [filesWithValidDates, filesWithInvalidDates] = partition(
filesInDmcaNoticeFolders,
isFilepathDateValid
);
const [filesInCorrectFolder, filesNotInCorrectFolder] = partition(
filesWithValidDates,
isFileInCorrectFolder
);
let commentCopyForAllDmcaNoticesInRepo =
filesWithInvalidDates.length || filesNotInCorrectFolder.length
? `\n# Other Files in Repo\nI've also scanned the rest of the repo and found some files to double check:`
: `\n# Other Files in Repo\nThe other DMCA notices in the repo appear to have valid YYYY-MM-DD dates and seem to be in the correct folder.`;
const invalidDatesList = filesWithInvalidDates.length
? `\n\n**Notices that don't contain a date that conforms to the YYYY-MM-DD format:**\n\n${filesWithInvalidDates
.map((file) => `- ${file}`)
.join("\n")}`
: "";
const incorrectFolderList = filesNotInCorrectFolder.length
? `\n\n**Notices that might need to be moved to the correct year/month folder:**\n\n${filesNotInCorrectFolder
.map((file) => `- ${file}`)
.join("\n")}`
: "";
commentCopyForAllDmcaNoticesInRepo += invalidDatesList;
commentCopyForAllDmcaNoticesInRepo += incorrectFolderList;
commentBody += commentCopyForAllDmcaNoticesInRepo;
setOutput("COMMENT_BODY", commentBody);
/**
* Returns true if the file is in a folder at the root directory of the repo of the form YYYY
* @param {string} filepath
* @returns {boolean}
*/
function isFileInsideAYearFolder(filepath) {
return filepath.match(/^\d{4}/) !== null;
}