blob: c9a4dbe350408fefc65a6bf8fa3f0f788316b397 [file] [log] [blame]
Rob Mohr038bf992021-06-18 13:03:35 -07001# 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 Mohr34a1f8e2021-08-19 08:37:43 -070016import json
Rob Mohr038bf992021-06-18 13:03:35 -070017import re
18
19import attr
Rob Mohr34a1f8e2021-08-19 08:37:43 -070020from google.protobuf import json_format
Rob Mohr038bf992021-06-18 13:03:35 -070021from recipe_engine import recipe_api
22
23
24@attr.s
Rob Mohr82c0e352022-07-27 20:57:40 +000025class ChangeWithComments:
Rob Mohr038bf992021-06-18 13:03:35 -070026 change = attr.ib()
Rob Mohr753e1832021-07-30 15:04:45 -070027 details = attr.ib()
Rob Mohr5acf39e2021-11-11 08:10:58 -080028 commit_message = attr.ib()
Rob Mohr038bf992021-06-18 13:03:35 -070029 comments = attr.ib()
30
31
32class UtilApi(recipe_api.RecipeApi):
33 def get_change_with_comments(self):
34 input_ = self.m.buildbucket.build.input
Rob Mohr753e1832021-07-30 15:04:45 -070035 change = input_.gerrit_changes[0]
36 change_id = str(change.change)
37 details = self.m.gerrit.change_details(
Rob Mohr038bf992021-06-18 13:03:35 -070038 'change details',
39 change_id=change_id,
40 host=input_.gerrit_changes[0].host,
Rob Mohr807f3ce2021-09-08 11:16:04 -070041 query_params=['ALL_COMMITS', 'ALL_REVISIONS', 'ALL_FILES'],
Rob Mohr038bf992021-06-18 13:03:35 -070042 test_data=self.m.json.test_api.output(
43 {
44 'owner': {'email': 'coder@example.com',},
45 'current_revision': 'a' * 40,
46 'revisions': {
Rob Mohr5a79c0f2021-07-26 12:27:35 -070047 'a'
48 * 40: {
49 'files': [],
50 'commit': {'message': '',},
51 'description': 'description',
52 }
Rob Mohr038bf992021-06-18 13:03:35 -070053 },
54 'revert_of': 0,
55 }
56 ),
57 ).json.output
58
Rob Mohr753e1832021-07-30 15:04:45 -070059 current_revision = details['revisions'][details['current_revision']]
Rob Mohr5acf39e2021-11-11 08:10:58 -080060 commit_message = current_revision['commit']['message']
61
62 comments = []
Rob Mohr038bf992021-06-18 13:03:35 -070063
Rob Mohr753e1832021-07-30 15:04:45 -070064 for revision in details['revisions'].values():
Rob Mohr5a79c0f2021-07-26 12:27:35 -070065 if revision.get('description'):
66 comments.append(revision['description'])
67
Rob Mohr038bf992021-06-18 13:03:35 -070068 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 Mohr34453952021-08-04 06:46:54 -070076 for _, comment_data in comments_result.items():
77 comments.extend(x['message'] for x in comment_data)
Rob Mohr038bf992021-06-18 13:03:35 -070078
Rob Mohr5acf39e2021-11-11 08:10:58 -080079 return ChangeWithComments(change, details, commit_message, comments)
Rob Mohr038bf992021-06-18 13:03:35 -070080
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 Mohr4806b6b2023-02-03 18:03:32 +000086 with self.m.step.nest(f'comment ({i})') as pres:
Rob Mohr038bf992021-06-18 13:03:35 -070087 pres.step_summary_text = comment
88 match = re.search(rx, comment)
89 if match:
Rob Mohr4806b6b2023-02-03 18:03:32 +000090 pres.step_summary_text = f'MATCH: {comment}'
Rob Mohr038bf992021-06-18 13:03:35 -070091 result = match
92 break
93
94 if result:
95 with self.m.step.nest('found'):
96 pass
97
98 return result
Rob Mohr34a1f8e2021-08-19 08:37:43 -070099
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 }