implement CLI
This commit is contained in:
parent
9a2f1f7cb8
commit
b74fd6695f
@ -2,6 +2,7 @@
|
||||
# upload on github
|
||||
# add CLI interface
|
||||
# add config file
|
||||
# add unit test
|
||||
|
||||
import json
|
||||
|
||||
@ -16,31 +17,41 @@ import requests
|
||||
|
||||
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_acceptance_jobs_that_were_run(list_of_jobs):
|
||||
name_filter = filter(lambda x: x['name'] == 'deploy-acceptance' and x['status'] == 'success', list_of_jobs)
|
||||
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
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
NUMBER_OF_ITEMS_PER_PAGE = 100
|
||||
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)
|
||||
|
||||
url_template = 'https://gitlab.atb-bremen.de/api/v4/projects/244/jobs?per_page={}&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, 1))
|
||||
iterations = list(range(0, number_of_pages))
|
||||
|
||||
response_array = []
|
||||
|
||||
@ -48,8 +59,9 @@ if __name__ == '__main__':
|
||||
'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(NUMBER_OF_ITEMS_PER_PAGE, i), headers=headers)
|
||||
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))
|
||||
@ -57,5 +69,5 @@ if __name__ == '__main__':
|
||||
import itertools
|
||||
|
||||
flat_list_of_jobs = list(itertools.chain(*response_array))
|
||||
find_acceptance_jobs_that_were_run(flat_list_of_jobs)
|
||||
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
16
glcifinder-cli.py
Normal 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
8
requirements.txt
Normal 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
|
Loading…
x
Reference in New Issue
Block a user