Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added class for URL generation, restored "-R" option behaviour

  • Loading branch information...
commit 05573b020c8ec3c088359509d12f1d49a4c25eaf 1 parent 4fb0911
@MichaelMayorov MichaelMayorov authored
Showing with 48 additions and 41 deletions.
  1. +15 −10 sympy-bot
  2. +17 −11 url_templates.py
  3. +16 −20 utils.py
View
25 sympy-bot
@@ -18,6 +18,7 @@ from utils import (cmd, github_get_pull_request,
github_add_comment_to_pull_request, list_pull_requests, format_repo,
get_interpreter_version_info, get_interpreter_exe, github_get_user_info)
from testrunner import run_tests
+from url_templates import URLs
default_testcommand = "setup.py test"
default_interpreter = "python"
@@ -94,14 +95,16 @@ Commands:
options.user = config.get("user")
options.password = config.get("password")
+ gh_user, gh_repo = options.repository.split("/")
+ urls = URLs(user=gh_user, repo=gh_repo)
if len(args) == 1:
arg, = args
if arg == "list":
- list_pull_requests(options.repository)
+ list_pull_requests(urls)
return
elif arg == "list-numbers":
- list_pull_requests(options.repository, numbers_only=True)
+ list_pull_requests(urls, numbers_only=True)
return
print "Unknown command"
sys.exit(1)
@@ -114,13 +117,13 @@ Commands:
print "Reviewing all *mergeable* pull requests"
print
nonmergeable, mergeable = list_pull_requests( \
- options.repository, numbers_only=True)
+ urls, numbers_only=True)
nums = mergeable
elif args[0] == "all":
print "Reviewing *all* pull requests"
print
nonmergeable, mergeable = list_pull_requests( \
- options.repository, numbers_only=True)
+ urls, numbers_only=True)
nums = nonmergeable + mergeable
else:
# just a pull request #, convert it:
@@ -129,12 +132,12 @@ Commands:
# list of pull request numbers, convert it:
nums = [int(x) for x in args]
if options.upload:
- username, password = github_authenticate(options)
+ username, password = github_authenticate(urls.api_url, options)
else:
username = password = None
try:
for n in nums:
- review(n, options, username=username, password=password, \
+ review(urls, n, options, username=username, password=password, \
proto=proto
)
except KeyboardInterrupt:
@@ -257,7 +260,7 @@ def get_platform_version(interpreter):
r += "*Cache:* %s\n" % use_cache
return r
-def review(n, config, **kwargs):
+def review(urls, n, config, **kwargs):
# Make sure python3 is setup correctly
# If set Python 3 interpreter with -i, be sure python3 is True
username = kwargs.get("username", None)
@@ -271,14 +274,14 @@ def review(n, config, **kwargs):
tmpdir = mkdtemp(prefix="sympy-bot-tmp")
print "> Working directory: %s" % tmpdir
- pull = github_get_pull_request(n)
+ pull = github_get_pull_request(urls.single_pull_template, n)
assert pull["number"] == n
print "> Reviewing pull request #%d" % n
repo = pull["head"]['repo']["html_url"]
repo = repo.replace(default_proto, proto) if proto != default_proto else repo
branch = pull["head"]["ref"]
user = pull["head"]["user"]
- user_info = github_get_user_info(username)
+ user_info = github_get_user_info(urls.user_info_template, username)
author = "\"%s\" <%s>" % (user_info.get("name", "unknown"),
user_info.get("email", ""))
print "> Pull request info:"
@@ -345,7 +348,9 @@ def review(n, config, **kwargs):
print review
if config.upload and config.comment:
print "> Uploading the review to the GitHub pull request ..."
- github_add_comment_to_pull_request(username, password, n, review)
+ github_add_comment_to_pull_request(urls.issue_comment_template, \
+ username, password, n, review
+ )
print "> Done."
print format_repo("> Check the results: https://github.com/{repo}/pull/%d", config.repository) % n
print "> Merged pull request available in: %s" % tmpdir
View
28 url_templates.py
@@ -1,11 +1,17 @@
-# This file contains predefined values for communicating with GitHub
-# If you want to run bot on another repo, then replace "gh_user" and "gh_repo" with
-# yours values
-gh_api_url = "https://api.github.com"
-gh_user = "sympy"
-gh_repo = "sympy"
-gh_pull_list_url = gh_api_url + "/repos" + "/" + gh_user + "/" + gh_repo + "/pulls"
-gh_pull_template = gh_pull_list_url + "/%d"
-gh_user_info_template = gh_api_url + "/users/%s"
-gh_issue_comment_template = gh_api_url + "/repos" + "/" + gh_user + "/" + gh_repo + \
- "/issues/%d" + "/comments"
+class URLs(object):
+ """
+ This class contains URLs and templates which used in requests to GitHub API
+ """
+
+ def __init__(self, user="sympy", repo="sympy", api_url="https://api.github.com"):
+ """ Generates all URLs and templates """
+
+ self.user = user
+ self.repo = repo
+ self.api_url = api_url
+
+ self.pull_list_url = api_url + "/repos" + "/" + user + "/" + repo + "/pulls"
+ self.single_pull_template = self.pull_list_url + "/%d"
+ self.user_info_template = api_url + "/users/%s"
+ self.issue_comment_template = \
+ api_url + "/repos" + "/" + user + "/" + repo + "/issues/%d" + "/comments"
View
36 utils.py
@@ -13,8 +13,6 @@
from jsonrpc import JSONRPCService
-import gh_values
-
class CmdException(Exception):
pass
@@ -108,18 +106,17 @@ def get_xpassed_info_from_log(log):
return lines.splitlines()
return []
-def github_get_pull_request_all():
+def github_get_pull_request_all(url):
"""
Returns all github pull requests.
"""
- url = gh_values.gh_pull_list_url
return query2github(url)
-def github_get_pull_request(n):
+def github_get_pull_request(url, n):
"""
Returns pull request 'n'.
"""
- url = gh_values.gh_pull_template % n
+ url = url % n
timer = 1
while True:
try:
@@ -132,8 +129,8 @@ def github_get_pull_request(n):
return pull
-def github_get_user_info(username):
- url = gh_values.gh_user_info_template % username
+def github_get_user_info(url, username):
+ url = url % username
timer = 1
while True:
try:
@@ -146,14 +143,13 @@ def github_get_user_info(username):
return user_info
-def github_check_authentication(username, password):
+def github_check_authentication(url, username, password):
"""
Checks that username & password is valid.
"""
- url = gh_values.gh_api_url
query2github(url, username, password)
-def github_add_comment_to_pull_request(username, password, n, comment):
+def github_add_comment_to_pull_request(url, username, password, n, comment):
"""
Adds a 'comment' to the pull request 'n'.
@@ -164,7 +160,7 @@ def github_add_comment_to_pull_request(username, password, n, comment):
"body" : comment
}
)
- url = gh_values.gh_issue_comment_template % n
+ url = url % n
response = query2github(url, username, password, enc_comment)
assert response["body"] == comment
@@ -213,14 +209,14 @@ def reviews_sympy_org_upload(data, url_base):
timer *= 2
return r["task_url"]
-def list_pull_requests(repo, numbers_only=False):
+def list_pull_requests(urls, numbers_only=False):
"""
Returns the pull requests numbers.
It returns a tuple of (nonmergeable, mergeable), where "nonmergeable"
and "mergeable" are lists of the pull requests numbers.
"""
- pulls = github_get_pull_request_all()
+ pulls = github_get_pull_request_all(urls.pull_list_url)
formated_pulls = []
print "Total pull count", len(pulls)
sys.stdout.write("Processing pulls...")
@@ -228,7 +224,7 @@ def list_pull_requests(repo, numbers_only=False):
n = pull["number"]
sys.stdout.write(" %d" % n)
sys.stdout.flush()
- pull_info = github_get_pull_request(n)
+ pull_info = github_get_pull_request(urls.single_pull_template, n)
mergeable = pull_info["mergeable"]
repo = pull["head"]["repo"]["html_url"]
branch = pull["head"]["ref"]
@@ -236,9 +232,9 @@ def list_pull_requests(repo, numbers_only=False):
created_at = time.strptime(created_at, "%Y-%m-%dT%H:%M:%SZ")
created_at = time.mktime(created_at)
username = pull["head"]["user"]["login"]
- user_info = github_get_user_info(username)
+ user_info = github_get_user_info(urls.user_info_template, username)
author = "\"%s\" <%s>" % (user_info.get("name", "unknown"),
- user_info.get("email", ""))
+ user_info.get("email", ""))
formated_pulls.append((created_at, n, repo, branch, author, mergeable))
formated_pulls.sort(key=lambda x: x[0])
print "\nPatches that cannot be merged without conflicts:"
@@ -277,14 +273,14 @@ def list_pull_requests(repo, numbers_only=False):
https to authenticate with GitHub, otherwise not saved anywhere else:\
"""
-def github_authenticate(config):
+def github_authenticate(url, config):
def get_password():
while True:
password = getpass("Password: ")
try:
print "> Checking username and password ..."
- github_check_authentication(username, password)
+ github_check_authentication(url, username, password)
except AuthenticationFailed:
print "> Authentication failed."
else:
@@ -299,7 +295,7 @@ def get_password():
try:
print "> Checking username and password ..."
- github_check_authentication(username, password)
+ github_check_authentication(url, username, password)
except AuthenticationFailed:
print "> Authentication failed."
password = get_password()
Please sign in to comment.
Something went wrong with that request. Please try again.