Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions buildrunner/config/fetch/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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}")

Expand Down
2 changes: 1 addition & 1 deletion buildrunner/config/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""

Expand Down
12 changes: 11 additions & 1 deletion docs/fetching-files.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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.
Expand Down
12 changes: 11 additions & 1 deletion tests/test_config_validation/test_global_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def fixture_override_master_config_file(tmp_path):
""",
["Extra inputs are not permitted"],
),
# Valid github config
# Valid github configs
(
"""
github:
Expand All @@ -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
(
"""
Expand Down
Loading