blob: 148ae81dab488ecfab7866ae6bf5aaf60ecae37c [file] [log] [blame]
Tristan Honscheid52ba4d22023-02-09 11:59:29 -07001# 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
7Handles the endpoint for running copybot and generating the protobuf.
8"""
9
10from pathlib import Path
11import tempfile
12
13from chromite.third_party.google.protobuf import json_format
14
15from chromite.api import controller
16from chromite.api import faux
17from chromite.api import validate
18from chromite.api.gen.chromite.api import copybot_pb2
19from chromite.lib import constants
20from chromite.lib import cros_build_lib
21
22
23def _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
32def 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
Tristan Honscheid7a78af12023-02-16 16:36:56 -070079 for po in input_proto.push_options:
80 cmd.extend(["--push-option", po.opt])
81
Tristan Honscheid52ba4d22023-02-09 11:59:29 -070082 cmd.append(f"{input_proto.upstream.url}:{input_proto.upstream.branch}")
83 cmd.append(f"{input_proto.downstream.url}:{input_proto.downstream.branch}")
84
85 with tempfile.TemporaryDirectory() as temp_dir:
86 json_output_path = Path(temp_dir) / "copybot_output.json"
87 cmd.extend(["--json-out", json_output_path])
88
89 try:
90 cros_build_lib.run(cmd)
91 except cros_build_lib.RunCommandError:
92 # In case of failure, load details about the error from CopyBot's JSON
93 # output into the output protobuf. (If CopyBot ran successfully, the
94 # default values are simply used). CopyBot's output matches the JSON
95 # representation of the RunCopybotResponse protobuf.
96
97 if not json_output_path.exists():
98 return controller.RETURN_CODE_UNRECOVERABLE
99
100 json_format.Parse(json_output_path.read_text(), output_proto)
101 return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE