-
Notifications
You must be signed in to change notification settings - Fork 58
/
out_command.go
204 lines (166 loc) · 4.48 KB
/
out_command.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
200
201
202
203
204
package resource
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/google/go-github/v66/github"
)
type OutCommand struct {
github GitHub
writer io.Writer
}
func NewOutCommand(github GitHub, writer io.Writer) *OutCommand {
return &OutCommand{
github: github,
writer: writer,
}
}
func (c *OutCommand) Run(sourceDir string, request OutRequest) (OutResponse, error) {
params := request.Params
name, err := c.fileContents(filepath.Join(sourceDir, request.Params.NamePath))
if err != nil {
return OutResponse{}, err
}
tag, err := c.fileContents(filepath.Join(sourceDir, request.Params.TagPath))
if err != nil {
return OutResponse{}, err
}
tag = request.Params.TagPrefix + tag
var body string
bodySpecified := false
if request.Params.BodyPath != "" {
bodySpecified = true
body, err = c.fileContents(filepath.Join(sourceDir, request.Params.BodyPath))
if err != nil {
return OutResponse{}, err
}
}
targetCommitish := ""
if request.Params.CommitishPath != "" {
targetCommitish, err = c.fileContents(filepath.Join(sourceDir, request.Params.CommitishPath))
if err != nil {
return OutResponse{}, err
}
}
draft := request.Source.Drafts
prerelease := false
if request.Source.PreRelease == true && request.Source.Release == false {
prerelease = request.Source.PreRelease
}
generateReleaseNotes := request.Params.GenerateReleaseNotes
release := &github.RepositoryRelease{
Name: github.String(name),
TagName: github.String(tag),
Body: github.String(body),
Draft: github.Bool(draft),
Prerelease: github.Bool(prerelease),
TargetCommitish: github.String(targetCommitish),
GenerateReleaseNotes: github.Bool(generateReleaseNotes),
}
existingReleases, err := c.github.ListReleases()
if err != nil {
return OutResponse{}, err
}
var existingRelease *github.RepositoryRelease
for _, e := range existingReleases {
if e.TagName != nil && *e.TagName == tag {
existingRelease = e
break
}
}
if existingRelease != nil {
releaseAssets, err := c.github.ListReleaseAssets(*existingRelease)
if err != nil {
return OutResponse{}, err
}
existingRelease.Name = github.String(name)
existingRelease.TargetCommitish = github.String(targetCommitish)
existingRelease.Draft = github.Bool(draft)
existingRelease.Prerelease = github.Bool(prerelease)
if bodySpecified {
existingRelease.Body = github.String(body)
} else {
existingRelease.Body = nil
}
for _, asset := range releaseAssets {
fmt.Fprintf(c.writer, "clearing existing asset: %s\n", *asset.Name)
err := c.github.DeleteReleaseAsset(*asset)
if err != nil {
return OutResponse{}, err
}
}
fmt.Fprintf(c.writer, "updating release %s\n", name)
release, err = c.github.UpdateRelease(*existingRelease)
if err != nil {
return OutResponse{}, err
}
} else {
fmt.Fprintf(c.writer, "creating release %s\n", name)
release, err = c.github.CreateRelease(*release)
if err != nil {
return OutResponse{}, err
}
}
for _, fileGlob := range params.Globs {
matches, err := filepath.Glob(filepath.Join(sourceDir, fileGlob))
if err != nil {
return OutResponse{}, err
}
if len(matches) == 0 {
return OutResponse{}, fmt.Errorf("could not find file that matches glob '%s'", fileGlob)
}
for _, filePath := range matches {
err := c.upload(release, filePath)
if err != nil {
return OutResponse{}, err
}
}
}
return OutResponse{
Version: versionFromRelease(release),
Metadata: metadataFromRelease(release, ""),
}, nil
}
func (c *OutCommand) fileContents(path string) (string, error) {
contents, err := ioutil.ReadFile(path)
if err != nil {
return "", err
}
return strings.TrimSpace(string(contents)), nil
}
func (c *OutCommand) upload(release *github.RepositoryRelease, filePath string) error {
fmt.Fprintf(c.writer, "uploading %s\n", filePath)
name := filepath.Base(filePath)
var retryErr error
for i := 0; i < 10; i++ {
file, err := os.Open(filePath)
if err != nil {
return err
}
defer file.Close()
retryErr = c.github.UploadReleaseAsset(*release, name, file)
if retryErr == nil {
break
}
assets, err := c.github.ListReleaseAssets(*release)
if err != nil {
return err
}
for _, asset := range assets {
if asset.Name != nil && *asset.Name == name {
err = c.github.DeleteReleaseAsset(*asset)
if err != nil {
return err
}
break
}
}
}
if retryErr != nil {
return retryErr
}
return nil
}