Gerrit Project Builder Tools
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.

170 lines
6.4KB

  1. #!/usr/bin/env python
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain 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. # This is designed to be called by a gerrit hook. It searched new
  15. # patchsets for those from a first time commiter, then posts a helpful
  16. # message welcoming them to the community and explaining the review process
  17. #
  18. # For example, this might be called as follows
  19. # python welcome_message.py --change Ia1fea1eab3976f1a9cb89ceb3ce1c6c6a7e79c42
  20. # --change-url \ https://review-dev.openstack.org/81 --project gtest-org/test \
  21. # --branch master --uploader User A. Example (user@example.com) --commit \
  22. # 05508ae633852469d2fd7786a3d6f1d06f87055b --patchset 1 patchset-merged \
  23. # --ssh-user=user --ssh-key=/home/user/.ssh/id_rsa
  24. # and if this was the first commit from "user@example.com", a message
  25. # would be posted on review 81.
  26. import argparse
  27. import logging
  28. import paramiko
  29. import jeepyb.gerritdb
  30. import jeepyb.log as l
  31. logger = logging.getLogger('welcome_reviews')
  32. def is_newbie(uploader):
  33. """Determine if the owner of the patch is a first-timer."""
  34. # Retrieve uploader email
  35. try:
  36. searchkey = uploader[uploader.rindex("(") + 1:-1]
  37. except ValueError:
  38. logger.info('Couldnt get email for %s', uploader)
  39. return False
  40. # this query looks for all distinct patchsets for the given
  41. # user. If there's only 1, they're a first-timer.
  42. query = """SELECT COUNT(DISTINCT p.change_id + p.patch_set_id)
  43. FROM patch_sets p, account_external_ids a
  44. WHERE a.email_address = %s
  45. AND a.account_id = p.uploader_account_id;"""
  46. cursor = jeepyb.gerritdb.connect().cursor()
  47. cursor.execute(query, searchkey)
  48. data = cursor.fetchone()
  49. if data:
  50. if data[0] == 1:
  51. logger.info('We found a newbie: %s', uploader)
  52. return True
  53. else:
  54. return False
  55. def post_message(commit, gerrit_user, gerrit_ssh_key, message_file):
  56. """Post a welcome message on the patch set specified by the commit."""
  57. default_text = """\
  58. Congratulations, you've proposed your first change in OpenDev.
  59. Your submission will now be tested automatically by Zuul, our gatekeeper,
  60. and reviewed by other friendly developers. They will give you feedback and
  61. may require you to refine it.
  62. People seldom get their patch approved on the first try, so don't be
  63. concerned if requested to make corrections. Feel free to modify your patch
  64. and resubmit a new change-set.
  65. Patches often take days (and sometimes weeks) to get reviewed, so be
  66. patient. Don't hesitate to ask for help, and answer questions about your
  67. work promptly if you can. The more you get to know reviewers and get to be
  68. known by them, the smoother the review and approval process will become. The
  69. fastest way to accomplish this is by reviewing other proposed changes
  70. yourself: anybody can do it, and it's a great way to learn the code base.
  71. Thanks again for participating in OpenDev, we look forward to seeing you
  72. around.
  73. Workflow Guide: https://docs.openstack.org/infra/manual/developers.html
  74. """
  75. if message_file:
  76. try:
  77. with open(message_file, 'r') as _file:
  78. welcome_text = _file.read()
  79. except (OSError, IOError):
  80. logger.exception("Could not open message file")
  81. welcome_text = default_text
  82. else:
  83. welcome_text = default_text
  84. # post the above message, using ssh.
  85. command = ('gerrit review '
  86. '--message="{message}" {commit}').format(
  87. message=welcome_text,
  88. commit=commit)
  89. logger.info('Welcoming: %s', commit)
  90. ssh = paramiko.SSHClient()
  91. ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  92. ssh.connect('localhost', username=gerrit_user,
  93. key_filename=gerrit_ssh_key, port=29418)
  94. stdin, stdout, stderr = ssh.exec_command(command)
  95. stdout_text = stdout.read()
  96. stderr_text = stderr.read()
  97. ssh.close()
  98. if stdout_text:
  99. logger.debug('stdout: %s' % stdout_text)
  100. if stderr_text:
  101. logger.error('stderr: %s' % stderr_text)
  102. def main():
  103. parser = argparse.ArgumentParser()
  104. parser.add_argument('hook')
  105. # common
  106. parser.add_argument('--change', default=None)
  107. parser.add_argument('--change-url', default=None)
  108. parser.add_argument('--project', default=None)
  109. parser.add_argument('--branch', default=None)
  110. parser.add_argument('--commit', default=None)
  111. parser.add_argument('--topic', default=None)
  112. parser.add_argument('--change-owner', default=None)
  113. # patchset-abandoned
  114. parser.add_argument('--abandoner', default=None)
  115. parser.add_argument('--reason', default=None)
  116. # change-merged
  117. parser.add_argument('--submitter', default=None)
  118. parser.add_argument('--newrev', default=None)
  119. # patchset-created
  120. parser.add_argument('--uploader', default=None)
  121. parser.add_argument('--patchset', default=None)
  122. parser.add_argument('--is-draft', default=None)
  123. parser.add_argument('--kind', default=None)
  124. # for Welcome Message
  125. parser.add_argument('--ssh-user', dest='ssh_user',
  126. help='The gerrit welcome message user')
  127. parser.add_argument('--ssh-key', dest='ssh_key',
  128. help='The gerrit welcome message SSH key file')
  129. parser.add_argument('--message-file', dest='message_file', default=None,
  130. help='The gerrit welcome message')
  131. # Don't actually post the message
  132. parser.add_argument('--dryrun', dest='dryrun', action='store_true')
  133. parser.add_argument('--no-dryrun', dest='dryrun', action='store_false')
  134. parser.set_defaults(dryrun=False)
  135. l.setup_logging_arguments(parser)
  136. args = parser.parse_args()
  137. l.configure_logging(args)
  138. # they're a first-timer, post the message on 1st patchset
  139. if is_newbie(args.uploader) and args.patchset == '1' and not args.dryrun:
  140. post_message(args.commit, args.ssh_user, args.ssh_key,
  141. args.message_file)
  142. if __name__ == "__main__":
  143. main()