woodpecker/pkg/store/store.go

197 lines
5.1 KiB
Go
Raw Normal View History

package store
import (
2015-06-15 06:26:49 +00:00
"fmt"
"io"
2015-06-15 06:26:49 +00:00
"net/url"
2015-06-15 06:26:49 +00:00
"github.com/drone/drone/pkg/types"
)
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)
}
type Store interface {
// User returns a user by user ID.
2015-06-15 06:26:49 +00:00
User(id int64) (*types.User, error)
// UserLogin returns a user by user login.
2015-06-15 06:26:49 +00:00
UserLogin(string) (*types.User, error)
// UserList returns a list of all registered users.
2015-06-15 06:26:49 +00:00
UserList() ([]*types.User, error)
// 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)
// 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
// SetUser updates an existing user.
2015-06-15 06:26:49 +00:00
SetUser(*types.User) error
// DelUser removes the user from the datastore.
2015-06-15 06:26:49 +00:00
DelUser(*types.User) error
//
// Token returns a token by ID.
2015-06-15 06:26:49 +00:00
Token(int64) (*types.Token, error)
// TokenLabel returns a token by label
2015-06-15 06:26:49 +00:00
TokenLabel(*types.User, string) (*types.Token, error)
// TokenList returns a list of all user tokens.
2015-06-15 06:26:49 +00:00
TokenList(*types.User) ([]*types.Token, error)
// 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
// DelToken removes the DelToken from the datastore.
2015-06-15 06:26:49 +00:00
DelToken(*types.Token) error
//
2015-04-08 22:43:59 +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
// AddStar stars a repository.
2015-06-15 06:26:49 +00:00
AddStar(*types.User, *types.Repo) error
// DelStar unstars a repository.
2015-06-15 06:26:49 +00:00
DelStar(*types.User, *types.Repo) error
//
// 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
// 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)
// 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)
// AddRepo inserts a repo in the datastore.
2015-06-15 06:26:49 +00:00
AddRepo(*types.Repo) error
// SetRepo updates a repo in the datastore.
2015-06-15 06:26:49 +00:00
SetRepo(*types.Repo) error
// DelRepo removes the repo from the datastore.
2015-06-15 06:26:49 +00:00
DelRepo(*types.Repo) error
//
// Build gets a build by ID
Build(int64) (*types.Build, error)
// BuildNumber gets the specified build number for the
// named repository and build number
BuildNumber(*types.Repo, int) (*types.Build, error)
// BuildLast gets the last executed build for the
// named repository and branch
BuildLast(*types.Repo, string) (*types.Build, error)
// BuildList gets a list of recent builds for the
// named repository.
BuildList(*types.Repo, int, int) ([]*types.Build, error)
// AddBuild inserts a new build in the datastore.
AddBuild(*types.Build) error
// SetBuild updates an existing build and build jobs.
SetBuild(*types.Build) error
2015-04-29 22:26:22 +00:00
// KillBuilds updates all pending or started builds
// in the datastore settings the status to killed.
KillBuilds() error
//
// Build returns a build by ID.
2015-06-19 00:36:52 +00:00
Job(int64) (*types.Job, error)
2015-06-19 00:36:52 +00:00
// JobNumber returns a jobs by sequence number.
JobNumber(*types.Build, int) (*types.Job, error)
// 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
//
// 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)
// 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
//
// Agent returns an agent by ID.
Agent(*types.Build) (string, error)
// SetAgent updates an agent in the datastore.
SetAgent(*types.Build, string) error
}