Skip to content
Merged
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
40 changes: 22 additions & 18 deletions github_release.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@
from requests import request


GITHUB_API = "https://api.github.com"

REQ_BUFFER_SIZE = 65536 # Chunk size when iterating a download body

_github_token_cli_arg = None
_github_api_url = None


class _NoopProgressReporter(object):
Expand Down Expand Up @@ -105,7 +104,7 @@ def _check_for_credentials(func):
@wraps(func)
def with_check_for_credentials(*args, **kwargs):
has_github_token_env_var = "GITHUB_TOKEN" in os.environ
has_netrc = requests.utils.get_netrc_auth(GITHUB_API)
has_netrc = requests.utils.get_netrc_auth(_github_api_url)
if (not _github_token_cli_arg
and not has_github_token_env_var and not has_netrc):
raise EnvironmentError(
Expand Down Expand Up @@ -173,16 +172,21 @@ def _validate_repo_name(ctx, param, value):
@click.group()
@click.option("--github-token", envvar='GITHUB_TOKEN', default=None,
help="[default: GITHUB_TOKEN env. variable]")
@click.option('--github-api-url', envvar='GITHUB_API_URL',
default='https://api.github.com',
help='[default: https://api.github.com]')
@click.option("--progress/--no-progress", default=True,
help="Display progress bar (default: yes).")
def main(github_token, progress):
def main(github_token, github_api_url, progress):
"""A CLI to easily manage GitHub releases, assets and references."""
global progress_reporter_cls
progress_reporter_cls.reportProgress = sys.stdout.isatty() and progress
if progress_reporter_cls.reportProgress:
progress_reporter_cls = _progress_bar
global _github_token_cli_arg
_github_token_cli_arg = github_token
global _github_api_url
_github_api_url = github_api_url


@main.group("release")
Expand Down Expand Up @@ -255,7 +259,7 @@ def get_releases(repo_name, verbose=False):

releases = []
_recursive_gh_get(
GITHUB_API + '/repos/{0}/releases'.format(repo_name), releases)
_github_api_url + '/repos/{0}/releases'.format(repo_name), releases)

if verbose:
list(map(print_release_info,
Expand Down Expand Up @@ -373,7 +377,7 @@ def patch_release(repo_name, current_tag_name, **values):
data.update(values)

if not dry_run:
url = GITHUB_API + '/repos/{0}/releases/{1}'.format(
url = _github_api_url + '/repos/{0}/releases/{1}'.format(
repo_name, release['id'])
response = _request(
'PATCH', url,
Expand All @@ -394,7 +398,7 @@ def get_assets(repo_name, tag_name, verbose=False):
raise Exception('Release with tag_name {0} not found'.format(tag_name))

assets = []
_recursive_gh_get(GITHUB_API + '/repos/{0}/releases/{1}/assets'.format(
_recursive_gh_get(_github_api_url + '/repos/{0}/releases/{1}/assets'.format(
repo_name, release["id"]), assets)

if verbose:
Expand Down Expand Up @@ -465,7 +469,7 @@ def gh_release_create(repo_name, tag_name, asset_pattern=None, name=None, body=N
data["target_commitish"] = target_commitish
if not dry_run:
response = _request(
'POST', GITHUB_API + '/repos/{0}/releases'.format(repo_name),
'POST', _github_api_url + '/repos/{0}/releases'.format(repo_name),
data=json.dumps(data),
headers={'Content-Type': 'application/json'})
response.raise_for_status()
Expand Down Expand Up @@ -540,7 +544,7 @@ def gh_release_delete(repo_name, pattern, keep_pattern=None,
print('deleting release {0}'.format(release['tag_name']))
if dry_run:
continue
url = (GITHUB_API
url = (_github_api_url
+ '/repos/{0}/releases/{1}'.format(repo_name, release['id']))
response = _request('DELETE', url)
response.raise_for_status()
Expand Down Expand Up @@ -683,7 +687,7 @@ def _upload_release_file(
print(" deleting %s (invalid asset "
"with state set to 'new')" % asset['name'])
url = (
GITHUB_API
_github_api_url
+ '/repos/{0}/releases/assets/{1}'.format(
repo_name, asset['id'])
)
Expand Down Expand Up @@ -747,7 +751,7 @@ def gh_asset_upload(repo_name, tag_name, pattern, dry_run=False, verbose=False):
# Raise exception if no token is specified AND netrc file is found
# BUT only api.github.com is specified. See #17
has_github_token = "GITHUB_TOKEN" in os.environ
has_netrc = requests.utils.get_netrc_auth(GITHUB_API)
has_netrc = requests.utils.get_netrc_auth(_github_api_url)
if not has_github_token and has_netrc:
if requests.utils.get_netrc_auth(upload_url) is None:
raise EnvironmentError(
Expand Down Expand Up @@ -832,7 +836,7 @@ def gh_asset_delete(repo_name, tag_name, pattern,
if dry_run:
continue
url = (
GITHUB_API
_github_api_url
+ '/repos/{0}/releases/assets/{1}'.format(repo_name, asset['id'])
)
response = _request('DELETE', url)
Expand Down Expand Up @@ -860,7 +864,7 @@ def _cli_asset_download(*args, **kwargs):
def _download_file(repo_name, asset):
response = _request(
method='GET',
url=GITHUB_API + '/repos/{0}/releases/assets/{1}'.format(
url=_github_api_url + '/repos/{0}/releases/assets/{1}'.format(
repo_name, asset['id']),
allow_redirects=False,
headers={'Accept': 'application/octet-stream'},
Expand Down Expand Up @@ -930,7 +934,7 @@ def get_refs(repo_name, tags=None, pattern=None):

refs = []
_recursive_gh_get(
GITHUB_API + '/repos/{0}/git/refs'.format(repo_name), refs)
_github_api_url + '/repos/{0}/git/refs'.format(repo_name), refs)

# If "tags" is True, keep only "refs/tags/*"
data = refs
Expand All @@ -945,7 +949,7 @@ def get_refs(repo_name, tags=None, pattern=None):
try:
tags = []
_recursive_gh_get(
GITHUB_API + '/repos/{0}/git/refs/tags'.format(repo_name), tags)
_github_api_url + '/repos/{0}/git/refs/tags'.format(repo_name), tags)
for ref in tags:
if ref["ref"] not in tag_names:
data.append(ref)
Expand Down Expand Up @@ -1001,7 +1005,7 @@ def gh_ref_create(repo_name, reference, sha):
'sha': sha
}
response = _request(
'POST', GITHUB_API + '/repos/{0}/git/refs'.format(repo_name),
'POST', _github_api_url + '/repos/{0}/git/refs'.format(repo_name),
data=json.dumps(data),
headers={'Content-Type': 'application/json'})
response.raise_for_status()
Expand Down Expand Up @@ -1040,7 +1044,7 @@ def gh_ref_delete(repo_name, pattern, keep_pattern=None, tags=False,
continue
response = _request(
'DELETE',
GITHUB_API + '/repos/{0}/git/{1}'.format(repo_name, ref['ref']))
_github_api_url + '/repos/{0}/git/{1}'.format(repo_name, ref['ref']))
response.raise_for_status()
return len(removed_refs) > 0

Expand All @@ -1053,7 +1057,7 @@ def gh_commit_get(repo_name, sha):
try:
response = _request(
'GET',
GITHUB_API + '/repos/{0}/git/commits/{1}'.format(repo_name, sha))
_github_api_url + '/repos/{0}/git/commits/{1}'.format(repo_name, sha))
response.raise_for_status()
return response.json()
except requests.exceptions.HTTPError as exc_info:
Expand Down