Implemented Item class (partially).
This commit is contained in:
		
							
								
								
									
										76
									
								
								devops.py
									
									
									
									
									
								
							
							
						
						
									
										76
									
								
								devops.py
									
									
									
									
									
								
							@@ -1,7 +1,5 @@
 | 
			
		||||
import requests
 | 
			
		||||
import urllib.request
 | 
			
		||||
import urllib.parse
 | 
			
		||||
import json
 | 
			
		||||
from uuid import UUID
 | 
			
		||||
 | 
			
		||||
DEVOPS_SCOPE = "https://app.vssps.visualstudio.com/.default"
 | 
			
		||||
@@ -25,6 +23,7 @@ def auto_properties(names=None):
 | 
			
		||||
        return property(getter)
 | 
			
		||||
 | 
			
		||||
    def decorator(cls):
 | 
			
		||||
        cls.__auto_properties__ = names
 | 
			
		||||
        for name in names:
 | 
			
		||||
            setattr(cls, name, make_property(name))
 | 
			
		||||
        return cls
 | 
			
		||||
@@ -46,12 +45,12 @@ class Organization:
 | 
			
		||||
        r = requests.get(url=url, params=request_parameters, headers={
 | 
			
		||||
            "Authorization": f"Bearer {self._token}"
 | 
			
		||||
        })
 | 
			
		||||
        r.raise_for_status() # Ensure we raise an error for bad responses
 | 
			
		||||
        return r
 | 
			
		||||
 | 
			
		||||
    @property
 | 
			
		||||
    def projects(self):
 | 
			
		||||
        r = self.get("_apis/projects")
 | 
			
		||||
        r.raise_for_status()
 | 
			
		||||
 | 
			
		||||
        # Return a list of Project instances
 | 
			
		||||
        projects_data = r.json().get("value", [])
 | 
			
		||||
@@ -87,8 +86,7 @@ class Project:
 | 
			
		||||
            self._description = description if description is not None else "" # Ensure description is a string
 | 
			
		||||
        else:
 | 
			
		||||
            # Fetch project details from the API
 | 
			
		||||
            r = org.get(f"_apis/projects/{urllib.parse.quote(self._id)}")
 | 
			
		||||
            r.raise_for_status()
 | 
			
		||||
            r = org.get(f"_apis/projects/{self._id}")
 | 
			
		||||
            proj_data = r.json()
 | 
			
		||||
            self._id = proj_data.get("id", "")
 | 
			
		||||
            self._name = proj_data.get("name", "")
 | 
			
		||||
@@ -98,10 +96,12 @@ class Project:
 | 
			
		||||
    def __str__(self):
 | 
			
		||||
        return f"Project(name={self._name}, id={self._id})"
 | 
			
		||||
 | 
			
		||||
    def get(self, path: str, params: dict = {}):
 | 
			
		||||
        return self._org.get(f"{self._id}/{path.lstrip('/')}", params=params)
 | 
			
		||||
 | 
			
		||||
    @property
 | 
			
		||||
    def repositories(self):
 | 
			
		||||
        r = self._org.get(f"{self._id}/_apis/git/repositories")
 | 
			
		||||
        r.raise_for_status()
 | 
			
		||||
        r = self.get(f"_apis/git/repositories")
 | 
			
		||||
 | 
			
		||||
        repos_data = r.json().get("value", [])
 | 
			
		||||
        # Return a list of Repository instances
 | 
			
		||||
@@ -122,15 +122,14 @@ class Project:
 | 
			
		||||
@auto_properties(names=["name", "url", "default_branch", "disabled", "in_maintenance", "remote_url", "ssh_url", "web_url"])
 | 
			
		||||
class Repository:
 | 
			
		||||
    def _get(self, repo_name: str):
 | 
			
		||||
        r = self._project._org.get(f"{self._project._id}/_apis/git/repositories/{urllib.parse.quote(repo_name)}")
 | 
			
		||||
        r.raise_for_status()
 | 
			
		||||
        r = self._project.get(f"_apis/git/repositories/{urllib.parse.quote(repo_name)}")
 | 
			
		||||
        json_data = r.json()
 | 
			
		||||
        self._id = json_data.get("id", "")
 | 
			
		||||
        self._name = json_data.get("name", "")
 | 
			
		||||
        self._url = json_data.get("url", "")
 | 
			
		||||
        self._default_branch = json_data.get("defaultBranch", "")
 | 
			
		||||
        self._disabled = json_data.get("isDisabled", False)
 | 
			
		||||
        self._in_maintenance = json_data.get("isInMaintenance", False)
 | 
			
		||||
        self._is_disabled = json_data.get("isDisabled", False)
 | 
			
		||||
        self._is_in_maintenance = json_data.get("isInMaintenance", False)
 | 
			
		||||
        self._remote_url = json_data.get("remoteUrl", "")
 | 
			
		||||
        self._ssh_url = json_data.get("sshUrl", "")
 | 
			
		||||
        self._web_url = json_data.get("webUrl", "")
 | 
			
		||||
