Skip to content

Commit

Permalink
Using tldr bot on a separate server
Browse files Browse the repository at this point in the history
- Sending the PR id and message as a POST request.
- The bot running on a separate server receives the message and posts
the comment on the PR.
  • Loading branch information
agnivade committed May 9, 2017
1 parent 8125f34 commit f73955f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 43 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ cache: false
after_success:
- bash scripts/build.sh

after_failure:
- cat test_result | python scripts/send_to_bot.py

env:
global:
- secure: ewQMcFd3wH8IHOeGhIkBr2kAgadONmesv2KB+9bPRCZ5gvtFFwlkCMdPnQBR9Kd3GKC8r+EO0JNNFWOvrDjPYhEHi9Ab26Q/qodwfwne9YloKXT+C48Zrbrj8q7kb/FeMYPev+RMuM96j5E5QyqBVohGP7hC2bpU3mvAhQ2wBCE=
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"precommit": "npm test",
"lint-markdown": "markdownlint pages/**/*.md",
"lint-tldr": "tldr-lint ./pages",
"test": "markdownlint pages/ && tldr-lint ./pages",
"test": "bash -c 'markdownlint pages/ && tldr-lint ./pages 2>&1 | tee test_result; test ${PIPESTATUS[0]} -eq 0'",
"build-index": "node ./scripts/build-index.js > pages/index.json"
},
"repository": "tldr-pages/tldr",
Expand Down
42 changes: 0 additions & 42 deletions scripts/post_pr_comment.py

This file was deleted.

38 changes: 38 additions & 0 deletions scripts/send_to_bot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import json
import os
import sys
import urllib2

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:
print f.read()


# 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})"
" for this PR has failed with the following message:"
"\n```\n"
"{comment_body}"
"\n```\n"
"Please fix the error(s) and push again."
).format(build_id=BUILD_ID, comment_body=test_result)

# If its a PR, post a comment on it
if PR_NUMBER != "false":
post_comment(PR_NUMBER, comment)

0 comments on commit f73955f

Please sign in to comment.