woodpecker/model/repo.go

122 lines
3.5 KiB
Go
Raw Normal View History

2015-09-30 01:21:17 +00:00
package model
2015-09-30 01:21:17 +00:00
import (
"github.com/drone/drone/shared/database"
"github.com/russross/meddler"
2015-10-02 23:16:41 +00:00
"strings"
2015-09-30 01:21:17 +00:00
)
type RepoLite struct {
Owner string `json:"owner"`
Name string `json:"name"`
FullName string `json:"full_name"`
2015-09-30 01:21:17 +00:00
Avatar string `json:"avatar_url"`
}
2015-09-30 01:21:17 +00:00
type Repo struct {
ID int64 `json:"id" meddler:"repo_id,pk"`
UserID int64 `json:"-" meddler:"repo_user_id"`
Owner string `json:"owner" meddler:"repo_owner"`
Name string `json:"name" meddler:"repo_name"`
FullName string `json:"full_name" meddler:"repo_full_name"`
Avatar string `json:"avatar_url" meddler:"repo_avatar"`
Link string `json:"link_url" meddler:"repo_link"`
Clone string `json:"clone_url" meddler:"repo_clone"`
Branch string `json:"default_branch" meddler:"repo_branch"`
Timeout int64 `json:"timeout" meddler:"repo_timeout"`
IsPrivate bool `json:"private" meddler:"repo_private"`
IsTrusted bool `json:"trusted" meddler:"repo_trusted"`
IsStarred bool `json:"starred,omitempty" meddler:"-"`
AllowPull bool `json:"allow_pr" meddler:"repo_allow_pr"`
AllowPush bool `json:"allow_push" meddler:"repo_allow_push"`
AllowDeploy bool `json:"allow_deploys" meddler:"repo_allow_deploys"`
AllowTag bool `json:"allow_tags" meddler:"repo_allow_tags"`
Hash string `json:"-" meddler:"repo_hash"`
}
func GetRepo(db meddler.DB, id int64) (*Repo, error) {
var repo = new(Repo)
var err = meddler.Load(db, repoTable, repo, id)
return repo, err
}
2015-09-30 01:21:17 +00:00
func GetRepoName(db meddler.DB, owner, name string) (*Repo, error) {
2015-10-02 23:16:41 +00:00
return GetRepoFullName(db, owner+"/"+name)
}
func GetRepoFullName(db meddler.DB, name string) (*Repo, error) {
2015-09-30 01:21:17 +00:00
var repo = new(Repo)
2015-10-02 23:16:41 +00:00
var err = meddler.QueryRow(db, repo, database.Rebind(repoNameQuery), name)
2015-09-30 01:21:17 +00:00
return repo, err
2015-06-19 01:50:57 +00:00
}
2015-09-30 01:21:17 +00:00
func GetRepoList(db meddler.DB, user *User) ([]*Repo, error) {
2015-10-02 23:16:41 +00:00
// we don't have real-time access to the intersection
// of github repos and drone repos. So we cheat by simply
// getting the distinct list of repos that the user
// has created builds for.
2015-09-30 01:21:17 +00:00
var repos = []*Repo{}
2015-10-02 23:16:41 +00:00
var err = meddler.QueryAll(db, &repos, database.Rebind(repoListQuery), user.Login)
return repos, err
}
func GetRepoListOf(db meddler.DB, listof []string) ([]*Repo, error) {
var repos = []*Repo{}
var qs = make([]string, len(listof), len(listof))
var in = make([]interface{}, len(listof), len(listof))
for i, repo := range listof {
qs[i] = "?"
in[i] = repo
}
var stmt = "SELECT * FROM repos WHERE repo_id IN (" + strings.Join(qs, ",") + ")"
var err = meddler.QueryAll(db, &repos, database.Rebind(stmt), in...)
2015-09-30 01:21:17 +00:00
return repos, err
2015-06-19 01:50:57 +00:00
}
2015-09-30 01:21:17 +00:00
func CreateRepo(db meddler.DB, repo *Repo) error {
return meddler.Insert(db, repoTable, repo)
}
2015-09-30 01:21:17 +00:00
func UpdateRepo(db meddler.DB, repo *Repo) error {
return meddler.Update(db, repoTable, repo)
}
func DeleteRepo(db meddler.DB, repo *Repo) error {
var _, err = db.Exec(database.Rebind(repoDeleteStmt), repo.ID)
return err
}
const repoTable = "repos"
const repoNameQuery = `
SELECT *
FROM repos
2015-10-02 23:16:41 +00:00
WHERE repo_full_name = ?
2015-09-30 01:21:17 +00:00
LIMIT 1;
`
2015-10-02 23:16:41 +00:00
const repoStarsQuery = `
2015-09-30 01:21:17 +00:00
SELECT r.*
FROM
repos r
,stars s
WHERE r.repo_id = s.star_repo_id
AND s.star_user_id = ?
`
2015-10-02 23:16:41 +00:00
const repoListQuery = `
SELECT *
FROM repos
WHERE repo_id IN (
SELECT DISTINCT build_repo_id
FROM builds
WHERE build_author = ?
)
ORDER BY repo_full_name
2015-10-02 23:16:41 +00:00
`
2015-09-30 01:21:17 +00:00
const repoDeleteStmt = `
DELETE FROM repos
WHERE repo_id = ?
`