Get more info about a PR.

Since the Jenkins PR plugin does now set it in the environ.
This commit is contained in:
Pedro Algarvio 2014-01-10 17:24:55 +00:00
parent 453d8e5d8e
commit eaf3a50725

View file

@ -19,6 +19,7 @@ import hashlib
import optparse
import subprocess
# Import Salt libs
try:
from salt.utils.nb_popen import NonBlockingPopen
except ImportError:
@ -39,6 +40,12 @@ except ImportError:
)
from nb_popen import NonBlockingPopen
# Import 3rd-party libs
try:
import github
HAS_GITHUB = True
except ImportError:
HAS_GITHUB = False
def generate_vm_name(platform):
'''
@ -77,13 +84,46 @@ def echo_parseable_environment(options):
Echo NAME=VAL parseable output
'''
name = generate_vm_name(options.platform)
output = (
'JENKINS_SALTCLOUD_VM_PROVIDER={provider}\n'
'JENKINS_SALTCLOUD_VM_PLATFORM={platform}\n'
'JENKINS_SALTCLOUD_VM_NAME={name}\n').format(name=name,
provider=options.provider,
platform=options.platform)
sys.stdout.write(output)
output = [
'JENKINS_SALTCLOUD_VM_PROVIDER={0}'.format(options.provider),
'JENKINS_SALTCLOUD_VM_PLATFORM={0}'.format(options.platform),
'JENKINS_SALTCLOUD_VM_NAME={0}'.format(name=name)
]
if 'ghprbPullId' in os.environ:
# This is a Jenkins triggered Pull Request
# We need some more data about the Pull Request available to the
# environment
if not HAS_GITHUB:
print('# The script NEEDS the github python package installed')
sys.stdout.write('\n'.join(output))
sys.stdout.flush()
return
github_access_token_path = os.path.join(
os.environ['JENKINS_HOME'], '.github_token'
)
if not os.path.isfile(github_access_token_path):
print(
'# The github token file({0}) does not exit'.format(
github_access_token_path
)
)
sys.stdout.write('\n'.join(output))
sys.stdout.flush()
return
GITHUB = github.Github(open(github_access_token_path).read().strip())
REPO = GITHUB.get_repo('saltstack/salt')
try:
PR = REPO.get_pull(int(os.environ['ghprbPullId']))
output.extend([
'SALT_PR_GIT_URL={0}'.format(PR.head.repo.clone_url),
'SALT_PR_GIT_COMMIT={0}'.format(PR.head.sha)
])
except ValueError:
print('# Failed to get the PR id from the environment')
pass
sys.stdout.write('\n'.join(output))
sys.stdout.flush()