send_to_bot.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import json
  2. import os
  3. import sys
  4. try:
  5. import urllib.request as urllib2
  6. except ImportError:
  7. import urllib2
  8. URL = 'https://tldr-bot.starbeamrainbowlabs.com/'
  9. def post_comment(pr_id, comment_body):
  10. # Constructing the url
  11. req = urllib2.Request(URL,
  12. json.dumps({'body': comment_body, 'pr_id': pr_id }),
  13. {'Content-Type': 'application/json'})
  14. # Making the request
  15. f = urllib2.urlopen(req)
  16. if f.getcode() != 200:
  17. print(f.read())
  18. # Get the environment variables
  19. PR_NUMBER = os.environ.get('TRAVIS_PULL_REQUEST')
  20. BUILD_ID = os.environ.get('TRAVIS_BUILD_ID')
  21. # Read the test result output from stdin
  22. test_result = sys.stdin.read().strip()
  23. # Populate the template text
  24. comment = (
  25. "The [build]"
  26. "(https://travis-ci.org/tldr-pages/tldr/builds/{build_id})"
  27. " for this PR has failed with the following error(s):"
  28. "\n```\n"
  29. "{comment_body}"
  30. "\n```\n"
  31. "Please fix the error(s) and push again."
  32. ).format(build_id=BUILD_ID, comment_body=test_result)
  33. # If it's a PR, post a comment on it
  34. if PR_NUMBER != "false":
  35. post_comment(PR_NUMBER, comment)