-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathmultiple_tests.py
More file actions
160 lines (124 loc) · 6.47 KB
/
Copy pathmultiple_tests.py
File metadata and controls
160 lines (124 loc) · 6.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#################################################################################
# Licensed to the FIWARE Foundation (FF) under one #
# or more contributor license agreements. The FF licenses this file #
# to you under the Apache License, Version 2.0 (the "License") #
# you may not use this file except in compliance with the License. #
# You may obtain a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
# Author: Alberto Abella #
#################################################################################
# version 26/02/25 - 1
from sys import argv
from json import dump
from requests import get
from datetime import datetime
from master_tests import quality_analysis
def get_subdirectories(subject_root):
"""
Get the list of first-level subdirectories in the specified root directory of a GitHub repository.
Parameters:
subject_root (str): The full path to the root directory in the GitHub repository (e.g., https://github.com/smart-data-models/incubated/tree/d7b7b48f03b9b221d141e074e1d311985ab04f25/SMARTMANUFACTURING/dataModel.PredictiveMaintenance).
Returns:
list: List of subdirectory names.
"""
# Extract the owner and repo name from the URL
api_url = get_api_url(subject_root=subject_root)
try:
# Fetch the contents of the root directory
response = get(api_url)
if response.status_code != 200:
raise Exception(f"Failed to fetch directory contents: HTTP {response.status_code}")
contents = response.json()
return [item['name'] for item in contents if item['type'] == 'dir']
except Exception as e:
raise Exception(f"Error fetching subdirectories: {e}") from e
def get_api_url(subject_root: str) -> str:
"""
Construct the GitHub API URL to fetch the contents of a directory.
Constructs the URL based on the provided subject_root, which can point to either the master branch or a specific branch/commit.
The URL is used to retrieve directory contents from the GitHub API.
Parameters:
subject_root (str): The URL of the GitHub repository, including the root directory.
Returns:
str: The GitHub API URL.
Raises:
ValueError: If the subject_root URL is invalid.
"""
# Extract the owner and repo name from the URL
parts = subject_root.strip("/").split("/")
owner = parts[3] # e.g., "smart-data-models"
repo = parts[4] # e.g., "incubated"
if 'tree' in parts:
if len(parts) < 7:
raise ValueError("Invalid subject_root URL. It must include owner, repo, branch, and root directory.")
branch = parts[6] # e.g., "d7b7b48f03b9b221d141e074e1d311985ab04f25"
root_directory = "/".join(parts[7:]) # e.g., "SMARTMANUFACTURING/dataModel.PredictiveMaintenance"
# GitHub API URL to list contents of the root directory
api_url = f"https://api.github.com/repos/{owner}/{repo}/contents/{root_directory}?ref={branch}"
else:
if len(parts) < 5:
raise ValueError("Invalid subject_root URL. It must include owner, repo, branch, and root directory.")
root_directory = "/".join(parts[5:]) # e.g., "SMARTMANUFACTURING/dataModel.PredictiveMaintenance"
# GitHub API URL to list contents of the root directory
api_url = f"https://api.github.com/repos/{owner}/{repo}/contents/{root_directory}?ref=master"
return api_url
def run_master_tests(subject_root: str, subdirectory: str, email:str, only_report_errors: bool) -> dict:
"""
Run the master_tests.py script for a specific subdirectory.
Parameters:
subject_root (str): The full path to the root directory in the GitHub repository.
subdirectory (str): The subdirectory to test.
email (str): The email address for reporting results.
only_report_errors (bool): Whether to report only errors.
Returns:
dict: The results from master_tests.py.
"""
try:
# Construct the full URL to the subdirectory
# Remove any trailing slashes and append the subdirectory
subject_root = subject_root.rstrip("/")
subdirectory_url = f"{subject_root}/{subdirectory}"
print(f"Testing subdirectory: {subdirectory_url}")
# Run the master_tests.py script
result = quality_analysis(base_url=subdirectory_url,
email=email,
only_report_errors=only_report_errors)
return result
except Exception as e:
print(f"Error running tests for {subdirectory}: {e}")
return {"error": str(e)}
def main():
"""
Main function to execute tests on multiple subdirectories of a GitHub repository.
Retrieves the subdirectories from the specified GitHub repository URL and runs quality analysis for each subdirectory.
The results are then saved to a JSON file.
"""
if len(argv) != 4:
print("Usage: python3 multiple_tests.py <subject_root> <email> <only_report_errors>")
exit(1)
subject_root = argv[1]
email = argv[2]
only_report_errors = argv[3].lower() == "true"
# Get the list of subdirectories
subdirectories = get_subdirectories(subject_root)
# Run tests for each subdirectory and collect results
results = []
results = \
[{"datamodel": subdirectory,
"result": run_master_tests(subject_root, subdirectory, email, only_report_errors)}
for subdirectory in subdirectories]
# Save the results to a JSON file
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
output_filename = f"test_results_{timestamp}.json"
with open(output_filename, "w") as f:
dump(results, f, indent=4)
print(f"Test results saved to {output_filename}")
if __name__ == "__main__":
main()