issues

Github, Jira, Gitlab issues from Plan 9's acme
Log | Files | Refs | README | LICENSE

commit 170631b778589a9942d5ebc1628b76cb4ea612e5
parent 47d2055f18788c0250c64a9b435cb9cdab88b676
Author: Russ Cox <rsc@golang.org>
Date:   Thu, 18 Apr 2024 13:33:45 -0400

github: avoid a few more nil derefs in corner case responses

Diffstat:
Missue.go | 30++++++++++++++++++++----------
1 file changed, 20 insertions(+), 10 deletions(-)

diff --git a/issue.go b/issue.go @@ -403,7 +403,7 @@ func toLabel(s *schema.Label) *Label { Name: s.Name, Description: s.Description, ID: string(s.Id), - Owner: s.Repository.Owner.Interface.(interface{ GetLogin() string }).GetLogin(), + Owner: toOwner(&s.Repository.Owner), Repo: s.Repository.Name, } } @@ -417,12 +417,26 @@ type Discussion struct { Body string } +func toAuthor(a *schema.Actor) string { + if a != nil && a.Interface != nil { + return a.Interface.GetLogin() + } + return "" +} + +func toOwner(o *schema.RepositoryOwner) string { + if o != nil && o.Interface != nil { + return o.Interface.(interface{ GetLogin() string }).GetLogin() + } + return "" +} + func toDiscussion(s *schema.Discussion) *Discussion { return &Discussion{ Locked: s.Locked, Title: s.Title, Number: s.Number, - Owner: s.Repository.Owner.Interface.(interface{ GetLogin() string }).GetLogin(), + Owner: toOwner(&s.Repository.Owner), Repo: s.Repository.Name, Body: s.Body, } @@ -460,20 +474,16 @@ type Issue struct { } func toIssue(s *schema.Issue) *Issue { - author := "" - if s.Author.Interface != nil { - author = s.Author.Interface.GetLogin() - } return &Issue{ ID: string(s.Id), Title: s.Title, Number: s.Number, - Author: author, + Author: toAuthor(&s.Author), Closed: s.Closed, ClosedAt: toTime(s.ClosedAt), CreatedAt: toTime(s.CreatedAt), LastEditedAt: toTime(s.LastEditedAt), - Owner: s.Repository.Owner.Interface.(interface{ GetLogin() string }).GetLogin(), + Owner: toOwner(&s.Repository.Owner), Repo: s.Repository.Name, Milestone: toMilestone(s.Milestone), Labels: apply(toLabel, s.Labels.Nodes), @@ -504,14 +514,14 @@ type IssueComment struct { func toIssueComment(s *schema.IssueComment) *IssueComment { return &IssueComment{ - Author: s.Author.Interface.GetLogin(), + Author: toAuthor(&s.Author), Body: s.Body, CreatedAt: toTime(s.CreatedAt), ID: string(s.Id), PublishedAt: toTime(s.PublishedAt), UpdatedAt: toTime(s.UpdatedAt), Issue: s.Issue.GetNumber(), - Owner: s.Repository.Owner.Interface.(interface{ GetLogin() string }).GetLogin(), + Owner: toOwner(&s.Repository.Owner), Repo: s.Repository.Name, } }