From 2a9060aef40d1912b19887a8d4b6951760b7a8c7 Mon Sep 17 00:00:00 2001 From: Agniva De Sarker Date: Sun, 6 Nov 2016 22:59:44 +0530 Subject: [PATCH] Add script to post Travid build errors on PRs (#1138) --- .gitignore | 4 ++++ .travis.yml | 3 +++ package.json | 2 +- scripts/post_pr_comment.py | 42 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 scripts/post_pr_comment.py diff --git a/.gitignore b/.gitignore index be241d1de..5fbfdc6e5 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,10 @@ node_modules npm-debug.log +# A temp file used to store the output from npm test +# used to update a PR with a comment +test_result + # We used to have an index file in the repository, # but it's been superseded by a static asset # hosted at https://tldr-pages.github.io/assets/index.json diff --git a/.travis.yml b/.travis.yml index 924a330af..0447a8487 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,6 +10,9 @@ cache: false after_success: - bash scripts/build.sh +after_failure: +- cat test_result | python scripts/post_pr_comment.py + env: global: - secure: ewQMcFd3wH8IHOeGhIkBr2kAgadONmesv2KB+9bPRCZ5gvtFFwlkCMdPnQBR9Kd3GKC8r+EO0JNNFWOvrDjPYhEHi9Ab26Q/qodwfwne9YloKXT+C48Zrbrj8q7kb/FeMYPev+RMuM96j5E5QyqBVohGP7hC2bpU3mvAhQ2wBCE= diff --git a/package.json b/package.json index f95ccd844..cf27f849f 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "precommit": "npm test", "lint-markdown": "markdownlint pages/**/*.md", "lint-tldr": "tldr-lint ./pages", - "test": "markdownlint pages/ && tldr-lint ./pages", + "test": "markdownlint pages/ && tldr-lint ./pages 2>&1 | tee test_result", "build-index": "node ./scripts/build-index.js > pages/index.json" }, "repository": "tldr-pages/tldr", diff --git a/scripts/post_pr_comment.py b/scripts/post_pr_comment.py new file mode 100644 index 000000000..9060ecae0 --- /dev/null +++ b/scripts/post_pr_comment.py @@ -0,0 +1,42 @@ +import json +import os +import sys +import urllib2 + +GITHUB_URL = 'https://api.github.com' + +def post_comment(pr_id, repo_slug, comment_body, user_token): + # Constructing the url + url = '{api_url}/repos/{slug}/issues/{number}/comments'.format( + api_url=GITHUB_URL, slug=repo_slug, number=pr_id) + req = urllib2.Request(url=url, + data=json.dumps({'body': comment_body})) + req.add_header('Authorization', 'token ' + user_token) + # Making the request + f = urllib2.urlopen(req) + if f.getcode() != 201: + print f.read() + + +# Get the environment variables +PR_NUMBER = os.environ.get('TRAVIS_PULL_REQUEST') +REPO_SLUG = os.environ.get('TRAVIS_REPO_SLUG') # owner_name/repo_name +BOT_TOKEN = os.environ.get('TRAVIS_BOT_GITHUB_TOKEN') +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, REPO_SLUG, comment, BOT_TOKEN)