Last active
April 1, 2016 06:37
-
-
Save arunkumar-patange/db39a74889c52f094cbf to your computer and use it in GitHub Desktop.
Check PR for specs
This file contains hidden or 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
# | |
# | |
# Github API wrapper | |
# | |
# | |
import sh | |
from pygithub3 import Github | |
_ = lambda s: s.replace('\n', '') | |
class Ghub(object): | |
git = sh.git.bake(_cwd='.') | |
git_config = dict( | |
user='onelogin', | |
# repo=_(git('rev-parse', '--show-toplevel').split('/')[-1]), | |
repo=_(git.config('--get', 'remote.origin.url').split('/')[-1].split('.')[0]) | |
) | |
sha = _(git('rev-parse', 'HEAD')) | |
tests = dict( | |
rb=('spec/',), | |
js=('test/', 'spec/javascripts/'), | |
py=('tests/'), | |
) | |
@classmethod | |
def get_files(cls): | |
files = ( | |
(_(f).rsplit('.', 1)[-1], _(f)) | |
for f in cls.git('ls-tree', '--full-name', '--name-only', '-r', cls.sha) | |
) | |
_f = {} | |
for type, f in files: | |
_f.setdefault(type, []).append(f) | |
print _f | |
return _f | |
def __init__(self, token=None): | |
self.gh = Github(token=token, **self.git_config) | |
self.gh.users.set_token(token) | |
def test_files(self): | |
for f in self.get_files(): | |
if self.tests['node'] in f: | |
# test file found | |
break | |
else: | |
raise Exception('No test files found') | |
def find_pr(self): | |
gh = self.gh | |
for pr in gh.pull_requests.list(state='open', **self.git_config).all(): | |
for commit in gh.pull_requests.list_commits(pr.number, **self.git_config).all(): | |
if commit.sha == self.sha: | |
return pr | |
def post_comment(self, pr): | |
# for f in self.gh.pull_requests.list_files(pr.number, **self.git_config).all(): | |
for type, files in self.get_files().iteritems(): | |
if type in self.tests: | |
for f in files: | |
for path in self.tests[type]: | |
if path in f: | |
body = ":+1: for specs" | |
self.gh.issues.comments.create(pr.number, body, **self.git_config) | |
print 'tests found' | |
return | |
else: | |
print 'no tests found' | |
body = "TeamCity Bot: No tests found in this PR: {}".format(pr.title) | |
self.gh.issues.comments.create(pr.number, body, **self.git_config) | |
return self | |
# files = gh.pull_requests.list_files(pr.number, **git_config) | |
if __name__ == "__main__": | |
import sys | |
token = None | |
if len(sys.argv) > 1: | |
token = sys.argv.pop() | |
gh = Ghub(token) | |
pr = gh.find_pr() | |
gh.post_comment(pr) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment