diff --git a/doctr/__init__.py b/doctr/__init__.py index 53b3a702..e088bc23 100644 --- a/doctr/__init__.py +++ b/doctr/__init__.py @@ -1,6 +1,6 @@ from .local import (encrypt_variable, encrypt_to_file, GitHub_post, generate_GitHub_token, upload_GitHub_deploy_key, generate_ssh_key, - check_repo_exists, guess_github_repo) + check_repo_exists, guess_github_repo, activate_travis) from .travis import (decrypt_file, setup_deploy_key, get_token, run, setup_GitHub_push, checkout_deploy_branch, deploy_branch_exists, set_git_user_email, create_deploy_branch, copy_to_tmp, sync_from_log, @@ -9,7 +9,7 @@ __all__ = [ 'encrypt_variable', 'encrypt_to_file', 'GitHub_post', 'generate_GitHub_token', 'upload_GitHub_deploy_key', 'generate_ssh_key', - 'check_repo_exists', 'guess_github_repo', + 'check_repo_exists', 'guess_github_repo', 'activate_travis', 'decrypt_file', 'setup_deploy_key', 'get_token', 'run', 'setup_GitHub_push', 'set_git_user_email', 'checkout_deploy_branch', 'deploy_branch_exists', diff --git a/doctr/__main__.py b/doctr/__main__.py index 86bcbe93..34475f09 100644 --- a/doctr/__main__.py +++ b/doctr/__main__.py @@ -36,7 +36,7 @@ from .local import (generate_GitHub_token, encrypt_variable, encrypt_to_file, upload_GitHub_deploy_key, generate_ssh_key, check_repo_exists, - GitHub_login, guess_github_repo, AuthenticationFailed) + GitHub_login, guess_github_repo, activate_travis, AuthenticationFailed) from .travis import (setup_GitHub_push, commit_docs, push_docs, get_current_repo, sync_from_log, find_sphinx_build_dir, run, get_travis_branch, copy_to_tmp, checkout_deploy_branch) @@ -397,7 +397,27 @@ def configure(args, parser): else: build_repo = input("What repo do you want to build the docs for (org/reponame, like 'drdoctr/doctr')? ") is_private = check_repo_exists(build_repo, service='github', **login_kwargs) - check_repo_exists(build_repo, service='travis') + try: + check_repo_exists(build_repo, service='travis') + except RuntimeError: + if not args.upload_key: + # Activating a repo on Travis requires authentication + raise + activate = 'x' + while activate not in 'yn': + activate = input("{build_repo} is not activated on Travis. Would you like to activate it? [Y/n] ".format(build_repo=build_repo)) + if not activate: + activate = 'y' + activate = activate[0].lower() + if activate == 'n': + raise + tld = '' + while tld not in ['.com', '.org']: + tld = input("Would you like to activate on travis-ci.org or travis-ci.com? [.org] ") + if not tld: + tld = '.org' + activate_travis(build_repo, tld=tld, **login_kwargs) + get_build_repo = True except RuntimeError as e: print(red('\n{!s:-^{}}\n'.format(e, 70))) diff --git a/doctr/local.py b/doctr/local.py index 9a6b1fdb..af241fb8 100644 --- a/doctr/local.py +++ b/doctr/local.py @@ -296,3 +296,33 @@ def guess_github_repo(): if not m: return False return m.group(1) + +def activate_travis(repo, tld='.org', **login_kwargs): + APIv2 = {'Accept': 'application/vnd.travis-ci.2+json'} + APIv3 = {"Travis-API-Version": "3"} + _headers = { + 'Content-Type': 'application/json', + 'User-Agent': 'MyClient/1.0.0', + } + headersv2 = {**_headers, **APIv2} + headersv3 = {**_headers, **APIv3} + + token_id = None + try: + print("I need to generate a temporary token with GitHub to authenticate with Travis.") + print("It will be deleted immediately. If you still see it after this at https://github.com/settings/tokens after please delete it manually.") + # /auth/github doesn't seem to exist in the Travis API v3. + tok_dict = generate_GitHub_token(scopes=["read:org", "user:email", "repo"], + note="temporary token for doctr to auth against travis (delete me)", + **login_kwargs) + data = {'github_token': tok_dict['token']} + token_id = tok_dict['id'] + res = requests.post('https://api.travis-ci{tld}/auth/github'.format(tld=tld), data=json.dumps(data), headers=headersv2) + res.raise_for_status() + headersv3['Authorization'] = 'token {}'.format(res.json()['access_token']) + res = requests.get('https://api.travis-ci{tld}/repo/{repo}/activate'.format(tld=tld, repo=urllib.parse.quote(repo, safe='')), headers=headersv3) + res.raise_for_status() + finally: + # Remove temporary GH token + if token_id: + delete_GitHub_token(token_id, **login_kwargs)