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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 voluptuous as v
  17. from zuul.reporter import BaseReporter
  18. class SQLReporter(BaseReporter):
  19. """Sends off reports to a database."""
  20. name = 'sql'
  21. log = logging.getLogger("zuul.SQLReporter")
  22. def report(self, item):
  23. """Create an entry into a database."""
  24. if not self.connection.tables_established:
  25. self.log.warn("SQL reporter (%s) is disabled " % self)
  26. return
  27. with self.connection.getSession() as db:
  28. db_buildset = db.createBuildSet(
  29. tenant=item.pipeline.tenant.name,
  30. pipeline=item.pipeline.name,
  31. project=item.change.project.name,
  32. change=getattr(item.change, 'number', None),
  33. patchset=getattr(item.change, 'patchset', None),
  34. ref=getattr(item.change, 'ref', ''),
  35. oldrev=getattr(item.change, 'oldrev', ''),
  36. newrev=getattr(item.change, 'newrev', ''),
  37. branch=getattr(item.change, 'branch', ''),
  38. zuul_ref=item.current_build_set.ref,
  39. ref_url=item.change.url,
  40. result=item.current_build_set.result,
  41. message=self._formatItemReport(item, with_jobs=False),
  42. )
  43. for job in item.getJobs():
  44. build = item.current_build_set.getBuild(job.name)
  45. if not build:
  46. # build hasn't begun. The sql reporter can only send back
  47. # stats about builds. It doesn't understand how to store
  48. # information about the change.
  49. continue
  50. (result, url) = item.formatJobResult(job)
  51. start = end = None
  52. if build.start_time:
  53. start = datetime.datetime.fromtimestamp(
  54. build.start_time,
  55. tz=datetime.timezone.utc)
  56. if build.end_time:
  57. end = datetime.datetime.fromtimestamp(
  58. build.end_time,
  59. tz=datetime.timezone.utc)
  60. db_buildset.createBuild(
  61. uuid=build.uuid,
  62. job_name=build.job.name,
  63. result=result,
  64. start_time=start,
  65. end_time=end,
  66. voting=build.job.voting,
  67. log_url=url,
  68. node_name=build.node_name,
  69. )
  70. def getSchema():
  71. sql_reporter = v.Schema(None)
  72. return sql_reporter