Renamed _get_attributes to _get and implemented for Item.
This commit is contained in:
		
							
								
								
									
										30
									
								
								devops.py
									
									
									
									
									
								
							
							
						
						
									
										30
									
								
								devops.py
									
									
									
									
									
								
							@@ -20,7 +20,7 @@ def auto_properties(mapping: dict[str,str] | None = None):
 | 
				
			|||||||
                pass
 | 
					                pass
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            # Fetch repository details from the API if it is set to None or not existing
 | 
					            # 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 getattr(self, private_var)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return property(fget=getter)
 | 
					        return property(fget=getter)
 | 
				
			||||||
@@ -76,10 +76,10 @@ class DevOps():
 | 
				
			|||||||
        r.raise_for_status() # Ensure we raise an error for bad responses
 | 
					        r.raise_for_status() # Ensure we raise an error for bad responses
 | 
				
			||||||
        return r
 | 
					        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
 | 
					        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.
 | 
					        and the URL.
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
        r = self._get_url_path(get_url, params=params) # Fetch the object data from 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"
 | 
					    "description": "description"
 | 
				
			||||||
    })
 | 
					    })
 | 
				
			||||||
class Project(DevOps):
 | 
					class Project(DevOps):
 | 
				
			||||||
    def _get_attributes(self):
 | 
					    def _get(self):
 | 
				
			||||||
        self._get_entity_attributes(
 | 
					        self._get_entity(
 | 
				
			||||||
            key_name="id",
 | 
					            key_name="id",
 | 
				
			||||||
            get_url=f"_apis/projects/{self._id}"
 | 
					            get_url=f"_apis/projects/{self._id}"
 | 
				
			||||||
        )
 | 
					        )
 | 
				
			||||||
@@ -162,8 +162,8 @@ class Project(DevOps):
 | 
				
			|||||||
    "web_url": "webUrl"
 | 
					    "web_url": "webUrl"
 | 
				
			||||||
    })
 | 
					    })
 | 
				
			||||||
class Repository(DevOps):
 | 
					class Repository(DevOps):
 | 
				
			||||||
    def _get_attributes(self):
 | 
					    def _get(self):
 | 
				
			||||||
        self._get_entity_attributes(
 | 
					        self._get_entity(
 | 
				
			||||||
            key_name="id",
 | 
					            key_name="id",
 | 
				
			||||||
            get_url=f"{self._project.id}/_apis/git/repositories/{self._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
 | 
					            UUID(id) # Check if it's a valid UUID
 | 
				
			||||||
        except ValueError:
 | 
					        except ValueError:
 | 
				
			||||||
            # Called with a repository name, fetch by name
 | 
					            # Called with a repository name, fetch by name
 | 
				
			||||||
            self._get_attributes()
 | 
					            self._get()
 | 
				
			||||||
            if kwargs:
 | 
					            if kwargs:
 | 
				
			||||||
                raise ValueError("Automatic properties cannot be set when retrieving by name.")
 | 
					                raise ValueError("Automatic properties cannot be set when retrieving by name.")
 | 
				
			||||||
            return
 | 
					            return
 | 
				
			||||||
@@ -189,6 +189,10 @@ class Repository(DevOps):
 | 
				
			|||||||
    def id(self):
 | 
					    def id(self):
 | 
				
			||||||
        return self._id
 | 
					        return self._id
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @property
 | 
				
			||||||
 | 
					    def project(self):
 | 
				
			||||||
 | 
					        return self._project
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __str__(self):
 | 
					    def __str__(self):
 | 
				
			||||||
        return f"Repository(name={self.name}, id={self._id})"
 | 
					        return f"Repository(name={self.name}, id={self._id})"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -219,6 +223,16 @@ class Item(DevOps):
 | 
				
			|||||||
        self._path = path
 | 
					        self._path = path
 | 
				
			||||||
        self.set_auto_properties(**kwargs) # set properties defined in decorator
 | 
					        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
 | 
					    @property
 | 
				
			||||||
    def path(self):
 | 
					    def path(self):
 | 
				
			||||||
        return self._path
 | 
					        return self._path
 | 
				
			||||||
							
								
								
									
										10
									
								
								harvester.py
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								harvester.py
									
									
									
									
									
								
							@@ -1,6 +1,6 @@
 | 
				
			|||||||
#!/usr/bin/env python3
 | 
					#!/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 azure.identity import DefaultAzureCredential
 | 
				
			||||||
from json import dumps
 | 
					from json import dumps
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -30,3 +30,11 @@ print(f"Listing items in the {repo.name} repository:")
 | 
				
			|||||||
for item in repo.items:
 | 
					for item in repo.items:
 | 
				
			||||||
    print(f"{item.path} ({item.git_object_type}): {item.commit_id}")
 | 
					    print(f"{item.path} ({item.git_object_type}): {item.commit_id}")
 | 
				
			||||||
print()
 | 
					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()
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user