2017-04-27 09:02:32 +01:00
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import sys
|
2018-08-24 12:47:39 +01:00
|
|
|
try:
|
|
|
|
import urllib.request as urllib2
|
|
|
|
except ImportError:
|
|
|
|
import urllib2
|
2017-04-27 09:02:32 +01:00
|
|
|
|
|
|
|
URL = 'https://tldr-bot.starbeamrainbowlabs.com/'
|
|
|
|
|
|
|
|
def post_comment(pr_id, comment_body):
|
|
|
|
# Constructing the url
|
|
|
|
req = urllib2.Request(URL,
|
|
|
|
json.dumps({'body': comment_body, 'pr_id': pr_id }),
|
|
|
|
{'Content-Type': 'application/json'})
|
|
|
|
# Making the request
|
|
|
|
f = urllib2.urlopen(req)
|
|
|
|
if f.getcode() != 200:
|
2018-08-24 10:50:41 +01:00
|
|
|
print(f.read())
|
2017-04-27 09:02:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
# Get the environment variables
|
|
|
|
PR_NUMBER = os.environ.get('TRAVIS_PULL_REQUEST')
|
|
|
|
BUILD_ID = os.environ.get('TRAVIS_BUILD_ID')
|
|
|
|
|
|
|
|
# Read the test result output from stdin
|
|
|
|
test_result = sys.stdin.read().strip()
|
|
|
|
# Populate the template text
|
|
|
|
comment = (
|
|
|
|
"The [build]"
|
|
|
|
"(https://travis-ci.org/tldr-pages/tldr/builds/{build_id})"
|
2017-05-22 14:13:23 +01:00
|
|
|
" for this PR has failed with the following error(s):"
|
2017-04-27 09:02:32 +01:00
|
|
|
"\n```\n"
|
|
|
|
"{comment_body}"
|
|
|
|
"\n```\n"
|
|
|
|
"Please fix the error(s) and push again."
|
|
|
|
).format(build_id=BUILD_ID, comment_body=test_result)
|
|
|
|
|
2017-07-20 19:53:15 +01:00
|
|
|
# If it's a PR, post a comment on it
|
2017-04-27 09:02:32 +01:00
|
|
|
if PR_NUMBER != "false":
|
|
|
|
post_comment(PR_NUMBER, comment)
|