more bad code

This commit is contained in:
Lucas Gravley 2020-02-04 09:03:29 -06:00
parent fc9d45e49d
commit edc665d550
3 changed files with 271 additions and 314 deletions

View file

@ -16,6 +16,9 @@ var orgRepos = []
// var creator = "" // var creator = ""
var foo = someFunction();
var bar = a + 1;
http.createServer(function (req, res) { http.createServer(function (req, res) {
handler(req, res, function (err) { handler(req, res, function (err) {
console.log(err) console.log(err)

View file

@ -1,178 +1,156 @@
#!/usr/bin/python
########################################################
#### Jenkins Launch Job @LukasG ########################
########################################################
###########
# Imports #
###########
import json import json
import string from os import getenv, path
import os from pprint import pprint
import subprocess import sys
import requests
########### import click # pylint: disable=import-error
# GLOBALS # from dotenv import load_dotenv # pylint: disable=import-error
########### import requests # pylint: disable=import-error
jenkinsJob = # Job passed from command line to run
triggerJobUrl = "" # Url of the created trigger job
parameters = "" # parameters string passed from command line
##################### env = load_dotenv()
# Jenkins Variables # api_url = getenv(API_URL, default='https://api.github.com/graphql' )
##################### github_token = getenv("GITHUB_TOKEN",
user = os.environ.get('HUBOT_JENKINS_AUTH_USER') # User to connect to Jenkins default=None)
key = os.environ.get('HUBOT_JENKINS_AUTH_PASSWD') # API key to connect with
jenkinsUrl = os.environ.get('HUBOT_JENKINS_SHORT_URL') # jenkins url
token = os.environ.get('HUBOT_JENKINS_SECRET') # Special key
########################################################## if github_token is None
################### SUB ROUTINES BELOW ################### sys.exit("GitHub Token is not set." +
########################################################## "Please set the GITHUB_TOKEN env variable in your system or " +
########################################################## "the .env file of your project.")
#### SUB ROUTINE StartJob ################################
def StartJob():
# Build the master Url client_id = getenv(CLIENT_ID, default='copy_labels.py')
url = "" headers = {
'Authorization': 'bearer {github_token}'.format(github_token=github_token),
'Accept': 'application/vnd.github.bane-preview+json'
'Content-Type': 'application/json'
}
if (parameters = 'NONE' or parameters == "null"): def create_label(repo_id, label):
url = "http://%s:%s@%s/job/%s/build?token=%s" % (user,key,jenkinsUrl,jenkinsJob,token) """
Create label in the supplied repo.
:param repo_id: Unique ID that represents the repo in GitHub
:type repo_id: str
:param label: Object with label information.
:type label: dict
:return: GitHub API request response
"""
query_variables = {
"createLabelInput": {
"color": label["color"],
"description": label["description"],
"name": label["name"],
"repositoryId": repo_id
}
}
with open(path.join(path.dirname(__file__), 'queries/create_label.gql'), 'r') as query_file:
query = "".join(query_file.readlines())
payload = {"query": query, "variables": query_variables}
response = requests.post(api_url, data=json.dumps(payload), headers=headers).json()
print('Created label {label}'.format(label=label["name"]))
return response
def get_labels(owner, repo):
"""
Gets a list of labels from the supplied repo.
:param owner: Repo owner GitHub login.
:type owner: str
:param repo: Repository name.
:type repo: str
:return: A tuple with the GitHub id for the repository and a list of labels defined in the repository
"""
query_variables = { "owner": owner, "name": repo, }
with open(path.join(path.dirname(__file__), 'queries/get_repo_data.gql'), 'r') as query_file:
query = "".join(query_file.readlines())
payload = {"query": query, "variables": query_variables}
response = requests.post(api_url, data=json.dumps(payload), headers=headers)
status_code = response.status_code
result = response.json()
if status_code >= 200 and status_code <= 300:
repo_id = result["data"]["repository"]["id"]
labels = result["data"]["repository"]["labels"]["nodes"]
return repo_id, labels
else: else:
url = "http://%s:%s@%s/job/%s/buildWithParameters?%s&token=%s" % (user,key,jenkinsUrl,jenkinsJob,parameters,token) raise Exception(
'[ERROR] getting issue labels. Status Code: {status_code} - Message: {result}'.format(
status_code=status_code, result=result["message"]))
# Print url for debug def delete_label(label_id):
#print "Url:[%s]" %(url) """
Delete the specified label
:param label_id: Label's node id.
:type label_id: str
:return: Github API request response.
"""
# build the header query_variables = {
headers = {"Content-Type": "application/json"} "deleteLabelInput": {
"clientMutationId": client_id,
"id": label_id,
}
}
# Send the request with open(path.join(path.dirname(__file__), 'queries/delete_label.gql'), 'r') as query_file:
response = requests.post(url, headers=headers) query = "".join(query_file.readlines())
# Check the response payload = {"query": query, "variables": query_variables}
#print "Response:[%s]" % (response) result = requests.post(api_url, data=json.dumps(payload), headers=headers).json()
if (response.status_code !== 201):
print "Failed to Launch Jenkins job:[%s]!" % (jenkinsJobs)
exit(1)
#print response.status_code return result
#print response.json()
#print response.headers['content-type']
# Need to get Location from headers @click.command()
#print response.headers['location'] @click.option('--dry', is_flag=True)
location = response.headers['location'] @click.argument('source_repo')
@click.argument('target_repo')
def copy_labels(source_repo, target_repo, dry):
"""
Copy labels from the source repository to the target repository.
\f
:param source: The full name of a GitHub repo from where the labels will be copied from. Eg. github/opensourcefriday
:type source: str
:param target: The full name of a GitHub repo to where the labels will be copied. Eg. github/opensourcefriday
:type target: str
:return:
"""
source_owner, source_repo_name = source_repo.split("/")
target_owner, target_repo_name = target_repo.split("/")
# Allow jenkins to queue
AllowQueue()
AllowQueue1()
# Need to get the jobid
GetJobId(location)
# Closing prints
print "Jenkins Job Link:"
# Removing http:// to shorten the length
cleanedUrl = triggerJobUrl[7:]
# Print the goods
print "http://%s" % (cleanedUrls)
##########################################################
#### SUB ROUTINE AllowQueue ##############################
def AllowQueue():
# Need to sleep for some time to allow jenkins to set the job
# Jenkins is a dumb bastard who queues shit up, then waits for it to get a real job
# So we must wait for it...
cmd = "sleep 13s 2>&1"
#print "Waiting for few secods to allow jenkins to queue job"
status, output = commands.getstatusoutput(cmd)
#print "Status:[%s]" % (status)
#print "Output:[%s]" % (output)
# We have a success
#if (status == 0):
##########################################################
#### SUB ROUTINE GetJobId ################################
def GetJobId(location):
# Load Globals
global triggerJobUrl
# Need to get the number out of the location string
# example: http://internal-jenkins-elb-340832960.us-east-1.elb.amazonaws.com/queue/item/29529/
# Remove any space chars
location.replace(" ","")
# Remove the trailing "/" char
location = location[:-1]
# Split the string on the last "/"
var1,var2 = location.rsplit('/',1)
location = var2
# Need to call jenkins with the queued location to get back job
url = "http://%s:%s@%s/queue/item/%s/api/json" % (user,key,jenkinsUrl,location)
# Build the header
headers = {"Content-Type": "application/json"}
# Call to jenkins
response = requests.post(url, headers=headers)
# check the response back from Jenkins
if (response.status_code != 200):
genericLink = "%s/job/%s" % (jenkinsUrl,jenkinsJob)
print "Failed to get specific Jenkins job Url!"
print "Generic Link:"
print "%s" % (genericLink)
exit(1)
#print response.json()
#print response.status_code
# Need to convert to json for parsing
response = response.json()
# Try to pull out the ProjectID
try: try:
triggerJobUrl = response['executable']['url'] print('Fetching labels for {source_repo_name} repo.'.format(source_repo_name=source_repo_name))
#print "New Url:[%s]" % (triggerJobUrl) _, source_repo_labels = get_labels(source_owner, source_repo_name)
except: print('Fetched labels for {source_repo_name}'.format(source_repo_name=source_repo_name))
genericLink = "%s/job/%s" % (jenkinsUrl,jenkinsJob)
print "Failed to get specific jenkinsJob job Url!"
print "Generic Link:"
print "%s" % (genericLink)
exit(1)
########################################################## print('Fetching labels for {target_repo_name} repo.'.format(target_repo_name=target_repo_name))
########################################################## target_repo_id, target_repo_labels = get_labels(target_owner, target_repo_name)
########################################################## print('Fetched labels for {target_repo_name}'.format(target_repo_name=target_repo_name))
########################## MAIN ##########################
##########################################################
# Need to split if there passed as a single var filtered_labels = list(filter(lambda x: x not in target_repo_labels, source_repo_labels))
if len(sys.argv) == 2:
# Need to see if we have job and parameters
if "," in sys.argv[1]:
jenkinsJob,parameters=sys.argv[1].split(",",1)
if dry:
print('This is just a dry run. No labels will be copied/created.')
print('{label_count} labels would have been created.'.format(label_count=len(filtered_labels)))
pprint(filtered_labels, indent=4)
else: else:
jenkinsJob=sys.argv[1] print('Preparing to created {label_count} labels in {target_repo}'.format(
parameters="NONE" label_count=len(filtered_labels), target_repo=target_repo))
#print "DEBUG --- JenkinsJob:[%s]" % (jenkinsJob) for label in filtered_labels:
#print "DEBUG --- Paramaters:[%s]" % (parameters) create_label(target_repo_id, label)
except Exception as error:
sys.exit(error)
else: print('Done')
print "usage: python {0} <JenkinsJob><,OptionalParametersString>]".format(sys.argv[0])
sys.exit(2)
# Start the orphan Job if __name__ == "__main__":
StartJob() # Pylint doesn't know that @click.command takes care of injecting the
# function parameters. Disabling Pylint error.
copy_labels() # pylint: disable=no-value-for-parameter

View file

@ -1,179 +1,155 @@
#!/usr/bin/python
########################################################
#### Jenkins Launch Job @LukasG ########################
########################################################
###########
# Imports #
###########
import json import json
import string from os import getenv, path
import os from pprint import pprint
import subprocess
import requests
import os.path
import commands
import sys import sys
########### import click # pylint: disable=import-error
# GLOBALS # from dotenv import load_dotenv # pylint: disable=import-error
########### import requests # pylint: disable=import-error
jenkinsJob = "" # Job passed from command line to run
triggerJobUrl = "" # Url of the created trigger job
parameters = "" # parameters string passed from command line
##################### env = load_dotenv()
# Jenkins Variables # api_url = getenv('API_URL', default='https://api.github.com/graphql')
##################### github_token = getenv("GITHUB_TOKEN", default=None)
user = os.environ.get('HUBOT_JENKINS_AUTH_USER') # User to connect to Jenkins
key = os.environ.get('HUBOT_JENKINS_AUTH_PASSWD') # API key to connect with
jenkinsUrl = os.environ.get('HUBOT_JENKINS_SHORT_URL') # jenkins url
token = os.environ.get('HUBOT_JENKINS_SECRET') # Special key
########################################################## if github_token is None:
################### SUB ROUTINES BELOW ################### sys.exit("GitHub Token is not set." +
########################################################## "Please set the GITHUB_TOKEN env variable in your system or " +
########################################################## "the .env file of your project.")
#### SUB ROUTINE StartJob ################################
def StartJob():
# Build the master Url client_id = getenv('CLIENT_ID', default='copy_labels.py')
url = "" headers = {
'Authorization': 'bearer {github_token}'.format(github_token=github_token),
'Accept': 'application/vnd.github.bane-preview+json',
'Content-Type': 'application/json'
}
if (parameters == "NONE" or parameters =="null"): def create_label(repo_id, label):
url = "http://%s:%s@%s/job/%s/build?token=%s" % (user,key,jenkinsUrl,jenkinsJob,token) """
Create label in the supplied repo.
:param repo_id: Unique ID that represents the repo in GitHub
:type repo_id: str
:param label: Object with label information.
:type label: dict
:return: GitHub API request response
"""
query_variables = {
"createLabelInput": {
"color": label["color"],
"description": label["description"],
"name": label["name"],
"repositoryId": repo_id
}
}
with open(path.join(path.dirname(__file__), 'queries/create_label.gql'), 'r') as query_file:
query = "".join(query_file.readlines())
payload = {"query": query, "variables": query_variables}
response = requests.post(api_url, data=json.dumps(payload), headers=headers).json()
print('Created label {label}'.format(label=label["name"]))
return response
def get_labels(owner, repo):
"""
Gets a list of labels from the supplied repo.
:param owner: Repo owner GitHub login.
:type owner: str
:param repo: Repository name.
:type repo: str
:return: A tuple with the GitHub id for the repository and a list of labels defined in the repository
"""
query_variables = { "owner": owner, "name": repo, }
with open(path.join(path.dirname(__file__), 'queries/get_repo_data.gql'), 'r') as query_file:
query = "".join(query_file.readlines())
payload = {"query": query, "variables": query_variables}
response = requests.post(api_url, data=json.dumps(payload), headers=headers)
status_code = response.status_code
result = response.json()
if status_code >= 200 and status_code <= 300:
repo_id = result["data"]["repository"]["id"]
labels = result["data"]["repository"]["labels"]["nodes"]
return repo_id, labels
else: else:
url = "http://%s:%s@%s/job/%s/buildWithParameters?%s&token=%s" % (user,key,jenkinsUrl,jenkinsJob,parameters,token) raise Exception(
'[ERROR] getting issue labels. Status Code: {status_code} - Message: {result}'.format(
status_code=status_code, result=result["message"]))
# Print url for debug def delete_label(label_id):
#print "Url:[%s]" %(url) """
Delete the specified label
:param label_id: Label's node id.
:type label_id: str
:return: Github API request response.
"""
# build the header query_variables = {
headers = {"Content-Type": "application/json"} "deleteLabelInput": {
"clientMutationId": client_id,
"id": label_id,
}
}
# Send the request with open(path.join(path.dirname(__file__), 'queries/delete_label.gql'), 'r') as query_file:
response = requests.post(url, headers=headers) query = "".join(query_file.readlines())
# Check the response payload = {"query": query, "variables": query_variables}
#print "Response:[%s]" % (response) result = requests.post(api_url, data=json.dumps(payload), headers=headers).json()
if (response.status_code != 201):
print "Failed to Launch Jenkins job:[%s]!" % (jenkinsJob)
#print response.status_code return result
#print response.json()
#print response.headers['content-type']
# Need to get Location from headers @click.command()
#print response.headers['location'] @click.option('--dry', is_flag=True)
location = response.headers['location'] @click.argument('source_repo')
@click.argument('target_repo')
def copy_labels(source_repo, target_repo, dry):
"""
Copy labels from the source repository to the target repository.
\f
:param source: The full name of a GitHub repo from where the labels will be copied from. Eg. github/opensourcefriday
:type source: str
:param target: The full name of a GitHub repo to where the labels will be copied. Eg. github/opensourcefriday
:type target: str
:return:
"""
source_owner, source_repo_name = source_repo.split("/")
target_owner, target_repo_name = target_repo.split("/")
# Allow jenkins to queue
AllowQueue()
# Need to get the jobid
GetJobId(location)
# Closing prints
print "Jenkins Job Link:"
# Removing http:// to shorten the length
cleanedUrl = triggerJobUrl[7:]
# Print the goods
print "http://%s" % (cleanedUrl)
##########################################################
#### SUB ROUTINE AllowQueue ##############################
def AllowQueue():
# Need to sleep for some time to allow jenkins to set the job
# Jenkins is a dumb bastard who queues shit up, then waits for it to get a real job
# So we must wait for it...
cmd = "sleep 13s 2>&1"
#print "Waiting for few secods to allow jenkins to queue job"
status, output = commands.getstatusoutput(cmd)
#print "Status:[%s]" % (status)
#print "Output:[%s]" % (output)
# We have a success
#if (status == 0):
##########################################################
#### SUB ROUTINE GetJobId ################################
def GetJobId(location):
# Load Globals
global triggerJobUrl
# Need to get the number out of the location string
# example: http://internal-jenkins-elb-340832960.us-east-1.elb.amazonaws.com/queue/item/29529/
# Remove any space chars
location.replace(" ","")
# Remove the trailing "/" char
location = location[:-1]
# Split the string on the last "/"
var1,var2 = location.rsplit('/',1)
location = var2
# Need to call jenkins with the queued location to get back job
url = "http://%s:%s@%s/queue/item/%s/api/json" % (user,key,jenkinsUrl,location)
# Build the header
headers = {"Content-Type": "application/json"}
# Call to jenkins
response = requests.post(url, headers=headers)
# check the response back from Jenkins
if (response.status_code != 200):
genericLink = "%s/job/%s" % (jenkinsUrl,jenkinsJob)
print "Failed to get specific Jenkins job Url!"
print "Generic Link:"
print "%s" % (genericLink)
exit(1)
#print response.json()
#print response.status_code
# Need to convert to json for parsing
response = response.json()
# Try to pull out the ProjectID
try: try:
triggerJobUrl = response['executable']['url'] print('Fetching labels for {source_repo_name} repo.'.format(source_repo_name=source_repo_name))
#print "New Url:[%s]" % (triggerJobUrl) _, source_repo_labels = get_labels(source_owner, source_repo_name)
except: print('Fetched labels for {source_repo_name}'.format(source_repo_name=source_repo_name))
genericLink = "%s/job/%s" % (jenkinsUrl,jenkinsJob)
print "Failed to get specific jenkinsJob job Url!"
print "Generic Link:"
print "%s" % (genericLink)
exit(1)
########################################################## print('Fetching labels for {target_repo_name} repo.'.format(target_repo_name=target_repo_name))
########################################################## target_repo_id, target_repo_labels = get_labels(target_owner, target_repo_name)
########################################################## print('Fetched labels for {target_repo_name}'.format(target_repo_name=target_repo_name))
########################## MAIN ##########################
##########################################################
# Need to split if there passed as a single var filtered_labels = list(filter(lambda x: x not in target_repo_labels, source_repo_labels))
if len(sys.argv) == 2:
# Need to see if we have job and parameters
if "," in sys.argv[1]:
jenkinsJob,parameters=sys.argv[1].split(",",1)
if dry:
print('This is just a dry run. No labels will be copied/created.')
print('{label_count} labels would have been created.'.format(label_count=len(filtered_labels)))
pprint(filtered_labels, indent=4)
else: else:
jenkinsJob=sys.argv[1] print('Preparing to created {label_count} labels in {target_repo}'.format(
parameters="NONE" label_count=len(filtered_labels), target_repo=target_repo))
#print "DEBUG --- JenkinsJob:[%s]" % (jenkinsJob) for label in filtered_labels:
#print "DEBUG --- Paramaters:[%s]" % (parameters) create_label(target_repo_id, label)
except Exception as error:
sys.exit(error)
else: print('Done')
print "usage: python {0} <JenkinsJob><,OptionalParametersString>]".format(sys.argv[0])
sys.exit(2)
# Start the orphan Job if __name__ == "__main__":
StartJob() # Pylint doesn't know that @click.command takes care of injecting the
# function parameters. Disabling Pylint error.
copy_labels() # pylint: disable=no-value-for-parameter