diff --git a/buildrunner/config/fetch/github.py b/buildrunner/config/fetch/github.py index 1c3f15e7..3e3c9f85 100644 --- a/buildrunner/config/fetch/github.py +++ b/buildrunner/config/fetch/github.py @@ -16,7 +16,11 @@ ) -def v3_fetch_file(parsed_url, gh_config: GithubModel): +def _clean_nones(items): + return list(filter(None, items)) + + +def _fetch_file(parsed_url, gh_config: GithubModel): """ Fetch files using Github v3 protocol. """ @@ -30,11 +34,13 @@ def v3_fetch_file(parsed_url, gh_config: GithubModel): auth = (username, gh_config.app_token) url = "/".join( - ( - endpoint, - version, - "users", - username, + _clean_nones( + [ + endpoint, + version, + "users", + username, + ] ) ) resp = requests.get(url, auth=auth, timeout=180) @@ -47,7 +53,7 @@ def v3_fetch_file(parsed_url, gh_config: GithubModel): raise ValueError('URL must begin with "/"') fpath = parsed_url.path.split("/") - ubuild = [endpoint, version, "repos", fpath[1], fpath[2], "contents"] + ubuild = _clean_nones([endpoint, version, "repos", fpath[1], fpath[2], "contents"]) ubuild.extend(fpath[3:]) url = "/".join(ubuild) resp = requests.get(url, auth=auth, timeout=180) @@ -97,9 +103,9 @@ def fetch_file(parsed_url, config: GlobalConfig): ) ver = nlcfg.version - # NOTE: potentially the v3_fetch_file() works for other github API versions. - if ver == "v3": - contents = v3_fetch_file(parsed_url, nlcfg) + # NOTE: potentially the _fetch_file() works for other github API versions. + if ver == "v3" or ver is None: + contents = _fetch_file(parsed_url, nlcfg) else: raise NotImplementedError(f"No version support for github API version {ver}") diff --git a/buildrunner/config/models.py b/buildrunner/config/models.py index 403fb8eb..9c37f47d 100644 --- a/buildrunner/config/models.py +++ b/buildrunner/config/models.py @@ -30,7 +30,7 @@ class GithubModel(BaseModel, extra="forbid"): endpoint: str - version: str + version: str = None username: str = os.getenv("USER", os.getenv("LOGNAME")) app_token: str = "" diff --git a/docs/fetching-files.rst b/docs/fetching-files.rst index c9bba597..158643c1 100644 --- a/docs/fetching-files.rst +++ b/docs/fetching-files.rst @@ -57,7 +57,7 @@ The ``github://`` facility requires the following configuration entries: github: LABEL: endpoint: 'https://HOSTNAME/API_PATH' - version: 'VERSION' + version: 'VERSION' // optional username: 'USERNAME' app_token: 'APP_TOKEN' @@ -72,6 +72,16 @@ The following is suggested for an entry to reference files for GitHub Enterprise username: 'USERNAME' app_token: 'APP_TOKEN' +The following is suggested for an entry to reference files for GitHub.com: + +.. code:: yaml + + github: + github: + endpoint: 'https://api.github.com' + username: 'USERNAME' + app_token: 'APP_TOKEN' + :username: The individual username used to access the Github Enterprise instance. :app_token: The user-specific application token generated by the user on Github for Buildrunner access. It is a 40 hex digit token. diff --git a/tests/test_config_validation/test_global_config.py b/tests/test_config_validation/test_global_config.py index 0dc772e4..1b9b8d0a 100644 --- a/tests/test_config_validation/test_global_config.py +++ b/tests/test_config_validation/test_global_config.py @@ -65,7 +65,7 @@ def fixture_override_master_config_file(tmp_path): """, ["Extra inputs are not permitted"], ), - # Valid github config + # Valid github configs ( """ github: @@ -77,6 +77,16 @@ def fixture_override_master_config_file(tmp_path): """, [], ), + ( + """ + github: + company_github: + endpoint: 'https://api.github.com' + username: 'USERNAME' + app_token: 'APP_TOKEN' + """, + [], + ), # Invalid github config ( """