generated from LinuxSuRen/github-go
-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
release.go
112 lines (97 loc) · 2.98 KB
/
release.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
package pkg
import (
"context"
"github.com/google/go-github/v29/github"
)
// ReleaseClient is the client of jcli github
type ReleaseClient struct {
Client *github.Client
Org string
Repo string
ctx context.Context
}
// ReleaseAsset is the asset from GitHub release
type ReleaseAsset struct {
TagName string
Body string
}
// Init init the GitHub client
func (g *ReleaseClient) Init() {
g.Client = github.NewClient(nil)
g.ctx = context.TODO()
}
// ListReleases returns the release list
func (g *ReleaseClient) ListReleases(owner, repo string, count int) (list []ReleaseAsset, err error) {
opt := &github.ListOptions{
PerPage: count,
}
var releaseList []*github.RepositoryRelease
if releaseList, _, err = g.Client.Repositories.ListReleases(g.ctx, owner, repo, opt); err == nil {
for i := range releaseList {
list = append(list, ReleaseAsset{
TagName: releaseList[i].GetTagName(),
Body: releaseList[i].GetBody(),
})
}
}
return
}
// GetLatestJCLIAsset returns the latest jcli asset
// deprecated, please use GetLatestAsset instead
func (g *ReleaseClient) GetLatestJCLIAsset() (*ReleaseAsset, error) {
return g.GetLatestReleaseAsset(g.Org, g.Repo)
}
// GetLatestAsset returns the latest release asset which can accept preRelease or not
func (g *ReleaseClient) GetLatestAsset(acceptPreRelease bool) (*ReleaseAsset, error) {
if acceptPreRelease {
return g.GetLatestPreReleaseAsset(g.Org, g.Repo)
}
return g.GetLatestReleaseAsset(g.Org, g.Repo)
}
// GetLatestPreReleaseAsset returns the release asset that could be preRelease
func (g *ReleaseClient) GetLatestPreReleaseAsset(owner, repo string) (ra *ReleaseAsset, err error) {
ctx := context.Background()
var list []*github.RepositoryRelease
if list, _, err = g.Client.Repositories.ListReleases(ctx, owner, repo, &github.ListOptions{
Page: 1,
PerPage: 5,
}); err == nil {
ra = &ReleaseAsset{
TagName: list[0].GetTagName(),
Body: list[0].GetBody(),
}
}
return
}
// GetLatestReleaseAsset returns the latest release asset
func (g *ReleaseClient) GetLatestReleaseAsset(owner, repo string) (ra *ReleaseAsset, err error) {
ctx := context.Background()
var release *github.RepositoryRelease
if release, _, err = g.Client.Repositories.GetLatestRelease(ctx, owner, repo); err == nil {
ra = &ReleaseAsset{
TagName: release.GetTagName(),
Body: release.GetBody(),
}
}
return
}
// GetJCLIAsset returns the asset from a tag name
func (g *ReleaseClient) GetJCLIAsset(tagName string) (*ReleaseAsset, error) {
return g.GetReleaseAssetByTagName(g.Org, g.Repo, tagName)
}
// GetReleaseAssetByTagName returns the release asset by tag name
func (g *ReleaseClient) GetReleaseAssetByTagName(owner, repo, tagName string) (ra *ReleaseAsset, err error) {
var list []ReleaseAsset
if list, err = g.ListReleases(owner, repo, 99999); err == nil {
for _, item := range list {
if item.TagName == tagName {
ra = &ReleaseAsset{
TagName: item.TagName,
Body: item.Body,
}
break
}
}
}
return
}