-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathrelease.go
More file actions
234 lines (198 loc) · 6.06 KB
/
release.go
File metadata and controls
234 lines (198 loc) · 6.06 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package actions
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/pkg/errors"
"github.com/speakeasy-api/sdk-generation-action/internal/cli"
"github.com/speakeasy-api/sdk-generation-action/internal/configuration"
"github.com/speakeasy-api/sdk-generation-action/internal/environment"
"github.com/speakeasy-api/sdk-generation-action/internal/git"
"github.com/speakeasy-api/sdk-generation-action/internal/logging"
"github.com/speakeasy-api/sdk-generation-action/internal/run"
"github.com/speakeasy-api/sdk-generation-action/pkg/releases"
)
func Release() error {
accessToken := environment.GetAccessToken()
if accessToken == "" {
return errors.New("github access token is required")
}
g, err := initAction()
if err != nil {
return err
}
dir := "."
usingReleasesMd := false
var providesExplicitTarget bool
if specificTarget := environment.SpecifiedTarget(); specificTarget != "" {
workflow, err := configuration.GetWorkflowAndValidateLanguages(true)
if err != nil {
return err
}
if target, ok := workflow.Targets[specificTarget]; ok {
if target.Output != nil {
dir = strings.TrimPrefix(*target.Output, "./")
}
dir = filepath.Join(environment.GetWorkingDirectory(), dir)
providesExplicitTarget = true
}
}
if !providesExplicitTarget {
// This searches for files that would be referenced in the GH Action trigger
files, err := g.GetCommitedFiles()
if err != nil {
fmt.Printf("Failed to get commited files: %s\n", err.Error())
}
if environment.IsDebugMode() {
for _, file := range files {
logging.Debug("Found commited file: %s", file)
}
}
dir, usingReleasesMd = GetDirAndShouldUseReleasesMD(files, dir, usingReleasesMd)
}
var latestRelease *releases.ReleasesInfo
if usingReleasesMd {
latestRelease, err = releases.GetLastReleaseInfo(dir)
if err != nil {
return err
}
} else {
latestRelease, err = releases.GetReleaseInfoFromGenerationFiles(dir)
if err != nil {
return err
}
}
outputs := map[string]string{}
for lang, info := range latestRelease.Languages {
outputs[fmt.Sprintf("%s_regenerated", lang)] = "true"
outputs[fmt.Sprintf("%s_directory", lang)] = info.Path
}
if err = addPublishOutputs(dir, outputs); err != nil {
return err
}
if err := g.CreateRelease(*latestRelease, outputs); err != nil {
return err
}
if err = setOutputs(outputs); err != nil {
return err
}
if os.Getenv("SPEAKEASY_API_KEY") != "" {
if err = addCurrentBranchTagging(g, latestRelease.Languages); err != nil {
return errors.Wrap(err, "failed to tag registry images")
}
}
return nil
}
func GetDirAndShouldUseReleasesMD(files []string, dir string, usingReleasesMd bool) (string, bool) {
for _, file := range files {
// Maintain Support for RELEASES.MD for backward compatibility with existing publishing actions
if strings.Contains(file, "RELEASES.md") {
// file = ./RELEASES.md
// dir = .
dir = filepath.Dir(file)
logging.Info("Found RELEASES.md in %s\n", dir)
usingReleasesMd = true
break
}
if strings.Contains(file, "gen.lock") {
// file = .speakeasy/gen.lock
dir = filepath.Dir(file)
if strings.Contains(dir, ".speakeasy") {
dir = filepath.Dir(dir)
}
logging.Info("Found gen.lock in %s\n", dir)
}
}
return dir, usingReleasesMd
}
func addPublishOutputs(dir string, outputs map[string]string) error {
wf, err := configuration.GetWorkflowAndValidateLanguages(false)
if err != nil {
return err
}
for _, target := range wf.Targets {
// Only add outputs for the target that was regenerated, based on output directory
if dir != "." && target.Output != nil {
output, err := filepath.Rel(".", *target.Output)
if err != nil {
return err
}
if environment.GetWorkingDirectory() != "" {
output = filepath.Join(environment.GetWorkingDirectory(), output)
}
if output != dir {
continue
}
}
run.AddTargetPublishOutputs(target, outputs, nil)
}
return nil
}
func addCurrentBranchTagging(g *git.Git, latestRelease map[string]releases.LanguageReleaseInfo) error {
_, err := cli.Download("latest", g)
if err != nil {
return err
}
var sources, targets []string
// a tag that is applied if the target contributing is published
var isPublished bool
branch := strings.TrimPrefix(os.Getenv("GITHUB_REF"), "refs/heads/")
workflow, err := configuration.GetWorkflowAndValidateLanguages(true)
if err != nil {
return err
}
// the tagging library treats targets synonymously with code samples
if specificTarget := environment.SpecifiedTarget(); specificTarget != "" {
if target, ok := workflow.Targets[specificTarget]; ok {
isPublished = target.IsPublished()
if source, ok := workflow.Sources[target.Source]; ok && source.Registry != nil {
sources = append(sources, target.Source)
}
if target.CodeSamples != nil && target.CodeSamples.Registry != nil {
targets = append(targets, specificTarget)
}
}
} else {
for name, target := range workflow.Targets {
if releaseInfo, ok := latestRelease[target.Target]; ok {
var targetIsMatched bool
releasePath, err := filepath.Rel(".", releaseInfo.Path)
if err != nil {
return err
}
// check for no SDK output path
if (releasePath == "" || releasePath == ".") && target.Output == nil {
targetIsMatched = true
}
if target.Output != nil {
outputPath, err := filepath.Rel(".", *target.Output)
if err != nil {
return err
}
outputPath = filepath.Join(environment.GetWorkingDirectory(), outputPath)
if outputPath == releasePath {
targetIsMatched = true
}
}
if targetIsMatched {
isPublished = isPublished || target.IsPublished()
if source, ok := workflow.Sources[target.Source]; ok && source.Registry != nil {
sources = append(sources, target.Source)
}
if target.CodeSamples != nil && target.CodeSamples.Registry != nil {
targets = append(targets, name)
}
}
}
}
}
if (len(sources) > 0 || len(targets) > 0) && branch != "" {
tags := []string{branch}
if isPublished {
tags = append(tags, "published")
}
return cli.Tag(tags, sources, targets)
}
return nil
}