roll_commenter: Add build summary markdowns

Bug: b/369718402
Change-Id: I88fb186f44f6416264f1a8dc027afb39d5ea42ec
Reviewed-on: https://pigweed-review.googlesource.com/c/infra/recipes/+/254140
Presubmit-Verified: CQ Bot Account <pigweed-scoped@luci-project-accounts.iam.gserviceaccount.com>
Commit-Queue: Auto-Submit <auto-submit@pigweed-service-accounts.iam.gserviceaccount.com>
Pigweed-Auto-Submit: Rob Mohr <mohrr@google.com>
Reviewed-by: Danielle Kay <danikay@google.com>
Lint: Lint 🤖 <android-build-ayeaye@system.gserviceaccount.com>
diff --git a/recipes/roll_commenter.expected/initial_run.json b/recipes/roll_commenter.expected/initial_run.json
index 3be42ba..153226b 100644
--- a/recipes/roll_commenter.expected/initial_run.json
+++ b/recipes/roll_commenter.expected/initial_run.json
@@ -458,6 +458,7 @@
     "name": "initial run"
   },
   {
-    "name": "$result"
+    "name": "$result",
+    "summaryMarkdown": "Initial run, not doing anything."
   }
 ]
\ No newline at end of file
diff --git a/recipes/roll_commenter.expected/new_roller.json b/recipes/roll_commenter.expected/new_roller.json
index 9d70da3..b784b36 100644
--- a/recipes/roll_commenter.expected/new_roller.json
+++ b/recipes/roll_commenter.expected/new_roller.json
@@ -676,6 +676,7 @@
     ]
   },
   {
-    "name": "$result"
+    "name": "$result",
+    "summaryMarkdown": "Commented on 1 newly rolled commits"
   }
 ]
\ No newline at end of file
diff --git a/recipes/roll_commenter.expected/no_forward_progress.json b/recipes/roll_commenter.expected/no_forward_progress.json
index 7b25898..d880f59 100644
--- a/recipes/roll_commenter.expected/no_forward_progress.json
+++ b/recipes/roll_commenter.expected/no_forward_progress.json
@@ -1827,6 +1827,7 @@
     ]
   },
   {
-    "name": "$result"
+    "name": "$result",
+    "summaryMarkdown": "Warned on 2 stuck commits"
   }
 ]
\ No newline at end of file
diff --git a/recipes/roll_commenter.expected/no_forward_progress_but_passing.json b/recipes/roll_commenter.expected/no_forward_progress_but_passing.json
index 84f13e4..ffe2730 100644
--- a/recipes/roll_commenter.expected/no_forward_progress_but_passing.json
+++ b/recipes/roll_commenter.expected/no_forward_progress_but_passing.json
@@ -524,6 +524,7 @@
     "name": "no trailing failing rollers"
   },
   {
-    "name": "$result"
+    "name": "$result",
+    "summaryMarkdown": "All rollers that are behind are passing, so no need to alert anybody. These rollers will either continue to pass in which case we're good, or they'll fail, in which case we can alert about the failure."
   }
 ]
\ No newline at end of file
diff --git a/recipes/roll_commenter.expected/no_forward_progress_dry_run.json b/recipes/roll_commenter.expected/no_forward_progress_dry_run.json
index ff9fc78..a7eaf38 100644
--- a/recipes/roll_commenter.expected/no_forward_progress_dry_run.json
+++ b/recipes/roll_commenter.expected/no_forward_progress_dry_run.json
@@ -1753,6 +1753,7 @@
     ]
   },
   {
-    "name": "$result"
+    "name": "$result",
+    "summaryMarkdown": "Warned on 2 stuck commits (dry-run)"
   }
 ]
\ No newline at end of file
diff --git a/recipes/roll_commenter.expected/one_successful_comment.json b/recipes/roll_commenter.expected/one_successful_comment.json
index 5d7bebc..1a2fd93 100644
--- a/recipes/roll_commenter.expected/one_successful_comment.json
+++ b/recipes/roll_commenter.expected/one_successful_comment.json
@@ -616,6 +616,7 @@
     ]
   },
   {
-    "name": "$result"
+    "name": "$result",
+    "summaryMarkdown": "Commented on 1 newly rolled commits"
   }
 ]
