irchandler.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. #! /usr/bin/env python
  2. # -*- coding: utf8 -*-
  3. #
  4. # gitlab irc sender
  5. # Copyright (C) 2016-2017 Andrei Karas (4144)
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 3 of the License, or
  10. # any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. from code.configuration import Configuration
  20. from code.core import Core
  21. class IrcHandler():
  22. def printPushHeader(self, channel, project, branch, url, data, commits, commitsCount):
  23. if data["before"] == "0000000000000000000000000000000000000000":
  24. url = Configuration.gitlab_commits_path.format(
  25. homepage = url,
  26. name = commits[0]["id"][:Configuration.commit_size]
  27. )
  28. elif commitsCount > 1:
  29. if len(commits) < commitsCount:
  30. print("Wrong commits count. Got count {0} and real commits number {1}".format(commitsCount, len(commits)))
  31. commitsCount = len(commits)
  32. url = Configuration.gitlab_compare_path.format(
  33. homepage = url,
  34. commit1 = data["before"][:Configuration.gitlab_compare_commit_size],
  35. commit2 = commits[commitsCount - 1]["id"][:Configuration.gitlab_compare_commit_size]
  36. )
  37. else:
  38. url = Configuration.gitlab_commit_path.format(
  39. homepage = url,
  40. commit = commits[0]["id"][:Configuration.commit_size]
  41. )
  42. Core.messageHandler.add(channel, Configuration.irc_push_header_message.format(
  43. project = project,
  44. author = data["user_name"],
  45. count = commitsCount,
  46. branch = branch,
  47. url = url
  48. ))
  49. def printCommitAuthor(self, channel, project, data, commit, message):
  50. Core.messageHandler.add(channel, Configuration.irc_push_commit_message.format(
  51. project = project,
  52. author = commit["author"]["name"],
  53. shortid = commit["id"][:Configuration.commit_size],
  54. message = message
  55. ))
  56. def printCommitMessage(self, channel, messages):
  57. for msg in messages:
  58. Core.messageHandler.add(channel, Configuration.irc_push_commit_message_continue.format(
  59. message = msg
  60. ))
  61. def printPushTag(self, channel, project, data, commits, tag, url):
  62. if data["after"] != "0000000000000000000000000000000000000000":
  63. url = Configuration.gitlab_commit_path.format(
  64. homepage = url,
  65. commit = data["after"][:Configuration.commit_size]
  66. )
  67. Core.messageHandler.add(channel, Configuration.irc_push_add_tag_message.format(
  68. project = project,
  69. author = data["user_name"],
  70. shortid = data["after"][:Configuration.commit_size],
  71. url = url,
  72. tag = tag
  73. ))
  74. else:
  75. Core.messageHandler.add(channel, Configuration.irc_push_remove_tag_message.format(
  76. project = project,
  77. author = data["user_name"],
  78. tag = tag
  79. ))
  80. def printCreateBranch(self, channel, project, data, branch, url):
  81. after = data["after"]
  82. if after == "0000000000000000000000000000000000000000":
  83. Core.messageHandler.add(channel, Configuration.irc_push_delete_branch_message.format(
  84. project = project,
  85. author = data["user_name"],
  86. branch = branch
  87. ))
  88. else:
  89. url = Configuration.gitlab_commits_path.format(
  90. homepage = url,
  91. name = branch
  92. )
  93. Core.messageHandler.add(channel, Configuration.irc_push_create_branch_message.format(
  94. project = project,
  95. author = data["user_name"],
  96. branch = branch,
  97. shortid = data["after"][:Configuration.commit_size],
  98. url = url
  99. ))
  100. def printAddCommitNote(self, channel, project, data, commit, url, iid):
  101. url = Configuration.gitlab_commit_path.format(
  102. homepage = url,
  103. commit = "{0}#note_{1}".format(commit["id"][:Configuration.commit_size], iid),
  104. )
  105. Core.messageHandler.add(channel, Configuration.irc_push_add_note_message.format(
  106. project = project,
  107. author = data["user"]["name"],
  108. shortid = commit["id"][:Configuration.commit_size],
  109. url = url
  110. ))
  111. def printBuildFailed(self, channel, project, branch, data, commit, url, build_id, build_stage, build_name):
  112. url = Configuration.gitlab_build_path.format(
  113. homepage = url,
  114. build = build_id
  115. )
  116. Core.messageHandler.add(channel, Configuration.irc_build_failed_message.format(
  117. project = project,
  118. shortid = commit["sha"][:Configuration.commit_size],
  119. branch = branch,
  120. url = url,
  121. build = build_id,
  122. stage = build_stage,
  123. name = build_name
  124. ))
  125. def printBuildSuccess(self, channel, project, branch, data, commit, url, build_id):
  126. url = Configuration.gitlab_build_path.format(
  127. homepage = url,
  128. build = build_id
  129. )
  130. Core.messageHandler.add(channel, Configuration.irc_build_success_message.format(
  131. project = project,
  132. shortid = commit["sha"][:Configuration.commit_size],
  133. branch = branch,
  134. url = url,
  135. build = build_id
  136. ))
  137. def printStageFailed(self, channel, project, branch, data, commit, url, build_id):
  138. url = Configuration.gitlab_build_path.format(
  139. homepage = url,
  140. build = build_id
  141. )
  142. Core.messageHandler.add(channel, Configuration.irc_stage_failed_message.format(
  143. project = project,
  144. shortid = commit["sha"][:Configuration.commit_size],
  145. branch = branch,
  146. url = url,
  147. build = build_id
  148. ))
  149. def printPipelineFailed(self, channel, project, branch, data, commit, url, pipeline_id):
  150. url = Configuration.gitlab_pipeline_path.format(
  151. homepage = url,
  152. pipeline = pipeline_id
  153. )
  154. Core.messageHandler.add(channel, Configuration.irc_pipeline_failed_message.format(
  155. project = project,
  156. shortid = commit["id"][:Configuration.commit_size],
  157. branch = branch,
  158. url = url,
  159. pipeline = pipeline_id
  160. ))
  161. def printPipelineSuccess(self, channel, project, branch, data, commit, url, pipeline_id):
  162. url = Configuration.gitlab_pipeline_path.format(
  163. homepage = url,
  164. pipeline = pipeline_id
  165. )
  166. Core.messageHandler.add(channel, Configuration.irc_pipeline_success_message.format(
  167. project = project,
  168. shortid = commit["id"][:Configuration.commit_size],
  169. branch = branch,
  170. url = url,
  171. pipeline = pipeline_id
  172. ))
  173. def printMROpened(self, channel, project, branch, source_namespace, source_branch, data, url, iid, title):
  174. Core.messageHandler.add(channel, Configuration.irc_merge_request_opened_message.format(
  175. project = project,
  176. branch = branch,
  177. url = url,
  178. source_namespace = source_namespace,
  179. source_branch = source_branch,
  180. author = data["user"]["name"],
  181. title = title,
  182. id = iid
  183. ))
  184. def printMRClosed(self, channel, project, branch, source_namespace, source_branch, data, url, iid, title):
  185. Core.messageHandler.add(channel, Configuration.irc_merge_request_closed_message.format(
  186. project = project,
  187. branch = branch,
  188. url = url,
  189. source_namespace = source_namespace,
  190. source_branch = source_branch,
  191. author = data["user"]["name"],
  192. title = title,
  193. id = iid
  194. ))
  195. def printMRReopened(self, channel, project, branch, source_namespace, source_branch, data, url, iid, title):
  196. Core.messageHandler.add(channel, Configuration.irc_merge_request_reopened_message.format(
  197. project = project,
  198. branch = branch,
  199. url = url,
  200. source_namespace = source_namespace,
  201. source_branch = source_branch,
  202. author = data["user"]["name"],
  203. title = title,
  204. id = iid
  205. ))
  206. def printMRUpdated(self, channel, project, branch, source_namespace, source_branch, data, url, iid, title):
  207. Core.messageHandler.add(channel, Configuration.irc_merge_request_updated_message.format(
  208. project = project,
  209. branch = branch,
  210. url = url,
  211. source_namespace = source_namespace,
  212. source_branch = source_branch,
  213. author = data["user"]["name"],
  214. title = title,
  215. id = iid
  216. ))
  217. def printMRMerged(self, channel, project, branch, source_namespace, source_branch, data, url, iid, title):
  218. Core.messageHandler.add(channel, Configuration.irc_merge_request_merged_message.format(
  219. project = project,
  220. branch = branch,
  221. url = url,
  222. source_namespace = source_namespace,
  223. source_branch = source_branch,
  224. author = data["user"]["name"],
  225. title = title,
  226. id = iid
  227. ))
  228. def printAddMRNote(self, channel, project, branch, source_namespace, source_branch, data, url, iid):
  229. Core.messageHandler.add(channel, Configuration.irc_merge_request_add_note_message.format(
  230. project = project,
  231. branch = branch,
  232. url = url,
  233. source_namespace = source_namespace,
  234. source_branch = source_branch,
  235. author = data["user"]["name"],
  236. id = iid
  237. ))
  238. def printAddIssueNote(self, channel, project, data, url, iid, title):
  239. Core.messageHandler.add(channel, Configuration.irc_issue_add_note_message.format(
  240. project = project,
  241. url = url,
  242. author = data["user"]["name"],
  243. title = title,
  244. id = iid
  245. ))
  246. def printAddSnippetNote(self, channel, project, data, url, iid, title):
  247. Core.messageHandler.add(channel, Configuration.irc_snippet_add_note_message.format(
  248. project = project,
  249. url = url,
  250. author = data["user"]["name"],
  251. title = title,
  252. id = iid
  253. ))
  254. def printIssueOpened(self, channel, project, data, url, iid, title):
  255. Core.messageHandler.add(channel, Configuration.irc_issue_opened_message.format(
  256. project = project,
  257. url = url,
  258. author = data["user"]["name"],
  259. title = title,
  260. id = iid
  261. ))
  262. def printIssueClosed(self, channel, project, data, url, iid, title):
  263. Core.messageHandler.add(channel, Configuration.irc_issue_closed_message.format(
  264. project = project,
  265. url = url,
  266. author = data["user"]["name"],
  267. title = title,
  268. id = iid
  269. ))
  270. def printIssueReopened(self, channel, project, data, url, iid, title):
  271. Core.messageHandler.add(channel, Configuration.irc_issue_reopened_message.format(
  272. project = project,
  273. url = url,
  274. author = data["user"]["name"],
  275. title = title,
  276. id = iid
  277. ))
  278. def printIssueUpdated(self, channel, project, data, url, iid, title):
  279. Core.messageHandler.add(channel, Configuration.irc_issue_updated_message.format(
  280. project = project,
  281. url = url,
  282. author = data["user"]["name"],
  283. title = title,
  284. id = iid
  285. ))
  286. def printCustomMessage(self, channel, message):
  287. Core.messageHandler.add(channel, message)