From 7b4fc1f091b8ba6de43825c186c0f2d0949c067d Mon Sep 17 00:00:00 2001 From: Aaron Meurer Date: Fri, 5 Apr 2019 14:43:44 -0600 Subject: [PATCH 1/2] Allow pushing from forked repos if the secure environment variable is set Fixes #342. --- doctr/travis.py | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/doctr/travis.py b/doctr/travis.py index 55f8d2e5..61e3b43b 100644 --- a/doctr/travis.py +++ b/doctr/travis.py @@ -120,7 +120,7 @@ def get_token(): """ token = os.environ.get("GH_TOKEN", None) if not token: - token = "GH_TOKEN environment variable not set" + raise RuntimeError("GH_TOKEN environment variable not set. Make sure you followed the instructions from 'doctr configure' properly. You may need to re-run 'doctr configure' to fix this error.") token = token.encode('utf-8') return token @@ -226,7 +226,6 @@ def setup_GitHub_push(deploy_repo, *, auth_type='deploy_key', branch_whitelist=branch_whitelist, TRAVIS_BRANCH=TRAVIS_BRANCH, TRAVIS_PULL_REQUEST=TRAVIS_PULL_REQUEST, - fork=fork, TRAVIS_TAG=TRAVIS_TAG, build_tags=build_tags) @@ -240,7 +239,18 @@ def setup_GitHub_push(deploy_repo, *, auth_type='deploy_key', print("Adding doctr remote") if canpush: if auth_type == 'token': - token = get_token() + try: + token = get_token() + except RuntimeError: + if fork: + print("This appears to be a fork repo without the Doctr secure environment variable. Not pushing.", file=sys.stderr) + canpush = False + else: + # Rate limits prevent this check from working every time. By default, we + # assume it isn't a fork so that things just work on non-fork builds. + if r.status_code == 403: + print(yellow("Warning: GitHub's API rate limits prevented doctr from detecting if this build is a forked repo. If it is, you may ignore the 'GH_TOKEN environment variable is not set' error that follows. If it is not, you should re-run 'doctr configure'. Note that doctr cannot deploy from fork builds due to limitations in Travis."), file=sys.stderr) + raise run(['git', 'remote', 'add', 'doctr_remote', 'https://{token}@github.com/{deploy_repo}.git'.format(token=token.decode('utf-8'), deploy_repo=deploy_repo)]) @@ -250,11 +260,15 @@ def setup_GitHub_push(deploy_repo, *, auth_type='deploy_key', try: setup_deploy_key(keypath=keypath, key_ext=key_ext, env_name=env_name) except RuntimeError: - # Rate limits prevent this check from working every time. By default, we - # assume it isn't a fork so that things just work on non-fork builds. - if r.status_code == 403: - print(yellow("Warning: GitHub's API rate limits prevented doctr from detecting if this build is a forked repo. If it is, you may ignore the 'DOCTR_DEPLOY_ENCRYPTION_KEY environment variable is not set' error that follows. If it is not, you should re-run 'doctr configure'. Note that doctr cannot deploy from fork builds due to limitations in Travis."), file=sys.stderr) - raise + if fork: + print("This appears to be a fork repo without the Doctr secure environment variable. Not pushing.", file=sys.stderr) + canpush = False + else: + # Rate limits prevent this check from working every time. By default, we + # assume it isn't a fork so that things just work on non-fork builds. + if r.status_code == 403: + print(yellow("Warning: GitHub's API rate limits prevented doctr from detecting if this build is a forked repo. If it is, you may ignore the 'DOCTR_DEPLOY_ENCRYPTION_KEY environment variable is not set' error that follows. If it is not, you should re-run 'doctr configure'. Note that doctr cannot deploy from fork builds due to limitations in Travis."), file=sys.stderr) + raise run(['git', 'remote', 'add', 'doctr_remote', 'git@github.com:{deploy_repo}.git'.format(deploy_repo=deploy_repo)]) @@ -569,7 +583,7 @@ def last_commit_by_doctr(): return False def determine_push_rights(*, branch_whitelist, TRAVIS_BRANCH, - TRAVIS_PULL_REQUEST, TRAVIS_TAG, build_tags, fork): + TRAVIS_PULL_REQUEST, TRAVIS_TAG, build_tags): """Check if Travis is running on ``master`` (or a whitelisted branch) to determine if we can/should push the docs to the deploy repo """ @@ -590,10 +604,6 @@ def determine_push_rights(*, branch_whitelist, TRAVIS_BRANCH, print("The website and docs are not pushed to gh-pages on pull requests", file=sys.stderr) canpush = False - if fork: - print("The website and docs are not pushed to gh-pages on fork builds.", file=sys.stderr) - canpush = False - if last_commit_by_doctr(): print(red("The last commit on this branch was pushed by doctr. Not pushing to " "avoid an infinite build-loop."), file=sys.stderr) From e53aa7fa66c271293a48c9553c0d0180bc94177a Mon Sep 17 00:00:00 2001 From: Aaron Meurer Date: Thu, 2 May 2019 12:15:20 -0600 Subject: [PATCH 2/2] Fix unused variable in a string format call --- doctr/__main__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doctr/__main__.py b/doctr/__main__.py index 87b7782a..8a35510e 100644 --- a/doctr/__main__.py +++ b/doctr/__main__.py @@ -500,7 +500,7 @@ def configure(args, parser): The deploy key has been added for {deploy_repo}. You can go to {deploy_keys_url} to revoke the deploy key.\ - """.format(deploy_repo=deploy_key_repo, deploy_keys_url=deploy_keys_url, keypath=keypath))) + """.format(deploy_repo=deploy_key_repo, deploy_keys_url=deploy_keys_url))) print(header) else: print(header)