Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow pushing from forked repos if the secure environment variable is set #343

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion doctr/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
36 changes: 23 additions & 13 deletions doctr/travis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)

Expand All @@ -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)])
Expand All @@ -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',
'[email protected]:{deploy_repo}.git'.format(deploy_repo=deploy_repo)])
Expand Down Expand Up @@ -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
"""
Expand All @@ -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)
Expand Down