Skip to content

Commit

Permalink
[qa] Added commit check and updated travis
Browse files Browse the repository at this point in the history
  • Loading branch information
ppabcd committed Nov 21, 2018
1 parent 951ce45 commit 526a64b
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ script:
- coverage run --source=openwisp_utils runtests.py

after_success:
coveralls
- coveralls
- python commitCheck.py $TRAVIS_REPO_SLUG $TRAVIS_COMMIT
96 changes: 96 additions & 0 deletions commitCheck.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import os
import re
import sys

import requests

# Get Repo and Commit Sha
repo = sys.argv[1]
commit = sys.argv[2]


# Function converter from php to python
def mb_substr(s, start, length=None):
return (s[start:(start + length)])


# Get Github token
github_token = os.environ["GH_TOKEN"]

# 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
}
review = requests.post(
api +
response_url,
json=post_fields,
headers=headers)

0 comments on commit 526a64b

Please sign in to comment.