Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make CI enforce correct license banner + correct DCO sign-off in commit message #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
yarn-error.log
.idea
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is just something you do on your system, might be more appropriate to do something like I do:

In /home/pepsi/.gitconfig:

[core]
        excludesfile = /home/pepsi/.gitignore_global

In /home/pepsi/.gitignore_global:

*~
\#*
.#*
NOTES.*

Copy link
Author

@martijnthe martijnthe Apr 20, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main project's .gitignore has a bunch of IDE related lines:
https://github.com/jerryscript-project/jerryscript/blob/master/.gitignore#L9

I don't feel strongly about it though, although I've got bitten by system-level gitignore in the past where a repo actually did have files that were being ignored by my own system-level gitignore (and touching those files didn't appear as changed). So I've lived with an empty one ever since...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My thoughts are with @martijnthe :) I feel alike

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine with me; I'd never seen .idea before. :)

5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ cache:
jobs:
include:
- stage: lint
script: yarn lint
script:
- ./tools/check-signed-off.sh
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you'll need ./tools/check-signed-off.sh --travis here. When merging a PR, Travis tends to rewrite the author info of the commit with the info found in the public profile of the PR's author. Which can be different from the settings found on the local machine of the author. At least that was the case some time back and caused all validations to check out perfectly while Travis was running for the PR but going red as soon as patches got merged into master. The solution (workaround?) was to be strict when checking the PR but be tolerant when the PR gets merged. (That works if master can progress only through PRs.)

- ./tools/check-license.py
- yarn lint
- stage: build
script: yarn prepare
- stage: test
Expand Down
81 changes: 81 additions & 0 deletions tools/check-license.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/usr/bin/env python

# Copyright JS Foundation and other contributors, http://js.foundation
#
# Licensed 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.

from __future__ import print_function

import os
import re
import sys

LICENSE = re.compile(
r'((#|//|\*) Copyright .*\n'
r')+\s?\2\n'
r'\s?\2 Licensed under the Apache License, Version 2.0 \(the "License"\);\n'
r'\s?\2 you may not use this file except in compliance with the License.\n'
r'\s?\2 You may obtain a copy of the License at\n'
r'\s?\2\n'
r'\s?\2 http://www.apache.org/licenses/LICENSE-2.0\n'
r'\s?\2\n'
r'\s?\2 Unless required by applicable law or agreed to in writing, software\n'
r'\s?\2 distributed under the License is distributed on an "AS IS" BASIS\n'
r'\s?\2 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n'
r'\s?\2 See the License for the specific language governing permissions and\n'
r'\s?\2 limitations under the License.\n'
)

INCLUDE_DIRS = [
'src',
'tools',
]

EXCLUDE_DIRS = [
]

EXTENSIONS = [
'.c',
'.cmake',
'.cpp',
'.h',
'.js',
'.py',
'.S',
'.sh',
'.tcl',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it makes sense to keep these around to catch future additions?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I considered it. But most of them only make sense in the context of a C project. I think it makes more sense to just add as needed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm also voting for the shorter list. For now, something like the following might be needed: .sh, .py, .js, .ts?

'.ts',
]


def main():
is_ok = True

for dname in INCLUDE_DIRS:
for root, _, files in os.walk(dname):
if any(root.startswith(exclude) for exclude in EXCLUDE_DIRS):
continue
for fname in files:
if any(fname.endswith(ext) for ext in EXTENSIONS):
fpath = os.path.join(root, fname)
with open(fpath) as curr_file:
if not LICENSE.search(curr_file.read()):
print('%s: incorrect license' % fpath)
is_ok = False

if not is_ok:
sys.exit(1)


if __name__ == '__main__':
main()
110 changes: 110 additions & 0 deletions tools/check-signed-off.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#!/bin/bash

# Copyright JS Foundation and other contributors, http://js.foundation
#
# Licensed 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.

# Usage
function print_usage
{
echo "Usage: $0 [--help] [--tolerant] [--travis]"
}

function print_help
{
echo "$0: Check Signed-off-by message of the latest commit"
echo ""
print_usage
echo ""
echo "Optional arguments:"
echo " --help print this help message"
echo " --tolerant check the existence of the message only but don't"
echo " require the name and email address to match the author"
echo " of the commit"
echo " --travis perform check in tolerant mode if on Travis CI and not"
echo " checking a pull request, perform strict check otherwise"
echo ""
echo "The last line of every commit message must follow the form of:"
echo "'JerryScript-DCO-1.0-Signed-off-by: NAME EMAIL', where NAME and EMAIL must"
echo "match the name and email address of the author of the commit (unless in"
echo "tolerant mode)."
}

# Processing command line
TOLERANT="no"
while [ "$#" -gt 0 ]
do
if [ "$1" == "--help" ]
then
print_help
exit 0
elif [ "$1" == "--tolerant" ]
then
TOLERANT="yes"
shift
elif [ "$1" == "--travis" ]
then
if [ "$TRAVIS_PULL_REQUEST" == "" ]
then
echo -e "\e[1;33mWarning! Travis-tolerant mode requested but not running on Travis CI! \e[0m"
elif [ "$TRAVIS_PULL_REQUEST" == "false" ]
then
TOLERANT="yes"
else
TOLERANT="no"
fi
shift
else
print_usage
exit 1
fi
done

# Determining latest commit
parent_hashes=(`git show -s --format=%p HEAD | head -1`)

if [ "${#parent_hashes[@]}" -eq 1 ]
then
commit_hash=`git show -s --format=%h HEAD | head -1`
elif [ "${#parent_hashes[@]}" -eq 2 ]
then
commit_hash=${parent_hashes[1]}
else
echo "$0: cannot handle commit with ${#parent_hashes[@]} parents ${parent_hashes[@]}"
exit 1
fi

# Checking the last line
actual_signed_off_by_line=`git show -s --format=%B $commit_hash | sed '/^$/d' | tr -d '\015' | tail -n 1`

if [ "$TOLERANT" == "no" ]
then
author_name=`git show -s --format=%an $commit_hash`
author_email=`git show -s --format=%ae $commit_hash`
required_signed_off_by_line="JerryScript-DCO-1.0-Signed-off-by: $author_name $author_email"

if [ "$actual_signed_off_by_line" != "$required_signed_off_by_line" ]
then
echo -e "\e[1;33mSigned-off-by message is incorrect. The following line should be at the end of the $commit_hash commit's message: '$required_signed_off_by_line'. \e[0m"
exit 1
fi
else
echo -e "\e[1;33mWarning! The name and email address of the author of the $commit_hash commit is not checked in tolerant mode! \e[0m"
if echo "$actual_signed_off_by_line" | grep -q -v '^JerryScript-DCO-1.0-Signed-off-by:'
then
echo -e "\e[1;33mSigned-off-by message is incorrect. The following line should be at the end of the $commit_hash commit's message: '$required_signed_off_by_line'. \e[0m"
exit 1
fi
fi

exit 0