From 7164f6e225b470511cf006ddeb0210faf156af9b Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Wed, 17 Jan 2024 23:12:22 +0100 Subject: [PATCH] fix: Prevent nil dereference in `pr view`. (#8566) The API may return a `null` project, maybe related to "legacy projects". This is translated to a nil pointer in Go. When accessing `project.Column`, the nil pointer was dereferenced, causing a segmentation fault. --- .../fixtures/prViewPreviewWithNilProject.json | 33 +++++++++++++++++++ pkg/cmd/pr/view/view.go | 3 ++ pkg/cmd/pr/view/view_test.go | 10 ++++++ 3 files changed, 46 insertions(+) create mode 100644 pkg/cmd/pr/view/fixtures/prViewPreviewWithNilProject.json diff --git a/pkg/cmd/pr/view/fixtures/prViewPreviewWithNilProject.json b/pkg/cmd/pr/view/fixtures/prViewPreviewWithNilProject.json new file mode 100644 index 000000000..9ed9b1166 --- /dev/null +++ b/pkg/cmd/pr/view/fixtures/prViewPreviewWithNilProject.json @@ -0,0 +1,33 @@ +{ + "data": { + "repository": { + "pullRequest": { + "number": 12, + "title": "Blueberries are from a fork", + "state": "MERGED", + "body": "**blueberries taste good**", + "url": "https://github.com/OWNER/REPO/pull/12", + "author": { + "login": "nobody" + }, + "commits": { + "totalCount": 12 + }, + "additions": 100, + "deletions": 10, + "baseRefName": "master", + "headRefName": "blueberries", + "headRepositoryOwner": { + "login": "hubot" + }, + "projectCards": { + "nodes": [ + null + ], + "totalCount": 1 + }, + "isCrossRepository": true + } + } + } +} diff --git a/pkg/cmd/pr/view/view.go b/pkg/cmd/pr/view/view.go index 767e8836c..a834f4ae4 100644 --- a/pkg/cmd/pr/view/view.go +++ b/pkg/cmd/pr/view/view.go @@ -439,6 +439,9 @@ func prProjectList(pr api.PullRequest) string { projectNames := make([]string, 0, len(pr.ProjectCards.Nodes)) for _, project := range pr.ProjectCards.Nodes { + if project == nil { + continue + } colName := project.Column.Name if colName == "" { colName = "Awaiting triage" diff --git a/pkg/cmd/pr/view/view_test.go b/pkg/cmd/pr/view/view_test.go index 5e2c4b922..6c2936b95 100644 --- a/pkg/cmd/pr/view/view_test.go +++ b/pkg/cmd/pr/view/view_test.go @@ -333,6 +333,16 @@ func TestPRView_Preview_nontty(t *testing.T) { `auto-merge:\tenabled\thubot\tsquash\n`, }, }, + "PR with nil project": { + branch: "master", + args: "12", + fixtures: map[string]string{ + "PullRequestByNumber": "./fixtures/prViewPreviewWithNilProject.json", + }, + expectedOutputs: []string{ + `projects:\t\n`, + }, + }, } for name, tc := range tests {