mirror of
https://github.com/samsonjs/retrogit.git
synced 2026-04-27 15:07:43 +00:00
Small refactoring to break up the large handler functions.
This commit is contained in:
parent
dc1225d12d
commit
539c0e41e4
1 changed files with 88 additions and 64 deletions
152
app/githop.go
152
app/githop.go
|
|
@ -28,9 +28,46 @@ type RepoDigest struct {
|
||||||
|
|
||||||
type Digest struct {
|
type Digest struct {
|
||||||
User *github.User
|
User *github.User
|
||||||
|
StartTime time.Time
|
||||||
|
EndTime time.Time
|
||||||
RepoDigests []*RepoDigest
|
RepoDigests []*RepoDigest
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (digest *Digest) Fetch(repos []github.Repository, githubClient *github.Client) error {
|
||||||
|
type RepoDigestResponse struct {
|
||||||
|
repoDigest *RepoDigest
|
||||||
|
err error
|
||||||
|
}
|
||||||
|
ch := make(chan *RepoDigestResponse)
|
||||||
|
for _, repo := range repos {
|
||||||
|
go func(repo github.Repository) {
|
||||||
|
commits, _, err := githubClient.Repositories.ListCommits(
|
||||||
|
*repo.Owner.Login,
|
||||||
|
*repo.Name,
|
||||||
|
&github.CommitsListOptions{
|
||||||
|
Author: *digest.User.Login,
|
||||||
|
Since: digest.StartTime,
|
||||||
|
Until: digest.EndTime,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
ch <- &RepoDigestResponse{nil, err}
|
||||||
|
} else {
|
||||||
|
ch <- &RepoDigestResponse{&RepoDigest{&repo, commits}, nil}
|
||||||
|
}
|
||||||
|
}(repo)
|
||||||
|
}
|
||||||
|
for i := 0; i < len(repos); i++ {
|
||||||
|
select {
|
||||||
|
case r := <-ch:
|
||||||
|
if r.err != nil {
|
||||||
|
return r.err
|
||||||
|
}
|
||||||
|
digest.RepoDigests = append(digest.RepoDigests, r.repoDigest)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func initGithubOAuthConfig() {
|
func initGithubOAuthConfig() {
|
||||||
path := "config/github-oauth"
|
path := "config/github-oauth"
|
||||||
if appengine.IsDevAppServer() {
|
if appengine.IsDevAppServer() {
|
||||||
|
|
@ -39,7 +76,7 @@ func initGithubOAuthConfig() {
|
||||||
path += ".json"
|
path += ".json"
|
||||||
configBytes, err := ioutil.ReadFile(path)
|
configBytes, err := ioutil.ReadFile(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicf("Could not read GitHut OAuth config from %s: %s", path, err.Error())
|
log.Panicf("Could not read GitHub OAuth config from %s: %s", path, err.Error())
|
||||||
}
|
}
|
||||||
err = json.Unmarshal(configBytes, &githubOauthConfig)
|
err = json.Unmarshal(configBytes, &githubOauthConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -67,23 +104,14 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
http.Redirect(w, r, githubOauthConfig.AuthCodeURL(""), http.StatusFound)
|
http.Redirect(w, r, githubOauthConfig.AuthCodeURL(""), http.StatusFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
tokenBytes, err := base64.URLEncoding.DecodeString(tokenEncoded)
|
token, err := decodeOAuthToken(tokenEncoded)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var token oauth.Token
|
|
||||||
err = json.Unmarshal(tokenBytes, &token)
|
oauthTransport := githubOAuthTransport(r)
|
||||||
if err != nil {
|
oauthTransport.Token = token
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
appengineContext := appengine.NewContext(r)
|
|
||||||
oauthTransport := &oauth.Transport{
|
|
||||||
Config: &githubOauthConfig,
|
|
||||||
Transport: &urlfetch.Transport{Context: appengineContext},
|
|
||||||
Token: &token,
|
|
||||||
}
|
|
||||||
githubClient := github.NewClient(oauthTransport.Client())
|
githubClient := github.NewClient(oauthTransport.Client())
|
||||||
|
|
||||||
user, _, err := githubClient.Users.Get("")
|
user, _, err := githubClient.Users.Get("")
|
||||||
|
|
@ -92,10 +120,6 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
|
||||||
|
|
||||||
// The username parameter must be left blank so that we can get all of the
|
// The username parameter must be left blank so that we can get all of the
|
||||||
// repositories the user has access to, not just ones that they own.
|
// repositories the user has access to, not just ones that they own.
|
||||||
repos, _, err := githubClient.Repositories.List("", nil)
|
repos, _, err := githubClient.Repositories.List("", nil)
|
||||||
|
|
@ -103,42 +127,18 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
digest := Digest{User: user, RepoDigests: make([]*RepoDigest, 0, len(repos))}
|
now := time.Now()
|
||||||
type RepoDigestResponse struct {
|
digestStartTime := time.Date(now.Year()-1, now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
|
||||||
repoDigest *RepoDigest
|
digestEndTime := digestStartTime.AddDate(0, 0, 7)
|
||||||
err error
|
digest := Digest{
|
||||||
|
User: user,
|
||||||
|
RepoDigests: make([]*RepoDigest, 0, len(repos)),
|
||||||
|
StartTime: digestStartTime,
|
||||||
|
EndTime: digestEndTime,
|
||||||
}
|
}
|
||||||
ch := make(chan *RepoDigestResponse)
|
err = digest.Fetch(repos, githubClient)
|
||||||
for _, repo := range repos {
|
if err != nil {
|
||||||
go func(repo github.Repository) {
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
commits, _, err := githubClient.Repositories.ListCommits(
|
|
||||||
*repo.Owner.Login,
|
|
||||||
*repo.Name,
|
|
||||||
&github.CommitsListOptions{
|
|
||||||
Author: *user.Login,
|
|
||||||
Since: digestStartTime,
|
|
||||||
Until: digestEndTime,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
ch <- &RepoDigestResponse{nil, err}
|
|
||||||
} else {
|
|
||||||
ch <- &RepoDigestResponse{&RepoDigest{&repo, commits}, nil}
|
|
||||||
}
|
|
||||||
}(repo)
|
|
||||||
}
|
|
||||||
loop:
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case r := <-ch:
|
|
||||||
if r.err != nil {
|
|
||||||
http.Error(w, r.err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
digest.RepoDigests = append(digest.RepoDigests, r.repoDigest)
|
|
||||||
if len(digest.RepoDigests) == len(repos) {
|
|
||||||
break loop
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := indexTemplate.Execute(w, digest); err != nil {
|
if err := indexTemplate.Execute(w, digest); err != nil {
|
||||||
|
|
@ -148,25 +148,49 @@ loop:
|
||||||
|
|
||||||
func githubOAuthCallbackHandler(w http.ResponseWriter, r *http.Request) {
|
func githubOAuthCallbackHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
code := r.FormValue("code")
|
code := r.FormValue("code")
|
||||||
appengineContext := appengine.NewContext(r)
|
oauthTransport := githubOAuthTransport(r)
|
||||||
oauthTransport := &oauth.Transport{
|
|
||||||
Config: &githubOauthConfig,
|
|
||||||
Transport: &urlfetch.Transport{Context: appengineContext},
|
|
||||||
}
|
|
||||||
token, err := oauthTransport.Exchange(code)
|
token, err := oauthTransport.Exchange(code)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
tokenBytes, err := json.Marshal(token)
|
tokenEncoded, err := encodeOAuthToken(token)
|
||||||
tokenEncoded := base64.StdEncoding.EncodeToString(tokenBytes)
|
|
||||||
redirectUrl, err := router.GetRoute("index").URL()
|
|
||||||
redirectParams := url.Values{}
|
|
||||||
redirectParams.Add("token", tokenEncoded)
|
|
||||||
redirectUrl.RawQuery = redirectParams.Encode()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
redirectUrl, _ := router.GetRoute("index").URL()
|
||||||
|
redirectParams := url.Values{}
|
||||||
|
redirectParams.Add("token", tokenEncoded)
|
||||||
|
redirectUrl.RawQuery = redirectParams.Encode()
|
||||||
http.Redirect(w, r, redirectUrl.String(), http.StatusFound)
|
http.Redirect(w, r, redirectUrl.String(), http.StatusFound)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func githubOAuthTransport(r *http.Request) *oauth.Transport {
|
||||||
|
appengineContext := appengine.NewContext(r)
|
||||||
|
return &oauth.Transport{
|
||||||
|
Config: &githubOauthConfig,
|
||||||
|
Transport: &urlfetch.Transport{Context: appengineContext},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func encodeOAuthToken(token *oauth.Token) (string, error) {
|
||||||
|
tokenBytes, err := json.Marshal(token)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return base64.StdEncoding.EncodeToString(tokenBytes), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func decodeOAuthToken(tokenEncoded string) (*oauth.Token, error) {
|
||||||
|
tokenBytes, err := base64.URLEncoding.DecodeString(tokenEncoded)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var token oauth.Token
|
||||||
|
err = json.Unmarshal(tokenBytes, &token)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &token, nil
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue