From edc665d55011581a5ae6bec0b615e641c903f398 Mon Sep 17 00:00:00 2001 From: Lucas Gravley <29484535+admiralAwkbar@users.noreply.github.com> Date: Tue, 4 Feb 2020 09:03:29 -0600 Subject: [PATCH] more bad code --- .../test/javascript/javascript_bad_1.js | 3 + .automation/test/python/python_bad_1.py | 292 ++++++++---------- .automation/test/python/python_good_1.py | 290 ++++++++--------- 3 files changed, 271 insertions(+), 314 deletions(-) diff --git a/.automation/test/javascript/javascript_bad_1.js b/.automation/test/javascript/javascript_bad_1.js index 9f2fae8d..d79300ed 100644 --- a/.automation/test/javascript/javascript_bad_1.js +++ b/.automation/test/javascript/javascript_bad_1.js @@ -16,6 +16,9 @@ var orgRepos = [] // var creator = "" +var foo = someFunction(); +var bar = a + 1; + http.createServer(function (req, res) { handler(req, res, function (err) { console.log(err) diff --git a/.automation/test/python/python_bad_1.py b/.automation/test/python/python_bad_1.py index 24d3ea83..843aad50 100644 --- a/.automation/test/python/python_bad_1.py +++ b/.automation/test/python/python_bad_1.py @@ -1,178 +1,156 @@ -#!/usr/bin/python -######################################################## -#### Jenkins Launch Job @LukasG ######################## -######################################################## - -########### -# Imports # -########### import json -import string -import os -import subprocess -import requests +from os import getenv, path +from pprint import pprint +import sys -########### -# GLOBALS # -########### -jenkinsJob = # Job passed from command line to run -triggerJobUrl = "" # Url of the created trigger job -parameters = "" # parameters string passed from command line +import click # pylint: disable=import-error +from dotenv import load_dotenv # pylint: disable=import-error +import requests # pylint: disable=import-error -##################### -# Jenkins Variables # -##################### -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 +env = load_dotenv() +api_url = getenv(API_URL, default='https://api.github.com/graphql' ) +github_token = getenv("GITHUB_TOKEN", +default=None) -########################################################## -################### SUB ROUTINES BELOW ################### -########################################################## -########################################################## -#### SUB ROUTINE StartJob ################################ -def StartJob(): +if github_token is None + sys.exit("GitHub Token is not set." + + "Please set the GITHUB_TOKEN env variable in your system or " + + "the .env file of your project.") - # Build the master Url - url = "" +client_id = getenv(CLIENT_ID, default='copy_labels.py') +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"): - url = "http://%s:%s@%s/job/%s/build?token=%s" % (user,key,jenkinsUrl,jenkinsJob,token) +def create_label(repo_id, label): + """ + 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: - 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 - #print "Url:[%s]" %(url) +def delete_label(label_id): + """ + Delete the specified label + :param label_id: Label's node id. + :type label_id: str + :return: Github API request response. + """ - # build the header - headers = {"Content-Type": "application/json"} + query_variables = { + "deleteLabelInput": { + "clientMutationId": client_id, + "id": label_id, + } + } - # Send the request - response = requests.post(url, headers=headers) + with open(path.join(path.dirname(__file__), 'queries/delete_label.gql'), 'r') as query_file: + query = "".join(query_file.readlines()) - # Check the response - #print "Response:[%s]" % (response) - if (response.status_code !== 201): - print "Failed to Launch Jenkins job:[%s]!" % (jenkinsJobs) - exit(1) + payload = {"query": query, "variables": query_variables} + result = requests.post(api_url, data=json.dumps(payload), headers=headers).json() - #print response.status_code - #print response.json() - #print response.headers['content-type'] + return result - # Need to get Location from headers - #print response.headers['location'] - location = response.headers['location'] +@click.command() +@click.option('--dry', is_flag=True) +@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: - triggerJobUrl = response['executable']['url'] - #print "New Url:[%s]" % (triggerJobUrl) - except: - 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 {source_repo_name} repo.'.format(source_repo_name=source_repo_name)) + _, source_repo_labels = get_labels(source_owner, source_repo_name) + print('Fetched labels for {source_repo_name}'.format(source_repo_name=source_repo_name)) -########################################################## -########################################################## -########################################################## -########################## MAIN ########################## -########################################################## + 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)) -# Need to split if there passed as a single var -if len(sys.argv) == 2: + filtered_labels = list(filter(lambda x: x not in target_repo_labels, source_repo_labels)) - # 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: + print('Preparing to created {label_count} labels in {target_repo}'.format( + label_count=len(filtered_labels), target_repo=target_repo)) - else: - jenkinsJob=sys.argv[1] - parameters="NONE" + for label in filtered_labels: + create_label(target_repo_id, label) + except Exception as error: + sys.exit(error) - #print "DEBUG --- JenkinsJob:[%s]" % (jenkinsJob) - #print "DEBUG --- Paramaters:[%s]" % (parameters) + print('Done') -else: - print "usage: python {0} <,OptionalParametersString>]".format(sys.argv[0]) - sys.exit(2) - -# Start the orphan Job -StartJob() +if __name__ == "__main__": + # 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 diff --git a/.automation/test/python/python_good_1.py b/.automation/test/python/python_good_1.py index a679ffa2..f808c297 100644 --- a/.automation/test/python/python_good_1.py +++ b/.automation/test/python/python_good_1.py @@ -1,179 +1,155 @@ -#!/usr/bin/python -######################################################## -#### Jenkins Launch Job @LukasG ######################## -######################################################## - -########### -# Imports # -########### import json -import string -import os -import subprocess -import requests -import os.path -import commands +from os import getenv, path +from pprint import pprint import sys -########### -# GLOBALS # -########### -jenkinsJob = "" # Job passed from command line to run -triggerJobUrl = "" # Url of the created trigger job -parameters = "" # parameters string passed from command line +import click # pylint: disable=import-error +from dotenv import load_dotenv # pylint: disable=import-error +import requests # pylint: disable=import-error -##################### -# Jenkins Variables # -##################### -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 +env = load_dotenv() +api_url = getenv('API_URL', default='https://api.github.com/graphql') +github_token = getenv("GITHUB_TOKEN", default=None) -########################################################## -################### SUB ROUTINES BELOW ################### -########################################################## -########################################################## -#### SUB ROUTINE StartJob ################################ -def StartJob(): +if github_token is None: + sys.exit("GitHub Token is not set." + + "Please set the GITHUB_TOKEN env variable in your system or " + + "the .env file of your project.") - # Build the master Url - url = "" +client_id = getenv('CLIENT_ID', default='copy_labels.py') +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"): - url = "http://%s:%s@%s/job/%s/build?token=%s" % (user,key,jenkinsUrl,jenkinsJob,token) +def create_label(repo_id, label): + """ + 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: - 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 - #print "Url:[%s]" %(url) +def delete_label(label_id): + """ + Delete the specified label + :param label_id: Label's node id. + :type label_id: str + :return: Github API request response. + """ - # build the header - headers = {"Content-Type": "application/json"} + query_variables = { + "deleteLabelInput": { + "clientMutationId": client_id, + "id": label_id, + } + } - # Send the request - response = requests.post(url, headers=headers) + with open(path.join(path.dirname(__file__), 'queries/delete_label.gql'), 'r') as query_file: + query = "".join(query_file.readlines()) - # Check the response - #print "Response:[%s]" % (response) - if (response.status_code != 201): - print "Failed to Launch Jenkins job:[%s]!" % (jenkinsJob) + payload = {"query": query, "variables": query_variables} + result = requests.post(api_url, data=json.dumps(payload), headers=headers).json() - #print response.status_code - #print response.json() - #print response.headers['content-type'] + return result - # Need to get Location from headers - #print response.headers['location'] - location = response.headers['location'] +@click.command() +@click.option('--dry', is_flag=True) +@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: - triggerJobUrl = response['executable']['url'] - #print "New Url:[%s]" % (triggerJobUrl) - except: - 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 {source_repo_name} repo.'.format(source_repo_name=source_repo_name)) + _, source_repo_labels = get_labels(source_owner, source_repo_name) + print('Fetched labels for {source_repo_name}'.format(source_repo_name=source_repo_name)) -########################################################## -########################################################## -########################################################## -########################## MAIN ########################## -########################################################## + 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)) -# Need to split if there passed as a single var -if len(sys.argv) == 2: + filtered_labels = list(filter(lambda x: x not in target_repo_labels, source_repo_labels)) - # 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: + print('Preparing to created {label_count} labels in {target_repo}'.format( + label_count=len(filtered_labels), target_repo=target_repo)) - else: - jenkinsJob=sys.argv[1] - parameters="NONE" + for label in filtered_labels: + create_label(target_repo_id, label) + except Exception as error: + sys.exit(error) - #print "DEBUG --- JenkinsJob:[%s]" % (jenkinsJob) - #print "DEBUG --- Paramaters:[%s]" % (parameters) + print('Done') -else: - print "usage: python {0} <,OptionalParametersString>]".format(sys.argv[0]) - sys.exit(2) - -# Start the orphan Job -StartJob() +if __name__ == "__main__": + # 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