diff --git a/devops.py b/devops.py index 7da2a38..a14f2b5 100644 --- a/devops.py +++ b/devops.py @@ -20,7 +20,7 @@ def auto_properties(mapping: dict[str,str] | None = None): pass # Fetch repository details from the API if it is set to None or not existing - self._get_attributes() + self._get() return getattr(self, private_var) return property(fget=getter) @@ -76,10 +76,10 @@ class DevOps(): r.raise_for_status() # Ensure we raise an error for bad responses return r - def _get_entity_attributes(self, key_name: str, get_url: str, params: dict = {}) -> object: + def _get_entity(self, key_name: str, get_url: str, params: dict = {}) -> object: """ Each entity class can use this method to populate its attributes, by defining - its own _get_attributes method that calls this one with the key name, + its own _get method that calls this one with the key name, and the URL. """ r = self._get_url_path(get_url, params=params) # Fetch the object data from the URL @@ -121,8 +121,8 @@ class Organization(DevOps): "description": "description" }) class Project(DevOps): - def _get_attributes(self): - self._get_entity_attributes( + def _get(self): + self._get_entity( key_name="id", get_url=f"_apis/projects/{self._id}" ) @@ -162,8 +162,8 @@ class Project(DevOps): "web_url": "webUrl" }) class Repository(DevOps): - def _get_attributes(self): - self._get_entity_attributes( + def _get(self): + self._get_entity( key_name="id", get_url=f"{self._project.id}/_apis/git/repositories/{self._id}" ) @@ -177,7 +177,7 @@ class Repository(DevOps): UUID(id) # Check if it's a valid UUID except ValueError: # Called with a repository name, fetch by name - self._get_attributes() + self._get() if kwargs: raise ValueError("Automatic properties cannot be set when retrieving by name.") return @@ -189,6 +189,10 @@ class Repository(DevOps): def id(self): return self._id + @property + def project(self): + return self._project + def __str__(self): return f"Repository(name={self.name}, id={self._id})" @@ -219,6 +223,16 @@ class Item(DevOps): self._path = path self.set_auto_properties(**kwargs) # set properties defined in decorator + def _get(self): + self._get_entity( + key_name="path", + get_url=f"{self._repository._project.id}/_apis/git/repositories/{self._repository.id}/items", + params={ + "path": self._path, + "$format": "json" + } + ) + @property def path(self): - return self._path \ No newline at end of file + return self._path diff --git a/harvester.py b/harvester.py index a0212f0..d1c4a80 100755 --- a/harvester.py +++ b/harvester.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 -from devops import Organization, Project, Repository, DEVOPS_SCOPE +from devops import Organization, Project, Repository, Item, DEVOPS_SCOPE from azure.identity import DefaultAzureCredential from json import dumps @@ -30,3 +30,11 @@ print(f"Listing items in the {repo.name} repository:") for item in repo.items: print(f"{item.path} ({item.git_object_type}): {item.commit_id}") print() + +print("Getting specific item details:") +item = Item(repo, path="/generate-pat.py") +print(f"Item path: {item.path}") +print(f"Item type: {item.git_object_type}") +print(f"Item commit ID: {item.commit_id}") +print(f"Item URL: {item.url}") +print()