fix: remove mutable argument in team creation function

pull/32/head^2
Langenfeld 2024-08-22 16:58:59 +02:00
rodzic 65c763ae66
commit ead89d325b
2 zmienionych plików z 34 dodań i 16 usunięć

Wyświetl plik

@ -1,11 +1,35 @@
import logging import logging
from datetime import datetime from datetime import datetime
from typing import List, Tuple, Dict, Sequence, Optional, Union, Set from typing import List, Tuple, Dict, Sequence, Optional, Union, Set
from dataclasses import dataclass, fields
from .baseapiobject import ReadonlyApiObject, ApiObject from .baseapiobject import ReadonlyApiObject, ApiObject
from .exceptions import * from .exceptions import *
@dataclass(frozen=True)
class RepoUnits:
code: str = "none"
issues: str = "none"
ext_issues: str = "none"
wiki: str = "none"
pulls: str = "none"
releases: str = "none"
ext_wiki: str = "none"
def to_dict(self) -> dict[str, str]:
"""Return the correctly prefixed (added "repo.") representation for gitea Repository runit Rights"""
return {
f"repo.{field.name}": getattr(self, field.name) for field in fields(self)
}
@classmethod
def from_dict(cls, unit_dict: dict[str, str]) -> "RepoUnits":
"""Parse all known repo units from the dictionary returned by the api"""
return RepoUnits(
**{k[5:]: v for k, v in unit_dict.items() if k[5:] in fields(cls)}
)
class Organization(ApiObject): class Organization(ApiObject):
"""see https://try.gitea.io/api/swagger#/organization/orgGetAll""" """see https://try.gitea.io/api/swagger#/organization/orgGetAll"""
@ -119,10 +143,12 @@ class Organization(ApiObject):
setattr(t, "_organization", self) setattr(t, "_organization", self)
return teams return teams
def get_team(self, name, ignore_case : bool = False) -> "Team": def get_team(self, name, ignore_case: bool = False) -> "Team":
teams = self.get_teams() teams = self.get_teams()
for team in teams: for team in teams:
if (not ignore_case and team.name == name) or (ignore_case and team.name.lower() == name.lower()): if (not ignore_case and team.name == name) or (
ignore_case and team.name.lower() == name.lower()
):
return team return team
raise NotFoundException("Team not existent in organization.") raise NotFoundException("Team not existent in organization.")
@ -916,7 +942,8 @@ class Team(ApiObject):
return hash(self.organization) ^ hash(self.id) return hash(self.organization) ^ hash(self.id)
_fields_to_parsers = { _fields_to_parsers = {
"organization": lambda gitea, o: Organization.parse_response(gitea, o) "organization": lambda gitea, o: Organization.parse_response(gitea, o),
"units_map": lambda gitea, o: RepoUnits.from_dict(o),
} }
_patchable_fields = { _patchable_fields = {

Wyświetl plik

@ -1,12 +1,11 @@
import logging import logging
import json import json
from typing import List, Dict, Union from typing import List, Dict, Union
from immutabledict import immutabledict from immutabledict import immutabledict
import requests import requests
import urllib3 import urllib3
from .apiobject import User, Organization, Repository, Team from .apiobject import User, Organization, Repository, Team, RepoUnits
from .exceptions import NotFoundException, ConflictException, AlreadyExistsException from .exceptions import NotFoundException, ConflictException, AlreadyExistsException
@ -352,15 +351,7 @@ class Gitea:
"repo.releases", "repo.releases",
"repo.ext_wiki", "repo.ext_wiki",
), ),
units_map: Dict[str, str] = { units_map: "RepoUnits" = RepoUnits(),
"repo.code": "none",
"repo.issues": "none",
"repo.ext_issues": "none",
"repo.wiki": "none",
"repo.pulls": "none",
"repo.releases": "none",
"repo.ext_wiki": "none",
},
): ):
"""Creates a Team. """Creates a Team.
@ -379,7 +370,7 @@ class Gitea:
"can_create_org_repo": can_create_org_repo, "can_create_org_repo": can_create_org_repo,
"includes_all_repositories": includes_all_repositories, "includes_all_repositories": includes_all_repositories,
"units": units, "units": units,
"units_map": units_map, "units_map": units_map.to_dict(),
}, },
) )
if "id" in result: if "id" in result: