2019-11-15 04:34:32 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2017-04-27 09:02:32 +01:00
|
|
|
import os
|
|
|
|
import sys
|
2019-11-15 04:34:32 +00:00
|
|
|
import json
|
|
|
|
import urllib.request
|
|
|
|
|
|
|
|
BOT_URL = 'https://tldr-bot.starbeamrainbowlabs.com'
|
|
|
|
|
|
|
|
COMMENT_ERROR="""
|
2020-01-13 19:45:31 +00:00
|
|
|
The [build](https://travis-ci.org/tldr-pages/tldr/builds/{build_id}) for this PR failed with the following error(s):
|
2019-11-15 04:34:32 +00:00
|
|
|
|
|
|
|
```
|
|
|
|
{content}
|
|
|
|
```
|
|
|
|
|
|
|
|
Please fix the error(s) and push again.
|
|
|
|
"""
|
|
|
|
|
|
|
|
COMMENT_CHECK="""
|
|
|
|
Hello! I've noticed something unusual when checking this PR:
|
|
|
|
|
|
|
|
{content}
|
|
|
|
|
2020-01-13 19:45:31 +00:00
|
|
|
Is this intended? If so, just ignore this comment. Otherwise, please double-check the commits.
|
2019-11-15 04:34:32 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
################################################################################
|
|
|
|
|
|
|
|
def post_comment(pr_id, body, once):
|
2019-11-18 21:55:29 +00:00
|
|
|
endpoint = BOT_URL + '/comment'
|
|
|
|
|
2019-11-15 04:34:32 +00:00
|
|
|
if once:
|
2019-11-18 21:55:29 +00:00
|
|
|
endpoint += '/once'
|
2019-11-15 04:34:32 +00:00
|
|
|
|
|
|
|
headers = {'Content-Type': 'application/json'}
|
|
|
|
data = json.dumps({'pr_id': pr_id, 'body': body})
|
|
|
|
req = urllib.request.Request(endpoint, data.encode(), headers)
|
|
|
|
|
|
|
|
try:
|
|
|
|
resp = urllib.request.urlopen(req)
|
|
|
|
code = resp.getcode()
|
|
|
|
except Exception as e:
|
|
|
|
print('Error sending data to tldr-bot:', str(e), file=sys.stderr)
|
|
|
|
return False
|
|
|
|
|
|
|
|
if code != 200:
|
|
|
|
print('Error: tldr-bot responded with code', code, file=sys.stderr)
|
|
|
|
print(resp.read(), file=sys.stderr)
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
def main(action):
|
2019-11-18 21:55:29 +00:00
|
|
|
if action not in ('report-errors', 'report-check-results'):
|
2019-11-15 04:34:32 +00:00
|
|
|
print('Unknown action:', action, file=sys.stderr)
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
content = sys.stdin.read().strip()
|
|
|
|
|
2019-11-18 21:55:29 +00:00
|
|
|
if action == 'report-errors':
|
2019-11-15 04:34:32 +00:00
|
|
|
comment_body = COMMENT_ERROR.format(build_id=BUILD_ID, content=content)
|
|
|
|
comment_once = False
|
2019-11-18 21:55:29 +00:00
|
|
|
elif action == 'report-check-results':
|
2019-11-15 04:34:32 +00:00
|
|
|
comment_body = COMMENT_CHECK.format(content=content)
|
|
|
|
comment_once = True
|
|
|
|
|
|
|
|
if post_comment(PR_ID, comment_body, comment_once):
|
|
|
|
print('Success.')
|
|
|
|
else:
|
|
|
|
print('Error sending data to tldr-bot!', file=sys.stderr)
|
|
|
|
|
|
|
|
################################################################################
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
REPO_SLUG = os.environ.get('TRAVIS_REPO_SLUG')
|
|
|
|
PR_ID = os.environ.get('TRAVIS_PULL_REQUEST')
|
|
|
|
BUILD_ID = os.environ.get('TRAVIS_BUILD_ID')
|
|
|
|
|
|
|
|
if PR_ID is None or BUILD_ID is None or REPO_SLUG is None:
|
|
|
|
print('Needed environment variables are not set.', file=sys.stderr)
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
if PR_ID is None or PR_ID == 'false':
|
|
|
|
print('Not a pull request, refusing to run.', file=sys.stderr)
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
if len(sys.argv) != 2:
|
|
|
|
print('Usage:', sys.argv[0], '<ACTION>', file=sys.stderr)
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
main(sys.argv[1])
|