forked from apache/incubator-weex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dangerfile.js
188 lines (172 loc) · 5.16 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { danger, fail, warn } from "danger";
import fs from "fs";
import path from 'path';
// Make sure there are changelog entries
// const hasChangelog = danger.git.modified_files.includes("changelog.md")
// if (!hasChangelog) { fail("No Changelog changes!") }
const jsFiles = danger.git.created_files.filter(path => path.endsWith("js"));
function absolute (relPath) {
return path.resolve(__dirname, relPath)
}
const flowIgnorePaths = [
'node_modules',
'test',
'build',
'examples',
'doc',
'android',
'ios',
'bin',
'dist',
'flow-typed'
].map(function (rel) {
return absolute(rel)
});
// new js files should have `@flow` at the top
const unFlowedFiles = jsFiles.filter(filepath => {
let i = 0
const len = flowIgnorePaths.length
while (i < len) {
const p = flowIgnorePaths[i]
if (absolute(filepath).indexOf(p) > -1) {
// ignore this file because it's in the flow-ignore-paths.
return false;
}
i++
}
const content = fs.readFileSync(filepath);
return !content.includes("@flow");
});
if (unFlowedFiles.length > 0) {
warn(
`These new JS files do not have Flow enabled: ${unFlowedFiles.join(", ")}`
);
}
// Error or Warn when delete public interface
var methion_break_change = false;
for (let c of danger.git.commits) {
// console.log("msg:" + c.message);
if (c.message && c.message.match(/break\s+change/i)) {
methion_break_change = true;
break;
}
}
// File name match any of these patterns will be ignored.
function is_ignored_public_check(file) {
var ignored_break_change_pattern = [
/^android\/sdk\/src\/test\/.+/,
/^android\/playground\/.+/
];
for (let p of ignored_break_change_pattern) {
if (file.match(p)) {
return true;
}
}
return false;
}
var has_app_changes = false;
var has_test_changes = false;
var codefiles = [];
for (let file of danger.git.modified_files) {
console.log("check file:" + file);
if (file.match(/WeexSDK\/Source/)) {
has_app_changes = true;
} else if (file.match(/WeexSDKTests/)) {
has_test_changes = true;
}
if (!is_ignored_public_check(file) && file.endsWith(".java")) {
var diff = danger.git.diffForFile(file);
// console.log("diff:" + diff+ typeof diff);
if (diff && diff.match(/^-\s*?public\s+[\s\S]+$/gm)) {
if (methion_break_change) {
warn("Potential BREAK CHANGE. Modify public in " + file);
} else {
warn(
"Potential BREAK CHANGE. Modify public in " +
file +
" without metion it in commit message. You'd better add 'break change' in your commit log. "
);
}
}
}
if (
file.endsWith(".h") ||
file.endsWith(".m") ||
file.endsWith(".mm") ||
file.endsWith(".java") ||
file.endsWith(".js")
) {
codefiles.push(file);
}
}
if(danger.git.added_files){
for (let file of danger.git.added_files) {
if (
file.endsWith(".h") ||
file.endsWith(".m") ||
file.endsWith(".mm") ||
file.endsWith(".java") ||
file.endsWith(".js")
) {
codefiles.push(file);
}
}
}
if (danger.git.lines_of_code > 500) {
warn("Big PR");
}
if (danger.git.lines_of_code > 100 && has_app_changes && !has_test_changes) {
warn("This PR may need tests.");
}
//check ios copyright
//see scripts/rh/header.template
const copyright_header_components = [
"Licensed to the Apache Software Foundation \\(ASF\\) under one",
"or more contributor license agreements. See the NOTICE file",
"distributed with this work for additional information",
"regarding copyright ownership\\. The ASF licenses this file",
'to you under the Apache License, Version 2\\.0 \\(the',
'"License"\\); you may not use this file except in compliance',
'with the License\\. You may obtain a copy of the License at'
];
//path prefix
const ignoreCopyrightVerifyPath = [
'test',
'android/playground/app/src/main/assets',
'android/sdk/assets',
'ios/playground/bundlejs',
'ios/sdk/WeexSDK/Resources',
'ios/sdk/WeexSDK/Sources/Layout'
]
codefiles.forEach(filepath => {
for(var i=ignoreCopyrightVerifyPath.length-1;i>=0;i--){
if(filepath.startsWith(ignoreCopyrightVerifyPath[i])){
return
}
}
const content = fs.readFileSync(filepath).toString();
for (const line of copyright_header_components) {
if (!content.match(new RegExp(line))) {
fail("Code file "+ filepath +" does not have the copyright header.");
return;
}
}
});