Skip to content

Commit 0a83611

Browse files
author
Jacob Truman
committed
XENG-9362 Support github api without version
1 parent 618bd25 commit 0a83611

File tree

4 files changed

+39
-13
lines changed

4 files changed

+39
-13
lines changed

buildrunner/config/fetch/github.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616
)
1717

1818

19-
def v3_fetch_file(parsed_url, gh_config: GithubModel):
19+
def _clean_nones(items):
20+
return list(filter(None, items))
21+
22+
23+
def _fetch_file(parsed_url, gh_config: GithubModel):
2024
"""
2125
Fetch files using Github v3 protocol.
2226
"""
@@ -30,11 +34,13 @@ def v3_fetch_file(parsed_url, gh_config: GithubModel):
3034

3135
auth = (username, gh_config.app_token)
3236
url = "/".join(
33-
(
34-
endpoint,
35-
version,
36-
"users",
37-
username,
37+
_clean_nones(
38+
[
39+
endpoint,
40+
version,
41+
"users",
42+
username,
43+
]
3844
)
3945
)
4046
resp = requests.get(url, auth=auth, timeout=180)
@@ -47,7 +53,7 @@ def v3_fetch_file(parsed_url, gh_config: GithubModel):
4753
raise ValueError('URL must begin with "/"')
4854

4955
fpath = parsed_url.path.split("/")
50-
ubuild = [endpoint, version, "repos", fpath[1], fpath[2], "contents"]
56+
ubuild = _clean_nones([endpoint, version, "repos", fpath[1], fpath[2], "contents"])
5157
ubuild.extend(fpath[3:])
5258
url = "/".join(ubuild)
5359
resp = requests.get(url, auth=auth, timeout=180)
@@ -97,9 +103,9 @@ def fetch_file(parsed_url, config: GlobalConfig):
97103
)
98104

99105
ver = nlcfg.version
100-
# NOTE: potentially the v3_fetch_file() works for other github API versions.
101-
if ver == "v3":
102-
contents = v3_fetch_file(parsed_url, nlcfg)
106+
# NOTE: potentially the _fetch_file() works for other github API versions.
107+
if ver == "v3" or ver is None:
108+
contents = _fetch_file(parsed_url, nlcfg)
103109
else:
104110
raise NotImplementedError(f"No version support for github API version {ver}")
105111

buildrunner/config/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
class GithubModel(BaseModel, extra="forbid"):
3232
endpoint: str
33-
version: str
33+
version: str = None
3434
username: str = os.getenv("USER", os.getenv("LOGNAME"))
3535
app_token: str = ""
3636

docs/fetching-files.rst

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ The ``github://`` facility requires the following configuration entries:
5757
github:
5858
LABEL:
5959
endpoint: 'https://HOSTNAME/API_PATH'
60-
version: 'VERSION'
60+
version: 'VERSION' // optional
6161
username: 'USERNAME'
6262
app_token: 'APP_TOKEN'
6363
@@ -72,6 +72,16 @@ The following is suggested for an entry to reference files for GitHub Enterprise
7272
username: 'USERNAME'
7373
app_token: 'APP_TOKEN'
7474
75+
The following is suggested for an entry to reference files for GitHub.com:
76+
77+
.. code:: yaml
78+
79+
github:
80+
github:
81+
endpoint: 'https://api.github.com'
82+
username: 'USERNAME'
83+
app_token: 'APP_TOKEN'
84+
7585
:username: The individual username used to access the Github Enterprise instance.
7686
:app_token: The user-specific application token generated by the user on Github for Buildrunner
7787
access. It is a 40 hex digit token.

tests/test_config_validation/test_global_config.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def fixture_override_master_config_file(tmp_path):
6565
""",
6666
["Extra inputs are not permitted"],
6767
),
68-
# Valid github config
68+
# Valid github configs
6969
(
7070
"""
7171
github:
@@ -77,6 +77,16 @@ def fixture_override_master_config_file(tmp_path):
7777
""",
7878
[],
7979
),
80+
(
81+
"""
82+
github:
83+
company_github:
84+
endpoint: 'https://api.github.com'
85+
username: 'USERNAME'
86+
app_token: 'APP_TOKEN'
87+
""",
88+
[],
89+
),
8090
# Invalid github config
8191
(
8292
"""

0 commit comments

Comments
 (0)