From 3b9a42cb73811dd531a920391b0b743d9f441ee4 Mon Sep 17 00:00:00 2001 From: Mihai Parparita Date: Mon, 7 Jul 2014 23:41:26 -0700 Subject: [PATCH] Add basic digest showing commits from a year ago in a user's repositories. --- TODO | 4 ++-- app/githop.go | 47 ++++++++++++++++++++++++++++++++++++++-- app/templates/index.html | 22 ++++++++++++++----- 3 files changed, 63 insertions(+), 10 deletions(-) diff --git a/TODO b/TODO index 28218ce..c1badfa 100644 --- a/TODO +++ b/TODO @@ -1,7 +1,7 @@ Notes: -- Don't use the events API, since only 300 events are supported -- Commits API supports timestamp ranges: https://developer.github.com/v3/repos/commits/ - Will need to get/store timezone of user (use https://bitbucket.org/pellepim/jstimezonedetect, use it with time.Location.LoadLocation) +- To get digests for non-user owned organizations, will need to list the organizations and their repositories +- What about getting other repositories the user has access to? Later - Option to only do this for public repos (repo vs. public_repo scopes) diff --git a/app/githop.go b/app/githop.go index 1638806..d2468f3 100644 --- a/app/githop.go +++ b/app/githop.go @@ -8,6 +8,7 @@ import ( "log" "net/http" "net/url" + "time" "appengine" "appengine/urlfetch" @@ -20,6 +21,16 @@ import ( var router *mux.Router var githubOauthConfig oauth.Config +type RepositoryDigest struct { + Repository *github.Repository + Commits []github.RepositoryCommit +} + +type Digest struct { + User *github.User + RepositoryDigests []RepositoryDigest +} + func initGithubOAuthConfig() { path := "config/github-oauth" if appengine.IsDevAppServer() { @@ -74,12 +85,44 @@ func indexHandler(w http.ResponseWriter, r *http.Request) { Token: &token, } githubClient := github.NewClient(oauthTransport.Client()) - events, _, err := githubClient.Activity.ListEventsPerformedByUser("mihaip", false, nil) + + user, _, err := githubClient.Users.Get("") if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } - if err := indexTemplate.Execute(w, events); err != nil { + + now := time.Now() + digestStartTime := time.Date(now.Year()-1, now.Month(), now.Day(), 0, 0, 0, 0, now.Location()) + digestEndTime := digestStartTime.AddDate(0, 0, 7) + + repos, _, err := githubClient.Repositories.List(*user.Login, nil) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + digest := Digest{User: user, RepositoryDigests: make([]RepositoryDigest, 0, len(repos))} + for i, repo := range repos { + commits, _, err := githubClient.Repositories.ListCommits( + *repo.Owner.Login, + *repo.Name, + &github.CommitsListOptions{ + Author: *user.Login, + Since: digestStartTime, + Until: digestEndTime, + }) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + if len(commits) > 0 { + n := len(digest.RepositoryDigests) + digest.RepositoryDigests = digest.RepositoryDigests[0 : n+1] + digest.RepositoryDigests[n] = RepositoryDigest{&repos[i], commits} + } + } + + if err := indexTemplate.Execute(w, digest); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } } diff --git a/app/templates/index.html b/app/templates/index.html index 31ae11e..96eb1f4 100644 --- a/app/templates/index.html +++ b/app/templates/index.html @@ -8,14 +8,24 @@

GitHop!

- Events: + {{.User.Login}}'s digest: