findmissing: gerrit_interface: Reference originating commit if there is no BUG

If we are generating a CL against a previous commit into a ChromeOS branch,
and the offending patch does not have a BUG= entry or specifies BUG=None,
we want to have a reference against that CL in the commit message.

While at it, select last instead of first BUG= and TEST= message, in the
assumption that the last message in the file (if there is more than one)
is more relevant than the first.

BUG=None
TEST=Run on GCE

Change-Id: Ic466e213c091187e1ae61c922b4eec73c7b513a4
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/dev-util/+/2200138
Reviewed-by: Curtis Malainey <cujomalainey@chromium.org>
Commit-Queue: Guenter Roeck <groeck@chromium.org>
Tested-by: Guenter Roeck <groeck@chromium.org>
diff --git a/contrib/findmissing/gerrit_interface.py b/contrib/findmissing/gerrit_interface.py
index 5e5fca6..ecd9bf9 100755
--- a/contrib/findmissing/gerrit_interface.py
+++ b/contrib/findmissing/gerrit_interface.py
@@ -171,8 +171,12 @@
     bug_matches = re.findall('^BUG=(.*)$', chrome_commit_msg, re.M)
     test_matches = re.findall('^TEST=(.*)$', chrome_commit_msg, re.M)
 
-    bug = bug_matches[0] if bug_matches else None
-    test = test_matches[0] if test_matches else None
+    if bug_matches:
+        bug = bug_matches[-1]
+    if bug is None or bug == 'None':
+        bug = 'None (see commit %s)' % chrome_sha
+    if test_matches:
+        test = test_matches[-1]
 
     return bug_test_line % (bug, test)