Forked from wehappyfew/fetch_jenkins_branch_name.py
Last active
August 29, 2015 14:24
-
-
Save alexmerser/6b9ae7de2a7256a653ae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__author__ = 'wehappyfew' | |
# the url of jenkins config.xml | |
jenkins_url = 'http://11.11.111.11:8686/job/TheJob/config.xml' | |
j_user = "someone" | |
j_pass = "somepass" | |
def get_jenkins_branch_name(jenkins_url, j_user, j_pass): | |
""" | |
The function goes to the provided jenkins XML url, | |
authenticates with an authenticated user, | |
grabs the xml, | |
turns it to dictionary, | |
searches inside the dictionary for the branch name | |
""" | |
import requests,xmltodict | |
from requests.auth import HTTPBasicAuth | |
# get the url with an authenticated user | |
response = requests.get(jenkins_url, auth=HTTPBasicAuth(j_user, j_pass)) #the response must be 200 | |
# the content of the response is the xml | |
xml = response.content | |
# parse the xml to a dictionary | |
jenkins_dict = xmltodict.parse(xml) | |
# grab the actual branch name | |
branch_name = jenkins_dict['project']['scm']['branches']['hudson.plugins.git.BranchSpec']['name'] | |
return branch_name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment