Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: extend gitlab to edit a previous release if exists#934

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
8 commits
Select commitHold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletionssemantic_release/hvcs/github.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -304,7 +304,7 @@ def edit_release_notes(self, release_id: int, release_notes: str) -> int:
"""
Edit a release with updated change notes
https://docs.github.com/rest/reference/repos#update-a-release
:paramid: ID of release to update
:paramrelease_id: ID of release to update
:param release_notes: The release notes for this version
:return: The ID of the release that was edited
"""
Expand All@@ -329,8 +329,9 @@ def create_or_update_release(
) -> int:
"""
Post release changelog
:paramversion: The version number
:paramtag: The version number
:param release_notes: The release notes for this version
:param prerelease: Whether or not this release should be created as a prerelease
:return: The status of the request
"""
log.info("Creating release for %s", tag)
Expand DownExpand Up@@ -427,8 +428,8 @@ def upload_release_asset(
def upload_dists(self, tag: str, dist_glob: str) -> int:
"""
Upload distributions to a release
:paramversion: Version to upload for
:parampath: Path to the dist directory
:paramtag: Version to upload for
:paramdist_glob: Path to the dist directory
:return: The number of distributions successfully uploaded
"""
# Find the release corresponding to this version
Expand Down
154 changes: 127 additions & 27 deletionssemantic_release/hvcs/gitlab.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -9,14 +9,21 @@
from typing import TYPE_CHECKING

import gitlab
import gitlab.exceptions
import gitlab.v4
import gitlab.v4.objects
from urllib3.util.url import Url, parse_url

from semantic_release.errors import UnexpectedResponse
from semantic_release.helpers import logged_function
from semantic_release.hvcs.remote_hvcs_base import RemoteHvcsBase
from semantic_release.hvcs.util import suppress_not_found

if TYPE_CHECKING:
from typing import Any, Callable

from gitlab.v4.objects import Project as GitLabProject


log = logging.getLogger(__name__)

Expand DownExpand Up@@ -45,6 +52,8 @@ def __init__(
) -> None:
super().__init__(remote_url)
self.token = token
self.project_namespace = f"{self.owner}/{self.repo_name}"
self._project: GitLabProject | None = None

domain_url = self._normalize_url(
hvcs_domain
Expand All@@ -63,8 +72,14 @@ def __init__(
).url.rstrip("/")
)

self._client = gitlab.Gitlab(self.hvcs_domain.url)
self._api_url = parse_url(self._client.url)
self._client = gitlab.Gitlab(self.hvcs_domain.url, private_token=self.token)
self._api_url = parse_url(self._client.api_url)

@property
def project(self) -> GitLabProject:
if self._project is None:
self._project = self._client.projects.get(self.project_namespace)
return self._project

@lru_cache(maxsize=1)
def _get_repository_owner_and_name(self) -> tuple[str, str]:
Expand All@@ -87,61 +102,146 @@ def create_release(
assets: list[str] | None = None, # noqa: ARG002
) -> str:
"""
Post release changelog
:param tag: Tag to create release for
:param release_notes: The release notes for this version
:param prerelease: This parameter has no effect
:return: The tag of the release
Create a release in a remote VCS, adding any release notes and assets to it

Arguments:
---------
tag(str): The tag to create the release for
release_notes(str): The changelog description for this version only
prerelease(bool): This parameter has no effect in GitLab
assets(list[str]): A list of paths to files to upload as assets (TODO: not implemented)

Returns:
-------
str: The tag of the release

Raises:
------
GitlabAuthenticationError: If authentication is not correct
GitlabCreateError: If the server cannot perform the request

"""
client = gitlab.Gitlab(self.hvcs_domain.url, private_token=self.token)
client.auth()
log.info("Creating release for %s", tag)
# ref: https://docs.gitlab.com/ee/api/releases/index.html#create-a-release
client.projects.get(self.owner + "/" + self.repo_name).releases.create(
self.project.releases.create(
{
"name":"Release " +tag,
"name": tag,
"tag_name": tag,
"tag_message": tag,
"description": release_notes,
}
)
log.info("Successfully created release for %s", tag)
return tag

# TODO: make str types accepted here
@logged_function(log)
@suppress_not_found
def get_release_by_tag(self, tag: str) -> gitlab.v4.objects.ProjectRelease | None:
"""
Get a release by its tag name

Arguments:
---------
tag(str): The tag name to get the release for

Returns:
-------
gitlab.v4.objects.ProjectRelease | None: The release object or None if not found

Raises:
------
gitlab.exceptions.GitlabAuthenticationError: If the user is not authenticated

"""
try:
return self.project.releases.get(tag)
except gitlab.exceptions.GitlabGetError:
log.debug("Release %s not found", tag)
return None
except KeyError as err:
raise UnexpectedResponse("JSON response is missing commit.id") from err

@logged_function(log)
def edit_release_notes( # type: ignore[override]
self,
release_id: str,
release: gitlab.v4.objects.ProjectRelease,
release_notes: str,
) -> str:
client = gitlab.Gitlab(self.hvcs_domain.url, private_token=self.token)
client.auth()
log.info("Updating release %s", release_id)
"""
Update the release notes for a given release

client.projects.get(self.owner + "/" + self.repo_name).releases.update(
release_id,
{
"description": release_notes,
},
Arguments:
---------
release(gitlab.v4.objects.ProjectRelease): The release object to update
release_notes(str): The new release notes

Returns:
-------
str: The release id

Raises:
------
GitlabAuthenticationError: If authentication is not correct
GitlabUpdateError: If the server cannot perform the request

"""
log.info(
"Updating release %s [%s]",
release.name,
release.attributes.get("commit", {}).get("id"),
)
return release_id
release.description = release_notes
release.save()
return str(release.get_id())

@logged_function(log)
def create_or_update_release(
self, tag: str, release_notes: str, prerelease: bool = False
) -> str:
"""
Create or update a release for the given tag in a remote VCS

Arguments:
---------
tag(str): The tag to create or update the release for
release_notes(str): The changelog description for this version only
prerelease(bool): This parameter has no effect in GitLab

Returns:
-------
str: The release id

Raises:
------
ValueError: If the release could not be created or updated
gitlab.exceptions.GitlabAuthenticationError: If the user is not authenticated
GitlabUpdateError: If the server cannot perform the request

"""
try:
return self.create_release(
tag=tag, release_notes=release_notes, prerelease=prerelease
)
except gitlab.GitlabCreateError:
log.info(
"Release%s could not be created for project%s/%s",
"New release%s could not be created for project %s",
tag,
self.owner,
self.repo_name,
self.project_namespace,
)

if (release_obj := self.get_release_by_tag(tag)) is None:
raise ValueError(
f"release for tag {tag} could not be found, and could not be created"
)
return self.edit_release_notes(release_id=tag, release_notes=release_notes)

log.debug(
"Found existing release commit %s, updating", release_obj.commit.get("id")
)
# If this errors we let it die
return self.edit_release_notes(
release=release_obj,
release_notes=release_notes,
)

def remote_url(self, use_token: bool = True) -> str:
"""Get the remote url including the token for authentication if requested"""
Expand All@@ -150,7 +250,7 @@ def remote_url(self, use_token: bool = True) -> str:

return self.create_server_url(
auth=f"gitlab-ci-token:{self.token}",
path=f"{self.owner}/{self.repo_name}.git",
path=f"{self.project_namespace}.git",
)

def compare_url(self, from_rev: str, to_rev: str) -> str:
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp