import os, requests, json, base64
# This directory is where you have all your results locally, generally named as `allure-results`
allureResultsDirectory = 'allure-results'
# This url is where the Allure container is deployed. We are using localhost as example
allureServer = 'http://127.0.0.1:5050'
currentDirectory = os.path.dirname(os.path.realpath(__file__))
resultsDirectory = currentDirectory + allureResultsDirectory
print('RESULTS DIRECTORY PATH: ' + resultsDirectory)
files = os.listdir(resultsDirectory)
print('FILES:')
results = []
for file in files:
result = {}
filePath = resultsDirectory + "/" + file
print(filePath)
if os.path.isfile(filePath):
try:
with open(filePath, "rb") as f:
content = f.read()
if content.strip():
b64Content = base64.b64encode(content)
result['file_name'] = file
result['content_base64'] = b64Content.decode('UTF-8')
results.append(result)
else:
print('Empty File skipped: '+ filePath)
finally :
f.close()
else:
print('Directory skipped: '+ filePath)
headers = {'Content-type': 'application/json'}
requestBody = {
"results": results
}
jsonRequestBody = json.dumps(requestBody)
response = requests.post(allureServer + '/send-results', headers=headers, data=jsonRequestBody)
print("RESPONSE:")
jsonResponseBody = json.loads(response.content)
jsonPrettierResponseBody = json.dumps(jsonResponseBody, indent=4, sort_keys=True)
print(jsonPrettierResponseBody)
print("STATUS CODE:")
print(response.status_code)
task sendReports {
group "Work Allure Server api"
description "send results test in remote server"
doLast {
/* Settings */
String reportPath = 'build/allure-results'
String allureServer = 'http://127.0.0.1:5050/send-results'
/* get files in build */
def dh = new File(reportPath)
def request = new Request()
dh.eachFile {
Reports files = new Reports(
file_name: it.name.replaceFirst(~/\.[^\.]+$/, ''),
content_base64: it.text.bytes.encodeBase64()
)
request.results.add(files)
}
//println JsonOutput.toJson(request)
/* Send report to api*/
def req = new URL(allureServer).openConnection()
req.setRequestMethod("POST")
req.setRequestProperty("Content-Type", "application/json; charset=UTF-8")
req.setDoOutput(true)
req.getOutputStream().write(JsonOutput.toJson(request).getBytes("UTF-8"))
logger.quiet "Status code: ${req.getResponseCode()}"
def resp = new JsonSlurper().parseText(req.getInputStream().getText())
logger.quiet "Response: ${resp}"
}
}
class Request {
List<Reports> results = new LinkedList<>()
}
class Reports {
String file_name
String content_base64
}
task sendReports {
group "Work Allure Server api"
description "send results test in remote server"
doLast {
/* Settings */
String reportPath = 'build/allure-results'
String allureServer = 'http://127.0.0.1:5050/send-results'
/* get files in build */
def dh = new File(reportPath)
def request = new Request()
dh.eachFile {
Reports files = new Reports(
file_name: it.name,
content_base64: it.text.bytes.encodeBase64()
)
request.results.add(files)
}
//println JsonOutput.toJson(request)
/* Send report to api*/
def req = new URL(allureServer).openConnection()
req.setRequestMethod("POST")
req.setRequestProperty("Content-Type", "application/json; charset=UTF-8")
req.setDoOutput(true)
req.getOutputStream().write(JsonOutput.toJson(request).getBytes("UTF-8"))
logger.quiet "Status code: ${req.getResponseCode()}"
def resp = new JsonSlurper().parseText(req.getInputStream().getText())
logger.quiet "Response: ${resp}"
}
}
class Request {
List<Reports> results = new LinkedList<>()
}
class Reports {
String file_name
String content_base64
}