1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
|
import base64
from typing import (
Any,
Callable,
cast,
Dict,
Iterator,
List,
Optional,
TYPE_CHECKING,
Union,
)
import requests
from gitlab import cli
from gitlab import exceptions as exc
from gitlab import utils
from gitlab.base import RESTManager, RESTObject
from gitlab.mixins import (
CreateMixin,
DeleteMixin,
GetMixin,
ObjectDeleteMixin,
SaveMixin,
UpdateMixin,
)
from gitlab.types import RequiredOptional
__all__ = [
"ProjectFile",
"ProjectFileManager",
]
class ProjectFile(SaveMixin, ObjectDeleteMixin, RESTObject):
_id_attr = "file_path"
_repr_attr = "file_path"
branch: str
commit_message: str
file_path: str
manager: "ProjectFileManager"
def decode(self) -> bytes:
"""Returns the decoded content of the file.
Returns:
The decoded content.
"""
return base64.b64decode(self.content)
# NOTE(jlvillal): Signature doesn't match SaveMixin.save() so ignore
# type error
def save( # type: ignore
self, branch: str, commit_message: str, **kwargs: Any
) -> None:
"""Save the changes made to the file to the server.
The object is updated to match what the server returns.
Args:
branch: Branch in which the file will be updated
commit_message: Message to send with the commit
**kwargs: Extra options to send to the server (e.g. sudo)
Raises:
GitlabAuthenticationError: If authentication is not correct
GitlabUpdateError: If the server cannot perform the request
"""
self.branch = branch
self.commit_message = commit_message
self.file_path = utils.EncodedId(self.file_path)
super().save(**kwargs)
@exc.on_http_error(exc.GitlabDeleteError)
# NOTE(jlvillal): Signature doesn't match DeleteMixin.delete() so ignore
# type error
def delete( # type: ignore
self, branch: str, commit_message: str, **kwargs: Any
) -> None:
"""Delete the file from the server.
Args:
branch: Branch from which the file will be removed
commit_message: Commit message for the deletion
**kwargs: Extra options to send to the server (e.g. sudo)
Raises:
GitlabAuthenticationError: If authentication is not correct
GitlabDeleteError: If the server cannot perform the request
"""
file_path = self.encoded_id
if TYPE_CHECKING:
assert isinstance(file_path, str)
self.manager.delete(file_path, branch, commit_message, **kwargs)
class ProjectFileManager(GetMixin, CreateMixin, UpdateMixin, DeleteMixin, RESTManager):
_path = "/projects/{project_id}/repository/files"
_obj_cls = ProjectFile
_from_parent_attrs = {"project_id": "id"}
_create_attrs = RequiredOptional(
required=("file_path", "branch", "content", "commit_message"),
optional=("encoding", "author_email", "author_name"),
)
_update_attrs = RequiredOptional(
required=("file_path", "branch", "content", "commit_message"),
optional=("encoding", "author_email", "author_name"),
)
@cli.register_custom_action("ProjectFileManager", ("file_path", "ref"))
# NOTE(jlvillal): Signature doesn't match UpdateMixin.update() so ignore
# type error
def get( # type: ignore
self, file_path: str, ref: str, **kwargs: Any
) -> ProjectFile:
"""Retrieve a single file.
Args:
file_path: Path of the file to retrieve
ref: Name of the branch, tag or commit
**kwargs: Extra options to send to the server (e.g. sudo)
Raises:
GitlabAuthenticationError: If authentication is not correct
GitlabGetError: If the file could not be retrieved
Returns:
The generated RESTObject
"""
return cast(ProjectFile, GetMixin.get(self, file_path, ref=ref, **kwargs))
@cli.register_custom_action(
"ProjectFileManager",
("file_path", "branch", "content", "commit_message"),
("encoding", "author_email", "author_name"),
)
@exc.on_http_error(exc.GitlabCreateError)
def create(
self, data: Optional[Dict[str, Any]] = None, **kwargs: Any
) -> ProjectFile:
"""Create a new object.
Args:
data: parameters to send to the server to create the
resource
**kwargs: Extra options to send to the server (e.g. sudo)
Returns:
a new instance of the managed object class built with
the data sent by the server
Raises:
GitlabAuthenticationError: If authentication is not correct
GitlabCreateError: If the server cannot perform the request
"""
if TYPE_CHECKING:
assert data is not None
self._create_attrs.validate_attrs(data=data)
new_data = data.copy()
file_path = utils.EncodedId(new_data.pop("file_path"))
path = f"{self.path}/{file_path}"
server_data = self.gitlab.http_post(path, post_data=new_data, **kwargs)
if TYPE_CHECKING:
assert isinstance(server_data, dict)
return self._obj_cls(self, server_data)
@exc.on_http_error(exc.GitlabUpdateError)
# NOTE(jlvillal): Signature doesn't match UpdateMixin.update() so ignore
# type error
def update( # type: ignore
self, file_path: str, new_data: Optional[Dict[str, Any]] = None, **kwargs: Any
) -> Dict[str, Any]:
"""Update an object on the server.
Args:
id: ID of the object to update (can be None if not required)
new_data: the update data for the object
**kwargs: Extra options to send to the server (e.g. sudo)
Returns:
The new object data (*not* a RESTObject)
Raises:
GitlabAuthenticationError: If authentication is not correct
GitlabUpdateError: If the server cannot perform the request
"""
new_data = new_data or {}
data = new_data.copy()
file_path = utils.EncodedId(file_path)
data["file_path"] = file_path
path = f"{self.path}/{file_path}"
self._update_attrs.validate_attrs(data=data)
result = self.gitlab.http_put(path, post_data=data, **kwargs)
if TYPE_CHECKING:
assert isinstance(result, dict)
return result
@cli.register_custom_action(
"ProjectFileManager", ("file_path", "branch", "commit_message")
)
@exc.on_http_error(exc.GitlabDeleteError)
# NOTE(jlvillal): Signature doesn't match DeleteMixin.delete() so ignore
# type error
def delete( # type: ignore
self, file_path: str, branch: str, commit_message: str, **kwargs: Any
) -> None:
"""Delete a file on the server.
Args:
file_path: Path of the file to remove
branch: Branch from which the file will be removed
commit_message: Commit message for the deletion
**kwargs: Extra options to send to the server (e.g. sudo)
Raises:
GitlabAuthenticationError: If authentication is not correct
GitlabDeleteError: If the server cannot perform the request
"""
file_path = utils.EncodedId(file_path)
path = f"{self.path}/{file_path}"
data = {"branch": branch, "commit_message": commit_message}
self.gitlab.http_delete(path, query_data=data, **kwargs)
@cli.register_custom_action("ProjectFileManager", ("file_path", "ref"))
@exc.on_http_error(exc.GitlabGetError)
def raw(
self,
file_path: str,
ref: str,
streamed: bool = False,
action: Optional[Callable[..., Any]] = None,
chunk_size: int = 1024,
*,
iterator: bool = False,
**kwargs: Any,
) -> Optional[Union[bytes, Iterator[Any]]]:
"""Return the content of a file for a commit.
Args:
ref: ID of the commit
filepath: Path of the file to return
streamed: If True the data will be processed by chunks of
`chunk_size` and each chunk is passed to `action` for
treatment
iterator: If True directly return the underlying response
iterator
action: Callable responsible of dealing with chunk of
data
chunk_size: Size of each chunk
**kwargs: Extra options to send to the server (e.g. sudo)
Raises:
GitlabAuthenticationError: If authentication is not correct
GitlabGetError: If the file could not be retrieved
Returns:
The file content
"""
file_path = utils.EncodedId(file_path)
path = f"{self.path}/{file_path}/raw"
query_data = {"ref": ref}
result = self.gitlab.http_get(
path, query_data=query_data, streamed=streamed, raw=True, **kwargs
)
if TYPE_CHECKING:
assert isinstance(result, requests.Response)
return utils.response_content(
result, streamed, action, chunk_size, iterator=iterator
)
@cli.register_custom_action("ProjectFileManager", ("file_path", "ref"))
@exc.on_http_error(exc.GitlabListError)
def blame(self, file_path: str, ref: str, **kwargs: Any) -> List[Dict[str, Any]]:
"""Return the content of a file for a commit.
Args:
file_path: Path of the file to retrieve
ref: Name of the branch, tag or commit
**kwargs: Extra options to send to the server (e.g. sudo)
Raises:
GitlabAuthenticationError: If authentication is not correct
GitlabListError: If the server failed to perform the request
Returns:
A list of commits/lines matching the file
"""
file_path = utils.EncodedId(file_path)
path = f"{self.path}/{file_path}/blame"
query_data = {"ref": ref}
result = self.gitlab.http_list(path, query_data, **kwargs)
if TYPE_CHECKING:
assert isinstance(result, list)
return result
|