diff --git a/server/datastore/commit.go b/server/datastore/commit.go index a9e08609c..9f94d955b 100644 --- a/server/datastore/commit.go +++ b/server/datastore/commit.go @@ -27,6 +27,10 @@ type Commitstore interface { // from the datastore accessible to the specified user. GetCommitListUser(user *model.User) ([]*model.CommitRepo, error) + // GetCommitListActivity retrieves an ungrouped list of latest commits + // from the datastore accessible to the specified user. + GetCommitListActivity(user *model.User) ([]*model.CommitRepo, error) + // PostCommit saves a commit in the datastore. PostCommit(commit *model.Commit) error @@ -72,6 +76,12 @@ func GetCommitListUser(c context.Context, user *model.User) ([]*model.CommitRepo return FromContext(c).GetCommitListUser(user) } +// GetCommitListActivity retrieves an ungrouped list of latest commits +// from the datastore accessible to the specified user. +func GetCommitListActivity(c context.Context, user *model.User) ([]*model.CommitRepo, error) { + return FromContext(c).GetCommitListActivity(user) +} + // PostCommit saves a commit in the datastore. func PostCommit(c context.Context, commit *model.Commit) error { return FromContext(c).PostCommit(commit) diff --git a/server/datastore/database/commit.go b/server/datastore/database/commit.go index 1acbd7013..3ad6e2c37 100644 --- a/server/datastore/database/commit.go +++ b/server/datastore/database/commit.go @@ -56,6 +56,14 @@ func (db *Commitstore) GetCommitListUser(user *model.User) ([]*model.CommitRepo, return commits, err } +// GetCommitListActivity retrieves an ungrouped list of latest commits +// from the datastore accessible to the specified user. +func (db *Commitstore) GetCommitListActivity(user *model.User) ([]*model.CommitRepo, error) { + var commits []*model.CommitRepo + var err = meddler.QueryAll(db, &commits, rebind(commitListActivityQuery), user.ID) + return commits, err +} + // PostCommit saves a commit in the datastore. func (db *Commitstore) PostCommit(commit *model.Commit) error { if commit.Created == 0 { @@ -97,7 +105,7 @@ WHERE commit_id = ? ` // SQL query to retrieve the latest Commits accessible -// to ta specific user account +// to a specific user account const commitListUserQuery = ` SELECT r.repo_remote, r.repo_host, r.repo_owner, r.repo_name, c.* FROM @@ -117,6 +125,21 @@ WHERE c.repo_id = r.repo_id ) ORDER BY c.commit_created DESC LIMIT 5; ` +// SQL query to retrieve the ungrouped, latest Commits +// accessible to a specific user account +const commitListActivityQuery = ` +SELECT r.repo_remote, r.repo_host, r.repo_owner, r.repo_name, c.* +FROM + commits c +,repos r +,perms p +WHERE c.repo_id = r.repo_id + AND r.repo_id = p.repo_id + AND p.user_id = ? +ORDER BY c.commit_created DESC +LIMIT 20 +` + // SQL query to retrieve the latest Commits across all branches. const commitListQuery = ` SELECT * diff --git a/server/datastore/database/commit_test.go b/server/datastore/database/commit_test.go index 54b5e88f1..d629ee250 100644 --- a/server/datastore/database/commit_test.go +++ b/server/datastore/database/commit_test.go @@ -1,6 +1,7 @@ package database import ( + "fmt" "testing" "github.com/drone/drone/shared/model" @@ -183,6 +184,13 @@ func TestCommitstore(t *testing.T) { Owner: "drone", Name: "drone", } + repo3 := model.Repo{ + UserID: 2, + Remote: "enterprise.github.com", + Host: "github.drone.io", + Owner: "droneio", + Name: "drone", + } rs.PostRepo(&repo1) rs.PostRepo(&repo2) commit1 := model.Commit{ @@ -209,10 +217,17 @@ func TestCommitstore(t *testing.T) { Sha: "d923a61d8ad3d8d02db4fef0bf40a726bad0fc03", Status: model.StatusStarted, } + commit5 := model.Commit{ + RepoID: repo3.ID, + Branch: "bar", + Sha: "d923a61d8ad3d8d02db4fef0bf40a726bad0fc03", + Status: model.StatusStarted, + } cs.PostCommit(&commit1) cs.PostCommit(&commit2) cs.PostCommit(&commit3) cs.PostCommit(&commit4) + cs.PostCommit(&commit5) perm1 := model.Perm{ RepoID: repo1.ID, UserID: 1, @@ -237,6 +252,12 @@ func TestCommitstore(t *testing.T) { g.Assert(commits[0].Sha).Equal(commit1.Sha) g.Assert(commits[1].Sha).Equal(commit4.Sha) g.Assert(commits[1].Status).Equal(commit4.Status) + + commits, err = cs.GetCommitListActivity(&model.User{ID: 1}) + fmt.Println(commits) + fmt.Println(err) + g.Assert(err == nil).IsTrue() + g.Assert(len(commits)).Equal(3) }) g.It("Should enforce unique Sha + Branch", func() { diff --git a/server/handler/user.go b/server/handler/user.go index e5e8a7348..3de0833d5 100644 --- a/server/handler/user.go +++ b/server/handler/user.go @@ -114,6 +114,27 @@ func GetUserFeed(c web.C, w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(&repos) } +// GetUserActivity accepts a request to get the user's latest +// build activity, across all repositories, from the datastore. +// The results are encoded and returned in JSON format. +// +// GET /api/user/activity +// +func GetUserActivity(c web.C, w http.ResponseWriter, r *http.Request) { + var ctx = context.FromC(c) + var user = ToUser(c) + if user == nil { + w.WriteHeader(http.StatusUnauthorized) + return + } + repos, err := datastore.GetCommitListActivity(ctx, user) + if err != nil { + w.WriteHeader(http.StatusNotFound) + return + } + json.NewEncoder(w).Encode(&repos) +} + // PostUserSync accepts a request to post user sync // // POST /api/user/sync diff --git a/server/router/router.go b/server/router/router.go index de61d4f87..0980a8482 100644 --- a/server/router/router.go +++ b/server/router/router.go @@ -61,6 +61,7 @@ func New() *web.Mux { user := web.New() user.Use(middleware.RequireUser) user.Get("/api/user/feed", handler.GetUserFeed) + user.Get("/api/user/activity", handler.GetUserActivity) user.Get("/api/user/repos", handler.GetUserRepos) user.Post("/api/user/sync", handler.PostUserSync) user.Get("/api/user", handler.GetUserCurrent)