Rob Mohr | 038bf99 | 2021-06-18 13:03:35 -0700 | [diff] [blame] | 1 | # Copyright 2021 The Pigweed Authors |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 4 | # use this file except in compliance with the License. You may obtain a copy of |
| 5 | # the License at |
| 6 | # |
| 7 | # https://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 under |
| 13 | # the License. |
| 14 | """Utility functions common to multiple recipes that don't fit elsewhere.""" |
| 15 | |
Rob Mohr | 34a1f8e | 2021-08-19 08:37:43 -0700 | [diff] [blame] | 16 | import json |
Rob Mohr | 038bf99 | 2021-06-18 13:03:35 -0700 | [diff] [blame] | 17 | import re |
| 18 | |
| 19 | import attr |
Rob Mohr | 34a1f8e | 2021-08-19 08:37:43 -0700 | [diff] [blame] | 20 | from google.protobuf import json_format |
Rob Mohr | 038bf99 | 2021-06-18 13:03:35 -0700 | [diff] [blame] | 21 | from recipe_engine import recipe_api |
| 22 | |
| 23 | |
| 24 | @attr.s |
Rob Mohr | 82c0e35 | 2022-07-27 20:57:40 +0000 | [diff] [blame] | 25 | class ChangeWithComments: |
Rob Mohr | 038bf99 | 2021-06-18 13:03:35 -0700 | [diff] [blame] | 26 | change = attr.ib() |
Rob Mohr | 753e183 | 2021-07-30 15:04:45 -0700 | [diff] [blame] | 27 | details = attr.ib() |
Rob Mohr | 5acf39e | 2021-11-11 08:10:58 -0800 | [diff] [blame] | 28 | commit_message = attr.ib() |
Rob Mohr | 038bf99 | 2021-06-18 13:03:35 -0700 | [diff] [blame] | 29 | comments = attr.ib() |
| 30 | |
| 31 | |
| 32 | class UtilApi(recipe_api.RecipeApi): |
| 33 | def get_change_with_comments(self): |
| 34 | input_ = self.m.buildbucket.build.input |
Rob Mohr | 753e183 | 2021-07-30 15:04:45 -0700 | [diff] [blame] | 35 | change = input_.gerrit_changes[0] |
| 36 | change_id = str(change.change) |
| 37 | details = self.m.gerrit.change_details( |
Rob Mohr | 038bf99 | 2021-06-18 13:03:35 -0700 | [diff] [blame] | 38 | 'change details', |
| 39 | change_id=change_id, |
| 40 | host=input_.gerrit_changes[0].host, |
Rob Mohr | 807f3ce | 2021-09-08 11:16:04 -0700 | [diff] [blame] | 41 | query_params=['ALL_COMMITS', 'ALL_REVISIONS', 'ALL_FILES'], |
Rob Mohr | 038bf99 | 2021-06-18 13:03:35 -0700 | [diff] [blame] | 42 | test_data=self.m.json.test_api.output( |
| 43 | { |
| 44 | 'owner': {'email': 'coder@example.com',}, |
| 45 | 'current_revision': 'a' * 40, |
| 46 | 'revisions': { |
Rob Mohr | 5a79c0f | 2021-07-26 12:27:35 -0700 | [diff] [blame] | 47 | 'a' |
| 48 | * 40: { |
| 49 | 'files': [], |
| 50 | 'commit': {'message': '',}, |
| 51 | 'description': 'description', |
| 52 | } |
Rob Mohr | 038bf99 | 2021-06-18 13:03:35 -0700 | [diff] [blame] | 53 | }, |
| 54 | 'revert_of': 0, |
| 55 | } |
| 56 | ), |
| 57 | ).json.output |
| 58 | |
Rob Mohr | 753e183 | 2021-07-30 15:04:45 -0700 | [diff] [blame] | 59 | current_revision = details['revisions'][details['current_revision']] |
Rob Mohr | 5acf39e | 2021-11-11 08:10:58 -0800 | [diff] [blame] | 60 | commit_message = current_revision['commit']['message'] |
| 61 | |
| 62 | comments = [] |
Rob Mohr | 038bf99 | 2021-06-18 13:03:35 -0700 | [diff] [blame] | 63 | |
Rob Mohr | 753e183 | 2021-07-30 15:04:45 -0700 | [diff] [blame] | 64 | for revision in details['revisions'].values(): |
Rob Mohr | 5a79c0f | 2021-07-26 12:27:35 -0700 | [diff] [blame] | 65 | if revision.get('description'): |
| 66 | comments.append(revision['description']) |
| 67 | |
Rob Mohr | 038bf99 | 2021-06-18 13:03:35 -0700 | [diff] [blame] | 68 | comments_result = self.m.gerrit.list_change_comments( |
| 69 | "list change comments", |
| 70 | change_id, |
| 71 | test_data=self.m.json.test_api.output( |
| 72 | {'/PATCHSET_LEVEL': [{'message': ''}],} |
| 73 | ), |
| 74 | ).json.output |
| 75 | |
Rob Mohr | 3445395 | 2021-08-04 06:46:54 -0700 | [diff] [blame] | 76 | for _, comment_data in comments_result.items(): |
| 77 | comments.extend(x['message'] for x in comment_data) |
Rob Mohr | 038bf99 | 2021-06-18 13:03:35 -0700 | [diff] [blame] | 78 | |
Rob Mohr | 5acf39e | 2021-11-11 08:10:58 -0800 | [diff] [blame] | 79 | return ChangeWithComments(change, details, commit_message, comments) |
Rob Mohr | 038bf99 | 2021-06-18 13:03:35 -0700 | [diff] [blame] | 80 | |
| 81 | def find_matching_comment(self, rx, comments): |
| 82 | """Find a comment in comments that matches regex object rx.""" |
| 83 | result = None |
| 84 | with self.m.step.nest('checking comments'): |
| 85 | for i, comment in enumerate(comments): |
Rob Mohr | 4806b6b | 2023-02-03 18:03:32 +0000 | [diff] [blame^] | 86 | with self.m.step.nest(f'comment ({i})') as pres: |
Rob Mohr | 038bf99 | 2021-06-18 13:03:35 -0700 | [diff] [blame] | 87 | pres.step_summary_text = comment |
| 88 | match = re.search(rx, comment) |
| 89 | if match: |
Rob Mohr | 4806b6b | 2023-02-03 18:03:32 +0000 | [diff] [blame^] | 90 | pres.step_summary_text = f'MATCH: {comment}' |
Rob Mohr | 038bf99 | 2021-06-18 13:03:35 -0700 | [diff] [blame] | 91 | result = match |
| 92 | break |
| 93 | |
| 94 | if result: |
| 95 | with self.m.step.nest('found'): |
| 96 | pass |
| 97 | |
| 98 | return result |
Rob Mohr | 34a1f8e | 2021-08-19 08:37:43 -0700 | [diff] [blame] | 99 | |
| 100 | def build_metadata(self): |
| 101 | return { |
| 102 | 'bb_id': self.m.buildbucket.build.id, |
| 103 | 'swarming_id': self.m.swarming.task_id, |
| 104 | 'builder': self.m.buildbucket_util.full_builder_name(), |
| 105 | 'url': self.m.buildbucket_util.build_url, |
| 106 | 'triggers': [ |
| 107 | json.loads(json_format.MessageToJson(x)) |
| 108 | for x in self.m.scheduler.triggers |
| 109 | ], |
| 110 | } |