blob: c9a4dbe350408fefc65a6bf8fa3f0f788316b397 [file] [log] [blame]
# Copyright 2021 The Pigweed Authors
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
"""Utility functions common to multiple recipes that don't fit elsewhere."""
import json
import re
import attr
from google.protobuf import json_format
from recipe_engine import recipe_api
class ChangeWithComments:
change = attr.ib()
details = attr.ib()
commit_message = attr.ib()
comments = attr.ib()
class UtilApi(recipe_api.RecipeApi):
def get_change_with_comments(self):
input_ =
change = input_.gerrit_changes[0]
change_id = str(change.change)
details = self.m.gerrit.change_details(
'change details',
'owner': {'email': '',},
'current_revision': 'a' * 40,
'revisions': {
* 40: {
'files': [],
'commit': {'message': '',},
'description': 'description',
'revert_of': 0,
current_revision = details['revisions'][details['current_revision']]
commit_message = current_revision['commit']['message']
comments = []
for revision in details['revisions'].values():
if revision.get('description'):
comments_result = self.m.gerrit.list_change_comments(
"list change comments",
{'/PATCHSET_LEVEL': [{'message': ''}],}
for _, comment_data in comments_result.items():
comments.extend(x['message'] for x in comment_data)
return ChangeWithComments(change, details, commit_message, comments)
def find_matching_comment(self, rx, comments):
"""Find a comment in comments that matches regex object rx."""
result = None
with self.m.step.nest('checking comments'):
for i, comment in enumerate(comments):
with self.m.step.nest(f'comment ({i})') as pres:
pres.step_summary_text = comment
match =, comment)
if match:
pres.step_summary_text = f'MATCH: {comment}'
result = match
if result:
with self.m.step.nest('found'):
return result
def build_metadata(self):
return {
'swarming_id': self.m.swarming.task_id,
'builder': self.m.buildbucket_util.full_builder_name(),
'url': self.m.buildbucket_util.build_url,
'triggers': [
for x in self.m.scheduler.triggers