Skip to content

Commit

Permalink
fix: use release-api for gitlab
Browse files Browse the repository at this point in the history
  • Loading branch information
SecondSanny authored and relekang committed Jun 25, 2021
1 parent e9d2916 commit 1ef5cab
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions semantic_release/hvcs.py
Expand Up @@ -5,7 +5,7 @@
import os
from typing import Optional, Union

import gitlab
from gitlab import gitlab, exceptions
from requests import HTTPError, Session
from requests.auth import AuthBase
from urllib3 import Retry
Expand Down Expand Up @@ -138,7 +138,8 @@ def auth() -> Optional[TokenAuth]:
def session(
raise_for_status=True, retry: Union[Retry, bool, int] = True
) -> Session:
session = build_requests_session(raise_for_status=raise_for_status, retry=retry)
session = build_requests_session(
raise_for_status=raise_for_status, retry=retry)
session.auth = Github.auth()
return session

Expand All @@ -158,7 +159,8 @@ def check_build_status(owner: str, repo: str, ref: str) -> bool:
url = "{domain}/repos/{owner}/{repo}/commits/{ref}/status"
try:
response = Github.session().get(
url.format(domain=Github.api_url(), owner=owner, repo=repo, ref=ref)
url.format(domain=Github.api_url(),
owner=owner, repo=repo, ref=ref)
)
return response.json().get("state") == "success"
except HTTPError as e:
Expand Down Expand Up @@ -261,11 +263,13 @@ def post_release_changelog(
success = Github.create_release(owner, repo, tag, changelog)

if not success:
logger.debug("Unsuccessful, looking for an existing release to update")
logger.debug(
"Unsuccessful, looking for an existing release to update")
release_id = Github.get_release(owner, repo, tag)
if release_id:
logger.debug(f"Updating release {release_id}")
success = Github.edit_release(owner, repo, release_id, changelog)
success = Github.edit_release(
owner, repo, release_id, changelog)
else:
logger.debug(f"Existing release not found")

Expand Down Expand Up @@ -383,7 +387,8 @@ def check_build_status(owner: str, repo: str, ref: str) -> bool:
"""
gl = gitlab.Gitlab(Gitlab.api_url(), private_token=Gitlab.token())
gl.auth()
jobs = gl.projects.get(owner + "/" + repo).commits.get(ref).statuses.list()
jobs = gl.projects.get(
owner + "/" + repo).commits.get(ref).statuses.list()
for job in jobs:
if job["status"] not in ["success", "skipped"]:
if job["status"] == "pending":
Expand All @@ -392,7 +397,8 @@ def check_build_status(owner: str, repo: str, ref: str) -> bool:
)
return False
elif job["status"] == "failed" and not job["allow_failure"]:
logger.debug(f"check_build_status: job {job['name']} failed")
logger.debug(
f"check_build_status: job {job['name']} failed")
return False
return True

Expand All @@ -414,13 +420,11 @@ def post_release_changelog(
gl = gitlab.Gitlab(Gitlab.api_url(), private_token=Gitlab.token())
gl.auth()
try:
tag = gl.projects.get(owner + "/" + repo).tags.get(ref)
tag.set_release_description(changelog)
except gitlab.exceptions.GitlabGetError:
logger.debug(f"Tag {ref} was not found for project {owner}/{repo}")
return False
except gitlab.exceptions.GitlabUpdateError:
logger.debug(f"Failed to update tag {ref} for project {owner}/{repo}")
gl.projects.get(owner + "/" + repo).releases.create(
{'name': 'Release ' + version, 'tag_name': ref, 'description': changelog})
except exceptions.GitlabCreateError:
logger.debug(
f"Release {ref} could not be created for project {owner}/{repo}")
return False

return True
Expand All @@ -436,7 +440,8 @@ def get_hvcs() -> Base:
try:
return globals()[hvcs.capitalize()]
except KeyError:
raise ImproperConfigurationError('"{0}" is not a valid option for hvcs.')
raise ImproperConfigurationError(
'"{0}" is not a valid option for hvcs.')


def check_build_status(owner: str, repository: str, ref: str) -> bool:
Expand All @@ -462,7 +467,8 @@ def post_changelog(owner: str, repository: str, version: str, changelog: str) ->
:param changelog: A string with the changelog in correct format
:return: a tuple with success status and payload from hvcs
"""
logger.debug(f"Posting release changelog for {owner}/{repository} {version}")
logger.debug(
f"Posting release changelog for {owner}/{repository} {version}")
return get_hvcs().post_release_changelog(owner, repository, version, changelog)


Expand Down

0 comments on commit 1ef5cab

Please sign in to comment.