Skip to content
This repository has been archived by the owner on Nov 6, 2019. It is now read-only.

Commit

Permalink
Merge 8934d55 into 30fc616
Browse files Browse the repository at this point in the history
  • Loading branch information
Hai Hoang Dang committed Jul 4, 2018
2 parents 30fc616 + 8934d55 commit 03959c5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
27 changes: 17 additions & 10 deletions sktm/patchwork.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@
'fail': 3}


def join_with_slash(base, suffix):
base = base.rstrip('/')
suffix = suffix.strip('/')
return "{}/{}".format(base, suffix)


class ObjectSummary(object):
"""A summary of an mbox-based Patchwork object"""

Expand Down Expand Up @@ -460,8 +466,8 @@ def get_project_id(self, project_name):
Returns:
Integer representing project's ID.
"""
response = requests.get("%s/%s" % (self.apiurls.get("projects"),
project_name))
response = requests.get(join_with_slash(self.apiurls.get("projects"),
project_name))
if response.status_code != requests.codes.ok:
raise Exception("Can't get project data: %s %d" %
(project_name, response.status_code))
Expand All @@ -475,7 +481,7 @@ def get_apiurls(self, baseurl):
Returns:
The JSON representation of the API URLs.
"""
response = requests.get("%s/api" % baseurl)
response = requests.get(join_with_slash(baseurl, "api"))
if response.status_code != 200:
raise Exception("Can't get apiurls: %d" % response.status_code)

Expand Down Expand Up @@ -700,7 +706,8 @@ def get_patch_by_id(self, pid):
set of supported attributes depends on which API versions are
supported by a specific Patchwork instance.
"""
response = requests.get("%s/%d" % (self.apiurls.get("patches"), pid))
response = requests.get(join_with_slash(self.apiurls.get("patches"),
str(pid)))

if response.status_code != 200:
raise Exception("Can't get patch by id %d (%d)" %
Expand Down Expand Up @@ -745,9 +752,9 @@ def get_patchsets_by_patch(self, url, seen=set()):
if sid in seen:
continue
else:
series_list += self.get_series_from_url("%s/%d" % (
series_list += self.get_series_from_url(join_with_slash(
self.apiurls.get("series"),
sid
str(sid)
))
seen.add(sid)

Expand Down Expand Up @@ -811,10 +818,10 @@ def get_patchsets(self, patchlist):
for series in patch.get("series"):
sid = series.get("id")
if sid not in seen:
series_list += self.get_series_from_url("%s/%d" % (
self.apiurls.get("series"),
sid
))
series_list += self.get_series_from_url(
join_with_slash(self.apiurls.get("series"),
str(sid))
)
seen.add(sid)

return series_list
Expand Down
19 changes: 19 additions & 0 deletions tests/test_patchwork.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,25 @@ def test_stringify_unicode(self):
unicode_string = u'☃'
self.assertEqual('\xe2\x98\x83', patchwork.stringify(unicode_string))

def test_join_with_slash(self):
"""Ensure join_with_slash return a good url, path string."""
base = "path/to/dir"
suffix = "file"
self.assertEqual("path/to/dir/file",
patchwork.join_with_slash(base, suffix))
base = "path/to/dir/"
suffix = "file"
self.assertEqual("path/to/dir/file",
patchwork.join_with_slash(base, suffix))
base = "http://url.com/"
suffix = "part"
self.assertEqual("http://url.com/part",
patchwork.join_with_slash(base, suffix))
base = "http://url.com"
suffix = "part"
self.assertEqual("http://url.com/part",
patchwork.join_with_slash(base, suffix))


class TestObjectSummary(unittest.TestCase):
"""Test cases for the ObjectSummary class."""
Expand Down

0 comments on commit 03959c5

Please sign in to comment.