2015-05-01 04:08:42 +00:00
|
|
|
package builtin
|
2015-04-07 08:20:55 +00:00
|
|
|
|
|
|
|
import (
|
2015-05-11 07:45:31 +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 Repostore struct {
|
|
|
|
*sql.DB
|
2015-04-07 08:20:55 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
func NewRepostore(db *sql.DB) *Repostore {
|
|
|
|
return &Repostore{db}
|
2015-04-15 05:04:38 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
// Repo retrieves a specific repo from the
|
|
|
|
// datastore for the given ID.
|
2015-06-13 23:48:06 +00:00
|
|
|
func (db *Repostore) Repo(id int64) (*types.Repo, error) {
|
|
|
|
return getRepo(db, rebind(stmtRepoSelect), id)
|
2015-04-07 08:20:55 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
// RepoName retrieves a repo from the datastore
|
|
|
|
// for the specified name.
|
2015-06-13 23:48:06 +00:00
|
|
|
func (db *Repostore) RepoName(owner, name string) (*types.Repo, error) {
|
|
|
|
return getRepo(db, rebind(stmtRepoSelectRepoOwnerName), owner, name)
|
2015-04-07 08:20:55 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
// RepoList retrieves a list of all repos from
|
|
|
|
// the datastore accessible by the given user ID.
|
2015-06-13 23:48:06 +00:00
|
|
|
func (db *Repostore) RepoList(user *types.User) ([]*types.Repo, error) {
|
|
|
|
return getRepos(db, rebind(repoListQuery), user.ID)
|
2015-04-07 08:20:55 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
// AddRepo inserts a repo in the datastore.
|
2015-06-13 23:48:06 +00:00
|
|
|
func (db *Repostore) AddRepo(repo *types.Repo) error {
|
|
|
|
return createRepo(db, rebind(stmtRepoInsert), repo)
|
2015-04-07 08:20:55 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
// SetRepo updates a repo in the datastore.
|
2015-06-13 23:48:06 +00:00
|
|
|
func (db *Repostore) SetRepo(repo *types.Repo) error {
|
|
|
|
return updateRepo(db, rebind(stmtRepoUpdate), repo)
|
2015-04-07 08:20:55 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
// DelRepo removes the repo from the datastore.
|
2015-06-13 23:48:06 +00:00
|
|
|
func (db *Repostore) DelRepo(repo *types.Repo) error {
|
|
|
|
var _, err = db.Exec(rebind(stmtRepoDelete), repo.ID)
|
2015-05-11 07:45:31 +00:00
|
|
|
return err
|
2015-04-14 04:39:05 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
// SQL statement to retrieve a list of Repos
|
|
|
|
// with permissions for the given User ID.
|
|
|
|
const repoListQuery = `
|
2015-06-13 23:48:06 +00:00
|
|
|
SELECT
|
2015-06-19 01:50:57 +00:00
|
|
|
repo_id
|
|
|
|
,repo_user_id
|
|
|
|
,repo_owner
|
|
|
|
,repo_name
|
|
|
|
,repo_full_name
|
2015-08-03 07:10:43 +00:00
|
|
|
,repo_avatar
|
2015-06-19 01:50:57 +00:00
|
|
|
,repo_self
|
|
|
|
,repo_link
|
|
|
|
,repo_clone
|
|
|
|
,repo_branch
|
|
|
|
,repo_private
|
|
|
|
,repo_trusted
|
|
|
|
,repo_timeout
|
|
|
|
,repo_keys_public
|
|
|
|
,repo_keys_private
|
|
|
|
,repo_hooks_pull_request
|
|
|
|
,repo_hooks_push
|
|
|
|
,repo_hooks_tags
|
|
|
|
,repo_params
|
2015-05-11 07:45:31 +00:00
|
|
|
FROM
|
|
|
|
repos r
|
|
|
|
,stars s
|
2015-06-14 00:49:21 +00:00
|
|
|
WHERE r.repo_id = s.star_repo_id
|
|
|
|
AND s.star_user_id = ?
|
2015-05-11 07:45:31 +00:00
|
|
|
`
|