@@ -161,11 +160,62 @@ class Repository:
 | 
			
		||||
        self._name = name if name is not None else None
 | 
			
		||||
        self._url = url if url is not None else None
 | 
			
		||||
        self._default_branch = default_branch if default_branch is not None else None
 | 
			
		||||
        self._disabled = disabled if disabled is not None else None
 | 
			
		||||
        self._in_maintenance = in_maintenance if in_maintenance is not None else None
 | 
			
		||||
        self._is_disabled = disabled if disabled is not None else None
 | 
			
		||||
        self._is_in_maintenance = in_maintenance if in_maintenance is not None else None
 | 
			
		||||
        self._remote_url = remote_url if remote_url is not None else None
 | 
			
		||||
        self._ssh_url = ssh_url if ssh_url is not None else None
 | 
			
		||||
        self._web_url = web_url if web_url is not None else None
 | 
			
		||||
 | 
			
		||||
    def __str__(self):
 | 
			
		||||
        return f"Repository(name={self._name}, id={self._id})"
 | 
			
		||||
 | 
			
		||||
    def _get_path(self, path: str, params: dict = {}):
 | 
			
		||||
        return self._project.get(f"_apis/git/repositories/{self._id}/{path.lstrip('/')}", params=params)
 | 
			
		||||
 | 
			
		||||
    def _get_items(self, scope_path: str = "/", recursion_level: str = "oneLevel"):
 | 
			
		||||
        r = self._get_path(f"items", params={"scopePath": scope_path, "recursionLevel": recursion_level})
 | 
			
		||||
        return r.json()
 | 
			
		||||
 | 
			
		||||
    @property
 | 
			
		||||
    def items(self):
 | 
			
		||||
        # GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/items?api-version=7.1
 | 
			
		||||
 | 
			
		||||
        items_data = self._get_items().get("value", [])
 | 
			
		||||
        # Return a list of Item instances
 | 
			
		||||
        return [
 | 
			
		||||
            Item(self,
 | 
			
		||||
                 path=item.get("path"),
 | 
			
		||||
                 object_id=item.get("objectId"),
 | 
			
		||||
                 git_object_type=item.get("gitObjectType"),
 | 
			
		||||
                 commit_id=item.get("commitId"),
 | 
			
		||||
                 is_folder=item.get("isFolder"),
 | 
			
		||||
                 url=item.get("url"),
 | 
			
		||||
                 ) for item in items_data
 | 
			
		||||
        ]
 | 
			
		||||
 | 
			
		||||
@auto_properties(names=["object_id", "git_object_type", "commit_id", "folder", "url"])
 | 
			
		||||
class Item:
 | 
			
		||||
    def _get(self, path):
 | 
			
		||||
        r = self._repository._get_path(f"items/{urllib.parse.quote(path)}")
 | 
			
		||||
 | 
			
		||||
        for name in self.__auto_properties__:
 | 
			
		||||
            setattr(self, f"_{name}", r.json().get(name, None))
 | 
			
		||||
 | 
			
		||||
    def __init__(self,
 | 
			
		||||
                 repository: Repository,
 | 
			
		||||
                 path: str,
 | 
			
		||||
                 object_id: str | None = None,
 | 
			
		||||
                 git_object_type: str | None = None,
 | 
			
		||||
                 commit_id: str | None = None,
 | 
			
		||||
                 is_folder: bool | None = None,
 | 
			
		||||
                 url: str | None = None
 | 
			
		||||
                 ):
 | 
			
		||||
 | 
			
		||||
        self._repository = repository
 | 
			
		||||
        self._path = path
 | 
			
		||||
 | 
			
		||||
        self._object_id = object_id if object_id is not None else None
 | 
			
		||||
        self._git_object_type = git_object_type if git_object_type is not None else None
 | 
			
		||||
        self._commit_id = commit_id if commit_id is not None else None
 | 
			
		||||
        self._is_folder = is_folder if is_folder is not None else None
 | 
			
		||||
        self._url = url if url is not None else None
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user