implement CLI

This commit is contained in:
dechert 2023-06-14 13:53:36 +02:00
parent 9a2f1f7cb8
commit b74fd6695f
3 changed files with 97 additions and 61 deletions

View File

@ -1,61 +1,73 @@
# TODOs:
# upload on github
# add CLI interface
# add config file
import json
import requests
# curl --request GET --header "PRIVATE-TOKEN: <your_access_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: <your_access_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: <your_access_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: <your_access_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

16
glcifinder-cli.py Normal file
View File

@ -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()

8
requirements.txt Normal file
View File

@ -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