Skip to content

Commit e79d9bc

Browse files
authored
Make more objects lazy (#3403)
These `GithubObjects` can be retrieved lazily with `Github(…, lazy=True)`: - AuthenticatedUser - CheckRun - CheckSuite - Commit - CommitComment - Deployment - Download - Environment - Gist - GistComment - GitRef - GitRelease - GitReleaseAsset - Hook - Issue - IssueComment - Label - Milestone - NamedUser - Organization - OrganizationSecret - OrganizationVariable - Project - PullRequest - PullRequestComment - Repository - RepositoryKey - Secret - Team - Variable - Workflow - WorkflowRun
1 parent c92f555 commit e79d9bc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+719
-281
lines changed

github/AuthenticatedUser.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ def create_repo_from_template(
544544

545545
headers, data = self._requester.requestJsonAndCheck(
546546
"POST",
547-
f"/repos/{repo.owner.login}/{repo.name}/generate",
547+
f"/repos/{repo.full_name}/generate",
548548
input=post_parameters,
549549
headers={"Accept": "application/vnd.github.v3+json"},
550550
)
@@ -704,6 +704,7 @@ def edit(
704704

705705
headers, data = self._requester.requestJsonAndCheck("PATCH", "/user", input=post_parameters)
706706
self._useAttributes(data)
707+
self._set_complete()
707708

708709
def get_authorization(self, id: int) -> Authorization:
709710
"""
@@ -898,9 +899,9 @@ def get_repo(self, name: str) -> Repository:
898899
:calls: `GET /repos/{owner}/{repo} <http://docs.github.com/en/rest/reference/repos>`_
899900
"""
900901
assert isinstance(name, str), name
901-
name = urllib.parse.quote(name)
902-
headers, data = self._requester.requestJsonAndCheck("GET", f"/repos/{self.login}/{name}")
903-
return github.Repository.Repository(self._requester, headers, data, completed=True)
902+
name = urllib.parse.quote(name, safe="")
903+
url = f"/repos/{self.login}/{name}"
904+
return github.Repository.Repository(self._requester, url=url)
904905

905906
def get_repos(
906907
self,
@@ -1132,7 +1133,7 @@ def get_organization_membership(self, org: str) -> Membership:
11321133
:calls: `GET /user/memberships/orgs/{org} <https://docs.github.com/en/rest/reference/orgs#get-an-organization-membership-for-the-authenticated-user>`_
11331134
"""
11341135
assert isinstance(org, str)
1135-
org = urllib.parse.quote(org)
1136+
org = urllib.parse.quote(org, safe="")
11361137
headers, data = self._requester.requestJsonAndCheck("GET", f"/user/memberships/orgs/{org}")
11371138
return github.Membership.Membership(self._requester, headers, data, completed=True)
11381139

github/CheckRun.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ def edit(
262262

263263
headers, data = self._requester.requestJsonAndCheck("PATCH", self.url, input=post_parameters)
264264
self._useAttributes(data)
265+
self._set_complete()
265266

266267
def _useAttributes(self, attributes: dict[str, Any]) -> None:
267268
if "app" in attributes: # pragma no branch
@@ -291,6 +292,10 @@ def _useAttributes(self, attributes: dict[str, Any]) -> None:
291292
self._html_url = self._makeStringAttribute(attributes["html_url"])
292293
if "id" in attributes: # pragma no branch
293294
self._id = self._makeIntAttribute(attributes["id"])
295+
elif "url" in attributes and attributes["url"]:
296+
id = attributes["url"].split("/")[-1]
297+
if id.isnumeric():
298+
self._id = self._makeIntAttribute(int(id))
294299
if "name" in attributes: # pragma no branch
295300
self._name = self._makeStringAttribute(attributes["name"])
296301
if "node_id" in attributes: # pragma no branch

github/CheckSuite.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,10 @@ def _useAttributes(self, attributes: dict[str, Any]) -> None:
292292
self._head_sha = self._makeStringAttribute(attributes["head_sha"])
293293
if "id" in attributes: # pragma no branch
294294
self._id = self._makeIntAttribute(attributes["id"])
295+
elif "url" in attributes and attributes["url"]:
296+
id = attributes["url"].split("/")[-1]
297+
if id.isnumeric():
298+
self._id = self._makeIntAttribute(int(id))
295299
if "latest_check_runs_count" in attributes: # pragma no branch
296300
self._latest_check_runs_count = self._makeIntAttribute(attributes["latest_check_runs_count"])
297301
if "node_id" in attributes: # pragma no branch

github/Commit.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747

4848
from __future__ import annotations
4949

50+
import urllib.parse
5051
from typing import TYPE_CHECKING, Any
5152

5253
import github.Branch
@@ -354,6 +355,10 @@ def _useAttributes(self, attributes: dict[str, Any]) -> None:
354355
self._repository = self._makeClassAttribute(github.Repository.Repository, attributes["repository"])
355356
if "sha" in attributes: # pragma no branch
356357
self._sha = self._makeStringAttribute(attributes["sha"])
358+
elif "url" in attributes and attributes["url"]:
359+
quoted_sha = attributes["url"].split("/")[-1]
360+
sha = urllib.parse.unquote(quoted_sha)
361+
self._sha = self._makeStringAttribute(sha)
357362
if "stats" in attributes: # pragma no branch
358363
self._stats = self._makeClassAttribute(github.CommitStats.CommitStats, attributes["stats"])
359364
if "text_matches" in attributes: # pragma no branch

github/CommitComment.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def _initAttributes(self) -> None:
9090
self._user: Attribute[NamedUser | Organization] = NotSet
9191

9292
def __repr__(self) -> str:
93-
return self.get__repr__({"id": self._id.value, "user": self.user})
93+
return self.get__repr__({"id": self._id.value, "user": self._user.value})
9494

9595
@property
9696
def author_association(self) -> str:
@@ -179,6 +179,7 @@ def edit(self, body: str) -> None:
179179
}
180180
headers, data = self._requester.requestJsonAndCheck("PATCH", self.url, input=post_parameters)
181181
self._useAttributes(data)
182+
self._set_complete()
182183

183184
def get_reactions(self) -> PaginatedList[Reaction]:
184185
"""
@@ -239,6 +240,10 @@ def _useAttributes(self, attributes: dict[str, Any]) -> None:
239240
self._html_url = self._makeStringAttribute(attributes["html_url"])
240241
if "id" in attributes: # pragma no branch
241242
self._id = self._makeIntAttribute(attributes["id"])
243+
elif "url" in attributes and attributes["url"]:
244+
id = attributes["url"].split("/")[-1]
245+
if id.isnumeric():
246+
self._id = self._makeIntAttribute(int(id))
242247
if "line" in attributes: # pragma no branch
243248
self._line = self._makeIntAttribute(attributes["line"])
244249
if "node_id" in attributes: # pragma no branch

github/Deployment.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,10 @@ def _useAttributes(self, attributes: dict[str, Any]) -> None:
277277
self._environment = self._makeStringAttribute(attributes["environment"])
278278
if "id" in attributes: # pragma no branch
279279
self._id = self._makeIntAttribute(attributes["id"])
280+
elif "url" in attributes and attributes["url"]:
281+
id = attributes["url"].split("/")[-1]
282+
if id.isnumeric():
283+
self._id = self._makeIntAttribute(int(id))
280284
if "node_id" in attributes: # pragma no branch
281285
self._node_id = self._makeStringAttribute(attributes["node_id"])
282286
if "original_environment" in attributes: # pragma no branch

github/Download.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,10 @@ def _useAttributes(self, attributes: dict[str, Any]) -> None:
213213
self._html_url = self._makeStringAttribute(attributes["html_url"])
214214
if "id" in attributes: # pragma no branch
215215
self._id = self._makeIntAttribute(attributes["id"])
216+
elif "url" in attributes and attributes["url"]:
217+
id = attributes["url"].split("/")[-1]
218+
if id.isnumeric():
219+
self._id = self._makeIntAttribute(int(id))
216220
if "mime_type" in attributes: # pragma no branch
217221
self._mime_type = self._makeStringAttribute(
218222
attributes["mime_type"]

github/Environment.py

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242

4343
from __future__ import annotations
4444

45+
import urllib.parse
4546
from datetime import datetime
4647
from typing import TYPE_CHECKING, Any
4748

@@ -172,14 +173,13 @@ def create_secret(self, secret_name: str, unencrypted_value: str) -> Secret:
172173
"key_id": public_key.key_id,
173174
"encrypted_value": payload,
174175
}
175-
self._requester.requestJsonAndCheck("PUT", f"{self.url}/secrets/{secret_name}", input=put_parameters)
176+
quoted_secret_name = urllib.parse.quote(secret_name, safe="")
177+
url = f"{self.url}/secrets/{quoted_secret_name}"
178+
self._requester.requestJsonAndCheck("PUT", url, input=put_parameters)
176179
return github.Secret.Secret(
177-
requester=self._requester,
178-
headers={},
179-
attributes={
180-
"name": secret_name,
181-
"url": f"{self.url}/secrets/{secret_name}",
182-
},
180+
self._requester,
181+
url=url,
182+
attributes={"name": secret_name},
183183
completed=False,
184184
)
185185

@@ -204,12 +204,8 @@ def get_secret(self, secret_name: str) -> Secret:
204204
:calls: `GET /repos/{owner}/{repo}/environments/{environment_name}/secrets/{secret_name} <https://docs.github.com/en/rest/secrets#get-an-organization-secret>`_
205205
"""
206206
assert isinstance(secret_name, str), secret_name
207-
return github.Secret.Secret(
208-
requester=self._requester,
209-
headers={},
210-
attributes={"url": f"{self.url}/secrets/{secret_name}"},
211-
completed=False,
212-
)
207+
secret_name = urllib.parse.quote(secret_name, safe="")
208+
return github.Secret.Secret(self._requester, url=f"{self.url}/secrets/{secret_name}")
213209

214210
def create_variable(self, variable_name: str, value: str) -> Variable:
215211
"""
@@ -222,13 +218,15 @@ def create_variable(self, variable_name: str, value: str) -> Variable:
222218
"value": value,
223219
}
224220
self._requester.requestJsonAndCheck("POST", f"{self.url}/variables", input=post_parameters)
221+
222+
quoted_variable_name = urllib.parse.quote(variable_name, safe="")
223+
url = f"{self.url}/variables/{quoted_variable_name}"
225224
return github.Variable.Variable(
226225
self._requester,
227-
headers={},
226+
url=url,
228227
attributes={
229228
"name": variable_name,
230229
"value": value,
231-
"url": f"{self.url}/variables/{variable_name}",
232230
},
233231
completed=False,
234232
)
@@ -253,12 +251,9 @@ def get_variable(self, variable_name: str) -> Variable:
253251
:rtype: Variable
254252
"""
255253
assert isinstance(variable_name, str), variable_name
256-
return github.Variable.Variable(
257-
requester=self._requester,
258-
headers={},
259-
attributes={"url": f"{self.url}/variables/{variable_name}"},
260-
completed=False,
261-
)
254+
variable_name = urllib.parse.quote(variable_name, safe="")
255+
url = f"{self.url}/variables/{variable_name}"
256+
return github.Variable.Variable(self._requester, url=url)
262257

263258
def delete_secret(self, secret_name: str) -> bool:
264259
"""
@@ -296,6 +291,10 @@ def _useAttributes(self, attributes: dict[str, Any]) -> None:
296291
self._id = self._makeIntAttribute(attributes["id"])
297292
if "name" in attributes: # pragma no branch
298293
self._name = self._makeStringAttribute(attributes["name"])
294+
elif "url" in attributes and attributes["url"]:
295+
quoted_name = attributes["url"].split("/")[-1]
296+
name = urllib.parse.unquote(quoted_name)
297+
self._name = self._makeStringAttribute(name)
299298
if "node_id" in attributes: # pragma no branch
300299
self._node_id = self._makeStringAttribute(attributes["node_id"])
301300
if "protection_rules" in attributes: # pragma no branch

github/Gist.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545

4646
from __future__ import annotations
4747

48+
import urllib.parse
4849
from datetime import datetime
4950
from typing import TYPE_CHECKING, Any
5051

@@ -257,15 +258,17 @@ def edit(self, description: Opt[str] = NotSet, files: Opt[dict[str, InputFileCon
257258
if is_defined(files):
258259
post_parameters["files"] = {key: None if value is None else value._identity for key, value in files.items()}
259260
headers, data = self._requester.requestJsonAndCheck("PATCH", self.url, input=post_parameters)
261+
260262
self._useAttributes(data)
263+
self._set_complete()
261264

262265
def get_comment(self, id: int) -> GistComment:
263266
"""
264267
:calls: `GET /gists/{gist_id}/comments/{comment_id} <https://docs.github.com/en/rest/reference/gists#comments>`_
265268
"""
266269
assert isinstance(id, int), id
267-
headers, data = self._requester.requestJsonAndCheck("GET", f"{self.url}/comments/{id}")
268-
return github.GistComment.GistComment(self._requester, headers, data, completed=True)
270+
url = f"{self.url}/comments/{id}"
271+
return github.GistComment.GistComment(self._requester, url=url)
269272

270273
def get_comments(self) -> PaginatedList[GistComment]:
271274
"""
@@ -330,6 +333,10 @@ def _useAttributes(self, attributes: dict[str, Any]) -> None:
330333
self._html_url = self._makeStringAttribute(attributes["html_url"])
331334
if "id" in attributes: # pragma no branch
332335
self._id = self._makeStringAttribute(attributes["id"])
336+
elif "url" in attributes and attributes["url"]:
337+
quoted_id = attributes["url"].split("/")[-1]
338+
id = urllib.parse.unquote(quoted_id)
339+
self._id = self._makeStringAttribute(id)
333340
if "node_id" in attributes: # pragma no branch
334341
self._node_id = self._makeStringAttribute(attributes["node_id"])
335342
if "owner" in attributes: # pragma no branch

github/GistComment.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ def edit(self, body: str) -> None:
132132
}
133133
headers, data = self._requester.requestJsonAndCheck("PATCH", self.url, input=post_parameters)
134134
self._useAttributes(data)
135+
self._set_complete()
135136

136137
def _useAttributes(self, attributes: dict[str, Any]) -> None:
137138
if "author_association" in attributes: # pragma no branch
@@ -142,6 +143,10 @@ def _useAttributes(self, attributes: dict[str, Any]) -> None:
142143
self._created_at = self._makeDatetimeAttribute(attributes["created_at"])
143144
if "id" in attributes: # pragma no branch
144145
self._id = self._makeIntAttribute(attributes["id"])
146+
elif "url" in attributes and attributes["url"]:
147+
id = attributes["url"].split("/")[-1]
148+
if id.isnumeric():
149+
self._id = self._makeIntAttribute(int(id))
145150
if "node_id" in attributes: # pragma no branch
146151
self._node_id = self._makeStringAttribute(attributes["node_id"])
147152
if "updated_at" in attributes: # pragma no branch

0 commit comments

Comments
 (0)