Skip to content

Commit

Permalink
Run Danger with GitHub Actions (TextureGroup#1635)
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenhuy authored and matthewd1234 committed Aug 25, 2019
1 parent f286726 commit 7509f9f
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 83 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/danger.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Danger

on: [pull_request]

jobs:
danger:
name: Danger JS
runs-on: ubuntu-latest
steps:
- name: Checkout the Git repository
uses: actions/checkout@v1
# Danger JS would fail because of the below missing module.
# TODO: Report to Danger JS
- name: Install missing module
run: yarn add -D @babel/plugin-transform-flow-strip-types
# TODO: Figure out why GITHUB_TOKEN isn't enough for Danger JS to create a comment.
# Our dangerfile.js escalates any warnings as failures to get more attention.
#
# Here is the error response from GitHub API:
#
# Request failed [403]: https://api.github.com/repos/TextureGroup/Texture/issues/1635/comments
# Response: {
# "message": "Resource not accessible by integration",
# "documentation_url": "https://developer.github.com/v3/issues/comments/#create-a-comment"
# }
#
# https://github.com/TextureGroup/Texture/pull/1635/checks?check_run_id=200541353
- name: Danger
uses: danger/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
83 changes: 0 additions & 83 deletions Dangerfile

This file was deleted.

89 changes: 89 additions & 0 deletions dangerfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,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);
}

0 comments on commit 7509f9f

Please sign in to comment.