/
bitbucket.go
69 lines (57 loc) · 1.63 KB
/
bitbucket.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
package git
import (
"github.com/tidwall/gjson"
"time"
"github.com/thanhpk/log"
"github.com/keegancsmith/shell"
)
type Bb struct {
root, username, password, email string
}
const (
bbhost = "https://bitbucket.com/"
bbapihost = "https://api.bitbucket.org/2.0/"
)
func NewBitbucketAPI(root, email, password string) *Bb {
if email == "" || password == "" {
panic("username and password must not be empty")
}
data := sendHTTPRequest("GET", bbapihost + "user", email, password)
username := gjson.Get(data, "username").Str
if username == "" {
panic("invalid email and password, cannot get username")
}
return &Bb{
username: username,
email: email,
password: password,
root: root,
}
}
func (m Bb) GetService() string {
return "bitbucket.org"
}
func (m Bb) LastUpdate(repo string) time.Time {
data := sendHTTPRequest("GET", bbapihost + "repositories/" + repo, m.email, m.password)
pushed := gjson.Get(data, "updated_on")
t, err := time.Parse(time.RFC3339Nano, pushed.Str)
if err != nil {
log.Log(err)
return time.Time{}
}
return t
}
func (m Bb) PullRepo(repo string) error {
cmd := shell.Commandf("cd %s && for remote in `git branch -r `; do git branch --track $remote; done || git remote update && git pull --all", m.root + repo)
return cmd.Run()
}
func (m Bb) CloneRepo(repo string) error {
cmd := shell.Commandf("git clone https://%s:%s@bitbucket.org/%s.git %s", m.username, m.password, repo, m.root + repo)
out, err := cmd.Output()
if err != nil {
log.Logf("git clone https://%s:%s@bitbucket.org/%s.git %s", m.username, m.password, repo, m.root + repo)
log.Log(string(out))
}
// o, _ := cmd.Output()
return err
}