-
Notifications
You must be signed in to change notification settings - Fork 0
/
gtest-json-result-push.py
executable file
·117 lines (89 loc) · 4.24 KB
/
gtest-json-result-push.py
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
#!/usr/bin/python3
####################################################################################
# If not stated otherwise in this file or this component's Licenses.txt file the
# following copyright and licenses apply:
#
# Copyright 2024 RDK Management
#
# Licensed 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.
####################################################################################
# This utility depends on the xmltodict, json and requests python library
import sys
import xmltodict
import json
import requests
import os
from requests.auth import HTTPBasicAuth
# function to read json file as input and return minified json data as string
def json_string(xml_file_path, git_folder_path):
# Change current working directory to the script directory
os.chdir(git_folder_path)
commit_id = os.popen('git log --pretty="format:%H" -n 1').read()
#The below command will work on the local docker container to get the component name
#component_name=os.popen('git remote -v | grep fetch | cut -d "/" -f2 | cut -d "." -f1').read()
#The below command will work on the github action container to get the component name
component_name=os.popen('git remote -v | grep fetch | cut -d "/" -f5 | cut -d " " -f1').read()
execution_link="https://github.com/rdkcentral"
developer=os.popen('git log --pretty="format:%an" -n 1').read()
jira_ticket=os.popen('git log --pretty="format:%s" -n 1 | cut -d ":" -f1').read()
print("commid_id : " + commit_id)
print("component_name : " + component_name)
print("developer : " + developer)
print("jira_ticket message: " + jira_ticket)
json_root = {
"commit_id": commit_id.strip(),
"component_name": component_name.strip(),
"execution_link": execution_link.strip(),
"developer": developer.strip(),
"jira_ticket": jira_ticket.strip()
}
with open(xml_file_path) as json_file:
# minify the json message
json_data = json.dumps(json.loads(json_file.read()))
json_root["test_cases_results"] = json.loads(json_data)
return json.dumps(json_root)
# function to http post json data to a url
def post_json(json_data, url):
# Get username and password from env variable
username = os.environ.get('AUTOMATICS_UNAME')
password = os.environ.get('AUTOMATICS_PWD')
passcode = os.environ.get('AUTOMATICS_PASSCODE')
passvalue = "Basic " + passcode
headers = {
'Content-type': 'application/json',
'Authorization': passvalue
}
response = requests.post(url, data=json_data, headers=headers)
print(response)
# Loop through all the xml files in the folder and call the xml_to_json function
# to convert xml to json and then call the post_json function to post the json data to a url
def post_json_to_url(json_folder_path, url, git_folder_path):
try :
for filename in os.listdir(json_folder_path):
if filename.endswith(".json"):
json_file_path = os.path.join(json_folder_path, filename)
json_data = json_string(json_file_path, git_folder_path)
print(json_data)
post_json(json_data, url)
except FileNotFoundError as e:
print(f"Test results are not found in the specified {git_folder_path} folder.")
print(f"Please check the folder path and try again. Error: {e}")
def main():
# Check if the script is called with the correct number of arguments
if len(sys.argv) != 4:
print("Usage: python3 gtest-xml-json-coverter.py <gtest-xml-folder-path> <upload url> <git-local-repo-path>")
sys.exit(1)
print("Converting xml to json")
post_json_to_url(sys.argv[1], sys.argv[2], sys.argv[3])
if __name__ == "__main__":
main()