Skip to content

Commit

Permalink
Add dangerfile.js
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenhuy committed Aug 22, 2019
1 parent 800269d commit 965bc0c
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/danger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,5 @@ jobs:
uses: actions/checkout@v1
- name: Danger
uses: danger/danger-js@9.1.8
with:
args: --dangerfile Dangerfile
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
75 changes: 75 additions & 0 deletions dangerfile.js
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);
}

0 comments on commit 965bc0c

Please sign in to comment.