diff --git a/github_release.py b/github_release.py index b1feebb..4d86b98 100755 --- a/github_release.py +++ b/github_release.py @@ -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): @@ -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( @@ -173,9 +172,12 @@ 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 @@ -183,6 +185,8 @@ def main(github_token, progress): 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") @@ -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, @@ -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, @@ -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: @@ -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() @@ -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() @@ -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']) ) @@ -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( @@ -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) @@ -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'}, @@ -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 @@ -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) @@ -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() @@ -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 @@ -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: