forked from 3scale-qe/3scale-tests
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjunit2reportportal
More file actions
executable file
·66 lines (54 loc) · 2.73 KB
/
Copy pathjunit2reportportal
File metadata and controls
executable file
·66 lines (54 loc) · 2.73 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
#!/usr/bin/env python
import argparse
import io
import os
import sys
import zipfile
from lxml import etree
import requests
aparser = argparse.ArgumentParser(description="Upload junit to reportportal")
aparser.add_argument("--reportportal", help="URL of reportportal. default: REPORTPORTAL env", default=os.environ.get("REPORTPORTAL"))
aparser.add_argument("--project", help="reportportal project where to import. default: RP_PROJECT env", default=os.environ.get("RP_PROJECT"))
aparser.add_argument("--launch-name", help="Desired launch name in reportportal. default: RP_LAUNCH_NAME env", default=os.environ.get("RP_LAUNCH_NAME", os.environ.get("RP_LAUNCH")))
aparser.add_argument("--token-variable", help="env variable with auth token. default: RP_TOKEN", default="RP_TOKEN")
aparser.add_argument("junitfile", nargs="+", help="junit file to import")
args = aparser.parse_args()
if not args.reportportal:
sys.exit("You must define reportportal URL")
if not args.token_variable:
sys.exit("You must define correct token-variable")
polish = etree.XSLT(etree.parse("./xslt/polish-junit.xsl"))
stream = io.BytesIO()
xml = None
with zipfile.ZipFile(stream, mode="w", compression=zipfile.ZIP_DEFLATED) as azip:
for junitfile in args.junitfile:
if zipfile.is_zipfile(junitfile):
with zipfile.ZipFile(junitfile) as inzip:
for file in [i for i in zipfile.Path(inzip, at="archive/").iterdir() if i.name.startswith("junit-")]:
with file.open() as junit:
xml = etree.parse(junit)
content = etree.tostring(polish(xml))
azip.writestr(file.name, content)
else:
xml = etree.parse(junitfile)
content = etree.tostring(polish(xml))
azip.writestr(os.path.basename(junitfile), content)
if not args.launch_name:
try:
args.launch_name = xml.xpath("//property[@name = 'polarion-testrun-title']/@value")[0]
assert args.launch_name
except Exception:
sys.exit("You must define reportportal launch")
if not args.project:
try:
args.project = xml.xpath("//property[@name = 'project']/@value")[0]
assert args.project
except Exception:
sys.exit("You must define reportportal project")
token = os.environ[args.token_variable]
reportportal = args.reportportal.rstrip("/")
auth = {"Authorization": f"Bearer {token}"}
launch_import = f"{reportportal}/api/v1/plugin/{args.project}/junit/import"
print(requests.post(launch_import, files={"file": (f"{args.launch_name}.zip", stream.getbuffer(), "application/zip"),
"launchImportRq": (None, f'{{"name": "{args.launch_name}"}}', "application/json")},
headers=auth).text)