Add basic digest showing commits from a year ago in a user's repositories.

This commit is contained in:
Mihai Parparita 2014-07-07 23:41:26 -07:00
parent d2bb784705
commit 3b9a42cb73
3 changed files with 63 additions and 10 deletions

4
TODO
View file

@ -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)

View file

@ -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)
}
}

View file

@ -8,14 +8,24 @@
<body>
<h1>GitHop!</h1>
Events:
{{.User.Login}}'s digest:
<ul>
{{range .}}
{{range $index, $repositoryDigest := .RepositoryDigests}}
<li>
{{.ID}}
<pre>
{{printf "%s" .RawPayload}}
</pre>
<a href="https://github.com/{{.Repository.FullName}}">
{{.Repository.FullName}}
</a>
<ul>
{{range .Commits }}
<li>
<code><a href="https://github.com/{{$repositoryDigest.Repository.FullName}}/commit/{{.SHA}}">{{.SHA}}</a></code>
-
<i>{{.Commit.Author.Date}}</i>
<br>
<pre>{{.Commit.Message}}</pre>
</li>
{{end}}
</ul>
</li>
{{end}}
</ul>