-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvgo.go
187 lines (170 loc) · 4.33 KB
/
vgo.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package vo
import (
"fmt"
"regexp"
"strings"
"time"
"github.com/astaxie/beego/httplib"
"github.com/everfore/exc"
"github.com/toukii/goutils"
"github.com/toukii/jsnm"
)
type Repo struct {
Raw string
rawUser string
rawRepo string
User string
Repo string
Name string // repo name
Branch string
Commit string
Version string
Exclude map[string]bool
}
var (
commitRegx = regexp.MustCompile("commit\\ (\\S{12})")
dateRegx = regexp.MustCompile(`Date\:\ \ \ ([\S\ ]+)`)
tagFmtUrl = "https://api.github.com/repos/%s/%s/tags?access_token=%s"
commitFmtUrl = "https://api.github.com/repos/%s/%s/commits/%s?access_token=%s"
branchLogdateFmt = "Mon Jan 2 15:04:05 2006 -0700"
branchApidateFmt = "2006-01-02T15:04:05Z"
tagFmt = "\"%s\" %s"
TOKEN = ""
)
func (r *Repo) Require() string {
repo := r.Name
if !strings.HasPrefix(repo, "gopkg.in") && !strings.HasPrefix(repo, "golang.org") {
repo = "github.com/" + repo
}
return fmt.Sprintf(tagFmt, repo, r.Tag())
}
/*
commit非空,获取commit的tag;
branch非空,获取branch的最新提交;
branch为空,先获取tag;没有tag,获取master最新提交;没有master;获取最近提交;
*/
func (r *Repo) Tag() string {
var tag string
var err error
if r.Commit != "" {
tag, err = r.CommitTag()
if err == nil {
return tag
} else {
fmt.Println(r.Name, err)
return ""
}
}
if r.Branch == "" {
tag, err = r.LatestTag()
if err != nil {
fmt.Println(r.Name, err)
}
if tag == "" {
r.Branch = "master"
tag, err = r.LocalLatestBranchCommit()
if err != nil {
fmt.Println(r.Name, err)
}
}
} else {
tag, err = r.LocalLatestBranchCommit()
if err != nil {
fmt.Println(r.Name, err)
}
}
if tag == "" {
return "latest"
}
return tag
}
func (r *Repo) LatestTag() (string, error) {
if strings.Contains(r.User, "golang.org") {
return "", fmt.Errorf("Tag api is not supported for golang.org")
}
req := httplib.NewBeegoRequest(fmt.Sprintf(tagFmtUrl, r.User, r.Repo, TOKEN), "GET")
bs, err := req.Bytes()
if err != nil {
return "", err
}
if len(bs) <= 0 {
return "", fmt.Errorf("response is nil.")
}
if strings.Contains(goutils.ToString(bs), `"message":`) {
return "", fmt.Errorf("%s", bs)
}
tag := ""
jsnm.BytesFmt(bs).Range(func(i int, ji *jsnm.Jsnm) bool {
tagTmp := ji.Get("name").RawData().String()
if len(r.Exclude) > 0 {
if _, ex := r.Exclude[tagTmp]; !ex && strings.HasPrefix(tagTmp, r.Version) {
tag = tagTmp
return true
}
} else if strings.HasPrefix(tagTmp, r.Version) {
tag = tagTmp
return true
}
return false
})
if tag == "" {
return "", fmt.Errorf("No wanted tag.")
}
return tag, nil
}
func (r *Repo) LocalLatestBranchCommit() (string, error) {
glog := exc.NewCMD(fmt.Sprintf("git log %s -1", r.Branch)).Env("GOPATH")
if strings.Contains(r.User, "golang.org") {
glog.Env("GOPATH").Cd("src").Cd(r.Name)
} else {
glog = glog.Cd("src/github.com/").Cd(r.User).Cd(r.Repo)
}
bs, err := glog.Do()
if err != nil {
fmt.Printf("%s,%+v\n", bs, err)
return "", err
}
log1 := goutils.ToString(bs)
var commit, date string
matchCommits := commitRegx.FindStringSubmatch(log1)
if len(matchCommits) > 0 {
commit = matchCommits[1]
}
matchDates := dateRegx.FindStringSubmatch(log1)
if len(matchDates) > 0 {
date = parseDate(branchLogdateFmt, matchDates[1])
}
ret := fmt.Sprintf("v0.0.0-%s-%s", date, commit)
if len(ret) < 10 {
return "", fmt.Errorf("version is missing.")
}
return ret, nil
}
func (r *Repo) CommitTag() (string, error) {
if strings.Contains(r.User, "golang.org") {
return "", fmt.Errorf("Api commits not supported for golang.org")
}
req := httplib.NewBeegoRequest(fmt.Sprintf(commitFmtUrl, r.User, r.Repo, r.Commit, TOKEN), "GET")
bs, err := req.Bytes()
if err != nil {
return "", err
}
if len(bs) <= 0 {
return "", fmt.Errorf("response is nil.")
}
if strings.Contains(goutils.ToString(bs), `"message":`) {
return "", fmt.Errorf("%s", bs)
}
js := jsnm.BytesFmt(bs)
cur := js.Get("sha").RawData().String()
date := parseDate(branchApidateFmt, js.PathGet("commit", "committer", "date").RawData().String())
commit := cur[:12]
return fmt.Sprintf("v0.0.0-%s-%s", date, commit), nil
}
func parseDate(dateFmt, date string) string {
d, err := time.Parse(dateFmt, date)
if err != nil {
panic(err)
}
return d.Format("20060102150405")
}