forked from openwisp/openwisp-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[qa] Added commit check and updated travis
Fixed openwisp#20
- Loading branch information
Showing
2 changed files
with
98 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |