This repository has been archived by the owner on May 12, 2020. It is now read-only.
forked from github-release/github-release
-
Notifications
You must be signed in to change notification settings - Fork 15
/
releases.go
153 lines (127 loc) · 4.23 KB
/
releases.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
package main
import (
"fmt"
"strings"
"time"
"github.com/aktau/github-release/github"
"github.com/dustin/go-humanize"
)
const (
RELEASE_LIST_URI = "/repos/%s/%s/releases"
RELEASE_ASSETS_URI = "/repos/%s/%s/releases/%d/assets%s"
RELEASE_LATEST_URI = "/repos/%s/%s/releases/latest"
RELEASE_DATE_FORMAT = "02/01/2006 at 15:04"
)
type Release struct {
Url string `json:"url"`
PageUrl string `json:"html_url"`
UploadUrl string `json:"upload_url"`
Id int `json:"id"`
Name string `json:"name"`
Description string `json:"body"`
TagName string `json:"tag_name"`
Draft bool `json:"draft"`
Prerelease bool `json:"prerelease"`
Created *time.Time `json:"created_at"`
Published *time.Time `json:"published_at"`
Assets []Asset `json:"assets"`
}
func (r *Release) CleanUploadUrl() string {
bracket := strings.Index(r.UploadUrl, "{")
if bracket == -1 {
return r.UploadUrl
}
return r.UploadUrl[0:bracket]
}
func (r *Release) String() string {
str := make([]string, len(r.Assets)+1)
str[0] = fmt.Sprintf(
"%s, name: '%s', description: '%s', id: %d, tagged: %s, published: %s, draft: %v, prerelease: %v",
r.TagName, r.Name, r.Description, r.Id,
timeFmtOr(r.Created, RELEASE_DATE_FORMAT, ""),
timeFmtOr(r.Published, RELEASE_DATE_FORMAT, ""),
Mark(r.Draft), Mark(r.Prerelease))
for idx, asset := range r.Assets {
str[idx+1] = fmt.Sprintf(" - artifact: %s, downloads: %d, state: %s, type: %s, size: %s, id: %d",
asset.Name, asset.Downloads, asset.State, asset.ContentType,
humanize.Bytes(asset.Size), asset.Id)
}
return strings.Join(str, "\n")
}
type ReleaseCreate struct {
TagName string `json:"tag_name"`
TargetCommitish string `json:"target_commitish,omitempty"`
Name string `json:"name"`
Body string `json:"body"`
Draft bool `json:"draft"`
Prerelease bool `json:"prerelease"`
}
func Releases(user, repo, token string) ([]Release, error) {
var releases []Release
err := github.Client{Token: token, BaseURL: EnvApiEndpoint}.Get(fmt.Sprintf(RELEASE_LIST_URI, user, repo), &releases)
if err != nil {
return nil, err
}
return releases, nil
}
func ReleaseAssets(user, repo string, releaseID int, token string) ([]Asset, error) {
if token != "" {
token = "&access_token=" + token
}
query := fmt.Sprintf("?per_page=100%s", token)
var assets []Asset
err := github.Client{Token: token, BaseURL: EnvApiEndpoint}.Get(fmt.Sprintf(RELEASE_ASSETS_URI, user, repo, releaseID, query), &assets)
if err != nil {
return nil, err
}
return assets, nil
}
func latestReleaseApi(user, repo, token string) (*Release, error) {
var release Release
return &release, github.Client{Token: token, BaseURL: EnvApiEndpoint}.Get(fmt.Sprintf(RELEASE_LATEST_URI, user, repo), &release)
}
func LatestRelease(user, repo, token string) (*Release, error) {
// If latestReleaseApi DOESN'T give an error, return the release.
if latestRelease, err := latestReleaseApi(user, repo, token); err == nil {
return latestRelease, nil
}
// The enterprise api doesnt support the latest release endpoint. Get
// all releases and compare the published date to get the latest.
releases, err := Releases(user, repo, token)
if err != nil {
return nil, err
}
var latestRelIndex = -1
maxDate := time.Time{}
for i, release := range releases {
if relDate := *release.Published; relDate.After(maxDate) {
maxDate = relDate
latestRelIndex = i
}
}
if latestRelIndex == -1 {
return nil, fmt.Errorf("could not find the latest release")
}
vprintln("Scanning ", len(releases), "releases, latest release is", releases[latestRelIndex])
return &releases[latestRelIndex], nil
}
func ReleaseOfTag(user, repo, tag, token string) (*Release, error) {
releases, err := Releases(user, repo, token)
if err != nil {
return nil, err
}
for _, release := range releases {
if release.TagName == tag {
return &release, nil
}
}
return nil, fmt.Errorf("could not find the release corresponding to tag %s", tag)
}
/* find the release-id of the specified tag */
func IdOfTag(user, repo, tag, token string) (int, error) {
release, err := ReleaseOfTag(user, repo, tag, token)
if err != nil {
return 0, err
}
return release.Id, nil
}