-
Notifications
You must be signed in to change notification settings - Fork 12
/
models.go
104 lines (93 loc) · 2.3 KB
/
models.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package models
import (
"log"
"strings"
"time"
_ "github.com/go-sql-driver/mysql"
"github.com/go-xorm/xorm"
"github.com/gobuild/gobuild/models/github"
)
type User struct {
Id int64 `json:"id"`
Name string `json:"name"`
Email string `xorm:"unique" json:"email"`
GithubToken string `xorm:"github_token" json:"-"`
Admin bool `json:"admin"`
CreatedAt time.Time `xorm:"created" json:"created_at"`
UpdatedAt time.Time `xorm:"updated" json:"updated_at"`
RepoUpdatedAt time.Time `json:"repo_updated_at"`
}
type Repository struct {
Id int64 `json:"id"`
Owner string `xorm:"unique(nn)" json:"owner"`
Repo string `xorm:"unique(nn)" json:"repo"`
UserId int64 `xorm:"'user_id'" json:"-"`
Refs []string `json:"refs"` // can be branch or tag
Valid bool `json:"valid"`
Official bool `json:"official"`
Download uint64 `json:"download"`
CreatedAt time.Time `xorm:"created" json:"created_at"`
UpdatedAt time.Time `xorm:"updated" json:"updated_at"`
TriggerAt time.Time `json:"trigger_at"`
}
var DB *xorm.Engine
func init() {
var err error
DB, err = xorm.NewEngine("mysql", MYSQL_URI)
if err != nil {
log.Fatal(err)
}
if err = DB.Sync(new(User), new(Repository)); err != nil {
log.Fatal(err)
}
}
func (user *User) SyncGithub() error {
gh := github.New(user.GithubToken)
repos, err := gh.Repositories()
if err != nil {
return err
}
for _, ghRepo := range repos {
parts := strings.Split(ghRepo.Fullname, "/")
if len(parts) != 2 {
continue
}
var repo = &Repository{
Owner: parts[0],
Repo: parts[1],
}
exists, err := DB.Get(repo)
if err != nil {
return err
}
repo.UserId = user.Id
if exists {
repoId := repo.Id
_, err := DB.Update(repo, &Repository{Id: repoId})
if err != nil {
return err
}
} else {
_, err := DB.Insert(repo)
if err != nil {
return err
}
}
}
user.RepoUpdatedAt = time.Now()
_, err = DB.Id(user.Id).Cols("repo_updated_at").Update(user)
if err != nil {
log.Println(err)
}
return nil
}
func (user *User) Repositories() ([]Repository, error) {
var repos []Repository
err := DB.Find(&repos, &Repository{UserId: user.Id})
return repos, err
}
func (r *Repository) AddBranch(name string) error {
// CDN_URL_BASE + "/"
// r.Branches
return nil
}