// CreateCommit creates a commit c for u in the database. func CreateCommit(u User, c GitHubCommitRepo) error { debug.Printf("Creating commit %v for user %s", c, u.Login) if c.Author == nil || u.Login != *c.Author.Login { // Ignore the commit if the commit has no GitHub // author, or the author does not match u. return nil } tx, err := db.DB.Beginx() if err != nil { return wrapError(err) } if c.Message == nil { c.Message = github.String("") } b := &db.Binder{} query := ` INSERT INTO commit(sha, uid, author_date, repo_name, message, additions, deletions) VALUES (` + b.Bind(*c.SHA, u.UID, *c.Commit.Author.Date, c.RepoName, *c.Commit.Message, *c.Stats.Additions, *c.Stats.Deletions) + `)` if _, err := tx.Exec(query, b.Items...); err != nil { // Ignore if we've seen this commit. if strings.Contains(err.Error(), "duplicate key value violates unique constraint") { return nil } return wrapError(err) } for _, f := range c.Files { b := &db.Binder{} // For empty files, Patch is nil. if f.Patch == nil { f.Patch = github.String("") } query := ` INSERT INTO commit_file(commit_sha, filename, status, additions, deletions, patch) VALUES (` + b.Bind(*c.SHA, *f.Filename, *f.Status, *f.Additions, *f.Deletions, *f.Patch) + `)` if _, err := tx.Exec(query, b.Items...); err != nil { return wrapError(err) } } if err := tx.Commit(); err != nil { return wrapError(err) } return nil }
func serveGitHubCallback(c web.C, w http.ResponseWriter, r *http.Request) error { a := NewApp(c) state := r.FormValue("state") if state != oauthStateString { return errors.Errorf("invalid oauth state, expected '%s', got '%s'\n", oauthStateString, state) } code := r.FormValue("code") token, err := oauthConf.Exchange(oauth2.NoContext, code) if err != nil { return wrapErrorf(err, "oauthConf.Exchange() failed") } oauthClient := oauthConf.Client(oauth2.NoContext, token) client := github.NewClient(oauthClient) ghUser, _, err := client.Users.Get("") if err != nil { return wrapErrorf(err, "client.Users.Get() failed") } debug.Printf("Logged in as GitHub user: %s\n", *ghUser.Login) // Save user to DB. user, err := GetCreateUser(*ghUser.Login) if err != nil { return wrapErrorf(err, "error saving user to the database") } a.Session.Values[UIDSessionKey] = user.UID if err := a.Session.Save(r, w); err != nil { return wrapErrorf(err, "error saving session") } var q redirectQuery if err := SchemaDecoder.Decode(&q, r.URL.Query()); err != nil { return wrapError(err) } if q.Redirect != "" { u, err := url.QueryUnescape(q.Redirect) if err != nil { return wrapError(err) } return &HTTPRedirect{To: u, Code: http.StatusSeeOther} } return &HTTPRedirect{To: "/", Code: http.StatusSeeOther} }