-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path__init__.py
More file actions
161 lines (134 loc) · 5.37 KB
/
__init__.py
File metadata and controls
161 lines (134 loc) · 5.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import logging
from typing import List, Optional
from dataclasses import dataclass, asdict
log = logging.getLogger("socketdev")
@dataclass
class RepositoryInfo:
id: str
created_at: str # Could be datetime if we want to parse it
updated_at: str # Could be datetime if we want to parse it
head_full_scan_id: str
name: str
description: str
homepage: str
visibility: str
archived: bool
default_branch: str
slug: Optional[str] = None
def __getitem__(self, key): return getattr(self, key)
def to_dict(self): return asdict(self)
@classmethod
def from_dict(cls, data: dict) -> "RepositoryInfo":
return cls(
id=data["id"],
created_at=data["created_at"],
updated_at=data["updated_at"],
head_full_scan_id=data["head_full_scan_id"],
name=data["name"],
description=data["description"],
homepage=data["homepage"],
visibility=data["visibility"],
archived=data["archived"],
default_branch=data["default_branch"],
slug=data.get("slug")
)
@dataclass
class GetRepoResponse:
success: bool
status: int
data: Optional[RepositoryInfo] = None
message: Optional[str] = None
def __getitem__(self, key): return getattr(self, key)
def to_dict(self): return asdict(self)
@classmethod
def from_dict(cls, data: dict) -> "GetRepoResponse":
return cls(
success=data["success"],
status=data["status"],
message=data.get("message"),
data=RepositoryInfo.from_dict(data.get("data")) if data.get("data") else None
)
class Repos:
def __init__(self, api):
self.api = api
def get(self, org_slug: str, **kwargs) -> dict[str, List[RepositoryInfo]]:
query_params = {}
if kwargs:
for key, val in kwargs.items():
query_params[key] = val
if len(query_params) == 0:
return {}
path = "orgs/" + org_slug + "/repos"
if query_params is not None:
path += "?"
for param in query_params:
value = query_params[param]
path += f"{param}={value}&"
path = path.rstrip("&")
response = self.api.do_request(path=path)
if response.status_code == 200:
raw_result = response.json()
result = {
key: [RepositoryInfo.from_dict(repo) for repo in repos]
for key, repos in raw_result.items()
}
return result
error_message = response.json().get("error", {}).get("message", "Unknown error")
log.error(f"Error getting repositories: {response.status_code}, message: {error_message}")
return {}
def repo(self, org_slug: str, repo_name: str) -> GetRepoResponse:
path = f"orgs/{org_slug}/repos/{repo_name}"
response = self.api.do_request(path=path)
if response.status_code == 200:
result = response.json()
return GetRepoResponse.from_dict({
"success": True,
"status": 200,
"data": result
})
error_message = response.json().get("error", {}).get("message", "Unknown error")
log.error(f"Failed to get repository: {response.status_code}, message: {error_message}")
return GetRepoResponse.from_dict({
"success": False,
"status": response.status_code,
"message": error_message
})
def delete(self, org_slug: str, name: str) -> dict:
path = f"orgs/{org_slug}/repos/{name}"
response = self.api.do_request(path=path, method="DELETE")
if response.status_code == 200:
result = response.json()
return result
error_message = response.json().get("error", {}).get("message", "Unknown error")
log.error(f"Error deleting repository: {response.status_code}, message: {error_message}")
return {}
def post(self, org_slug: str, **kwargs) -> dict:
params = {}
if kwargs:
for key, val in kwargs.items():
params[key] = val
if len(params) == 0:
return {}
path = "orgs/" + org_slug + "/repos"
response = self.api.do_request(path=path, method="POST", payload=params)
if response.status_code == 200:
result = response.json()
return result
error_message = response.json().get("error", {}).get("message", "Unknown error")
log.error(f"Error creating repository: {response.status_code}, message: {error_message}")
return {}
def update(self, org_slug: str, repo_name: str, **kwargs) -> dict:
params = {}
if kwargs:
for key, val in kwargs.keys():
params[key] = val
if len(params) == 0:
return {}
path = f"orgs/{org_slug}/repos/{repo_name}"
response = self.api.do_request(path=path, method="POST", payload=params)
if response.status_code == 200:
result = response.json()
return result
error_message = response.json().get("error", {}).get("message", "Unknown error")
log.error(f"Error updating repository: {response.status_code}, message: {error_message}")
return {}