Skip to content
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions assets/repo.html.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
{{with .Repo.Remote.Revision}}to {{template "commitID" .}}{{end}}
</div>
{{end}}
{{with .Error}}
<p class="presenter-error"><strong>Error:</strong> {{.}}</p>
{{end}}
</div>
<div style="clear: both;"></div>
</div>
Expand Down
5 changes: 5 additions & 0 deletions assets/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ div.list-entry-body {
margin-bottom: 0px;
padding-left: 64px;
}
.presenter-error {
white-space: pre-wrap;
margin-bottom: 0px;
padding-left: 64px;
}

.highlight-on-hover a {
color: gray;
Expand Down
28 changes: 14 additions & 14 deletions assets_vfsdata.go

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions presenter/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ func (genericPresenter) Image() template.URL {
}

func (genericPresenter) Changes() <-chan Change { return nil }

func (genericPresenter) Error() error { return nil }
34 changes: 30 additions & 4 deletions presenter/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
package github

import (
"fmt"
"html/template"
"log"
"net/http"
"strings"

"github.com/dustin/go-humanize"
"github.com/google/go-github/github"
"github.com/shurcooL/Go-Package-Store/pkg"
"github.com/shurcooL/Go-Package-Store/presenter"
Expand Down Expand Up @@ -71,28 +72,34 @@ type gitHubPresenter struct {

cc *github.CommitsComparison
image template.URL
err error
}

func newGitHubPresenter(repo *pkg.Repo, ghOwner, ghRepo string) *gitHubPresenter {
p := &gitHubPresenter{
repo: repo,
ghOwner: ghOwner,
ghRepo: ghRepo,

image: "https://github.com/images/gravatars/gravatar-user-420.png", // Default fallback.
}

// This might take a while.
if cc, _, err := gh.Repositories.CompareCommits(ghOwner, ghRepo, repo.Local.Revision, repo.Remote.Revision); err == nil {
p.cc = cc
} else if rateLimitErr, ok := err.(*github.RateLimitError); ok {
p.setFirstError(rateLimitError{rateLimitErr})
} else {
// TODO: If hit rate limit, consider not continuing or do something better than just spamming rate-limit-exceeded errors, etc.
log.Println("warning: gh.Repositories.CompareCommits:", err)
p.setFirstError(fmt.Errorf("gh.Repositories.CompareCommits: %v", err))
}

// Use the repo owner avatar image.
if user, _, err := gh.Users.Get(ghOwner); err == nil && user.AvatarURL != nil {
p.image = template.URL(*user.AvatarURL)
} else if rateLimitErr, ok := err.(*github.RateLimitError); ok {
p.setFirstError(rateLimitError{rateLimitErr})
} else {
p.image = "https://github.com/images/gravatars/gravatar-user-420.png"
p.setFirstError(fmt.Errorf("gh.Users.Get: %v", err))
}

return p
Expand Down Expand Up @@ -135,6 +142,25 @@ func (p gitHubPresenter) Changes() <-chan presenter.Change {
return out
}

func (p gitHubPresenter) Error() error { return p.err }

// setFirstError sets error if it's the first one. It does nothing otherwise.
func (p *gitHubPresenter) setFirstError(err error) {
if p.err != nil {
return
}
p.err = err
}

// rateLimitError is an error presentation wrapper for consistent display of *github.RateLimitError.
type rateLimitError struct {
err *github.RateLimitError
}

func (r rateLimitError) Error() string {
return fmt.Sprintf("GitHub API rate limit exceeded; it will be reset in %v", humanize.Time(r.err.Rate.Reset.Time))
}

// firstParagraph returns the first paragraph of a string.
func firstParagraph(s string) string {
index := strings.Index(s, "\n\n")
Expand Down
1 change: 1 addition & 0 deletions presenter/presenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Presenter interface {
Home() *template.URL // Home URL of the Go package. Optional (nil means none available).
Image() template.URL // Image representing the Go package, typically its owner.
Changes() <-chan Change // List of changes, starting with the most recent.
Error() error // Any error that occurred during presentation, to be displayed to user.
}

// Change represents a single commit message.
Expand Down