|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import os |
| 4 | +import sys |
| 5 | +import json |
| 6 | +import urllib.error |
| 7 | +import urllib.request |
| 8 | +import subprocess |
| 9 | + |
| 10 | +# Pipelines currently covered by e2e tests |
| 11 | +pipelines_covered_by_e2e = ["docker-build", "docker-build-oci-ta", "docker-build-multi-platform-oci-ta", "fbc-builder"] |
| 12 | + |
| 13 | +# Task list which are covered by e2e tests, generated dynamically |
| 14 | +tasks_covered_by_e2e = [] |
| 15 | + |
| 16 | +# Otherthan tasks and pipelines, related files for which e2e tests needs to be executed |
| 17 | +files_covered_by_e2e = [".tekton/pull-request.yaml", ".tekton/tasks/e2e-test.yaml", ".tekton/scripts/determine-if-e2e-execution-needed.py"] |
| 18 | + |
| 19 | +def add_only_unique_task_names(task_list): |
| 20 | + for task_name in task_list: |
| 21 | + if task_name not in tasks_covered_by_e2e: |
| 22 | + tasks_covered_by_e2e.append(task_name) |
| 23 | + |
| 24 | +def get_tasks_covered_by_e2e(): |
| 25 | + for pipeline_name in pipelines_covered_by_e2e: |
| 26 | + pipeline_path = f"pipelines/{pipeline_name}/{pipeline_name}.yaml" |
| 27 | + # Get the task names from pipeline spec |
| 28 | + result = subprocess.run([f"yq -e '.spec.tasks[].taskRef.name' {pipeline_path}"], shell=True, capture_output=True, text=True) |
| 29 | + if result.stderr != "": |
| 30 | + print(f"[ERROR] failed to get tasks inside spec.tasks: {result.stderr}") |
| 31 | + sys.exit(1) |
| 32 | + output = result.stdout |
| 33 | + task_names = output.split() |
| 34 | + add_only_unique_task_names(task_names) |
| 35 | + # Get the task names from pipeline finally |
| 36 | + result = subprocess.run([f"yq -e '.spec.finally[].taskRef.name' {pipeline_path}"], shell=True, capture_output=True, text=True) |
| 37 | + if result.stderr != "": |
| 38 | + print(f"[ERROR] failed to get tasks inside .spec.finally: {result.stderr}") |
| 39 | + sys.exit(1) |
| 40 | + output = result.stdout |
| 41 | + task_names = output.split() |
| 42 | + add_only_unique_task_names(task_names) |
| 43 | + |
| 44 | +def get_changed_files_from_pr(pull_number): |
| 45 | + updated_files = [] |
| 46 | + token = os.environ["GITHUB_TOKEN"] |
| 47 | + headers = {"Authorization": "Bearer " + token} |
| 48 | + base_url = "https://api.github.com" |
| 49 | + repo = "konflux-ci/build-definitions" |
| 50 | + url = f"{base_url}/repos/{repo}/pulls/{pull_number}/files" |
| 51 | + req = urllib.request.Request(url=url, method="GET", headers=headers) |
| 52 | + try: |
| 53 | + with urllib.request.urlopen(req) as resp: |
| 54 | + if resp.status != 200: |
| 55 | + print(f"[ERROR] Unknown response status code: {resp.status}") |
| 56 | + sys.exit(1) |
| 57 | + response_in_json = json.loads(resp.read()) |
| 58 | + for object in response_in_json: |
| 59 | + updated_files.append(object['filename']) |
| 60 | + except urllib.error.HTTPError as e: |
| 61 | + print(f"[ERROR] got error response: {e.read()} with status {e.code}") |
| 62 | + sys.exit(1) |
| 63 | + return updated_files |
| 64 | + |
| 65 | +def does_updated_files_covered_by_e2e(updated_files): |
| 66 | + required_to_run_e2e = False |
| 67 | + for file_path in updated_files: |
| 68 | + if file_path.startswith("task/"): |
| 69 | + task_name = file_path.split("/")[1] |
| 70 | + if task_name in tasks_covered_by_e2e: |
| 71 | + required_to_run_e2e = True |
| 72 | + break |
| 73 | + elif file_path.startswith("pipelines/"): |
| 74 | + pipeline_name = file_path.split("/")[1] |
| 75 | + if pipeline_name in pipelines_covered_by_e2e: |
| 76 | + required_to_run_e2e = True |
| 77 | + break |
| 78 | + elif file_path in files_covered_by_e2e: |
| 79 | + required_to_run_e2e = True |
| 80 | + break |
| 81 | + else: |
| 82 | + print("No need to run e2e tests") |
| 83 | + return required_to_run_e2e |
| 84 | + |
| 85 | +if __name__ == '__main__': |
| 86 | + if len(sys.argv) != 2: |
| 87 | + print("[ERROR] provide correct number of arguments") |
| 88 | + pr_number = sys.argv[1] |
| 89 | + get_tasks_covered_by_e2e() |
| 90 | + updated_files = get_changed_files_from_pr(pr_number) |
| 91 | + required_to_run_e2e = does_updated_files_covered_by_e2e(updated_files) |
| 92 | + if required_to_run_e2e: |
| 93 | + print("execute_e2e") |
| 94 | + else: |
| 95 | + print("dont_execute_e2e") |
0 commit comments