Add branch support to Item class and enhance get_content method
This commit is contained in:
31
sk/devops.py
31
sk/devops.py
@@ -243,8 +243,16 @@ class Item():
|
||||
def __init__(self, repository: Repository, **kwargs):
|
||||
self._repository = repository
|
||||
self.from_args(**kwargs) # type: ignore[attr-defined]
|
||||
if "branch" in kwargs:
|
||||
self._branch = kwargs.get("branch")
|
||||
log_entity_creation(log, Item, self.path)
|
||||
|
||||
@property
|
||||
def branch(self):
|
||||
if hasattr(self, "_branch"):
|
||||
return getattr(self, "_branch")
|
||||
return None
|
||||
|
||||
def get_auto_properties(self):
|
||||
r = self._repository._project.organization.get_path(
|
||||
path=f"{self._repository._project.id}/_apis/git/repositories/{self._repository.id}/items",
|
||||
@@ -256,17 +264,28 @@ class Item():
|
||||
)
|
||||
self.from_json(r.json()) # type: ignore[attr-defined]
|
||||
|
||||
def get_content(self) -> bytes:
|
||||
"""Get the content of the item if it is a file."""
|
||||
def get_content(self, branch: str | None = None, commit: str | None = None, tag: str | None = None) -> bytes:
|
||||
"""Get the content of the item with optional branch, commit, or tag."""
|
||||
if self.git_object_type != "blob": # type: ignore[attr-defined]
|
||||
raise ValueError("Content can only be fetched for blob items.")
|
||||
|
||||
params = { "path": self.path, "recursionLevel": "none" }
|
||||
if self.branch and branch is None:
|
||||
branch = self.branch
|
||||
|
||||
if branch:
|
||||
params["version"] = branch
|
||||
params["versionType"] = "branch"
|
||||
elif tag:
|
||||
params["version"] = tag
|
||||
params["versionType"] = "tag"
|
||||
elif commit:
|
||||
params["version"] = commit
|
||||
params["versionType"] = "commit"
|
||||
|
||||
r = self._repository._project.organization.get_path(
|
||||
path=f"{self._repository._project.id}/_apis/git/repositories/{self._repository.id}/items",
|
||||
params={
|
||||
"path": self.path,
|
||||
"recursionLevel": "none"
|
||||
}
|
||||
params=params
|
||||
)
|
||||
return r.content
|
||||
|
||||
|
||||
Reference in New Issue
Block a user