forked from TextureGroup/Texture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdangerfile.js
89 lines (78 loc) · 3.28 KB
/
dangerfile.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
const { danger, fail, warn, schedule } = require('danger');
const { readFileSync } = require('fs');
const source_pattern = /(\.m|\.mm|\.h)$/;
const modified_source_files = danger.git.modified_files.filter(f => source_pattern.test(f));
const has_modified_source_files = (Array.isArray(modified_source_files) && modified_source_files.length > 0);
const added_source_files = danger.git.created_files.filter(f => source_pattern.test(f));
const has_added_source_files = (Array.isArray(added_source_files) && added_source_files.length > 0);
// Make it more obvious that a PR is a work in progress and shouldn't be merged yet
if (danger.github.pr.title.includes("[WIP]")) {
const msg = "PR is classed as Work in Progress";
console.error("FAIL: " + msg);
fail(msg);
}
// Warn when there is a big PR
if (danger.github.pr.additions + danger.github.pr.deletions > 500) {
const msg = "This is a big PR, please consider splitting it up to ease code review.";
console.error("WARN: "+ msg);
warn(msg);
}
// Modifying the changelog will probably get overwritten.
if (danger.git.modified_files.includes("CHANGELOG.md")) {
if (danger.github.pr.title.includes("#changelog")) {
const msg = "PR modifies CHANGELOG.md, which is a generated file. #changelog added to the title to suppress this warning.";
console.error("WARN: "+ msg);
warn(msg);
} else {
const msg = "PR modifies CHANGELOG.md, which is a generated file. Add #changelog to the title to suppress this failure.";
console.error("FAIL: " + msg);
fail(msg);
}
}
// Reference: http://a32.me/2014/03/heredoc-multiline-variable-with-javascript/
function hereDoc(f) {
return f.toString().
replace(/^[^\/]+\/\*!?/, '').
replace(/\*\/[^\/]+$/, '');
}
function full_license(partial_license, filename) {
var license_header = hereDoc(function() {/*!
//
*/});
license_header += "// " + filename;
license_header += hereDoc(function() {/*!
// Texture
//*/});
license_header += partial_license;
return license_header;
}
function check_file_header(files_to_check, license) {
for (let file of files_to_check) {
const filename = file.replace(/^.*[\\\/]/, ''); // Reference: https://stackoverflow.com/a/423385
schedule(async () => {
const content = await danger.github.utils.fileContents(file);
if (!content.includes("Pinterest, Inc.")) {
const msg = "Please ensure license is correct for " + filename +":\n```" + full_license(license, filename) + "\n```";
console.error("FAIL: " + msg);
fail(msg);
}
});
}
}
const new_source_license_header = hereDoc(function() {/*!
// Copyright (c) Pinterest, Inc. All rights reserved.
// Licensed under Apache 2.0: http://www.apache.org/licenses/LICENSE-2.0
//*/});
const modified_source_license_header = hereDoc(function() {/*!
// Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.
// Changes after 4/13/2017 are: Copyright (c) Pinterest, Inc. All rights reserved.
// Licensed under Apache 2.0: http://www.apache.org/licenses/LICENSE-2.0
//*/});
// Ensure new files have proper header
if (has_added_source_files) {
check_file_header(added_source_files, new_source_license_header);
}
// Ensure modified files have proper header
if (has_modified_source_files) {
check_file_header(modified_source_files, modified_source_license_header);
}