Don't crash on empty PR bodies

GitHub sunds those as null values, so we can't deref them without
checking first. Modify the test to actually use a null body at least
once.
This commit is contained in:
Clemens Lang
2025-09-12 22:56:53 +02:00
parent 8dc706eaac
commit e0682b4b28
2 changed files with 18 additions and 12 deletions
+8 -6
View File
@@ -116,7 +116,7 @@ func (receiver *Receiver) processPullRequest(event *github.PullRequestEvent) {
if receiver.production {
mentionSymbol = "@"
}
if len(handles) > 0 && !strings.Contains(*event.PullRequest.Body, "[skip notification]") {
if len(handles) > 0 && (event.PullRequest.Body == nil || !strings.Contains(*event.PullRequest.Body, "[skip notification]")) {
body := "Notifying maintainers:\n"
for handle, ports := range handles {
body += mentionSymbol + handle + " for port " + strings.Join(ports, ", ") + ".\n"
@@ -177,13 +177,15 @@ func (receiver *Receiver) processPullRequest(event *github.PullRequestEvent) {
if strings.Contains(strings.ToLower(*event.PullRequest.Title), ": update") || strings.HasPrefix(strings.ToLower(*event.PullRequest.Title), "update") {
typeLabels = appendIfUnique(typeLabels, "type: update")
}
if cveRegexp.FindString(*event.PullRequest.Title) != "" || cveRegexp.FindString(*event.PullRequest.Body) != "" {
if cveRegexp.FindString(*event.PullRequest.Title) != "" || (event.PullRequest.Body != nil && cveRegexp.FindString(*event.PullRequest.Body) != "") {
typeLabels = appendIfUnique(typeLabels, "type: security fix")
}
typesFromBody := []string{"bugfix", "enhancement", "security fix", "update"}
for _, t := range typesFromBody {
if strings.Contains(*event.PullRequest.Body, "[x] "+t) {
typeLabels = appendIfUnique(typeLabels, "type: "+t)
if event.PullRequest.Body != nil {
typesFromBody := []string{"bugfix", "enhancement", "security fix", "update"}
for _, t := range typesFromBody {
if strings.Contains(*event.PullRequest.Body, "[x] "+t) {
typeLabels = appendIfUnique(typeLabels, "type: "+t)
}
}
}
+10 -6
View File
@@ -22,11 +22,15 @@ type PullRequestEventTest struct {
number int
sender string
title string
body string
body *string
comment string
labels []string
}
func addrof(s string) *string {
return &s;
}
func TestHandlePullRequest(t *testing.T) {
stubClient := stubGitHubClient{}
receiver := &Receiver{
@@ -61,13 +65,13 @@ func TestHandlePullRequest(t *testing.T) {
}
}`), &event)
prTests := []*PullRequestEventTest{
{number: 1, sender: "l2dy", title: "z: update to 1.1", labels: []string{"maintainer: none", "type: update", "by: member"}},
{number: 1, sender: "jverne", title: "z: update to 1.1", body: "[x] enhancement", labels: []string{"maintainer: none", "type: update", "type: enhancement"}},
{number: 1, sender: "jverne", title: "z: update to 1.1", body: "Fixes CVE-0000-0.", labels: []string{"maintainer: none", "type: update", "type: security fix"}},
{number: 1, sender: "l2dy", title: "z: update to 1.1", body: nil, labels: []string{"maintainer: none", "type: update", "by: member"}},
{number: 1, sender: "jverne", title: "z: update to 1.1", body: addrof("[x] enhancement"), labels: []string{"maintainer: none", "type: update", "type: enhancement"}},
{number: 1, sender: "jverne", title: "z: update to 1.1", body: addrof("Fixes CVE-0000-0."), labels: []string{"maintainer: none", "type: update", "type: security fix"}},
{number: 2, sender: "jverne", title: "upx-devel: new port", labels: []string{"type: submission"}},
{number: 3, sender: "l2dy", title: "upx: update to 1.1", labels: []string{"maintainer", "maintainer: open", "type: update", "by: member"}},
{number: 3, sender: "jverne", title: "upx: update to 1.1", comment: "Notifying maintainers:\n@_l2dy for port upx.\n", labels: []string{"maintainer: open", "type: update"}},
{number: 3, sender: "jverne", title: "upx: update to 1.1", body: "<!-- [skip notification] -->", labels: []string{"maintainer: open", "type: update"}},
{number: 3, sender: "jverne", title: "upx: update to 1.1", body: addrof("<!-- [skip notification] -->"), labels: []string{"maintainer: open", "type: update"}},
}
for _, prt := range prTests {
stubClient.newComment = ""
@@ -75,7 +79,7 @@ func TestHandlePullRequest(t *testing.T) {
event.Number = &prt.number
event.Sender.Login = &prt.sender
event.PullRequest.Title = &prt.title
event.PullRequest.Body = &prt.body
event.PullRequest.Body = prt.body
eventBody, err := json.Marshal(event)
if err != nil {
t.Error(err)