Skip to content

Commit 3b2a152

Browse files
authored
Merge pull request #646 from averevki/refactor-report-portal
Refactor report portal launch import flow
2 parents e1425cd + 0c55b7b commit 3b2a152

File tree

2 files changed

+41
-55
lines changed

2 files changed

+41
-55
lines changed

scripts/junit2reportportal

+29-19
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import argparse
44
import io
55
import os
66
import sys
7+
import json
78
import zipfile
89

910
from lxml import etree
@@ -23,17 +24,29 @@ aparser.add_argument(
2324
help="Desired launch name in reportportal. default: RP_LAUNCH_NAME env",
2425
default=os.environ.get("RP_LAUNCH_NAME", os.environ.get("RP_LAUNCH")),
2526
)
27+
aparser.add_argument(
28+
"--launch-description",
29+
help="Description of the imported launch. default: RP_LAUNCH_DESC env",
30+
default=os.environ.get("RP_LAUNCH_DESC", ""),
31+
)
32+
aparser.add_argument(
33+
"--ocp-version",
34+
help="OCP version of the cluster imported launch is from. default: OCP_VERSION env",
35+
default=os.environ.get("OCP_VERSION"),
36+
)
2637
aparser.add_argument("--token-variable", help="env variable with auth token. default: RP_TOKEN", default="RP_TOKEN")
2738
aparser.add_argument("junitfile", nargs="+", help="junit file to import")
2839
args = aparser.parse_args()
2940

3041
if not args.reportportal:
3142
sys.exit("You must define reportportal URL")
43+
if not args.project:
44+
sys.exit("You must define reportportal project")
45+
if not args.launch_name:
46+
sys.exit("You must define reportportal launch name")
3247
if not args.token_variable:
3348
sys.exit("You must define correct token-variable")
3449

35-
polish = etree.XSLT(etree.parse("./xslt/polish-junit.xsl"))
36-
3750
stream = io.BytesIO()
3851

3952
xml = None
@@ -44,37 +57,34 @@ with zipfile.ZipFile(stream, mode="w", compression=zipfile.ZIP_DEFLATED) as azip
4457
for file in [i for i in zipfile.Path(inzip, at="archive/").iterdir() if i.name.startswith("junit-")]:
4558
with file.open() as junit:
4659
xml = etree.parse(junit)
47-
content = etree.tostring(polish(xml))
60+
content = etree.tostring(xml)
4861
azip.writestr(file.name, content)
4962
else:
5063
xml = etree.parse(junitfile)
51-
content = etree.tostring(polish(xml))
64+
content = etree.tostring(xml)
5265
azip.writestr(os.path.basename(junitfile), content)
5366

54-
if not args.launch_name:
55-
try:
56-
args.launch_name = xml.xpath("//property[@name = 'polarion-testrun-title']/@value")[0]
57-
assert args.launch_name
58-
except Exception:
59-
sys.exit("You must define reportportal launch")
60-
61-
if not args.project:
62-
try:
63-
args.project = xml.xpath("//property[@name = 'project']/@value")[0]
64-
assert args.project
65-
except Exception:
66-
sys.exit("You must define reportportal project")
67-
6867
token = os.environ[args.token_variable]
6968
reportportal = args.reportportal.rstrip("/")
7069

7170
auth = {"Authorization": f"Bearer {token}"}
7271
launch_import = f"{reportportal}/api/v1/{args.project}/launch/import"
7372

73+
launch_import_rq = {
74+
"attributes": [
75+
{"key": "skippedIsNotIssue", "system": True, "value": "true"},
76+
],
77+
"description": args.launch_description,
78+
}
79+
launch_import_rq["attributes"].extend([{"key": "ocp", "value": args.ocp_version}] if args.ocp_version else [])
80+
7481
print(
7582
requests.post(
7683
launch_import,
77-
files={"file": (f"{args.launch_name}.zip", stream.getbuffer(), "application/zip")},
84+
files={
85+
"file": (f"{args.launch_name}.zip", stream.getbuffer(), "application/zip"),
86+
"launchImportRq": (None, json.dumps(launch_import_rq), "application/json"),
87+
},
7888
headers=auth,
7989
).text
8090
)

xslt/polish-junit.xsl

+12-36
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,25 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<xsl:stylesheet
3-
version="1.0"
4-
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
5-
6-
<!-- https://source.redhat.com/groups/public/polarion/polarion_wiki/polarion_results_xunit_importer -->
7-
<!-- https://mojo.redhat.com/docs/DOC-1073077 -->
8-
9-
<xsl:param name="rmfails"/>
10-
<xsl:param name="rmlogs" select="true()"/>
11-
<xsl:param name="polarionProperties"/>
2+
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
123

134
<xsl:template match="node()|@*">
14-
<!-- this copies all objects that do not match other template -->
5+
<!-- this copies all objects that do not match any other template -->
156
<xsl:copy>
167
<xsl:apply-templates select="node()|@*"/>
178
</xsl:copy>
189
</xsl:template>
1910

20-
<xsl:template match="/testsuites">
11+
<!-- Add full testsuite path to each testcase name -->
12+
<xsl:template match="testcase">
2113
<xsl:copy>
22-
<xsl:if test="$polarionProperties">
23-
<xsl:copy-of select="./testsuite/properties"/>
24-
</xsl:if>
25-
<xsl:apply-templates select="node()|@*"/>
14+
<!-- Copy all attributes except name -->
15+
<xsl:apply-templates select="@*[not(name() = 'name')]"/>
16+
<!-- Modify the name attribute -->
17+
<xsl:attribute name="name">
18+
<xsl:value-of select="concat(@classname, '.', @name)"/>
19+
</xsl:attribute>
20+
<!-- Copy child nodes -->
21+
<xsl:apply-templates/>
2622
</xsl:copy>
2723
</xsl:template>
2824

29-
<xsl:template match="testcase[skipped]"/>
30-
<xsl:template match="testsuite/@skipped">
31-
<xsl:attribute name="skipped">0</xsl:attribute>
32-
</xsl:template>
33-
34-
<xsl:template match="testcase[failure or error]">
35-
<xsl:if test="not($rmfails)">
36-
<xsl:copy>
37-
<xsl:apply-templates select="node()|@*"/>
38-
</xsl:copy>
39-
</xsl:if>
40-
</xsl:template>
41-
42-
<xsl:template match='testcase[not(failure or error)]/*[starts-with(name(), "system-")]/text()'>
43-
<xsl:if test="not($rmlogs)">
44-
<xsl:copy>
45-
<xsl:apply-templates select="node()|@*"/>
46-
</xsl:copy>
47-
</xsl:if>
48-
</xsl:template>
4925
</xsl:stylesheet>

0 commit comments

Comments
 (0)