-
Notifications
You must be signed in to change notification settings - Fork 31
/
github.go
121 lines (111 loc) · 2.81 KB
/
github.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
// Copyright (c) 2020-present Cloud <[email protected]>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of version 3 of the GNU General Public
// License as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"context"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
)
type Github struct {
Client *github.Client
}
func (g *Github) Init(n *Nami) error {
s, err := n.GetConfig("github.token")
if err != nil {
return err
}
if s == "" {
return errors.New("Please config github.token first: $ nami config github.token TOKEN")
}
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: s},
)
tc := oauth2.NewClient(ctx, ts)
g.Client = github.NewClient(tc)
return nil
}
func (g *Github) Latest(name string) (string, error) {
l := strings.Split(name, "/")
if len(l) != 3 {
return "", errors.New("Invalid github package name")
}
ctx := context.Background()
l1, _, err := g.Client.Repositories.ListReleases(ctx, l[1], l[2], nil)
if err != nil {
return "", err
}
if len(l1) == 0 {
return "", nil
}
return *(l1[0].TagName), nil
}
func (g *Github) Release(name, version, dir string) error {
l := strings.Split(name, "/")
if len(l) != 3 {
return errors.New("Invalid github package name")
}
ctx := context.Background()
r, _, err := g.Client.Repositories.GetReleaseByTag(ctx, l[1], l[2], version)
if err != nil {
if !strings.Contains(err.Error(), "404") {
return err
}
fmt.Println("Creating release")
r, _, err = g.Client.Repositories.CreateRelease(ctx, l[1], l[2], &github.RepositoryRelease{
TagName: &version,
Name: &version,
})
if err != nil {
return err
}
}
if len(r.Assets) > 0 {
fmt.Println("Deleting old assets")
}
for _, v := range r.Assets {
_, err := g.Client.Repositories.DeleteReleaseAsset(ctx, l[1], l[2], *(v.ID))
if err != nil {
return err
}
}
l1, err := ioutil.ReadDir(dir)
if err != nil {
return err
}
for _, v := range l1 {
if v.IsDir() {
continue
}
fmt.Println("Uploading " + filepath.Join(dir, v.Name()))
f, err := os.Open(filepath.Join(dir, v.Name()))
if err != nil {
return err
}
defer f.Close()
_, _, err = g.Client.Repositories.UploadReleaseAsset(ctx, l[1], l[2], *(r.ID), &github.UploadOptions{
Name: v.Name(),
}, f)
if err != nil {
return err
}
}
return nil
}