Created
December 17, 2015 12:01
-
-
Save furushchev/609ff429beea596d3009 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# Author: Yuki Furuta <furushchev@jsk.imi.i.u-tokyo.ac.jp> | |
from __future__ import print_function | |
import os | |
import sys | |
from optparse import OptionParser | |
import httplib2 | |
from urllib import urlencode | |
from urlparse import urljoin | |
from xml.etree import ElementTree | |
class PreCriticizer(object): | |
def __init__(self, cache_dir=None): | |
self.client = httplib2.Http(cache_dir) | |
def run(self, input_path): | |
x = ElementTree.parse(input_path) | |
if x.find(".//remote/github") != None: | |
self.get_github_info(x) | |
else: | |
print("No remote information found", file=sys.stderr) | |
return False | |
for f in x.iterfind(".//result/file"): | |
try: | |
self.criticize(f) | |
except Exception as e: | |
print("Error: %s" % e) | |
return False | |
print("All codes are criticized") | |
return True | |
def get_github_info(self, xml): | |
github = xml.find(".//remote/github") | |
self.owner = github.get("owner") | |
self.repo = github.get("repository") | |
self.pr_num = github.get("pullrequest") | |
def criticize(self, file_element): | |
path = file_element.get("path") | |
commit_id = file_element.get("commit") | |
for e in list(file_element): | |
pos = e.get("line") | |
body = "[%s] %s" % (e.tag.title(), e.text) | |
self.send_comment(self.owner, | |
self.repo, | |
self.pr_num, | |
body, commit_id, path, pos) | |
def send_comment(self, owner, repo, pr_num, body, commit_id, path, position): | |
# POST /repos/:owner/:repo/pulls/:number/comments | |
urlpath = os.path.join("https://api.github.com", | |
"repos", owner, repo, | |
"pulls", pr_num, "comments") | |
data = dict(body=body, | |
commit_id=commit_id, | |
path=path, | |
position=position) | |
print("sending request to %s" % urlpath) | |
res, content = self.client.request(urlpath, "POST", urlencode(data)) | |
print("received response %s" % res["status"]) | |
if res["status"] != '200': | |
print("Error: failed to send comment [%s]: %s" % (res, content), file=sys.stderr) | |
return False | |
else: | |
return True | |
if __name__ == '__main__': | |
p = OptionParser() | |
p.add_option("-i", dest="input_path", default=None, | |
help="path to output xml file of linter") | |
p.add_option("-c", dest="cache_dir", default=".cache", | |
help="path to cache directory") | |
o, a = p.parse_args() | |
if not o.input_path: | |
p.error("INPUT_PATH must be specified") | |
p.print_usage() | |
sys.exit(1) | |
if not os.path.exists(o.input_path): | |
p.error("file %s not exists" % o.input_path) | |
sys.exit(1) | |
c = PreCriticizer(cache_dir=o.cache_dir) | |
if c.run(input_path=o.input_path): | |
sys.exit(0) | |
else: | |
sys.exit(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<roslint> | |
<remote> | |
<github owner="furushchev" repository="hoge" pullrequest="20" /> | |
</remote> | |
<result> | |
<file path="hoge.cpp" commit="asddfsd"> | |
<warn line="20">fugafuga</warn> | |
<error line="21">mogamoga</error> | |
</file> | |
<file path="fuga.cpp" commit="aaaaaa"> | |
<warn line="33">gehogeho</warn> | |
</file> | |
</result> | |
</roslint> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment