From b74fd6695fb0c2e4b626ac6d6d1edee1be405cca Mon Sep 17 00:00:00 2001 From: dechert Date: Wed, 14 Jun 2023 13:53:36 +0200 Subject: [PATCH] implement CLI --- gitlab-api.py => gitlab_api_finder.py | 134 ++++++++++++++------------ glcifinder-cli.py | 16 +++ requirements.txt | 8 ++ 3 files changed, 97 insertions(+), 61 deletions(-) rename gitlab-api.py => gitlab_api_finder.py (55%) create mode 100644 glcifinder-cli.py create mode 100644 requirements.txt diff --git a/gitlab-api.py b/gitlab_api_finder.py similarity index 55% rename from gitlab-api.py rename to gitlab_api_finder.py index f8dfe09..e091575 100644 --- a/gitlab-api.py +++ b/gitlab_api_finder.py @@ -1,61 +1,73 @@ -# TODOs: -# upload on github -# add CLI interface -# add config file - -import json - -import requests - -# curl --request GET --header "PRIVATE-TOKEN: " "https://gitlab.example.com/api/v4/namespaces?per_page=50" - -# https://docs.gitlab.com/ee/api/rest/index.html#pagination - -# curl --globoff --header "PRIVATE-TOKEN: " "https://gitlab.example.com/api/v4/projects/1/jobs?scope[]=pending&scope[]=running" - - -import config_parser - - -# print(response_array) -# - -def write_to_file(response_array): - f = open("gitlab-jobs1.txt", "a") - f.write(str(response_array)) - f.close() - - -def find_acceptance_jobs_that_were_run(list_of_jobs): - name_filter = filter(lambda x: x['name'] == 'deploy-acceptance' and x['status'] == 'success', list_of_jobs) - filtered_list_of_jobs = list(name_filter) - - print(filtered_list_of_jobs) - return filtered_list_of_jobs - - -if __name__ == '__main__': - NUMBER_OF_ITEMS_PER_PAGE = 100 - - url_template = 'https://gitlab.atb-bremen.de/api/v4/projects/244/jobs?per_page={}&page={}' - - # get the last 100x50 jobs = 5000 - iterations = list(range(0, 1)) - - response_array = [] - - headers = { - 'PRIVATE-TOKEN': config_parser.token - } - - for i in iterations: - response = requests.request("GET", url_template.format(NUMBER_OF_ITEMS_PER_PAGE, i), headers=headers) - json_response = response.text - # print(response.text) - response_array.append(json.loads(json_response)) - - import itertools - - flat_list_of_jobs = list(itertools.chain(*response_array)) - find_acceptance_jobs_that_were_run(flat_list_of_jobs) - # TODO filter output for relevant fields +# TODOs: +# upload on github +# add CLI interface +# add config file +# add unit test + +import json + +import requests + +# curl --request GET --header "PRIVATE-TOKEN: " "https://gitlab.example.com/api/v4/namespaces?per_page=50" + +# https://docs.gitlab.com/ee/api/rest/index.html#pagination + +# curl --globoff --header "PRIVATE-TOKEN: " "https://gitlab.example.com/api/v4/projects/1/jobs?scope[]=pending&scope[]=running" + + +import config_parser + +# print(response_array) +# + +MAX_NUMBER_OF_ITEMS_PER_PAGE = 100 + + +def write_to_file(response_array): + f = open("gitlab-jobs1.txt", "a") + f.write(str(response_array)) + f.close() + + +def find_jobs_by_name_that_were_run(list_of_jobs, job_name): + name_filter = filter(lambda x: x['name'] == job_name and x['status'] == 'success', list_of_jobs) + filtered_list_of_jobs = list(name_filter) + + print(filtered_list_of_jobs) + return filtered_list_of_jobs + + +def calc_number_of_pages_number(total_number): + if total_number <= MAX_NUMBER_OF_ITEMS_PER_PAGE: + return total_number, 1 + else: + return MAX_NUMBER_OF_ITEMS_PER_PAGE, round(total_number / MAX_NUMBER_OF_ITEMS_PER_PAGE) + + +def find_jobs(count, project_id, job_name): + number_of_items_per_page, number_of_pages = calc_number_of_pages_number(count) + # NUMBER_OF_ITEMS_PER_PAGE = 100 + + url_template = 'https://gitlab.atb-bremen.de/api/v4/projects/{}/jobs?per_page={}&page={}' + + # get the last 100x50 jobs = 5000 + iterations = list(range(0, number_of_pages)) + + response_array = [] + + headers = { + 'PRIVATE-TOKEN': config_parser.token + } + + print("getting {} jobs from project {}...".format(count, project_id)) + for i in iterations: + response = requests.request("GET", url_template.format(project_id, number_of_items_per_page, i), headers=headers) + json_response = response.text + # print(response.text) + response_array.append(json.loads(json_response)) + + import itertools + + flat_list_of_jobs = list(itertools.chain(*response_array)) + find_jobs_by_name_that_were_run(flat_list_of_jobs, job_name) + # TODO filter output for relevant fields diff --git a/glcifinder-cli.py b/glcifinder-cli.py new file mode 100644 index 0000000..5862722 --- /dev/null +++ b/glcifinder-cli.py @@ -0,0 +1,16 @@ +import click + +import gitlab_api_finder + + +@click.command() +@click.option("--count", default=1, help="Number of jobs to search.") +@click.option("--projectid", prompt="Id of Gitlab Project", help="Id of Gitlab Project.") +@click.option("--jobname", prompt="name of job to filter", help="Id of Gitlab Project.") +def hello(count, projectid, jobname): + gitlab_api_finder.find_jobs(count, projectid, jobname) + + +# example usage: python glcifinder-cli.py --count 1000 --projectid 244 --jobname deploy-test +if __name__ == '__main__': + hello() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..7934798 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,8 @@ +certifi==2023.5.7 +charset-normalizer==3.1.0 +click==8.1.3 +colorama==0.4.6 +idna==3.4 +PyYAML==6.0 +requests==2.31.0 +urllib3==2.0.3