dmitri.shuralyov.com/service/change/...

gerritapi: detect empty body in commitMessageBody

The commit message should always have a subject and headers,
but the body (providing additional information) may be absent.
Detect that case and return empty string, rather than erroneously
returning the headers as the body.

Also add some basic tests.
dmitshur committed 5 years ago commit 7103ed879ef71d84225b767fb9c85316e826cc65
Collapse all
gerritapi/gerritapi.go
@@ -547,11 +547,12 @@ func commitMessageBody(s string) string {
		return ""
	}
	i += len("\n\n")
	j := strings.LastIndex(s, "\n\n")
	if i > j {
		return s[i:]
		// Only a subject and headers, no body.
		return ""
	}
	return s[i:j]
}

func project(repo string) string {
gerritapi/gerritapi_test.go
@@ -100,5 +100,54 @@ func TestParsePSMessage(t *testing.T) {
		if got, want := body, tc.wantBody; got != want {
			t.Errorf("%d: got body: %q, want: %q", i, got, want)
		}
	}
}

func TestCommitMessageBody(t *testing.T) {
	for i, tc := range []struct {
		in   string
		want string
	}{
		{
			in: `cmd/gopherbot: assign reviewers based on commit message prefixes

Previously, we assigned reviewers based on the file paths involved.

In practice, many changes focused in one directory have trivial
repercussions elsewhere, so that heuristic tends to involve too many
reviewers.

I added a path expansion function in CL 170863, so let's use that here
too: a human selected the paths for the commit message, so use that
human's choice to guide reviewer selection.

Fixes golang/go#30695

Change-Id: If28d6cb2511f4e3f3c651bf736dda394e098c17d
`,
			want: `Previously, we assigned reviewers based on the file paths involved.

In practice, many changes focused in one directory have trivial
repercussions elsewhere, so that heuristic tends to involve too many
reviewers.

I added a path expansion function in CL 170863, so let's use that here
too: a human selected the paths for the commit message, so use that
human's choice to guide reviewer selection.

Fixes golang/go#30695`,
		},
		{
			in: `transform, unicode/cldr: spell "Deprecated: Use etc" consistently

Change-Id: I5194e58a7679e33555856a413f6081fea26d8e34
`,
			want: "",
		},
	} {
		got := commitMessageBody(tc.in)
		if got != tc.want {
			t.Errorf("%d: got: %q, want: %q", i, got, tc.want)
		}
	}
}