diff --git a/README.md b/README.md index 99bb117..4f7c22f 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,12 @@ # gitlab-api-ci-finder (glcifinder) -## How to run -`python glcifinder-cli.py --count 1000 --projectid 244 --jobname deploy-test` +## How to use + - Pre-requisite: Python 3 installed + - install requirements: `pip install -U -r requirements.txt` + - `python glcifinder-cli.py --count 1000 --projectid 244 --jobname deploy-test` + +## TODOs: + - upload on github + - build executable for windows \ No newline at end of file diff --git a/config_parser.py b/config_parser.py index 598bf7d..5eb0ba9 100644 --- a/config_parser.py +++ b/config_parser.py @@ -8,12 +8,13 @@ DEFAULT_CONFIG_FILE_NAME = "config.yml" # else: config_file_to_load = DEFAULT_CONFIG_FILE_NAME -BASE_FODLER = os.path.dirname(os.path.abspath(__file__)) +BASE_FOLDER = os.path.dirname(os.path.abspath(__file__)) -path_to_config = os.path.join(BASE_FODLER, config_file_to_load) +path_to_config = os.path.join(BASE_FOLDER, config_file_to_load) print("opening config file " + path_to_config) -with open(path_to_config, "r") as ymlfile: - cfg = yaml.load(ymlfile, Loader=yaml.FullLoader) +with open(path_to_config, "r") as yml_config_file: + cfg = yaml.load(yml_config_file, Loader=yaml.FullLoader) +# read values from config token = cfg["token"] prod_mode = cfg["prod"] diff --git a/gitlab_api_finder.py b/gitlab_ci_job_finder.py similarity index 78% rename from gitlab_api_finder.py rename to gitlab_ci_job_finder.py index 804a7b7..1cd307d 100644 --- a/gitlab_api_finder.py +++ b/gitlab_ci_job_finder.py @@ -1,30 +1,14 @@ -# 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) -# - +# as per https://docs.gitlab.com/ee/api/rest/index.html#offset-based-pagination MAX_NUMBER_OF_ITEMS_PER_PAGE = 100 def write_to_file(response_array): - f = open("gitlab-jobs1.txt", "a") + f = open("output/gitlab-jobs1.txt", "a") f.write(str(response_array)) f.close() @@ -57,7 +41,6 @@ def filter_relevant_attributes(job): 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={}' @@ -74,12 +57,12 @@ def find_jobs(count, project_id, job_name): 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)) list_of_successfull_jobs = find_jobs_by_name_that_were_run(flat_list_of_jobs, job_name) + # filter output for relevant fields return list(map(filter_relevant_attributes, list_of_successfull_jobs)) diff --git a/glcifinder-cli.py b/glcifinder-cli.py index 2b860a5..5c72c36 100644 --- a/glcifinder-cli.py +++ b/glcifinder-cli.py @@ -1,6 +1,6 @@ import click -import gitlab_api_finder +import gitlab_ci_job_finder @click.command() @@ -8,10 +8,9 @@ import gitlab_api_finder @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.") # TODO add option "to-file" for storing the resulting json on disk -def hello(count, projectid, jobname): - gitlab_api_finder.find_jobs(count, projectid, jobname) +def execute_gl_ci_finder(count, projectid, jobname): + gitlab_ci_job_finder.find_jobs(count, projectid, jobname) -# example usage: python glcifinder-cli.py --count 1000 --projectid 244 --jobname deploy-test if __name__ == '__main__': - hello() + execute_gl_ci_finder() diff --git a/test_glcifinder.py b/test_gitlab_ci_job_finder.py similarity index 51% rename from test_glcifinder.py rename to test_gitlab_ci_job_finder.py index f6b54a9..5a67fd3 100644 --- a/test_glcifinder.py +++ b/test_gitlab_ci_job_finder.py @@ -1,10 +1,10 @@ -import gitlab_api_finder +import gitlab_ci_job_finder -def test(): +def test_find_jobs(): count = 20 projectid = 244 jobname = "prepare" - result = gitlab_api_finder.find_jobs(count, projectid, jobname) + result = gitlab_ci_job_finder.find_jobs(count, projectid, jobname) one_job = result[0] assert one_job['status'] == 'success'