2015-05-01 04:08:42 +00:00
|
|
|
package builtin
|
2015-04-07 08:20:55 +00:00
|
|
|
|
|
|
|
import (
|
2015-06-13 23:48:06 +00:00
|
|
|
"database/sql"
|
2015-04-07 08:20:55 +00:00
|
|
|
|
2015-06-13 23:48:06 +00:00
|
|
|
"github.com/drone/drone/pkg/types"
|
2015-04-07 08:20:55 +00:00
|
|
|
)
|
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
type Userstore struct {
|
2015-06-13 23:48:06 +00:00
|
|
|
*sql.DB
|
2015-05-11 07:45:31 +00:00
|
|
|
}
|
2015-04-10 19:52:33 +00:00
|
|
|
|
2015-06-13 23:48:06 +00:00
|
|
|
func NewUserstore(db *sql.DB) *Userstore {
|
2015-05-11 07:45:31 +00:00
|
|
|
return &Userstore{db}
|
|
|
|
}
|
2015-04-10 19:52:33 +00:00
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
// User returns a user by user ID.
|
2015-06-13 23:48:06 +00:00
|
|
|
func (db *Userstore) User(id int64) (*types.User, error) {
|
|
|
|
return getUser(db, rebind(stmtUserSelect), id)
|
2015-04-07 08:20:55 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
// UserLogin returns a user by user login.
|
2015-06-13 23:48:06 +00:00
|
|
|
func (db *Userstore) UserLogin(login string) (*types.User, error) {
|
|
|
|
return getUser(db, rebind(stmtUserSelectUserLogin), login)
|
2015-04-07 08:20:55 +00:00
|
|
|
}
|
|
|
|
|
2015-04-15 05:04:38 +00:00
|
|
|
// UserList returns a list of all registered users.
|
2015-06-13 23:48:06 +00:00
|
|
|
func (db *Userstore) UserList() ([]*types.User, error) {
|
|
|
|
return getUsers(db, rebind(stmtUserSelectList))
|
2015-04-07 08:20:55 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
// UserFeed retrieves a digest of recent builds
|
|
|
|
// from the datastore accessible to the specified user.
|
2015-06-13 23:48:06 +00:00
|
|
|
func (db *Userstore) UserFeed(user *types.User, limit, offset int) ([]*types.RepoCommit, error) {
|
2015-06-14 01:40:07 +00:00
|
|
|
rows, err := db.Query(rebind(userFeedQuery), user.ID, limit, offset)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
return scanRepoCommits(rows)
|
2015-05-11 07:45:31 +00:00
|
|
|
}
|
2015-04-10 19:52:33 +00:00
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
// UserCount returns a count of all registered users.
|
|
|
|
func (db *Userstore) UserCount() (int, error) {
|
2015-06-13 23:48:06 +00:00
|
|
|
var count int
|
|
|
|
err := db.QueryRow(stmtUserSelectCount).Scan(&count)
|
|
|
|
return count, err
|
2015-04-07 08:20:55 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
// AddUser inserts a new user into the datastore.
|
|
|
|
// If the user login already exists an error is returned.
|
2015-06-13 23:48:06 +00:00
|
|
|
func (db *Userstore) AddUser(user *types.User) error {
|
|
|
|
return createUser(db, rebind(stmtUserInsert), user)
|
2015-05-11 07:45:31 +00:00
|
|
|
}
|
2015-04-10 19:52:33 +00:00
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
// SetUser updates an existing user.
|
2015-06-13 23:48:06 +00:00
|
|
|
func (db *Userstore) SetUser(user *types.User) error {
|
|
|
|
return updateUser(db, rebind(stmtUserUpdate), user)
|
2015-04-07 08:20:55 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
// DelUser removes the user from the datastore.
|
2015-06-13 23:48:06 +00:00
|
|
|
func (db *Userstore) DelUser(user *types.User) error {
|
|
|
|
var _, err = db.Exec(rebind(stmtUserDelete), user.ID)
|
2015-05-11 07:45:31 +00:00
|
|
|
return err
|
2015-04-07 08:20:55 +00:00
|
|
|
}
|
2015-05-11 07:45:31 +00:00
|
|
|
|
|
|
|
// SQL query to retrieve a build feed for the given
|
|
|
|
// user account.
|
|
|
|
const userFeedQuery = `
|
|
|
|
SELECT
|
|
|
|
r.repo_id
|
|
|
|
,r.repo_owner
|
|
|
|
,r.repo_name
|
2015-06-13 23:48:06 +00:00
|
|
|
,r.repo_full_name
|
2015-06-23 03:45:08 +00:00
|
|
|
,b.build_number
|
|
|
|
,b.build_status
|
|
|
|
,b.build_started
|
|
|
|
,b.build_finished
|
2015-05-11 07:45:31 +00:00
|
|
|
FROM
|
2015-06-23 03:45:08 +00:00
|
|
|
builds b
|
2015-05-11 07:45:31 +00:00
|
|
|
,repos r
|
|
|
|
,stars s
|
2015-06-23 03:45:08 +00:00
|
|
|
WHERE b.build_repo_id = r.repo_id
|
2015-06-14 00:49:21 +00:00
|
|
|
AND r.repo_id = s.star_repo_id
|
|
|
|
AND s.star_user_id = ?
|
2015-06-23 03:45:08 +00:00
|
|
|
ORDER BY b.build_number DESC
|
2015-05-11 07:45:31 +00:00
|
|
|
LIMIT ? OFFSET ?
|
|
|
|
`
|
2015-06-14 01:40:07 +00:00
|
|
|
|
|
|
|
func scanRepoCommits(rows *sql.Rows) ([]*types.RepoCommit, error) {
|
|
|
|
var err error
|
|
|
|
var vv []*types.RepoCommit
|
|
|
|
for rows.Next() {
|
|
|
|
v := &types.RepoCommit{}
|
|
|
|
err = rows.Scan(
|
|
|
|
&v.ID,
|
|
|
|
&v.Owner,
|
|
|
|
&v.Name,
|
|
|
|
&v.FullName,
|
|
|
|
&v.Number,
|
|
|
|
&v.State,
|
|
|
|
&v.Started,
|
|
|
|
&v.Finished,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return vv, err
|
|
|
|
}
|
|
|
|
vv = append(vv, v)
|
|
|
|
}
|
|
|
|
return vv, rows.Err()
|
|
|
|
}
|