The Gatekeeper, or a project gating system
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sqlreporter.py 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # Copyright 2015 Rackspace Australia
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  4. # not use this file except in compliance with the License. You may obtain
  5. # a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12. # License for the specific language governing permissions and limitations
  13. # under the License.
  14. import datetime
  15. import logging
  16. import time
  17. import voluptuous as v
  18. from zuul.reporter import BaseReporter
  19. class SQLReporter(BaseReporter):
  20. """Sends off reports to a database."""
  21. name = 'sql'
  22. log = logging.getLogger("zuul.SQLReporter")
  23. def report(self, item):
  24. """Create an entry into a database."""
  25. if not self.connection.tables_established:
  26. self.log.warn("SQL reporter (%s) is disabled " % self)
  27. return
  28. with self.connection.getSession() as db:
  29. db_buildset = db.createBuildSet(
  30. tenant=item.pipeline.tenant.name,
  31. pipeline=item.pipeline.name,
  32. project=item.change.project.name,
  33. change=getattr(item.change, 'number', None),
  34. patchset=getattr(item.change, 'patchset', None),
  35. ref=getattr(item.change, 'ref', ''),
  36. oldrev=getattr(item.change, 'oldrev', ''),
  37. newrev=getattr(item.change, 'newrev', ''),
  38. branch=getattr(item.change, 'branch', ''),
  39. zuul_ref=item.current_build_set.ref,
  40. ref_url=item.change.url,
  41. result=item.current_build_set.result,
  42. message=self._formatItemReport(item, with_jobs=False),
  43. )
  44. for job in item.getJobs():
  45. build = item.current_build_set.getBuild(job.name)
  46. if not build:
  47. # build hasn't begun. The sql reporter can only send back
  48. # stats about builds. It doesn't understand how to store
  49. # information about the change.
  50. continue
  51. # Ensure end_time is defined
  52. if not build.end_time:
  53. build.end_time = time.time()
  54. (result, url) = item.formatJobResult(job)
  55. start = end = None
  56. if build.start_time:
  57. start = datetime.datetime.fromtimestamp(
  58. build.start_time,
  59. tz=datetime.timezone.utc)
  60. if build.end_time:
  61. end = datetime.datetime.fromtimestamp(
  62. build.end_time,
  63. tz=datetime.timezone.utc)
  64. db_buildset.createBuild(
  65. uuid=build.uuid,
  66. job_name=build.job.name,
  67. result=result,
  68. start_time=start,
  69. end_time=end,
  70. voting=build.job.voting,
  71. log_url=url,
  72. node_name=build.node_name,
  73. )
  74. def getSchema():
  75. sql_reporter = v.Schema(None)
  76. return sql_reporter