/
repo.go
128 lines (112 loc) · 3.02 KB
/
repo.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package models
import (
"errors"
"strings"
"time"
"github.com/gobuild/gobuild2/pkg/base"
"github.com/gobuild/gobuild2/pkg/gowalker"
"github.com/gobuild/log"
)
var (
ErrRepositoryNotExists = errors.New("repo not found")
)
type Repository struct {
Id int64
Uri string `xorm:"unique(r)"`
Brief string
IsCgo bool
IsCmd bool
Tags []string
Created time.Time `xorm:"created"`
DownloadCount int64
}
type RepoStatistic struct {
Rid int64 `xorm:"pk"`
Pv int64
DownloadCount int64
Updated time.Time `xorm:"updated"`
}
type LastRepoUpdate struct {
Rid int64 `xorm:"unique(u)"`
TagBranch string `xorm:"unique(u)"`
Os string `xorm:"unique(u)"`
Arch string `xorm:"unique(u)"`
PushURI string
ZipBallUrl string
Updated time.Time `xorm:"updated"`
}
func AddRepository(repoName string) (r *Repository, err error) {
cvsinfo, err := base.ParseCvsURI(repoName) // base.SanitizedRepoPath(rf.Name)
if err != nil {
log.Errorf("parse cvs url error: %v", err)
return
}
repoUri := cvsinfo.FullPath
r = new(Repository)
r.Uri = repoUri
pkginfo, err := gowalker.GetPkgInfo(repoUri)
if err != nil {
log.Errorf("gowalker not passed check: %v", err)
return
}
r.IsCgo = pkginfo.IsCgo
r.IsCmd = pkginfo.IsCmd
r.Tags = strings.Split(pkginfo.Tags, "|||")
// description
r.Brief = pkginfo.Description
base.ParseCvsURI(repoUri)
if strings.HasPrefix(repoUri, "github.com") {
// comunicate with github
fields := strings.Split(repoUri, "/")
owner, repoName := fields[1], fields[2]
repo, _, err := GHClient.Repositories.Get(owner, repoName)
if err != nil {
log.Errorf("get information from github error: %v", err)
} else {
r.Brief = *repo.Description
}
}
if _, err = CreateRepository(r); err != nil {
log.Errorf("create repo error: %v", err)
return
}
return r, nil
}
func UpdateRepository(v *Repository, condi *Repository) (int64, error) {
return orm.UseBool().Update(v, condi)
}
func CreateRepository(r *Repository) (*Repository, error) {
// r := &Repository{Uri: repoUri}
if has, err := orm.Get(r); err == nil && has {
return r, nil
}
_, err := orm.Insert(r)
return r, err
}
func GetAllRepos(count, start int) ([]Repository, error) {
var rs []Repository
err := orm.Limit(count, start).Desc("created").Find(&rs)
return rs, err
}
func GetRepositoryById(id int64) (*Repository, error) {
r := new(Repository)
if has, err := orm.Id(id).Get(r); err == nil && has {
return r, nil
}
return nil, ErrRepositoryNotExists
}
func GetRepositoryByName(name string) (*Repository, error) {
r := &Repository{Uri: name}
if has, err := orm.Get(r); err == nil && has {
return r, nil
}
return nil, ErrRepositoryNotExists
}
func GetAllLastRepoByOsArch(os, arch string) (us []LastRepoUpdate, err error) {
err = orm.Asc("rid").Find(&us, &LastRepoUpdate{Os: os, Arch: arch})
return us, err
}
func GetAllLastRepoUpdate(rid int64) (us []LastRepoUpdate, err error) {
err = orm.Find(&us, &LastRepoUpdate{Rid: rid})
return
}