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
|
|
|
"time"
|
|
|
|
|
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 {
|
2015-04-07 08:20:55 +00:00
|
|
|
repo.Created = time.Now().UTC().Unix()
|
|
|
|
repo.Updated = time.Now().UTC().Unix()
|
2015-06-13 23:48:06 +00:00
|
|
|
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 {
|
2015-05-11 07:45:31 +00:00
|
|
|
repo.Updated = time.Now().UTC().Unix()
|
2015-06-13 23:48:06 +00:00
|
|
|
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
|
|
|
|
r.repo_id
|
|
|
|
,r.repo_user_id
|
|
|
|
,r.repo_owner
|
|
|
|
,r.repo_name
|
|
|
|
,r.repo_full_name
|
|
|
|
,r.repo_token
|
|
|
|
,r.repo_language
|
|
|
|
,r.repo_private
|
|
|
|
,r.repo_self
|
|
|
|
,r.repo_link
|
|
|
|
,r.repo_clone
|
|
|
|
,r.repo_branch
|
|
|
|
,r.repo_timeout
|
|
|
|
,r.repo_trusted
|
|
|
|
,r.repo_post_commit
|
|
|
|
,r.repo_pull_request
|
|
|
|
,r.repo_public_key
|
|
|
|
,r.repo_private_key
|
|
|
|
,r.repo_created
|
|
|
|
,r.repo_updated
|
|
|
|
,r.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
|
|
|
`
|