-
Notifications
You must be signed in to change notification settings - Fork 80
/
ghr.go
199 lines (164 loc) · 5.5 KB
/
ghr.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
188
189
190
191
192
193
194
195
196
197
198
199
package main
import (
"context"
"errors"
"fmt"
"io"
"path/filepath"
"time"
"github.com/google/go-github/v66/github"
"github.com/hashicorp/go-version"
"golang.org/x/sync/errgroup"
)
// GHR contains the top level GitHub object
type GHR struct {
GitHub GitHub
outStream io.Writer
}
// CreateRelease creates (or recreates) a new package release
func (g *GHR) CreateRelease(ctx context.Context, req *github.RepositoryRelease, recreate bool) (*github.RepositoryRelease, error) {
// When draft release creation is requested,
// create it without any check (it can).
if *req.Draft {
fmt.Fprintln(g.outStream, "==> Create a draft release")
return g.GitHub.CreateRelease(ctx, req)
}
// Always create release as draft first. After uploading assets, turn off
// draft unless the `-draft` flag is explicitly specified.
// It is to prevent users from seeing empty release.
req.Draft = github.Bool(true)
// Check release exists.
// If release is not found, then create a new release.
release, err := g.GitHub.GetRelease(ctx, *req.TagName)
if err != nil {
if !errors.Is(err, ErrReleaseNotFound) {
return nil, fmt.Errorf("failed to get release: %w", err)
}
Debugf("Release (with tag %s) not found: create a new one",
*req.TagName)
if recreate {
fmt.Fprintf(g.outStream,
"WARNING: '-recreate' is specified but release (%s) not found",
*req.TagName)
}
fmt.Fprintln(g.outStream, "==> Create a new release")
return g.GitHub.CreateRelease(ctx, req)
}
// recreate is not true. Then use that existing release.
if !recreate {
Debugf("Release (with tag %s) exists: use existing one",
*req.TagName)
fmt.Fprintf(g.outStream, "WARNING: found release (%s). Use existing one.\n",
*req.TagName)
return release, nil
}
// When recreate is requested, delete existing release and create a
// new release.
fmt.Fprintln(g.outStream, "==> Recreate a release")
if err := g.DeleteRelease(ctx, *release.ID, *req.TagName); err != nil {
return nil, err
}
return g.GitHub.CreateRelease(ctx, req)
}
func (g *GHR) GetLatestRelease(ctx context.Context) (*github.RepositoryRelease, error) {
release, err := g.GitHub.GetLatestRelease(ctx)
if err != nil {
return nil, fmt.Errorf("Unable to fetch latest Github release: %w", err)
}
return release, err
}
func (g *GHR) IsNewerSemverRelease(newRelease *github.RepositoryRelease, latestRelease *github.RepositoryRelease) (bool, error) {
newReleaseVer, error := version.NewVersion(*newRelease.TagName)
if error != nil {
return false, fmt.Errorf("Unable to parse new release version as semver%s: %w", *newRelease.TagName, error)
}
latestReleaseVer, error := version.NewVersion(*latestRelease.TagName)
if error != nil {
return false, fmt.Errorf("Unable to parse latest release version as semver %s: %w", *newRelease.TagName, error)
}
if latestReleaseVer.LessThan(newReleaseVer) {
return true, nil
}
return false, nil
}
// DeleteRelease removes an existing release, if it exists. If it does not exist,
// DeleteRelease returns an error
func (g *GHR) DeleteRelease(ctx context.Context, releaseID int64, tag string) error {
err := g.GitHub.DeleteRelease(ctx, releaseID)
if err != nil {
return err
}
err = g.GitHub.DeleteTag(ctx, tag)
if err != nil {
return err
}
// This is because sometimes the process of creating a release on GitHub
// is faster than deleting a tag.
time.Sleep(5 * time.Second)
return nil
}
// UploadAssets uploads the designated assets in parallel (determined by parallelism setting)
func (g *GHR) UploadAssets(ctx context.Context, releaseID int64, localAssets []string, parallel int) error {
start := time.Now()
defer func() {
Debugf("UploadAssets: time: %d ms", int(time.Since(start).Seconds()*1000))
}()
eg, ctx := errgroup.WithContext(ctx)
semaphore := make(chan struct{}, parallel)
for _, localAsset := range localAssets {
localAsset := localAsset
eg.Go(func() error {
semaphore <- struct{}{}
defer func() {
<-semaphore
}()
fmt.Fprintf(g.outStream, "--> Uploading: %15s\n", filepath.Base(localAsset))
_, err := g.GitHub.UploadAsset(ctx, releaseID, localAsset)
if err != nil {
return fmt.Errorf("failed to upload asset: %s %w", localAsset, err)
}
return nil
})
}
if err := eg.Wait(); err != nil {
return fmt.Errorf("one of the goroutines failed: %w", err)
}
return nil
}
// DeleteAssets removes uploaded assets for a given release
func (g *GHR) DeleteAssets(ctx context.Context, releaseID int64, localAssets []string, parallel int) error {
start := time.Now()
defer func() {
Debugf("DeleteAssets: time: %d ms", int(time.Since(start).Seconds()*1000))
}()
eg, ctx := errgroup.WithContext(ctx)
assets, err := g.GitHub.ListAssets(ctx, releaseID)
if err != nil {
return fmt.Errorf("failed to list assets: %w", err)
}
semaphore := make(chan struct{}, parallel)
for _, localAsset := range localAssets {
for _, asset := range assets {
// https://golang.org/doc/faq#closures_and_goroutines
localAsset, asset := localAsset, asset
// Uploaded asset name is same as basename of local file
if *asset.Name == filepath.Base(localAsset) {
eg.Go(func() error {
semaphore <- struct{}{}
defer func() {
<-semaphore
}()
fmt.Fprintf(g.outStream, "--> Deleting: %15s\n", *asset.Name)
if err := g.GitHub.DeleteAsset(ctx, *asset.ID); err != nil {
return fmt.Errorf("failed to delete asset: %s %w", *asset.Name, err)
}
return nil
})
}
}
}
if err := eg.Wait(); err != nil {
return fmt.Errorf("one of the goroutines failed: %w", err)
}
return nil
}