caellion-python-commons
reports.py
Go to the documentation of this file.
1 """!
2 This module provides utlilities related to creating Jenkins' warnings-ng-plugin-compatible reports from codeanalysis
3 """
4 
5 import json
6 import re
7 
8 
10  """!
11  This class provides a set of methods to create reports that are compatible with Jenkins' warnings-ng plugin
12  """
13 
14  issues_all = None
15 
16  def __init__(self):
17  """!
18  Initialize the ReportBuilder
19  """
20  if self.issues_all is None:
21  self.issues_all = {"_class": "io.jenkins.plugins.analysis.core.restapi.ReportApi", "issues": [], "size": 0}
22  else:
23  raise ValueError("Could not initialize the ReportBuilder, because its 'issues_all' property was not None.")
24 
25  def extract_basename_file(self, path):
26  """!
27  Extracts basename of a given path (only files). Should Work with any OS Path on any OS
28 
29  @param path path to get base folder path of
30  @returns base folder path
31  """
32  basename = re.search(r"[^\\/]+(?![\\/])$", path)
33  if basename:
34  return basename.group(0)
35 
36  def addIssue(self, path, severity, message, lineStart=-1, lineEnd=-1, columnStart=-1, columnEnd=-1, category=None, type=None, description=None, packageName=None, moduleName=None, additionalProperties=None):
37  """!
38  Adds a new issue to the report
39 
40  @warning This function will not add duplicate issue to the report
41 
42  @param path path to the file in which the issue happens
43  @param severity severity level for the issue, allowed options are LOW, NORMAL, HIGH, CRITICAL or ERROR
44  @param message issue message (brief description of the problem)
45  @param lineStart line at which issue happens or starting line of a block in which it happens
46  @param lineEnd ending line of a block in which issue hapens
47  @param columnStart column at which issue happens or starting column of a block in which it happens
48  @param columnEnd ending column of a block in which issue happens
49  @param category issue category to show in jenkins
50  @param type issue type to show in jenkins
51  @param description issue description (longer and more precise than message)
52  @param packageName fully qualified name of the package in which the issue happens
53  @param moduleName fully qualifies name of the module in which the issue happens
54  @param additionalProperties additional parameters to pass to Jenkins' warnings-ng plugin
55  """
56  lineStart = int(lineStart)
57  lineEnd = int(lineEnd)
58  columnStart = int(columnStart)
59  columnEnd = int(columnEnd)
60 
61  filename = self.extract_basename_file(path)
62  severity = severity.upper()
63  if filename is None or filename == "":
64  raise Exception("Path is not a file path!")
65  if severity not in ["LOW", "NORMAL", "HIGH", "CRITICAL", "ERROR"]:
66  raise Exception("Path is not a file path!")
67  if message is None or message == "":
68  raise Exception("Message must not be empty!")
69  dirname = path.replace(filename, "")
70 
71  issue = {"fileName": path, "directory": dirname, "severity": severity, "message": message}
72 
73  if lineStart > -1:
74  issue.update({"lineStart": lineStart})
75 
76  if lineStart > -1 and lineEnd > -1: # requires start to have end
77  issue.update({"lineEnd": lineEnd})
78 
79  if columnStart > -1:
80  issue.update({"columnStart": columnStart})
81 
82  if columnStart > -1 and columnEnd > -1: # requires start to have end
83  issue.update({"columnEnd": columnEnd})
84 
85  if category is not None:
86  issue.update({"category": category})
87 
88  if type is not None:
89  issue.update({"type": type})
90 
91  if description is not None:
92  issue.update({"description": description})
93 
94  if packageName is not None:
95  issue.update({"packageName": packageName})
96 
97  if moduleName is not None:
98  issue.update({"moduleName": moduleName})
99 
100  if additionalProperties is not None:
101  issue.update({"additionalProperties": additionalProperties})
102 
103  if issue not in self.issues_all["issues"]:
104  self.issues_all["issues"].append(issue)
105 
106  def generateReport(self):
107  """!
108  Generates the report in json format
109 
110  @returns json-formatted dictionary containing all issues and _class header
111  """
112  self.issues_all["size"] = len(self.issues_all["issues"])
113  return json.dumps(self.issues_all)
def addIssue(self, path, severity, message, lineStart=-1, lineEnd=-1, columnStart=-1, columnEnd=-1, category=None, type=None, description=None, packageName=None, moduleName=None, additionalProperties=None)
Adds a new issue to the report.
Definition: reports.py:36
This class provides a set of methods to create reports that are compatible with Jenkins' warnings-ng ...
Definition: reports.py:9
def generateReport(self)
Generates the report in json format.
Definition: reports.py:106
def extract_basename_file(self, path)
Extracts basename of a given path (only files).
Definition: reports.py:25
def __init__(self)
Initialize the ReportBuilder.
Definition: reports.py:16