Coverage for testrail_api_reporter/publishers/confluence_sender.py: 14%

1# -*- coding: utf-8 -*-

2""" Confluence sender module """

3

4 from atlassian import Confluence

5

6 from ..engines.plotly_reporter import PlotlyReporter

7 from ..utils.logger_config import setup_logger, DEFAULT_LOGGING_LEVEL

8

9

10 class ConfluenceSender:

11 """Class contains wrapper for generating and sends reports to Confluence"""

12

13 def __init__(

14 self,

15 url=None,

16 username=None,

17 password=None,

18 confluence_page=None,

19 automation_platforms=None,

20 type_platforms=None,

21 plotly_engine=None,

22 logger=None,

23 log_level=DEFAULT_LOGGING_LEVEL,

24 ):

25 """

26 General init

27

28 :param url: url of Confluence, string, required

29 :param username: username of Confluence user with proper access rights, string, required

30 :param password: password of Confluence user with proper access rights, string, required

31 :param confluence_page: confluence page short URL, string - only last part of it (it's id or str), optional

32 :param automation_platforms: list of dicts of automation platforms, dict = {'name': 'Desktop Chrome',

33 'internal_name': 'type_id',

34 'sections': [16276]}, optional

35 :param type_platforms: list of dicts, with sections ids, where dict = {'name': 'UI',

36 'sections': [16276]}, optional

37 :param plotly_engine: custom graphic reporter engine (PlotlyReporter), if none is selected, new will be created

38 :param logger: logger object, optional

39 :param log_level: logging level, optional, by default is 'logging.DEBUG'

40 """

41 if not logger:

42 self.___logger = setup_logger(name="ConfluenceReporter", log_file="ConfluenceReporter.log", level=log_level)

43 else:

44 self.___logger = logger

45 self.___logger.debug("ConfluenceReporter init")

46 if url is None or username is None or password is None:

47 raise ValueError("No confluence credentials are provided!")

48 self.__confluence = Confluence(url=url, username=username, password=password)

49 self.__confluence_page = confluence_page # confluence page may vary for each report if needed, None is possible

50 self.__plotly = (

51 plotly_engine if plotly_engine else PlotlyReporter(type_platforms=type_platforms, log_level=log_level)

52 )

53 self.__automation_platforms = automation_platforms # should be passed with specific TestRails sections

54 self.__type_platforms = type_platforms

55

56 def automation_state(self, confluence_page=None, reports=None, filename="current_automation.png"):

57 """

58 Generates and sends (attach) an image file (png) to confluence page with staked distribution (bar chart)

59 with automation type coverage (or similar).

60

61 :param confluence_page: confluence page short URL, string - only last part of it (it's id or str), optional

62 :param reports: report with stacked distribution, usually it output of

63 ATCoverageReporter().automation_state_report()

64 :param filename: filename of image (with a valid path), png expected

65 :return: none

66 """

67 if not confluence_page:

68 raise ValueError("No confluence page is provided, report aborted!")

69 if not reports:

70 raise ValueError("No TestRail reports are provided, report aborted!")

71 self.__plotly.draw_automation_state_report(reports=reports, filename=filename)

72 self.__confluence.attach_file(filename, page_id=confluence_page, title="current_automation")

73

74 def test_case_priority_distribution(

75 self, confluence_page=None, values=None, filename="current_priority_distribution.png"

76 ):

77 """

78 Generates and sends (attach) an image file (png) to confluence page with priority distribution (pie chart)

79

80 :param confluence_page: confluence page short URL, string - only last part of it (it's id or str), optional

81 :param values: list of values to draw a report with priority distribution, usually it's output from

82 ATCoverageReporter().test_case_by_priority()

83 :param filename: filename of image (maybe with a valid path), png expected

84 :return: none

85 """

86 if not confluence_page:

87 raise ValueError("No confluence page is provided, report aborted!")

88 if not values:

89 raise ValueError("No TestRail reports are provided, report aborted!")

90 self.__plotly.draw_test_case_by_priority(values=values, filename=filename)

91 self.__confluence.attach_file(filename, page_id=confluence_page, title="current_priority_distribution")

92

93 def test_case_area_distribution(self, confluence_page=None, cases=None, filename="current_area_distribution.png"):

94 """

95 Generates and sends (attach) an image file (png) to confluence page with sections distribution (pie chart)

96

97 :param confluence_page: confluence page short URL, string - only last part of it (it's id or str), optional

98 :param cases: list of values to draw a report with priority distribution, usually it's output from

99 ATCoverageReporter().test_case_by_type()

100 :param filename: filename of image (maybe with a valid path), png expected

101 :return: none

102 """

