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(files): allow decoding project files directly to string#2396

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

Open
nejch wants to merge1 commit intomain
base:main
Choose a base branch
Loading
fromfeat/decode-to-string
Open
Show file tree
Hide file tree
Changes fromall commits
Commits
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
5 changes: 4 additions & 1 deletiondocs/gl_objects/projects.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -434,9 +434,12 @@ Get a file::
# get the base64 encoded content
print(f.content)

# get the decoded content
# get the decoded content as bytes
print(f.decode())

# get the decoded content as a string
print(f.decode("utf-8"))

Get file details from headers, without fetching its entire content::

headers = project.files.head('README.rst', ref='main')
Expand Down
22 changes: 20 additions & 2 deletionsgitlab/v4/objects/files.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,6 +7,7 @@
Iterator,
List,
Optional,
overload,
TYPE_CHECKING,
Union,
)
Expand DownExpand Up@@ -41,13 +42,30 @@ class ProjectFile(SaveMixin, ObjectDeleteMixin, RESTObject):
file_path: str
manager: "ProjectFileManager"

@overload
def decode(self) -> bytes:
...

@overload
def decode(self, encoding: None) -> bytes:
...

@overload
def decode(self, encoding: str) -> str:
...

def decode(self, encoding: Optional[str] = None) -> Union[bytes, str]:
"""Returns the decoded content of the file.

Returns:
The decoded content.
The decoded content as bytes.
The decoded content as string if a valid encoding is provided.
"""
return base64.b64decode(self.content)
decoded_bytes = base64.b64decode(self.content)

if encoding is not None:
return decoded_bytes.decode(encoding)
return decoded_bytes

# NOTE(jlvillal): Signature doesn't match SaveMixin.save() so ignore
# type error
Expand Down
6 changes: 3 additions & 3 deletionstests/functional/api/test_repository.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -36,9 +36,9 @@ def test_repository_files(project):
}
)
readme = project.files.get(file_path="README.rst", ref="main")
# The first decode() is the ProjectFile method, the second one is the bytes
# object method
assert readme.decode().decode() == "Initial content"

assert readme.decode() == b"Initial content"
assert readme.decode("utf-8") == "Initial content"

headers = project.files.head("README.rst", ref="main")
assert headers["X-Gitlab-File-Path"] == "README.rst"
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp