send_to_bot.py 1.0 KB

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