diff --git a/app/admin.go b/app/admin.go new file mode 100644 index 0000000..64f5883 --- /dev/null +++ b/app/admin.go @@ -0,0 +1,128 @@ +package retrogit + +import ( + "net/http" + "sort" + "strconv" + + "appengine" + + "github.com/google/go-github/github" +) + +type AdminUserData struct { + Account *Account + User *github.User + EmailAddress string + Repos *Repos + ReposError error +} + +// sort.Interface implementation for sorting AdminUserDatas +type AdminUserDataByGitHubUserId []*AdminUserData + +func (a AdminUserDataByGitHubUserId) Len() int { return len(a) } +func (a AdminUserDataByGitHubUserId) Swap(i, j int) { a[i], a[j] = a[j], a[i] } +func (a AdminUserDataByGitHubUserId) Less(i, j int) bool { + return a[i].Account.GitHubUserId < a[j].Account.GitHubUserId +} + +func usersAdminHandler(w http.ResponseWriter, r *http.Request) *AppError { + c := appengine.NewContext(r) + accounts, err := getAllAccounts(c) + if err != nil { + return InternalError(err, "Could not look up accounts") + } + + ch := make(chan *AdminUserData) + for i := range accounts { + go func(account *Account) { + oauthTransport := githubOAuthTransport(c) + oauthTransport.Token = &account.OAuthToken + githubClient := github.NewClient(oauthTransport.Client()) + + user, _, err := githubClient.Users.Get("") + + emailAddress, err := account.GetDigestEmailAddress(githubClient) + if err != nil { + emailAddress = err.Error() + } + + repos, reposErr := getRepos(c, githubClient, account, user) + ch <- &AdminUserData{ + Account: account, + User: user, + EmailAddress: emailAddress, + Repos: repos, + ReposError: reposErr, + } + }(&accounts[i]) + } + + users := make([]*AdminUserData, 0) + totalRepos := 0 + for _ = range accounts { + select { + case r := <-ch: + users = append(users, r) + if r.Repos != nil { + totalRepos += len(r.Repos.AllRepos) + } + } + } + sort.Sort(AdminUserDataByGitHubUserId(users)) + + var data = map[string]interface{}{ + "Users": users, + "TotalUsers": len(users), + "TotalRepos": totalRepos, + } + return templates["users-admin"].Render(w, data) +} + +func digestAdminHandler(w http.ResponseWriter, r *http.Request) *AppError { + userId, err := strconv.Atoi(r.FormValue("user_id")) + if err != nil { + return BadRequest(err, "Malformed user_id value") + } + c := appengine.NewContext(r) + account, err := getAccount(c, userId) + if account == nil { + return BadRequest(err, "user_id does not point to an account") + } + if err != nil { + return InternalError(err, "Could not look up account") + } + + oauthTransport := githubOAuthTransport(c) + oauthTransport.Token = &account.OAuthToken + githubClient := github.NewClient(oauthTransport.Client()) + + digest, err := newDigest(c, githubClient, account) + if err != nil { + return GitHubFetchError(err, "digest") + } + digest.Redact() + var data = map[string]interface{}{ + "Digest": digest, + } + return templates["digest-admin"].Render(w, data) +} + +func deleteAccountAdminHandler(w http.ResponseWriter, r *http.Request) *AppError { + userId, err := strconv.Atoi(r.FormValue("user_id")) + if err != nil { + return BadRequest(err, "Malformed user_id value") + } + c := appengine.NewContext(r) + account, err := getAccount(c, userId) + if account == nil { + return BadRequest(err, "user_id does not point to an account") + } + if err != nil { + return InternalError(err, "Could not look up account") + } + + account.Delete(c) + return RedirectToRoute("users-admin") +} diff --git a/app/retrogit.go b/app/retrogit.go index f78b463..21d011a 100644 --- a/app/retrogit.go +++ b/app/retrogit.go @@ -538,111 +538,6 @@ func deleteAccountHandler(w http.ResponseWriter, r *http.Request, state *AppSign return RedirectToRoute("index") } -func usersAdminHandler(w http.ResponseWriter, r *http.Request) *AppError { - c := appengine.NewContext(r) - accounts, err := getAllAccounts(c) - if err != nil { - return InternalError(err, "Could not look up accounts") - } - - type AdminUserData struct { - Account *Account - User *github.User - EmailAddress string - Repos *Repos - ReposError error - } - ch := make(chan *AdminUserData) - for i := range accounts { - go func(account *Account) { - oauthTransport := githubOAuthTransport(c) - oauthTransport.Token = &account.OAuthToken - githubClient := github.NewClient(oauthTransport.Client()) - - user, _, err := githubClient.Users.Get("") - - emailAddress, err := account.GetDigestEmailAddress(githubClient) - if err != nil { - emailAddress = err.Error() - } - - repos, reposErr := getRepos(c, githubClient, account, user) - ch <- &AdminUserData{ - Account: account, - User: user, - EmailAddress: emailAddress, - Repos: repos, - ReposError: reposErr, - } - }(&accounts[i]) - } - - users := make([]*AdminUserData, 0) - totalRepos := 0 - for _ = range accounts { - select { - case r := <-ch: - users = append(users, r) - if r.Repos != nil { - totalRepos += len(r.Repos.AllRepos) - } - } - } - var data = map[string]interface{}{ - "Users": users, - "TotalUsers": len(users), - "TotalRepos": totalRepos, - } - return templates["users-admin"].Render(w, data) -} - -func digestAdminHandler(w http.ResponseWriter, r *http.Request) *AppError { - userId, err := strconv.Atoi(r.FormValue("user_id")) - if err != nil { - return BadRequest(err, "Malformed user_id value") - } - c := appengine.NewContext(r) - account, err := getAccount(c, userId) - if account == nil { - return BadRequest(err, "user_id does not point to an account") - } - if err != nil { - return InternalError(err, "Could not look up account") - } - - oauthTransport := githubOAuthTransport(c) - oauthTransport.Token = &account.OAuthToken - githubClient := github.NewClient(oauthTransport.Client()) - - digest, err := newDigest(c, githubClient, account) - if err != nil { - return GitHubFetchError(err, "digest") - } - digest.Redact() - var data = map[string]interface{}{ - "Digest": digest, - } - return templates["digest-admin"].Render(w, data) -} - -func deleteAccountAdminHandler(w http.ResponseWriter, r *http.Request) *AppError { - userId, err := strconv.Atoi(r.FormValue("user_id")) - if err != nil { - return BadRequest(err, "Malformed user_id value") - } - c := appengine.NewContext(r) - account, err := getAccount(c, userId) - if account == nil { - return BadRequest(err, "user_id does not point to an account") - } - if err != nil { - return InternalError(err, "Could not look up account") - } - - account.Delete(c) - return RedirectToRoute("users-admin") -} - func githubOAuthTransport(c appengine.Context) *oauth.Transport { appengineTransport := &urlfetch.Transport{Context: c} appengineTransport.Deadline = time.Second * 60