docs_builder: Add ability to comment on CLs

Bug: b/272587419
Change-Id: I728fadf0a2feef1f96fed83a4450bc2d8d3571eb
Reviewed-on: https://pigweed-review.googlesource.com/c/infra/recipes/+/151815
Pigweed-Auto-Submit: Rob Mohr <mohrr@google.com>
Reviewed-by: Marc-Antoine Ruel <maruel@google.com>
Commit-Queue: Rob Mohr <mohrr@google.com>
diff --git a/recipe_modules/checkout/api.py b/recipe_modules/checkout/api.py
index e19465b..03e0ceb 100644
--- a/recipe_modules/checkout/api.py
+++ b/recipe_modules/checkout/api.py
@@ -118,10 +118,14 @@
     commit_message = attr.ib(type=str, default='')
 
     @property
+    def gerrit_host(self):
+        return f'https://{self.gerrit_name}-review.googlesource.com'
+
+    @property
     def gerrit_url(self):
         if not self.number:
             return self.gitiles_url
-        return f'https://{self.gerrit_name}-review.googlesource.com/c/{self.number}'
+        return f'{self.gerrit_host}/c/{self.number}'
 
     @property
     def gitiles_url(self):
diff --git a/recipes/docs_builder.expected/docs-presubmit.json b/recipes/docs_builder.expected/docs-presubmit.json
index 322a0ef..baf90bd 100644
--- a/recipes/docs_builder.expected/docs-presubmit.json
+++ b/recipes/docs_builder.expected/docs-presubmit.json
@@ -3201,6 +3201,48 @@
     ]
   },
   {
+    "cmd": [
+      "[START_DIR]/cipd_tool/path/to/gerrit/version%3Apinned-version/gerrit",
+      "set-review",
+      "-host",
+      "https://pigweed-review.googlesource.com",
+      "-input",
+      "{\"change_id\": \"123456\", \"input\": {\"message\": \"Docs available at: https://pigweed.dev/?rev=8945511751514863184\", \"notify\": \"NONE\"}, \"revision_id\": \"current\"}",
+      "-output",
+      "/path/to/tmp/json"
+    ],
+    "infra_step": true,
+    "luci_context": {
+      "realm": {
+        "name": "project:try"
+      },
+      "resultdb": {
+        "current_invocation": {
+          "name": "invocations/build:8945511751514863184",
+          "update_token": "token"
+        },
+        "hostname": "rdbhost"
+      }
+    },
+    "name": "post CL comment",
+    "timeout": 600,
+    "~followup_annotations": [
+      "@@@STEP_LOG_END@json.output (invalid)@@@",
+      "@@@STEP_LOG_LINE@json.output (exception)@No JSON object could be decoded@@@",
+      "@@@STEP_LOG_END@json.output (exception)@@@",
+      "@@@STEP_LOG_LINE@json.input@{@@@",
+      "@@@STEP_LOG_LINE@json.input@  \"change_id\": \"123456\", @@@",
+      "@@@STEP_LOG_LINE@json.input@  \"input\": {@@@",
+      "@@@STEP_LOG_LINE@json.input@    \"message\": \"Docs available at: https://pigweed.dev/?rev=8945511751514863184\", @@@",
+      "@@@STEP_LOG_LINE@json.input@    \"notify\": \"NONE\"@@@",
+      "@@@STEP_LOG_LINE@json.input@  }, @@@",
+      "@@@STEP_LOG_LINE@json.input@  \"revision_id\": \"current\"@@@",
+      "@@@STEP_LOG_LINE@json.input@}@@@",
+      "@@@STEP_LOG_END@json.input@@@",
+      "@@@STEP_LINK@gerrit link@https://pigweed-review.googlesource.com/q/123456@@@"
+    ]
+  },
+  {
     "name": "$result",
     "summaryMarkdown": "Docs available at https://pigweed.dev/?rev=8945511751514863184."
   }
diff --git a/recipes/docs_builder.proto b/recipes/docs_builder.proto
index 2d761d9..d1e0005 100644
--- a/recipes/docs_builder.proto
+++ b/recipes/docs_builder.proto
@@ -48,4 +48,7 @@
   // Link to webpage. Only used in the summary_result of the build. Will be
   // evaluated using '.format()'. Default: "https://pigweed.dev/?rev={}".
   string link_template = 8;
+
+  // Whether to comment on the triggering CL with a link to the rendered docs.
+  bool comment = 9;
 }
diff --git a/recipes/docs_builder.py b/recipes/docs_builder.py
index 7d60f04..037ee27 100644
--- a/recipes/docs_builder.py
+++ b/recipes/docs_builder.py
@@ -99,6 +99,16 @@
     link_template = props.link_template or 'https://pigweed.dev/?rev={}'
     link = link_template.format(path)
 
+    if props.comment and api.buildbucket_util.is_tryjob:
+        for change in checkout.changes:
+            api.gerrit.set_review(
+                "post CL comment",
+                change.number,
+                host=change.gerrit_host,
+                message=f'Docs available at: {link}',
+                notify="NONE",
+            )
+
     return result.RawResult(
         summary_markdown=f'Docs available at {link}.', status=common.SUCCESS,
     )
@@ -130,6 +140,7 @@
         api.test('docs-presubmit')
         + api.checkout.try_test_data()
         + properties(
+            comment=True,
             dry_run=False,
             pw_presubmit_options=api.pw_presubmit.options(step=['step']),
         )