Skip to content

Commit

Permalink
[qa] Move commit check to qa.py
Browse files Browse the repository at this point in the history
  • Loading branch information
ppabcd committed Nov 22, 2018
1 parent fc795fc commit fab6cc1
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 3 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,4 @@ script:
- coverage run --source=openwisp_utils runtests.py

after_success:
- coveralls
- python commitCheck.py $TRAVIS_REPO_SLUG $TRAVIS_COMMIT
coveralls
122 changes: 122 additions & 0 deletions openwisp_utils/qa.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import os
import re

import requests


def _parse_args():
"""
Expand Down Expand Up @@ -51,3 +53,123 @@ def check_migration_name():
raise Exception("Migration %s %s in directory %s must "
"be renamed to something more descriptive."
% (file_, ', '.join(migrations), args.migration_path))


def _commit_check_args():
"""
Parse and return CLI arguements
"""
parser = argparse.ArgumentParser(description="Ensures migration files "
"created have a descriptive name. If "
"default name pattern is found, "
"raise exception!")
parser.add_argument("--token",
help="Github token for check commit")
parser.add_argument("--commit",
help="Commit sha for checking commit messages ")
parser.add_argument("--repo",
help="Repository target <username>/<repo>")
return parser.parse_args()


def check_commit_messaage():
args = _commit_check_args()

if not args.token:
if not os.environ["GH_TOKEN"]:
github_token = os.environ["GH_TOKEN"]
else:
raise Exception("CLI arguement `token` is required "
"but not found")
else:
github_token = args.token

if not args.commit:
raise Exception("CLI argument `commit` is required "
"but not found")
commit = args.commit
if not args.repo:
raise Exception("CLI argument `repo` is required"
"but not found")
repo = args.repo
# Api URL and commit url
api = "https://api.github.com"
api_main_url = "/repos/" + repo + "/commits/" + commit

res = requests.get(api + api_main_url)
api_body = res.json()

# Get commit username
api_username = api_body['author']['login']
# Get message commit
api_message = api_body['commit']['message']
# Explode commit message
api_message_explode = api_message.split("\n")
# Count Api Message when exploded
api_message_explode_count = len(api_message_explode)

errors = []

# Check dot in end of the first line
if api_message_explode[0][len(api_message_explode[0].strip()) - 1].strip() == '.':
errors.append("Do not add a final dot at the end of the first line.")

# Check prefix
prefix = re.match('\[(.*?)\]', api_message_explode[0])

if not prefix:
errors.append(
"Add prefix in beginning of the commit. "
"Example: [module/file/feature]"
)

# Check capital after prefix
commitMessagefirst = api_message_explode[0].replace(prefix.group(), '').strip()
if not commitMessagefirst[0].isupper():
errors.append("No capital letter after prefix")

# Check blank page before first line and second line
if api_message_explode_count > 1:
print(api_message_explode[1])
if api_message_explode[1] != '':
errors.append("No blank line before first line and second line")

# Check is error
if len(errors) == 0:
body = "All check done, no errors."
else:
body = "You have errors with commit message: \n"
for e in errors:
body += "- " + e + "\n"

search = requests.get(
api +
'/search/issues?q=typetype:pr%20sha:' +
mb_substr(commit, 0, 7) +
'%20is_unmerged%20author:' +
api_username +
'&&sort=updated'
)
search_body = search.json()

if len(search_body['items']) > 0:
search_data = search_body['items'][0]['number']
response_url = (
"/repos/" + str(repo) +
"/issues/" + str(search_data) + "/comments")

headers = {
'Authorization': 'token %s' % github_token
}
post_fields = {
'body': body
}
requests.post(
api +
response_url,
json=post_fields,
headers=headers)


def mb_substr(s, start, length=None):
return (s[start:(start + length)])
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
packages=find_packages(exclude=['tests', 'docs']),
entry_points={
'console_scripts': [
'checkmigrations = openwisp_utils.qa:check_migration_name'
'checkmigrations = openwisp_utils.qa:check_migration_name',
'commitcheck = openwisp_utils.qa:check_message_check',
],
},
include_package_data=True,
Expand Down

0 comments on commit fab6cc1

Please sign in to comment.