Tristan Honscheid | 52ba4d2 | 2023-02-09 11:59:29 -0700 | [diff] [blame^] | 1 | # Copyright 2023 The ChromiumOS Authors |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Copybot Controller. |
| 6 | |
| 7 | Handles the endpoint for running copybot and generating the protobuf. |
| 8 | """ |
| 9 | |
| 10 | from pathlib import Path |
| 11 | import tempfile |
| 12 | |
| 13 | from chromite.third_party.google.protobuf import json_format |
| 14 | |
| 15 | from chromite.api import controller |
| 16 | from chromite.api import faux |
| 17 | from chromite.api import validate |
| 18 | from chromite.api.gen.chromite.api import copybot_pb2 |
| 19 | from chromite.lib import constants |
| 20 | from chromite.lib import cros_build_lib |
| 21 | |
| 22 | |
| 23 | def _MockSuccess(_input_proto, _output_proto, _config_proto): |
| 24 | """Mock success output for the RunCopybot endpoint.""" |
| 25 | |
| 26 | # Successful response is the default protobuf, so no need to fill it out. |
| 27 | |
| 28 | |
| 29 | @faux.success(_MockSuccess) |
| 30 | @faux.empty_error |
| 31 | @validate.validation_complete |
| 32 | def RunCopybot(input_proto, output_proto, _config_proto): |
| 33 | """Run copybot. Translate all fields in the input protobuf to CLI args.""" |
| 34 | |
| 35 | cmd = [ |
| 36 | Path(constants.SOURCE_ROOT) |
| 37 | / "src/platform/dev/contrib/copybot/copybot.py" |
| 38 | ] |
| 39 | |
| 40 | if input_proto.topic: |
| 41 | cmd.extend(["--topic", input_proto.topic]) |
| 42 | |
| 43 | for label in input_proto.labels: |
| 44 | cmd.extend(["--label", label.label]) |
| 45 | |
| 46 | for reviewer in input_proto.reviewers: |
| 47 | cmd.extend(["--re", reviewer.user]) |
| 48 | |
| 49 | for cc in input_proto.ccs: |
| 50 | cmd.extend(["--cc", cc.user]) |
| 51 | |
| 52 | if input_proto.prepend_subject: |
| 53 | cmd.extend(["--prepend-subject", input_proto.prepend_subject]) |
| 54 | |
| 55 | if ( |
| 56 | input_proto.merge_conflict_behavior |
| 57 | == copybot_pb2.RunCopybotRequest.MERGE_CONFLICT_BEHAVIOR_SKIP |
| 58 | ): |
| 59 | cmd.extend(["--merge-conflict-behavior", "SKIP"]) |
| 60 | |
| 61 | if ( |
| 62 | input_proto.merge_conflict_behavior |
| 63 | == copybot_pb2.RunCopybotRequest.MERGE_CONFLICT_BEHAVIOR_FAIL |
| 64 | ): |
| 65 | cmd.extend(["--merge-conflict-behavior", "FAIL"]) |
| 66 | |
| 67 | for exclude in input_proto.exclude_file_patterns: |
| 68 | cmd.extend(["--exclude-file-pattern", exclude.pattern]) |
| 69 | |
| 70 | for ph in input_proto.keep_pseudoheaders: |
| 71 | cmd.extend(["--keep-pseudoheader", ph.name]) |
| 72 | |
| 73 | if input_proto.add_signed_off_by: |
| 74 | cmd.append("--add-signed-off-by") |
| 75 | |
| 76 | if input_proto.dry_run: |
| 77 | cmd.append("--dry-run") |
| 78 | |
| 79 | cmd.append(f"{input_proto.upstream.url}:{input_proto.upstream.branch}") |
| 80 | cmd.append(f"{input_proto.downstream.url}:{input_proto.downstream.branch}") |
| 81 | |
| 82 | with tempfile.TemporaryDirectory() as temp_dir: |
| 83 | json_output_path = Path(temp_dir) / "copybot_output.json" |
| 84 | cmd.extend(["--json-out", json_output_path]) |
| 85 | |
| 86 | try: |
| 87 | cros_build_lib.run(cmd) |
| 88 | except cros_build_lib.RunCommandError: |
| 89 | # In case of failure, load details about the error from CopyBot's JSON |
| 90 | # output into the output protobuf. (If CopyBot ran successfully, the |
| 91 | # default values are simply used). CopyBot's output matches the JSON |
| 92 | # representation of the RunCopybotResponse protobuf. |
| 93 | |
| 94 | if not json_output_path.exists(): |
| 95 | return controller.RETURN_CODE_UNRECOVERABLE |
| 96 | |
| 97 | json_format.Parse(json_output_path.read_text(), output_proto) |
| 98 | return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE |