Refactored to use Model and Database folders

This commit is contained in:
Brad Rydzewski
2014-06-12 16:41:04 -07:00
parent 554276de05
commit 36b01512c0
32 changed files with 407 additions and 477 deletions

84
shared/model/commit.go Normal file
View File

@@ -0,0 +1,84 @@
package model
import (
"time"
)
type Commit struct {
ID int64 `meddler:"commit_id,pk" json:"id"`
RepoID int64 `meddler:"repo_id" json:"-"`
Status string `meddler:"commit_status" json:"status"`
Started int64 `meddler:"commit_started" json:"started_at"`
Finished int64 `meddler:"commit_finished" json:"finished_at"`
Duration int64 `meddler:"commit_duration" json:"duration"`
Sha string `meddler:"commit_sha" json:"sha"`
Branch string `meddler:"commit_branch" json:"branch"`
PullRequest string `meddler:"commit_pr" json:"pull_request"`
Author string `meddler:"commit_author" json:"author"`
Gravatar string `meddler:"commit_gravatar" json:"gravatar"`
Timestamp string `meddler:"commit_timestamp" json:"timestamp"`
Message string `meddler:"commit_message" json:"message"`
Config string `meddler:"commit_yaml" json:"-"`
Created int64 `meddler:"commit_created" json:"created_at"`
Updated int64 `meddler:"commit_updated" json:"updated_at"`
}
// SetAuthor sets the author's email address and calculate the Gravatar hash.
func (c *Commit) SetAuthor(email string) {
c.Author = email
c.Gravatar = createGravatar(email)
}
// Returns the Short (--short) Commit Hash.
func (c *Commit) ShaShort() string {
if len(c.Sha) > 8 {
return c.Sha[:8]
} else {
return c.Sha
}
}
// Returns the Started Date as an ISO8601
// formatted string.
func (c *Commit) FinishedString() string {
return time.Unix(c.Finished, 0).Format("2006-01-02T15:04:05Z")
}
type CommitRepo struct {
Remote string `meddler:"repo_remote" json:"remote"`
Host string `meddler:"repo_host" json:"host"`
Owner string `meddler:"repo_owner" json:"owner"`
Name string `meddler:"repo_name" json:"name"`
CommitID int64 `meddler:"commit_id,pk" json:"-"`
RepoID int64 `meddler:"repo_id" json:"-"`
Status string `meddler:"commit_status" json:"status"`
Started int64 `meddler:"commit_started" json:"started_at"`
Finished int64 `meddler:"commit_finished" json:"finished_at"`
Duration int64 `meddler:"commit_duration" json:"duration"`
Sha string `meddler:"commit_sha" json:"sha"`
Branch string `meddler:"commit_branch" json:"branch"`
PullRequest string `meddler:"commit_pr" json:"pull_request"`
Author string `meddler:"commit_author" json:"author"`
Gravatar string `meddler:"commit_gravatar" json:"gravatar"`
Timestamp string `meddler:"commit_timestamp" json:"timestamp"`
Message string `meddler:"commit_message" json:"message"`
Config string `meddler:"commit_yaml" json:"-"`
Created int64 `meddler:"commit_created" json:"created_at"`
Updated int64 `meddler:"commit_updated" json:"updated_at"`
}
// Returns the Short (--short) Commit Hash.
func (c *CommitRepo) ShaShort() string {
if len(c.Sha) > 8 {
return c.Sha[:8]
} else {
return c.Sha
}
}
// Returns the Started Date as an ISO8601
// formatted string.
func (c *CommitRepo) FinishedString() string {
return time.Unix(c.Finished, 0).Format("2006-01-02T15:04:05Z")
}

1
shared/model/config.go Normal file
View File

@@ -0,0 +1 @@
package model

60
shared/model/repo.go Normal file
View File

@@ -0,0 +1,60 @@
package model
import (
"gopkg.in/yaml.v1"
)
var (
DefaultBranch = "master"
// default build timeout, in seconds
DefaultTimeout int64 = 7200
)
// RepoParams represents a set of private key value parameters
// for each Repository.
type RepoParams map[string]string
type Repo struct {
ID int64 `meddler:"repo_id,pk" json:"-"`
UserID int64 `meddler:"user_id" json:"-"`
Remote string `meddler:"repo_remote" json:"remote"`
Host string `meddler:"repo_host" json:"host"`
Owner string `meddler:"repo_owner" json:"owner"`
Name string `meddler:"repo_name" json:"name"`
URL string `meddler:"repo_url" json:"url"`
CloneURL string `meddler:"repo_clone_url" json:"clone_url"`
GitURL string `meddler:"repo_git_url" json:"git_url"`
SSHURL string `meddler:"repo_ssh_url" json:"ssh_url"`
Active bool `meddler:"repo_active" json:"active"`
Private bool `meddler:"repo_private" json:"private"`
Privileged bool `meddler:"repo_privileged" json:"privileged"`
PostCommit bool `meddler:"repo_post_commit" json:"post_commits"`
PullRequest bool `meddler:"repo_pull_request" json:"pull_requests"`
PublicKey string `meddler:"repo_public_key" json:"-"`
PrivateKey string `meddler:"repo_private_key" json:"-"`
Params string `meddler:"repo_params" json:"-"`
Timeout int64 `meddler:"repo_timeout" json:"timeout"`
Created int64 `meddler:"repo_created" json:"created_at"`
Updated int64 `meddler:"repo_updated" json:"updated_at"`
}
func NewRepo(remote, owner, name string) (*Repo, error) {
repo := Repo{}
repo.Remote = remote
repo.Owner = owner
repo.Name = name
repo.Active = false
repo.PostCommit = true
repo.PullRequest = true
repo.Timeout = DefaultTimeout
return &repo, nil
}
func (r *Repo) ParamMap() (map[string]string, error) {
out := map[string]string{}
err := yaml.Unmarshal([]byte(r.Params), out)
return out, err
}

11
shared/model/status.go Normal file
View File

@@ -0,0 +1,11 @@
package model
const (
StatusNone = "None"
StatusEnqueue = "Pending"
StatusStarted = "Started"
StatusSuccess = "Success"
StatusFailure = "Failure"
StatusError = "Error"
StatusKilled = "Killed"
)

55
shared/model/user.go Normal file
View File

@@ -0,0 +1,55 @@
package model
import (
"time"
)
type User struct {
ID int64 `meddler:"user_id,pk" json:"-"`
ParentID int64 `meddler:"user_parent_id" json:"-"`
Remote string `meddler:"user_remote" json:"remote"`
Login string `meddler:"user_login" json:"login"`
Access string `meddler:"user_access" json:"-"`
Secret string `meddler:"user_secret" json:"-"`
Name string `meddler:"user_name" json:"name"`
Email string `meddler:"user_email" json:"email,omitempty"`
Gravatar string `meddler:"user_gravatar" json:"gravatar"`
Token string `meddler:"user_token" json:"-"`
Admin bool `meddler:"user_admin" json:"admin"`
Active bool `meddler:"user_active" json:"active"`
Created int64 `meddler:"user_created" json:"created_at"`
Updated int64 `meddler:"user_updated" json:"updated_at"`
Synced int64 `meddler:"user_synced" json:"synced_at"`
}
func NewUser(remote, login, email string) *User {
user := User{}
user.Token = generateToken()
user.Login = login
user.Remote = remote
user.Active = true
user.SetEmail(email)
return &user
}
// SetEmail sets the email address and calculate the Gravatar hash.
func (u *User) SetEmail(email string) {
u.Email = email
u.Gravatar = createGravatar(email)
}
func (u *User) IsStale() bool {
switch {
case u.Synced == 0:
return true
// refresh every 24 hours
case u.Synced+DefaultExpires < time.Now().Unix():
return true
default:
return false
}
}
// by default, let's expire the user
// cache after 72 hours
var DefaultExpires = int64(time.Hour.Seconds() * 72)

48
shared/model/util.go Normal file
View File

@@ -0,0 +1,48 @@
package model
import (
"crypto/md5"
"crypto/rand"
"fmt"
"io"
"strings"
)
// standard characters allowed in token string.
var chars = []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
// default token length
var length = 40
// generateToken generates random strings good for use in URIs to
// identify unique objects.
func generateToken() string {
b := make([]byte, length)
r := make([]byte, length+(length/4)) // storage for random bytes.
clen := byte(len(chars))
maxrb := byte(256 - (256 % len(chars)))
i := 0
for {
io.ReadFull(rand.Reader, r)
for _, c := range r {
if c >= maxrb {
// Skip this number to avoid modulo bias.
continue
}
b[i] = chars[c%clen]
i++
if i == length {
return string(b)
}
}
}
}
// helper function to create a Gravatar Hash
// for the given Email address.
func createGravatar(email string) string {
email = strings.ToLower(strings.TrimSpace(email))
hash := md5.New()
hash.Write([]byte(email))
return fmt.Sprintf("%x", hash.Sum(nil))
}