-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Get bitbucket's files using their API (#71) * Retire the old bitbucket auth method * Update schema docs due bitbucket access changes * [Bitbucket API Access] Update changelog
- Loading branch information
1 parent
f46937c
commit 67a24b8
Showing
8 changed files
with
189 additions
and
51 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import base64 | ||
import os | ||
import re | ||
from typing import Dict | ||
import urllib | ||
|
||
|
||
NETLOC = os.getenv("BITBUCKET_NETLOC") or "bitbucket.org" | ||
API_NETLOC = os.getenv("BITBUCKET_API_NETLOC") or "api.bitbucket.org" | ||
USER = os.getenv("BITBUCKET_USER") | ||
PASS = os.getenv("BITBUCKET_PASSWORD") | ||
|
||
|
||
def prepare_request(url: str) -> urllib.request.Request: | ||
if not USER or not PASS: | ||
msg = "Credentials not found: `BITBUCKET_USER` or `BITBUCKET_PASSWORD` not set." | ||
raise ValueError(msg) | ||
|
||
api_url = convert_to_api_url(url, NETLOC, API_NETLOC) | ||
return urllib.request.Request(api_url, headers=get_auth_header(USER, PASS)) | ||
|
||
|
||
def convert_to_api_url(url: str, netloc: str, api_netloc: str) -> str: | ||
"""Support both regular and raw URLs""" | ||
try: | ||
user, repo, path = re.search( | ||
f"https://{netloc}/(.*?)/(.*?)/(?:raw|src)/(.*)", url | ||
).groups() | ||
except AttributeError: | ||
raise ValueError("Not a valid bitbucket URL: {url}") | ||
return f"https://{api_netloc}/2.0/repositories/{user}/{repo}/src/{path}" | ||
|
||
|
||
def get_auth_header(username: str, password: str) -> Dict[str, str]: | ||
base64string = base64.b64encode(f"{username}:{password}".encode()) | ||
return {"Authorization": f"Basic {base64string.decode()}"} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
from arche.tools import bitbucket | ||
import pytest | ||
|
||
|
||
urls = [ | ||
( | ||
"https://bitbucket.org/scrapinghub/customer/src/master/customer/schemas/ecommerce.json", | ||
"https://api.bitbucket.org/2.0/repositories/scrapinghub/customer/src/master" | ||
"/customer/schemas/ecommerce.json", | ||
), | ||
( | ||
"https://bitbucket.org/scrapinghub/customer/raw/" | ||
"9c4b0bf46f2012ab38bc066e1ebe774d72856013/customer/schemas/ecommerce.json", | ||
"https://api.bitbucket.org/2.0/repositories/scrapinghub/customer/src/" | ||
"9c4b0bf46f2012ab38bc066e1ebe774d72856013/customer/schemas/ecommerce.json", | ||
), | ||
] | ||
|
||
|
||
@pytest.mark.parametrize( | ||
["url", "expected"], | ||
[ | ||
( | ||
"https://bitbucket.org/scrapinghub/customer/src/master/customer/schemas/" | ||
"ecommerce.json", | ||
"https://api.bitbucket.org/2.0/repositories/scrapinghub/customer/src/" | ||
"master/customer/schemas/ecommerce.json", | ||
), | ||
( | ||
"https://bitbucket.org/scrapinghub/customer/raw/" | ||
"9c4b0bf46f2012ab38bc066e1ebe774d72856013/customer/schemas/" | ||
"ecommerce.json", | ||
"https://api.bitbucket.org/2.0/repositories/scrapinghub/customer/src/" | ||
"9c4b0bf46f2012ab38bc066e1ebe774d72856013/customer/schemas/" | ||
"ecommerce.json", | ||
), | ||
], | ||
) | ||
def test_convert_to_api_url(url, expected): | ||
api_url = bitbucket.convert_to_api_url(url, bitbucket.NETLOC, bitbucket.API_NETLOC) | ||
assert api_url == expected | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"url", | ||
[ | ||
"https://bitbucket.org/ecommerce.json", | ||
"https://bitbucket.org/user/ecommerce.json", | ||
"https://bitbucket.org/user/repo/ecommerce.json", | ||
"https://bitbucket.org/user/repo/foobar/ecommerce.json", | ||
], | ||
) | ||
def test_convert_to_api_url_using_an_invalid_url(url): | ||
with pytest.raises(ValueError): | ||
bitbucket.convert_to_api_url(url, bitbucket.NETLOC, bitbucket.API_NETLOC) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"credentials,expected", | ||
[(("foo", "bar"), "Zm9vOmJhcg=="), (("alice", "secret"), "YWxpY2U6c2VjcmV0")], | ||
) | ||
def test_get_auth_header(credentials, expected): | ||
assert bitbucket.get_auth_header(*credentials) == { | ||
"Authorization": f"Basic {expected}" | ||
} | ||
|
||
|
||
def test_prepare_request(): | ||
bitbucket.USER = "foo" | ||
bitbucket.PASS = "bar" | ||
|
||
url = ( | ||
"https://bitbucket.org/scrapinghub/customer/src/master/customer/schemas/" | ||
"ecommerce.json" | ||
) | ||
req = bitbucket.prepare_request(url) | ||
|
||
assert "api.bitbucket.org" == req.host | ||
assert "Authorization" in req.headers | ||
|
||
|
||
def test_prepare_request_raises_an_error_when_no_credentials_found(): | ||
bitbucket.USER = bitbucket.PASS = None | ||
|
||
with pytest.raises(ValueError): | ||
bitbucket.prepare_request("foo") |
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