Skip to content

Commit 3716bab

Browse files
authored
Use GET url or _links.self as object url (#3421)
The `Requester` extracts an URL from `_links.self` or uses the `GET` url, if a returned Github object misses the `url` attribute.
1 parent 61dcf49 commit 3716bab

9 files changed

Lines changed: 79 additions & 19 deletions

File tree

‎github/Branch.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ def _initAttributes(self) -> None:
104104
self._protection: Attribute[BranchProtection] = NotSet
105105
self._protection_url: Attribute[str] = github.GithubObject.NotSet
106106
self._required_approving_review_count: Attribute[int] = NotSet
107+
self._url: Attribute[str] = NotSet
107108

108109
def __repr__(self) -> str:
109110
return self.get__repr__({"name": self._name.value})
@@ -142,6 +143,10 @@ def protection_url(self) -> str:
142143
def required_approving_review_count(self) -> int:
143144
return self._required_approving_review_count.value
144145

146+
@property
147+
def url(self) -> str:
148+
return self._url.value
149+
145150
def get_protection(self) -> BranchProtection:
146151
"""
147152
:calls: `GET /repos/{owner}/{repo}/branches/{branch}/protection <https://docs.github.com/en/rest/reference/repos#branches>`_
@@ -658,3 +663,5 @@ def _useAttributes(self, attributes: dict[str, Any]) -> None:
658663
self._required_approving_review_count = self._makeIntAttribute(
659664
attributes["required_approving_review_count"]
660665
)
666+
if "url" in attributes:
667+
self._url = self._makeStringAttribute(attributes["url"])

‎github/GithubApp.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,8 @@ def _useAttributes(self, attributes: dict[str, Any]) -> None:
227227
if "updated_at" in attributes: # pragma no branch
228228
self._updated_at = self._makeDatetimeAttribute(attributes["updated_at"])
229229
if "url" in attributes:
230-
self._url = self._makeStringAttribute(attributes["url"])
230+
# we give precedence to the slug attribute
231+
if "slug" not in attributes:
232+
self._url = self._makeStringAttribute(attributes["url"])
231233
if "webhook_secret" in attributes: # pragma no branch
232234
self._webhook_secret = self._makeStringAttribute(attributes["webhook_secret"])

‎github/GitignoreTemplate.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class GitignoreTemplate(NonCompletableGithubObject):
6060
def _initAttributes(self) -> None:
6161
self._name: Attribute[str] = NotSet
6262
self._source: Attribute[str] = NotSet
63+
self._url: Attribute[str] = NotSet
6364

6465
def __repr__(self) -> str:
6566
return self.get__repr__({"name": self._name.value})
@@ -72,8 +73,14 @@ def name(self) -> str:
7273
def source(self) -> str:
7374
return self._source.value
7475

76+
@property
77+
def url(self) -> str:
78+
return self._url.value
79+
7580
def _useAttributes(self, attributes: dict[str, Any]) -> None:
7681
if "name" in attributes: # pragma no branch
7782
self._name = self._makeStringAttribute(attributes["name"])
7883
if "source" in attributes: # pragma no branch
7984
self._source = self._makeStringAttribute(attributes["source"])
85+
if "url" in attributes: # pragma no branch
86+
self._url = self._makeStringAttribute(attributes["url"])

‎github/RateLimitOverview.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class RateLimitOverview(NonCompletableGithubObject):
5252
def _initAttributes(self) -> None:
5353
self._rate: Attribute[Rate] = NotSet
5454
self._resources: Attribute[RateLimit] = NotSet
55+
self._url: Attribute[str] = NotSet
5556

5657
def __repr__(self) -> str:
5758
return self.get__repr__({"rate": self._rate.value})
@@ -64,8 +65,14 @@ def rate(self) -> Rate:
6465
def resources(self) -> RateLimit:
6566
return self._resources.value
6667

68+
@property
69+
def url(self) -> str:
70+
return self._url.value
71+
6772
def _useAttributes(self, attributes: dict[str, Any]) -> None:
6873
if "rate" in attributes: # pragma no branch
6974
self._rate = self._makeClassAttribute(github.Rate.Rate, attributes["rate"])
7075
if "resources" in attributes: # pragma no branch
7176
self._resources = self._makeClassAttribute(github.RateLimit.RateLimit, attributes["resources"])
77+
if "url" in attributes: # pragma no branch
78+
self._url = self._makeStringAttribute(attributes["url"])

‎github/Requester.py‎

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -618,16 +618,20 @@ def requestJsonAndCheck(
618618
:raises: :class:`GithubException` for error status codes
619619
620620
"""
621-
return self.__check(
622-
*self.requestJson(
623-
verb,
624-
url,
625-
parameters,
626-
headers,
627-
input,
628-
self.__customConnection(url),
629-
follow_302_redirect=follow_302_redirect,
630-
)
621+
return self.__postProcess(
622+
verb,
623+
url,
624+
*self.__check(
625+
*self.requestJson(
626+
verb,
627+
url,
628+
parameters,
629+
headers,
630+
input,
631+
self.__customConnection(url),
632+
follow_302_redirect=follow_302_redirect,
633+
)
634+
),
631635
)
632636

633637
def requestMultipartAndCheck(
@@ -647,7 +651,11 @@ def requestMultipartAndCheck(
647651
:raises: :class:`GithubException` for error status codes
648652
649653
"""
650-
return self.__check(*self.requestMultipart(verb, url, parameters, headers, input, self.__customConnection(url)))
654+
return self.__postProcess(
655+
verb,
656+
url,
657+
*self.__check(*self.requestMultipart(verb, url, parameters, headers, input, self.__customConnection(url))),
658+
)
651659

652660
def requestBlobAndCheck(
653661
self,
@@ -667,7 +675,11 @@ def requestBlobAndCheck(
667675
:raises: :class:`GithubException` for error status codes
668676
669677
"""
670-
return self.__check(*self.requestBlob(verb, url, parameters, headers, input, self.__customConnection(url)))
678+
return self.__postProcess(
679+
verb,
680+
url,
681+
*self.__check(*self.requestBlob(verb, url, parameters, headers, input, self.__customConnection(url))),
682+
)
671683

672684
@classmethod
673685
def paths_of_dict(cls, d: dict) -> dict:
@@ -851,6 +863,22 @@ def __check(
851863
raise self.createException(status, responseHeaders, data)
852864
return responseHeaders, data
853865

866+
def __postProcess(
867+
self, verb: str, url: str, responseHeaders: dict[str, Any], data: Any
868+
) -> tuple[dict[str, Any], Any]:
869+
if verb == "GET" and isinstance(data, dict) and "url" not in data:
870+
if "_links" in data and "self" in data["_links"] and data["_links"]["self"]:
871+
self_link = data["_links"]["self"]
872+
if isinstance(self_link, str):
873+
data["url"] = self_link
874+
elif isinstance(self_link, dict):
875+
href = self_link.get("href")
876+
if href:
877+
data["url"] = href
878+
else:
879+
data["url"] = url
880+
return responseHeaders, data
881+
854882
@classmethod
855883
def __hostnameHasDomain(cls, hostname: str, domain_or_domains: str | list[str]) -> bool:
856884
if isinstance(domain_or_domains, str):
@@ -1143,7 +1171,7 @@ def encode(_: Any) -> tuple[str, Any]:
11431171

11441172
status, responseHeaders, output = self.__requestEncode(cnx, verb, url, parameters, headers, file_like, encode)
11451173
if isinstance(output, str):
1146-
return self.__check(status, responseHeaders, output)
1174+
return self.__postProcess(verb, url, *self.__check(status, responseHeaders, output))
11471175
raise ValueError("requestMemoryBlobAndCheck() Expected a str, should never happen")
11481176

11491177
def __requestEncode(

‎tests/AuthenticatedUser.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@ def testGetNotification(self):
735735
notification.updated_at,
736736
datetime(2013, 3, 15, 5, 43, 11, tzinfo=timezone.utc),
737737
)
738-
self.assertEqual(notification.url, None)
738+
self.assertEqual(notification.url, "/notifications/threads/8406712")
739739
self.assertEqual(notification.subject.url, None)
740740
self.assertEqual(notification.subject.latest_comment_url, None)
741741
self.assertEqual(

‎tests/Organization.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,7 @@ def testGetCustomProperty(self):
784784
self.assertEqual(custom_property.required, True)
785785
self.assertEqual(custom_property.default_value, "foo")
786786
self.assertEqual(custom_property.description, "description")
787-
self.assertIsNone(custom_property.url)
787+
self.assertEqual(custom_property.url, "https://api.github.com/orgs/BeaverSoftware/properties/schema/property_1")
788788
self.assertEqual(custom_property.values_editable_by, "org_actors")
789789

790790
def testCreateCustomPropertyValues(self):

‎tests/PublicKey.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ def testAttributes(self):
5353
'PublicKey(key_id="568250167242549743", key="u5e1Z25+z8pmgVVt5Pd8k0z/sKpVL1MXYtRAecE4vm8=")',
5454
)
5555
self.assertIsNone(self.public_key.title)
56-
self.assertIsNone(self.public_key.url)
56+
self.assertEqual(
57+
self.public_key.url, "https://api.github.com/repos/jacquev6/PyGithub/actions/secrets/public-key"
58+
)
5759

5860
def testAttributes_with_int_key_id(self):
5961
self.public_key = self.g.get_user().get_repo("PyGithub").get_public_key()

‎tests/Repository.py‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,7 +1331,10 @@ def testGetLabels(self):
13311331
)
13321332

13331333
def testGetLanguages(self):
1334-
self.assertEqual(self.repo.get_languages(), {"Python": 127266, "Shell": 673})
1334+
self.assertEqual(
1335+
self.repo.get_languages(),
1336+
{"Python": 127266, "Shell": 673, "url": "https://api.github.com/repos/PyGithub/PyGithub/languages"},
1337+
)
13351338

13361339
def testGetMilestones(self):
13371340
self.assertListKeyEqual(self.repo.get_milestones(), lambda m: m.id, [93547])
@@ -2288,7 +2291,11 @@ def testTransferOwnershipInvalidOwner(self):
22882291
def testGetAutomatedSecurityFixes(self):
22892292
self.assertDictEqual(
22902293
self.repo.get_automated_security_fixes(),
2291-
{"enabled": True, "paused": False},
2294+
{
2295+
"enabled": True,
2296+
"paused": False,
2297+
"url": "https://api.github.com/repos/PyGithub/PyGithub/automated-security-fixes",
2298+
},
22922299
)
22932300

22942301

0 commit comments

Comments
 (0)