Skip to content

Commit 018efcf

Browse files
committed
feat: GraphQL version bug + server version info
1 parent 03ab5fc commit 018efcf

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

src/ghastoolkit/octokit/github.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
import tempfile
66
import subprocess
77
from dataclasses import dataclass
8-
from typing import Optional, Tuple, Union
8+
from typing import Dict, Optional, Tuple, Union
99
from urllib.parse import urlparse
1010

11+
from semantic_version import Version
12+
1113

1214
logger = logging.getLogger("ghastoolkit.octokit.github")
1315

@@ -252,6 +254,7 @@ class GitHub:
252254
"""GraphQL API URL"""
253255

254256
enterprise: Optional[str] = None
257+
server_version: Optional[Version] = None
255258

256259
github_app: bool = False
257260
"""GitHub App setting"""
@@ -288,6 +291,10 @@ def init(
288291
GitHub.instance = instance
289292
GitHub.api_rest, GitHub.api_graphql = GitHub.parseInstance(instance)
290293

294+
if GitHub.isEnterpriseServer():
295+
# Get the server version
296+
GitHub.getMetaInformation()
297+
291298
GitHub.enterprise = enterprise
292299

293300
return
@@ -302,11 +309,29 @@ def parseInstance(instance: str) -> Tuple[str, str]:
302309
api = url.scheme + "://api." + url.netloc
303310
return (api, f"{api}/graphql")
304311
# GitHub Ent Server
305-
api = url.scheme + "://" + url.netloc + "/api/v3"
312+
api = url.scheme + "://" + url.netloc + "/api"
306313

307-
return (api, f"{api}/graphql")
314+
return (f"{api}/v3", f"{api}/graphql")
315+
316+
@staticmethod
317+
def isEnterpriseServer() -> bool:
318+
"""Is the GitHub instance an Enterprise Server."""
319+
return GitHub.instance != "https://github.com"
308320

309321
@staticmethod
310322
def display() -> str:
311323
"""Display the GitHub Settings."""
312324
return f"GitHub('{GitHub.repository.display()}', '{GitHub.instance}')"
325+
326+
@staticmethod
327+
def getMetaInformation() -> Dict:
328+
"""Get the GitHub Meta Information."""
329+
from ghastoolkit.octokit.octokit import RestRequest
330+
331+
response = RestRequest().session.get(f"{GitHub.api_rest}/meta")
332+
333+
if response.headers.get("X-GitHub-Enterprise-Version"):
334+
version = response.headers.get("X-GitHub-Enterprise-Version")
335+
GitHub.server_version = Version(version)
336+
337+
return response.json()

tests/test_github.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def test_server(self):
2828
self.assertEqual(GitHub.instance, "https://github.geekmasher.dev")
2929
self.assertEqual(GitHub.api_rest, "https://github.geekmasher.dev/api/v3")
3030
self.assertEqual(
31-
GitHub.api_graphql, "https://github.geekmasher.dev/api/v3/graphql"
31+
GitHub.api_graphql, "https://github.geekmasher.dev/api/graphql"
3232
)
3333

3434
def test_parseReference(self):

0 commit comments

Comments
 (0)