103 if not confluence_page:

104 raise ValueError("No confluence page is provided, report aborted!")

105 if not cases:

106 raise ValueError("No TestRail cases are provided, report aborted!")

107 self.__plotly.draw_test_case_by_area(cases=cases, filename=filename)

108 self.__confluence.attach_file(filename, page_id=confluence_page, title="current_area_distribution")

109

110 def history_state_chart(self, confluence_page=None, automation_platforms=None):

111 """

112 Generates and sends (attach) an image file (png) to confluence page with state distribution (staked line chart)

113

114 :param confluence_page: confluence page short URL, string - only last part of it (it's id or str), optional

115 :param automation_platforms: list of dicts contains automation platforms = [{'name': 'Desktop Chrome',

116 'internal_name': 'type_id',

117 'sections': [16276]}]

118 :return: none

119 """

120 automation_platforms = automation_platforms if automation_platforms else self.__automation_platforms

121 if not confluence_page:

122 raise ValueError("No confluence page is provided, report aborted!")

123 if automation_platforms is None:

124 raise ValueError("No automation platforms provided, report aborted!")

125 for item in automation_platforms:

126 self.___logger.debug("generating chart for %s", item["name"])

127 filename = self.__plotly.draw_history_state_chart(chart_name=item["name"])

128 self.__confluence.attach_file(filename, page_id=confluence_page, title=filename[:-4])

129

130 def history_type_chart(

131 self, confluence_page=None, type_platforms=None, filename="current_area_distribution_history.png"

132 ):

133 """

134 Generates and sends (attach) an image file (png) to confluence page with state distribution (staked line chart)

135

136 :param confluence_page: confluence page short URL, string - only last part of it (it's id or str), optional

137 :param type_platforms: list of dicts, with sections ids, where dict = {'name': 'UI',

138 'sections': [16276]}

139 :param filename: filename of image (maybe with valid path), png expected

140 :return: none

141 """

142 type_platforms = type_platforms if type_platforms else self.__type_platforms

143 if not confluence_page:

144 raise ValueError("No confluence page is provided, report aborted!")

145 if not type_platforms:

146 raise ValueError("No type platforms specified, report aborted!")

147 self.__plotly.draw_history_type_chart(type_platforms=type_platforms, filename=filename)

148 self.__confluence.attach_file(filename, page_id=confluence_page, title="current_area_distribution_history")

149

150 def generate_report(

151 self,

152 confluence_page=None,

153 reports=None,

154 cases=None,

155 values=None,

156 type_platforms=None,

157 automation_platforms=None,

158 ):

159 """

160 Generates and sends (attach) an image file (png) to confluence page with state distribution (staked line chart)

161

162 :param confluence_page: confluence page short URL, string - only last part of it (it's id or str), optional

163 :param reports: report with stacked distribution, usually it's output of

164 ATCoverageReporter().automation_state_report()

165 :param cases: list of values to draw report with priority distribution, usually it's output from

166 ATCoverageReporter().test_case_by_type()

167 :param values: list of values to draw report with priority distribution, usually it's output from

168 ATCoverageReporter().test_case_by_priority()

169 :param type_platforms: list of dicts, with sections ids, where dict = {'name': 'UI',

170 'sections': [16276]}, optional

171 :param automation_platforms: list of dicts of automation platforms, dict = {'name': 'Desktop Chrome',

172 'internal_name': 'type_id',

173 'sections': [16276]}, optional

174 :return: none

175 """

176 confluence_page = confluence_page if confluence_page else self.__confluence_page

177 if not confluence_page:

178 raise ValueError("No confluence page is provided, report aborted!")

179 if not reports:

180 raise ValueError("No TestRail reports are provided, report aborted!")

181 if not cases:

182 raise ValueError("No TestRail cases are provided, report aborted!")

183 if not values:

184 raise ValueError("No TestRail values are provided, report aborted!")

185 type_platforms = type_platforms if type_platforms else self.__type_platforms

186 if not type_platforms:

187 raise ValueError("No type platforms specified, report aborted!")

188 automation_platforms = automation_platforms if automation_platforms else self.__automation_platforms

189 if not automation_platforms:

190 raise ValueError("No type platforms specified, report aborted!")

191 self.automation_state(confluence_page=confluence_page, reports=reports)

192 self.test_case_priority_distribution(confluence_page=confluence_page, values=values)

193 self.test_case_area_distribution(confluence_page=confluence_page, cases=cases)

194 self.history_type_chart(confluence_page=confluence_page)

195 self.history_state_chart(confluence_page=confluence_page)