Don't send digest emails if empty.

This commit is contained in:
Mihai Parparita 2014-08-05 22:34:21 -07:00
parent 8273cb18bb
commit ce4ef11042
2 changed files with 31 additions and 16 deletions

View file

@ -10,8 +10,9 @@ import (
) )
const ( const (
DisplayDateFormat = "3:04pm" CommitDisplayDateFormat = "3:04pm"
DisplayDateTooltipFormat = "Monday January 2 3:04pm" CommitDisplayDateTooltipFormat = "Monday January 2 3:04pm"
DigestDisplayDateFormat = "January 2, 2006 was a Monday"
) )
type DigestCommit struct { type DigestCommit struct {
@ -45,15 +46,15 @@ func newDigestCommit(commit *github.RepositoryCommit, repo *github.Repository, l
func (commit DigestCommit) DisplayDate() string { func (commit DigestCommit) DisplayDate() string {
// Prefer the date the comit was pushed, since that's what GitHub filters // Prefer the date the comit was pushed, since that's what GitHub filters
// and sorts by. // and sorts by.
return commit.PushDate.Format(DisplayDateFormat) return commit.PushDate.Format(CommitDisplayDateFormat)
} }
func (commit DigestCommit) DisplayDateTooltip() string { func (commit DigestCommit) DisplayDateTooltip() string {
// But show the full details in a tooltip // But show the full details in a tooltip
return fmt.Sprintf( return fmt.Sprintf(
"Pushed at %s\nCommited at %s", "Pushed at %s\nCommited at %s",
commit.PushDate.Format(DisplayDateTooltipFormat), commit.PushDate.Format(CommitDisplayDateTooltipFormat),
commit.CommitDate.Format(DisplayDateTooltipFormat)) commit.CommitDate.Format(CommitDisplayDateTooltipFormat))
} }
type RepoDigest struct { type RepoDigest struct {
@ -170,6 +171,15 @@ func (digest *Digest) fetch(repos []github.Repository, githubClient *github.Clie
return nil return nil
} }
func (digest *Digest) DisplayDate() string { func (digest *Digest) Empty() bool {
return digest.StartTime.Format("January 2, 2006 was a Monday") for _, repoDigest := range digest.RepoDigests {
if len(repoDigest.Commits) > 0 {
return false
}
}
return true
}
func (digest *Digest) DisplayDate() string {
return digest.StartTime.Format(DigestDisplayDateFormat)
} }

View file

@ -133,7 +133,7 @@ func sendDigestHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
err = sendDigestForAccount(account, c) _, err = sendDigestForAccount(account, c)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
return return
@ -149,24 +149,29 @@ func digestCronHandler(w http.ResponseWriter, r *http.Request) {
getAllAccounts(c, &accounts) getAllAccounts(c, &accounts)
for _, account := range accounts { for _, account := range accounts {
c.Infof("Sending digest for %d...", account.GitHubUserId) c.Infof("Sending digest for %d...", account.GitHubUserId)
err := sendDigestForAccount(&account, c) sent, err := sendDigestForAccount(&account, c)
if err != nil { if err != nil {
c.Errorf(" Error: %s", err.Error()) c.Errorf(" Error: %s", err.Error())
} else { } else if sent {
c.Infof(" Sent!") c.Infof(" Sent!")
} else {
c.Infof(" Not sent, digest was empty")
} }
} }
fmt.Fprint(w, "Done") fmt.Fprint(w, "Done")
} }
func sendDigestForAccount(account *Account, c appengine.Context) error { func sendDigestForAccount(account *Account, c appengine.Context) (bool, error) {
oauthTransport := githubOAuthTransport(c) oauthTransport := githubOAuthTransport(c)
oauthTransport.Token = &account.OAuthToken oauthTransport.Token = &account.OAuthToken
githubClient := github.NewClient(oauthTransport.Client()) githubClient := github.NewClient(oauthTransport.Client())
digest, err := newDigest(githubClient, account) digest, err := newDigest(githubClient, account)
if err != nil { if err != nil {
return err return false, err
}
if digest.Empty() {
return false, nil
} }
var digestHtml bytes.Buffer var digestHtml bytes.Buffer
@ -174,13 +179,13 @@ func sendDigestForAccount(account *Account, c appengine.Context) error {
digestHtml.Write(getDigestStyles()) digestHtml.Write(getDigestStyles())
digestHtml.WriteString("</style></head><body>") digestHtml.WriteString("</style></head><body>")
if err := templates.ExecuteTemplate(&digestHtml, "digest", digest); err != nil { if err := templates.ExecuteTemplate(&digestHtml, "digest", digest); err != nil {
return err return false, err
} }
digestHtml.WriteString("</body></html>") digestHtml.WriteString("</body></html>")
emails, _, err := githubClient.Users.ListEmails(nil) emails, _, err := githubClient.Users.ListEmails(nil)
if err != nil { if err != nil {
return err return false, err
} }
var primaryVerified *string var primaryVerified *string
for _, email := range emails { for _, email := range emails {
@ -191,7 +196,7 @@ func sendDigestForAccount(account *Account, c appengine.Context) error {
} }
} }
if primaryVerified == nil { if primaryVerified == nil {
return errors.New("No verified email addresses found in GitHub account") return false, errors.New("No verified email addresses found in GitHub account")
} }
digestMessage := &mail.Message{ digestMessage := &mail.Message{
@ -201,7 +206,7 @@ func sendDigestForAccount(account *Account, c appengine.Context) error {
HTMLBody: digestHtml.String(), HTMLBody: digestHtml.String(),
} }
err = mail.Send(c, digestMessage) err = mail.Send(c, digestMessage)
return err return true, err
} }
func getDigestStyles() []byte { func getDigestStyles() []byte {