build_manifest.py: Avoid duplicating some code

This commit is contained in:
Jordan Petridis 2018-10-25 14:13:12 +03:00
parent 07a1480fc9
commit f4d6e7e9c6
No known key found for this signature in database
GPG key ID: 902CC06D159744F5

View file

@ -54,8 +54,7 @@ def request(path: str) -> List[Dict[str, str]]:
return request_raw(path, token, project_url) return request_raw(path, token, project_url)
def get_project_branch(project_id: int, name: str) -> Dict[str, str]: def request_wrap(path: str) -> List[Dict[str, str]]:
path = f"projects/{project_id}/repository/branches?search={name}"
resp: List[Dict[str, str]] = request(path) resp: List[Dict[str, str]] = request(path)
if not resp: if not resp:
@ -69,6 +68,11 @@ def get_project_branch(project_id: int, name: str) -> Dict[str, str]:
return resp[0] return resp[0]
def get_project_branch(project_id: int, name: str) -> Dict[str, str]:
path = f"projects/{project_id}/repository/branches?search={name}"
return request_wrap(path)
def test_get_project_branch(): def test_get_project_branch():
id = 1353 id = 1353
os.environ["CI_JOB_TOKEN"] = "xxxxxxxxxxxxxxxxxxxx" os.environ["CI_JOB_TOKEN"] = "xxxxxxxxxxxxxxxxxxxx"
@ -92,17 +96,7 @@ def test_get_project_branch():
# Documentation: https://docs.gitlab.com/ce/api/projects.html#list-user-projects # Documentation: https://docs.gitlab.com/ce/api/projects.html#list-user-projects
def search_user_namespace(user: str, project: str) -> Dict[str, str]: def search_user_namespace(user: str, project: str) -> Dict[str, str]:
path = f"/users/{user}/projects?search={project}" path = f"/users/{user}/projects?search={project}"
resp: List[Dict[str, str]] = request(path) return request_wrap(path)
if not resp:
return None
if not resp[0]:
return None
# Not sure if there will be any edge cases where it returns more than one
# so lets see if anyone complains
assert len(resp) == 1
return resp[0]
def test_search_user_namespace(): def test_search_user_namespace():
@ -124,14 +118,7 @@ def test_search_user_namespace():
# Documentation: https://docs.gitlab.com/ee/api/search.html#group-search-api # Documentation: https://docs.gitlab.com/ee/api/search.html#group-search-api
def search_group_namespace(group_id: str, project: str) -> Dict[str, str]: def search_group_namespace(group_id: str, project: str) -> Dict[str, str]:
path = f"groups/{group_id}/search?scope=projects&search={project}" path = f"groups/{group_id}/search?scope=projects&search={project}"
resp: List[Dict[str, str]] = request(path) return request_wrap(path)
if not resp:
return None
if not resp[0]:
return None
return resp[0]
def test_search_group_namespace(): def test_search_group_namespace():