2015-05-17 18:10:43 +00:00
|
|
|
package store
|
2015-04-07 08:20:55 +00:00
|
|
|
|
2015-04-13 06:08:55 +00:00
|
|
|
import (
|
2015-06-15 06:26:49 +00:00
|
|
|
"fmt"
|
2015-04-13 06:08:55 +00:00
|
|
|
"io"
|
2015-06-15 06:26:49 +00:00
|
|
|
"net/url"
|
2015-04-13 06:08:55 +00:00
|
|
|
|
2015-06-15 06:26:49 +00:00
|
|
|
"github.com/drone/drone/pkg/types"
|
2015-04-13 06:08:55 +00:00
|
|
|
)
|
2015-04-07 08:20:55 +00:00
|
|
|
|
2015-06-15 06:26:49 +00:00
|
|
|
var drivers = make(map[string]DriverFunc)
|
|
|
|
|
|
|
|
// Register makes a datastore driver available by the provided name.
|
|
|
|
// If Register is called twice with the same name or if driver is nil,
|
|
|
|
// it panics.
|
|
|
|
func Register(name string, driver DriverFunc) {
|
|
|
|
if driver == nil {
|
|
|
|
panic("datastore: Register driver is nil")
|
|
|
|
}
|
|
|
|
if _, dup := drivers[name]; dup {
|
|
|
|
panic("datastore: Register called twice for driver " + name)
|
|
|
|
}
|
|
|
|
drivers[name] = driver
|
|
|
|
}
|
|
|
|
|
|
|
|
// DriverFunc returns a new connection to the datastore.
|
|
|
|
// The name is a string in a driver-specific format.
|
|
|
|
type DriverFunc func(name string) (Store, error)
|
|
|
|
|
|
|
|
// New creates a new database connection specified by its database driver
|
|
|
|
// name and a driver-specific data source name, usually consisting of at
|
|
|
|
// least a database name and connection information.
|
|
|
|
func New(dsn string) (Store, error) {
|
|
|
|
uri, err := url.Parse(dsn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
driver := uri.Scheme
|
|
|
|
fn, ok := drivers[driver]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("datastore: unknown driver %q", driver)
|
|
|
|
}
|
|
|
|
return fn(dsn)
|
|
|
|
}
|
|
|
|
|
2015-05-17 18:10:43 +00:00
|
|
|
type Store interface {
|
2015-04-07 08:20:55 +00:00
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
// User returns a user by user ID.
|
2015-06-15 06:26:49 +00:00
|
|
|
User(id int64) (*types.User, error)
|
2015-05-11 07:45:31 +00:00
|
|
|
|
|
|
|
// UserLogin returns a user by user login.
|
2015-06-15 06:26:49 +00:00
|
|
|
UserLogin(string) (*types.User, error)
|
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-15 06:26:49 +00:00
|
|
|
UserList() ([]*types.User, error)
|
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-15 06:26:49 +00:00
|
|
|
UserFeed(*types.User, int, int) ([]*types.RepoCommit, error)
|
2015-04-07 08:20:55 +00:00
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
// UserCount returns a count of all registered users.
|
|
|
|
UserCount() (int, error)
|
|
|
|
|
|
|
|
// AddUser inserts a new user into the datastore.
|
|
|
|
// If the user login already exists an error is returned.
|
2015-06-15 06:26:49 +00:00
|
|
|
AddUser(*types.User) error
|
2015-05-11 07:45:31 +00:00
|
|
|
|
|
|
|
// SetUser updates an existing user.
|
2015-06-15 06:26:49 +00:00
|
|
|
SetUser(*types.User) error
|
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-15 06:26:49 +00:00
|
|
|
DelUser(*types.User) error
|
2015-04-07 08:20:55 +00:00
|
|
|
|
2015-05-18 22:47:13 +00:00
|
|
|
//
|
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
// Token returns a token by ID.
|
2015-06-15 06:26:49 +00:00
|
|
|
Token(int64) (*types.Token, error)
|
2015-05-11 07:45:31 +00:00
|
|
|
|
|
|
|
// TokenLabel returns a token by label
|
2015-06-15 06:26:49 +00:00
|
|
|
TokenLabel(*types.User, string) (*types.Token, error)
|
2015-04-07 08:20:55 +00:00
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
// TokenList returns a list of all user tokens.
|
2015-06-15 06:26:49 +00:00
|
|
|
TokenList(*types.User) ([]*types.Token, error)
|
2015-04-07 08:20:55 +00:00
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
// AddToken inserts a new token into the datastore.
|
|
|
|
// If the token label already exists for the user
|
|
|
|
// an error is returned.
|
2015-06-15 06:26:49 +00:00
|
|
|
AddToken(*types.Token) error
|
2015-04-07 08:20:55 +00:00
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
// DelToken removes the DelToken from the datastore.
|
2015-06-15 06:26:49 +00:00
|
|
|
DelToken(*types.Token) error
|
2015-04-07 08:20:55 +00:00
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
//
|
2015-04-08 22:43:59 +00:00
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
// Starred returns true if the user starred
|
|
|
|
// the given repository.
|
2015-06-15 06:26:49 +00:00
|
|
|
Starred(*types.User, *types.Repo) (bool, error)
|
2015-04-08 22:43:59 +00:00
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
// AddStar stars a repository.
|
2015-06-15 06:26:49 +00:00
|
|
|
AddStar(*types.User, *types.Repo) error
|
2015-05-11 07:45:31 +00:00
|
|
|
|
|
|
|
// DelStar unstars a repository.
|
2015-06-15 06:26:49 +00:00
|
|
|
DelStar(*types.User, *types.Repo) error
|
2015-05-11 07:45:31 +00:00
|
|
|
|
|
|
|
//
|
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-15 06:26:49 +00:00
|
|
|
Repo(id int64) (*types.Repo, error)
|
2015-04-08 22:43:59 +00:00
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
// RepoName retrieves a repo from the datastore
|
|
|
|
// for the specified name.
|
2015-06-15 06:26:49 +00:00
|
|
|
RepoName(owner, name string) (*types.Repo, error)
|
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-15 06:26:49 +00:00
|
|
|
RepoList(*types.User) ([]*types.Repo, error)
|
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-15 06:26:49 +00:00
|
|
|
AddRepo(*types.Repo) error
|
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-15 06:26:49 +00:00
|
|
|
SetRepo(*types.Repo) error
|
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-15 06:26:49 +00:00
|
|
|
DelRepo(*types.Repo) error
|
2015-04-07 08:20:55 +00:00
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
//
|
2015-04-07 08:20:55 +00:00
|
|
|
|
2015-06-23 03:45:08 +00:00
|
|
|
// Build gets a build by ID
|
|
|
|
Build(int64) (*types.Build, error)
|
2015-04-07 08:20:55 +00:00
|
|
|
|
2015-06-23 03:45:08 +00:00
|
|
|
// BuildNumber gets the specified build number for the
|
|
|
|
// named repository and build number
|
|
|
|
BuildNumber(*types.Repo, int) (*types.Build, error)
|
2015-04-07 08:20:55 +00:00
|
|
|
|
2015-06-23 03:45:08 +00:00
|
|
|
// BuildLast gets the last executed build for the
|
2015-05-11 07:45:31 +00:00
|
|
|
// named repository and branch
|
2015-06-23 03:45:08 +00:00
|
|
|
BuildLast(*types.Repo, string) (*types.Build, error)
|
2015-04-07 08:20:55 +00:00
|
|
|
|
2015-06-23 03:45:08 +00:00
|
|
|
// BuildList gets a list of recent builds for the
|
2015-04-07 08:20:55 +00:00
|
|
|
// named repository.
|
2015-06-23 03:45:08 +00:00
|
|
|
BuildList(*types.Repo, int, int) ([]*types.Build, error)
|
2015-04-07 08:20:55 +00:00
|
|
|
|
2015-06-23 03:45:08 +00:00
|
|
|
// AddBuild inserts a new build in the datastore.
|
|
|
|
AddBuild(*types.Build) error
|
2015-04-07 08:20:55 +00:00
|
|
|
|
2015-06-23 03:45:08 +00:00
|
|
|
// SetBuild updates an existing build and build jobs.
|
|
|
|
SetBuild(*types.Build) error
|
2015-04-29 22:26:22 +00:00
|
|
|
|
2015-06-23 03:45:08 +00:00
|
|
|
// KillBuilds updates all pending or started builds
|
2015-05-11 07:45:31 +00:00
|
|
|
// in the datastore settings the status to killed.
|
2015-06-23 03:45:08 +00:00
|
|
|
KillBuilds() error
|
2015-04-07 08:20:55 +00:00
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
//
|
2015-04-16 06:45:24 +00:00
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
// Build returns a build by ID.
|
2015-06-19 00:36:52 +00:00
|
|
|
Job(int64) (*types.Job, error)
|
2015-04-16 06:45:24 +00:00
|
|
|
|
2015-06-19 00:36:52 +00:00
|
|
|
// JobNumber returns a jobs by sequence number.
|
2015-06-23 03:45:08 +00:00
|
|
|
JobNumber(*types.Build, int) (*types.Job, error)
|
2015-04-29 18:19:39 +00:00
|
|
|
|
2015-06-23 03:45:08 +00:00
|
|
|
// JobList returns a list of all build jobs
|
|
|
|
JobList(*types.Build) ([]*types.Job, error)
|
2015-04-29 22:26:22 +00:00
|
|
|
|
2015-06-19 00:36:52 +00:00
|
|
|
// SetJob updates an existing job.
|
|
|
|
SetJob(*types.Job) error
|
2015-04-29 22:26:22 +00:00
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
//
|
2015-04-29 18:19:39 +00:00
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
// Get retrieves an object from the blobstore.
|
|
|
|
GetBlob(path string) ([]byte, error)
|
|
|
|
|
|
|
|
// GetBlobReader retrieves an object from the blobstore.
|
|
|
|
// It is the caller's responsibility to call Close on
|
|
|
|
// the ReadCloser when finished reading.
|
|
|
|
GetBlobReader(path string) (io.ReadCloser, error)
|
2015-05-08 05:31:25 +00:00
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
// Set inserts an object into the blobstore.
|
|
|
|
SetBlob(path string, data []byte) error
|
|
|
|
|
|
|
|
// SetBlobReader inserts an object into the blobstore by
|
|
|
|
// consuming data from r until EOF.
|
|
|
|
SetBlobReader(path string, r io.Reader) error
|
|
|
|
|
|
|
|
// Del removes an object from the blobstore.
|
|
|
|
DelBlob(path string) error
|
2015-05-18 22:47:13 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// Agent returns an agent by ID.
|
2015-06-23 03:45:08 +00:00
|
|
|
Agent(*types.Build) (string, error)
|
2015-05-18 22:47:13 +00:00
|
|
|
|
|
|
|
// SetAgent updates an agent in the datastore.
|
2015-06-23 03:45:08 +00:00
|
|
|
SetAgent(*types.Build, string) error
|
2015-05-11 07:45:31 +00:00
|
|
|
}
|