git: aa90b92ac289 - main - git-arc: Fix find_author() for external users

From: Jose Luis Duran <jlduran_at_FreeBSD.org>
Date: Tue, 05 Nov 2024 02:11:55 UTC
The branch main has been updated by jlduran:

URL: https://cgit.FreeBSD.org/src/commit/?id=aa90b92ac289b2a7eec882caf870e56dbed16a5c

commit aa90b92ac289b2a7eec882caf870e56dbed16a5c
Author:     Jose Luis Duran <jlduran@FreeBSD.org>
AuthorDate: 2024-11-05 01:32:25 +0000
Commit:     Jose Luis Duran <jlduran@FreeBSD.org>
CommitDate: 2024-11-05 02:10:15 +0000

    git-arc: Fix find_author() for external users
    
    When an external user submits a review in Phabricator, the find_author()
    subroutine may receive the author_name and author_addr as "null" from
    the JSON API with git-arc patch.
    
    This "null" string gets tested for length by "[ -n", and returning that
    is greater than zero, in other words, the shell does not understand that
    "null" means nothing in this case.
    
    Fix it by adding a guard, that sets an empty string when the content is
    "null".
    
    While here, standardize the indentation style for find_author()
    
    Reviewed by:    emaste, markj
    Approved by:    emaste (mentor)
    Differential Revision:  https://reviews.freebsd.org/D46789
---
 tools/tools/git/git-arc.sh | 38 ++++++++++++++++++++++++--------------
 1 file changed, 24 insertions(+), 14 deletions(-)

diff --git a/tools/tools/git/git-arc.sh b/tools/tools/git/git-arc.sh
index cca70f61a4cd..f3cae28e3009 100644
--- a/tools/tools/git/git-arc.sh
+++ b/tools/tools/git/git-arc.sh
@@ -493,16 +493,16 @@ find_author()
     case "${addr}" in
     *.*) ;;		# external user
     *)
-	echo "${name} <${addr}@FreeBSD.org>"
-	return
-	;;
+        echo "${name} <${addr}@FreeBSD.org>"
+        return
+        ;;
     esac
 
     # Choice 2: author_addr and author_name were set in the bundle, so use
     # that. We may need to filter some known bogus ones, should they crop up.
     if [ -n "$author_name" -a -n "$author_addr" ]; then
-	echo "${author_name} <${author_addr}>"
-	return
+        echo "${author_name} <${author_addr}>"
+        return
     fi
 
     # Choice 3: We can find this user in the FreeBSD repo. They've submited
@@ -510,8 +510,8 @@ find_author()
     # similar to their phab username.
     email=$(git log -1 --author "$(echo ${addr} | tr _ .)" --pretty="%aN <%aE>")
     if [ -n "${email}" ]; then
-	echo "${email}"
-	return
+        echo "${email}"
+        return
     fi
 
     # Choice 4: We know this user. They've committed before, and they happened
@@ -519,11 +519,11 @@ find_author()
     # might not be a good idea, since names can be somewhat common (there
     # are two Andrew Turners that have contributed to FreeBSD, for example).
     if ! (echo "${name}" | grep -w "[Uu]ser" -q); then
-	email=$(git log -1 --author "${name}" --pretty="%aN <%aE>")
-	if [ -n "$email" ]; then
-	    echo "$email"
-	    return
-	fi
+        email=$(git log -1 --author "${name}" --pretty="%aN <%aE>")
+        if [ -n "$email" ]; then
+            echo "$email"
+            return
+        fi
     fi
 
     # Choice 5: Wing it as best we can. In this scenario, we replace the last _
@@ -534,8 +534,8 @@ find_author()
     a=$(printf "%s <%s>\n" "${name}" $(echo "$addr" | sed -e 's/\(.*\)_/\1@/'))
     echo "Making best guess: Truning ${addr} to ${a}"
     if ! prompt; then
-       echo "ABORT"
-       return
+        echo "ABORT"
+        return
     fi
     echo "${a}"
 }
@@ -572,6 +572,16 @@ patch_commit()
         jq -r '.response | flatten | .[]' > "$diff_data"
     author_addr=$(jq -r ".authorEmail?" "$diff_data" | sort -u)
     author_name=$(jq -r ".authorName?" "$diff_data" | sort -u)
+
+    # JSON will return "null" when a field is not populated.
+    # Turn this string into an empty one.
+    if [ "$author_addr" = "null" ]; then
+        author_addr=""
+    fi
+    if [ "$author_name" = "null" ]; then
+        author_name=""
+    fi
+
     author=$(find_author "$user_addr" "$user_name" "$author_addr" "$author_name")
     rm "$diff_data"