Skip to content

Commit

Permalink
fix: Run and Create a Pull Request
Browse files Browse the repository at this point in the history
  • Loading branch information
achillesrasquinha committed Feb 9, 2019
1 parent 94cae76 commit bc19e6e
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 6 deletions.
5 changes: 3 additions & 2 deletions src/pipupgrade/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def iterkeys(dict_, **kwargs):

if PY2:
# moves
from urllib2 import urlopen
from urllib2 import urlopen, Request, urlencode
from urllib2 import HTTPError

from __builtin__ import raw_input as input
Expand All @@ -51,7 +51,8 @@ def iterkeys(dict_, **kwargs):
from itertools import izip_longest as zip_longest
else:
# moves
from urllib.request import urlopen
from urllib.request import urlopen, Request
from urllib.parse import urlencode
from urllib.error import HTTPError

from builtins import input
Expand Down
4 changes: 4 additions & 0 deletions src/pipupgrade/cli/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ def get_parser():
help = "Target GitHub Username",
default = getenv("GITHUB_USERNAME")
)
parser.add_argument("--target-branch",
help = "Target Branch",
default = getenv("TARGET_BRANCH", "master")
)
parser.add_argument("-u", "--user",
action = "store_true",
help = "Install to the Python user install directory for environment \
Expand Down
22 changes: 20 additions & 2 deletions src/pipupgrade/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ def command(
github_access_token = None,
github_reponame = None,
github_username = None,
target_branch = "master",
latest = False,
self = False,
user = False,
Expand Down Expand Up @@ -287,13 +288,30 @@ def command(
branch = get_timestamp_str(format_ = "%Y%m%d%H%M%S")
popen("git checkout -B %s" % branch)

title = "fix(dependencies): Update dependencies to latest"
body = ""

# TODO: cross-check with "git add" ?
popen("git add %s" % " ".join(p.requirements), cwd = p.path)
popen("git commit -m 'fix(dependencies): Update dependencies to latest.'", cwd = p.path)
popen("git commit -m '%s'" % title, cwd = p.path)

popen("git push origin %s" % branch, cwd = p.path)

if not github_reponame:
raise ValueError(errstr % ("GitHub Reponame", "--github-reponame", getenvvar("GITHUB_REPONAME")))
if not github_username:
raise ValueError(errstr % ("GitHub Username", "--github-username", getenvvar("GITHUB_USERNAME")))
raise ValueError(errstr % ("GitHub Username", "--github-username", getenvvar("GITHUB_USERNAME")))

url = "/".join(["https://api.github.com", "repos", github_username, github_reponame])
params = dict(
head = "%s:branch" % git_username,
base = target_branch,
title = title,
body = body
)
response = req.post(url, params = params)

if response.ok:
pass
else:
response.raise_for_status()
23 changes: 22 additions & 1 deletion src/pipupgrade/request/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
# imports - compatibility imports
from pipupgrade._compat import urlopen, HTTPError
from pipupgrade._compat import (
urlopen,
Request,
urlencode,
HTTPError
)

# imports - standard imports
import json

# imports - module imports
from pipupgrade.request.response import Response
from pipupgrade.util.string import safe_encode

def get(*args, **kwargs):
try:
Expand Down Expand Up @@ -36,6 +42,21 @@ def post(*args, **kwargs):
return req.post(*args, **kwargs)
except ImportError:
url = kwargs.get("url")
params = kwargs.get("params", { })

response = Response()

try:
request = Request(url, safe_encode(urlencode(params)))
http_response = urlopen(request)

response.content = http_response.read()

http_response.close()
except HTTPError as e:
status_code = e.getcode()

response.url = url
response.status_code = status_code

return response
10 changes: 9 additions & 1 deletion src/pipupgrade/util/string.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,12 @@ def kebab_case(string, delimiter = " "):
words = string.replace(delimiter, " ").split()
kebab = "-".join([word.lower() for word in words])

return kebab
return kebab

def safe_encode(obj, encoding = "utf-8"):
try:
obj = obj.encode(encoding)
except (AttributeError, UnicodeEncodeError):
pass

return obj

0 comments on commit bc19e6e

Please sign in to comment.