From ce4ef1104220163f6364783957686316129edc9e Mon Sep 17 00:00:00 2001 From: Mihai Parparita Date: Tue, 5 Aug 2014 22:34:21 -0700 Subject: [PATCH] Don't send digest emails if empty. --- app/digest.go | 24 +++++++++++++++++------- app/githop.go | 23 ++++++++++++++--------- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/app/digest.go b/app/digest.go index 9ad5ba3..a6ecd23 100644 --- a/app/digest.go +++ b/app/digest.go @@ -10,8 +10,9 @@ import ( ) const ( - DisplayDateFormat = "3:04pm" - DisplayDateTooltipFormat = "Monday January 2 3:04pm" + CommitDisplayDateFormat = "3:04pm" + CommitDisplayDateTooltipFormat = "Monday January 2 3:04pm" + DigestDisplayDateFormat = "January 2, 2006 was a Monday" ) type DigestCommit struct { @@ -45,15 +46,15 @@ func newDigestCommit(commit *github.RepositoryCommit, repo *github.Repository, l func (commit DigestCommit) DisplayDate() string { // Prefer the date the comit was pushed, since that's what GitHub filters // and sorts by. - return commit.PushDate.Format(DisplayDateFormat) + return commit.PushDate.Format(CommitDisplayDateFormat) } func (commit DigestCommit) DisplayDateTooltip() string { // But show the full details in a tooltip return fmt.Sprintf( "Pushed at %s\nCommited at %s", - commit.PushDate.Format(DisplayDateTooltipFormat), - commit.CommitDate.Format(DisplayDateTooltipFormat)) + commit.PushDate.Format(CommitDisplayDateTooltipFormat), + commit.CommitDate.Format(CommitDisplayDateTooltipFormat)) } type RepoDigest struct { @@ -170,6 +171,15 @@ func (digest *Digest) fetch(repos []github.Repository, githubClient *github.Clie return nil } -func (digest *Digest) DisplayDate() string { - return digest.StartTime.Format("January 2, 2006 was a Monday") +func (digest *Digest) Empty() bool { + for _, repoDigest := range digest.RepoDigests { + if len(repoDigest.Commits) > 0 { + return false + } + } + return true +} + +func (digest *Digest) DisplayDate() string { + return digest.StartTime.Format(DigestDisplayDateFormat) } diff --git a/app/githop.go b/app/githop.go index a0127d9..648c9ae 100644 --- a/app/githop.go +++ b/app/githop.go @@ -133,7 +133,7 @@ func sendDigestHandler(w http.ResponseWriter, r *http.Request) { return } - err = sendDigestForAccount(account, c) + _, err = sendDigestForAccount(account, c) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return @@ -149,24 +149,29 @@ func digestCronHandler(w http.ResponseWriter, r *http.Request) { getAllAccounts(c, &accounts) for _, account := range accounts { c.Infof("Sending digest for %d...", account.GitHubUserId) - err := sendDigestForAccount(&account, c) + sent, err := sendDigestForAccount(&account, c) if err != nil { c.Errorf(" Error: %s", err.Error()) - } else { + } else if sent { c.Infof(" Sent!") + } else { + c.Infof(" Not sent, digest was empty") } } 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.Token = &account.OAuthToken githubClient := github.NewClient(oauthTransport.Client()) digest, err := newDigest(githubClient, account) if err != nil { - return err + return false, err + } + if digest.Empty() { + return false, nil } var digestHtml bytes.Buffer @@ -174,13 +179,13 @@ func sendDigestForAccount(account *Account, c appengine.Context) error { digestHtml.Write(getDigestStyles()) digestHtml.WriteString("") if err := templates.ExecuteTemplate(&digestHtml, "digest", digest); err != nil { - return err + return false, err } digestHtml.WriteString("") emails, _, err := githubClient.Users.ListEmails(nil) if err != nil { - return err + return false, err } var primaryVerified *string for _, email := range emails { @@ -191,7 +196,7 @@ func sendDigestForAccount(account *Account, c appengine.Context) error { } } 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{ @@ -201,7 +206,7 @@ func sendDigestForAccount(account *Account, c appengine.Context) error { HTMLBody: digestHtml.String(), } err = mail.Send(c, digestMessage) - return err + return true, err } func getDigestStyles() []byte {