Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid panic when user doesn't have permission to the repositories #75

Merged
merged 4 commits into from
Apr 20, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 46 additions & 46 deletions lib/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,28 @@ type Line struct {
status string
}

// NewLineByIssue is an initializer by Issue
func NewLineByIssue(repoName string, issue github.Issue) Line {
return Line{
title: *issue.Title,
repoName: repoName,
url: *issue.HTMLURL,
user: *issue.User.Login,
status: getIssueStatus(issue),
}
}

// NewLineByPullRequest is an initializer by PR
func NewLineByPullRequest(repoName string, pr github.PullRequest) Line {
return Line{
title: *pr.Title,
repoName: repoName,
url: *pr.HTMLURL,
user: *pr.User.Login,
status: getPullRequestStatus(pr),
}
}

// Line returns Issue/PR info retrieving from GitHub
func (f *Format) Line(event *github.Event, i int) Line {
if f.debug {
Expand All @@ -47,67 +69,45 @@ func (f *Format) Line(event *github.Event, i int) Line {
e := payload.(*github.IssuesEvent)
issue := getIssue(f.ctx, f.client, *event.Repo.Name, *e.Issue.Number)

if issue.PullRequestLinks == nil {
line = Line{
title: *issue.Title,
repoName: *event.Repo.Name,
url: *issue.HTMLURL,
user: *issue.User.Login,
status: getIssueStatus(issue),
if issue != nil {
if issue.PullRequestLinks == nil {
line = NewLineByIssue(*event.Repo.Name, *issue)
} else {
pr := getPullRequest(f.ctx, f.client, *event.Repo.Name, *e.Issue.Number)
line = NewLineByPullRequest(*event.Repo.Name, *pr)
}
} else {
pr := getPullRequest(f.ctx, f.client, *event.Repo.Name, *e.Issue.Number)

line = Line{
title: *pr.Title,
repoName: *event.Repo.Name,
url: *pr.HTMLURL,
user: *pr.User.Login,
status: getPullRequestStatus(pr),
}
line = NewLineByIssue(*event.Repo.Name, *e.Issue)
}
case "IssueCommentEvent":
e := payload.(*github.IssueCommentEvent)
issue := getIssue(f.ctx, f.client, *event.Repo.Name, *e.Issue.Number)

if issue.PullRequestLinks == nil {
line = Line{
title: *issue.Title,
repoName: *event.Repo.Name,
url: *issue.HTMLURL,
user: *issue.User.Login,
status: getIssueStatus(issue),
if issue != nil {
if issue.PullRequestLinks == nil {
line = NewLineByIssue(*event.Repo.Name, *issue)
} else {
pr := getPullRequest(f.ctx, f.client, *event.Repo.Name, *e.Issue.Number)
line = NewLineByPullRequest(*event.Repo.Name, *pr)
}
} else {
pr := getPullRequest(f.ctx, f.client, *event.Repo.Name, *e.Issue.Number)

line = Line{
title: *pr.Title,
repoName: *event.Repo.Name,
url: *pr.HTMLURL,
user: *pr.User.Login,
status: getPullRequestStatus(pr),
}
line = NewLineByIssue(*event.Repo.Name, *e.Issue)
}
case "PullRequestEvent":
e := payload.(*github.PullRequestEvent)
pr := getPullRequest(f.ctx, f.client, *event.Repo.Name, e.GetNumber())
line = Line{
title: *pr.Title,
repoName: *event.Repo.Name,
url: *pr.HTMLURL,
user: *pr.User.Login,
status: getPullRequestStatus(pr),
if pr != nil {
line = NewLineByPullRequest(*event.Repo.Name, *pr)
} else {
line = NewLineByPullRequest(*event.Repo.Name, *e.PullRequest)
}
case "PullRequestReviewCommentEvent":
e := payload.(*github.PullRequestReviewCommentEvent)
pr := getPullRequest(f.ctx, f.client, *event.Repo.Name, *e.PullRequest.Number)
line = Line{
title: *pr.Title,
repoName: *event.Repo.Name,
url: *pr.HTMLURL,
user: *pr.User.Login,
status: getPullRequestStatus(pr),
if pr != nil {
line = NewLineByPullRequest(*event.Repo.Name, *pr)
} else {
line = NewLineByPullRequest(*event.Repo.Name, *e.PullRequest)
}
}

Expand All @@ -126,15 +126,15 @@ func getPullRequest(ctx context.Context, client *github.Client, repoFullName str
return pr
}

func getIssueStatus(issue *github.Issue) string {
func getIssueStatus(issue github.Issue) string {
result := ""
if *issue.State == "closed" {
result = "closed"
}
return result
}

func getPullRequestStatus(pr *github.PullRequest) string {
func getPullRequestStatus(pr github.PullRequest) string {
result := ""
if *pr.Merged {
result = "merged"
Expand Down