-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
75 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
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]")) { | ||
warn("PR is classed as Work in Progress"); | ||
} | ||
|
||
// Warn when there is a big PR | ||
if (danger.github.pr.additions + danger.github.pr.deletions > 500) { | ||
warn("This is a big PR, please consider splitting it up to ease code review."); | ||
} | ||
|
||
// Modifying the changelog will probably get overwritten. | ||
if (danger.git.modified_files.includes("CHANGELOG.md") && !danger.github.pr.title.includes("#changelog")) { | ||
warn("PR modifies CHANGELOG.md, which is a generated file. Add #changelog to the title to suppress this warning."); | ||
} | ||
|
||
// 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(/^.*[\\\/]/, ''); | ||
schedule(async () => { | ||
const content = await danger.github.utils.fileContents(file); | ||
if (!content.includes("Pinterest, Inc.")) { | ||
warn ("Please ensure license is correct for " + filename +":\n```" + full_license(license, filename) + "\n```"); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
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); | ||
} |