\ No newline at end of file
diff --git a/recipes/roll_commenter.expected/one_successful_comment_dry_run_suppress.json b/recipes/roll_commenter.expected/one_successful_comment_dry_run_suppress.json
index fe4b37d..e0c23e1 100644
--- a/recipes/roll_commenter.expected/one_successful_comment_dry_run_suppress.json
+++ b/recipes/roll_commenter.expected/one_successful_comment_dry_run_suppress.json
@@ -579,6 +579,7 @@
     ]
   },
   {
-    "name": "$result"
+    "name": "$result",
+    "summaryMarkdown": "Commented on 1 newly rolled commits (dry-run)"
   }
 ]
\ No newline at end of file
diff --git a/recipes/roll_commenter.expected/two_successful_comments.json b/recipes/roll_commenter.expected/two_successful_comments.json
index a9cc5e6..73fd7be 100644
--- a/recipes/roll_commenter.expected/two_successful_comments.json
+++ b/recipes/roll_commenter.expected/two_successful_comments.json
@@ -712,6 +712,7 @@
     ]
   },
   {
-    "name": "$result"
+    "name": "$result",
+    "summaryMarkdown": "Commented on 2 newly rolled commits"
   }
 ]
\ No newline at end of file
diff --git a/recipes/roll_commenter.py b/recipes/roll_commenter.py
index 41272c4..1db3877 100644
--- a/recipes/roll_commenter.py
+++ b/recipes/roll_commenter.py
@@ -196,7 +196,10 @@
         state['last_successful_commit'] = new_successful_commit
         api.builder_state.save(state)
         api.step.empty('initial run')
-        return
+        return result_pb.RawResult(
+            summary_markdown='Initial run, not doing anything.',
+            status=common_pb.SUCCESS,
+        )
 
     # If rolls have moved forward since the last run, comment on the newly
     # rolled changes.
@@ -219,6 +222,7 @@
                 f'commit{"" if len(newly_rolled) == 1 else "s"}'
             )
 
+        num_comments_written = 0
         with api.defer.context() as defer:
             for revision in newly_rolled:
                 with api.step.nest(revision[0:7]) as pres:
@@ -238,6 +242,7 @@
                         f'Successfully rolled into {len(current_rolls)} '
                         f'project{"" if len(current_rolls) == 1 else "s"}'
                     )
+                    num_comments_written += 1
 
                     if props.dry_run:
                         pres.step_summary_text = 'dry run, not commenting'
@@ -265,7 +270,14 @@
 
         # For simplicity, if we moved forward at all on commits, don't comment
         # on changes about not rolling. If it's stuck the next run will comment.
-        return
+        dry_run_part = ' (dry-run)' if props.dry_run else ''
+        return result_pb.RawResult(
+            summary_markdown=(
+                f'Commented on {num_comments_written} newly rolled '
+                f'commits{dry_run_part}'
+            ),
+            status=common_pb.SUCCESS,
+        )
 
     current_time = api.time.utcnow()
     delta = datetime.timedelta(hours=props.hours_before_warning or 36)
@@ -312,7 +324,15 @@
 
     if not trailing_failing_rollers:
         api.step.empty('no trailing failing rollers')  # pragma: no cover
-        return
+        return result_pb.RawResult(
+            summary_markdown=(
+                'All rollers that are behind are passing, so no need to alert '
+                'anybody. These rollers will either continue to pass in which '
+                "case we're good, or they'll fail, in which case we can alert "
+                'about the failure.'
+            ),
+            status=common_pb.SUCCESS,
+        )
 
     num_failing = len(trailing_failing_rollers)
     summary = [
@@ -329,6 +349,8 @@
             url = api.buildbucket.builder_url(build=build)
             summary.append(f'* [{roller}]({url})')
 
+    num_warning_comments = 0
+
     with api.defer.context() as defer:
         for revision in unrolled:
             with api.step.nest(revision[0:7]) as pres:
@@ -375,6 +397,8 @@
                         api.step.empty('too recent')
                     continue
 
+                num_warning_comments += 1
+
                 if props.dry_run:
                     pres.step_summary_text = 'dry run, not warning'
                     warning = api.step.empty('warning').presentation
@@ -400,6 +424,14 @@
                             f'failed to warn\n\n{joined_summary}'
                         )
 
+    dry_run_part = ' (dry-run)' if props.dry_run else ''
+    return result_pb.RawResult(
+        summary_markdown=(
+            f'Warned on {num_warning_comments} stuck commits{dry_run_part}'
+        ),
+        status=common_pb.SUCCESS,
+    )
+
 
 def GenTests(api) -> Generator[recipe_test_api.TestData, None, None]:
     """Create tests."""