func Repos(w http.ResponseWriter, r *http.Request, currentUser *model.User) error { gh := repository.GitHub(currentUser.AccessToken) reposWithOwners := []repoWithOwner{} // user repos githubUserRepos, err := gh.Repos() if err != nil { return err } userRepos, err := upsertRepos(githubUserRepos) if err != nil { return err } userReposWithOwner := repoWithOwner{ Owner: currentUser.Login, Repos: userRepos, } reposWithOwners = append(reposWithOwners, userReposWithOwner) // org repos orgs, err := gh.Orgs() if err != nil { return err } for _, org := range orgs { r, err := gh.OrgRepos(org.Login) if err != nil { return err } repos, err := upsertRepos(r) if err != nil { return err } repoWithOwner := repoWithOwner{ Owner: org.Login, Repos: repos, } reposWithOwners = append(reposWithOwners, repoWithOwner) } repoObjectIds := []bson.ObjectId{} for _, ro := range reposWithOwners { for _, repo := range ro.Repos { repoObjectIds = append(repoObjectIds, repo.ObjectId) } } //currentUser.Repos = repoObjectIds //currentUser, err = repository.User().Upsert(currentUser) //if err != nil { //return err //} return json.NewEncoder(w).Encode(reposWithOwners) }
func LoginCallback(w http.ResponseWriter, r *http.Request) error { q := r.URL.Query() // TODO: verify state is the same state := q.Get("state") if state == "" { return fmt.Errorf("State can't be blank") } code := q.Get("code") if code == "" { return fmt.Errorf("Code can't be blank") } redirectURI := loginRedirectURI(r) accessToken, err := repository.GitHub("").CreateAccessToken(gitHubClientId, gitHubClientSecret, code, redirectURI) if err != nil { return err } githubUser, err := repository.GitHub(accessToken.Token).AuthenticatedUser() if err != nil { return err } userRepo := repository.User() user := &model.User{ ID: githubUser.ID, Login: githubUser.Login, Email: githubUser.Email, HTMLURL: githubUser.HTMLURL, AvatarURL: githubUser.AvatarURL, AccessToken: accessToken.Token, CreatedAt: githubUser.CreatedAt, } user, err = userRepo.Upsert(user) if err != nil { return err } c := cookie.Cookie{Name: "user", Path: "/", Values: cookie.CookieValues{"id": user.ObjectIdString()}} cookie.Write(w, &c) redirectTo(w, r, "/") return nil }
func fetchGitHubRepo(user *model.User, owner, name string) (*octokat.Repository, error) { return repository.GitHub(user.AccessToken).Repo(owner, name) }