-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathrun.go
More file actions
377 lines (308 loc) · 11.8 KB
/
run.go
File metadata and controls
377 lines (308 loc) · 11.8 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package run
import (
"context"
"fmt"
"path"
"path/filepath"
"strconv"
"strings"
"github.com/google/go-github/v63/github"
"github.com/speakeasy-api/sdk-generation-action/internal/utils"
"github.com/speakeasy-api/sdk-generation-action/internal/versionbumps"
"github.com/speakeasy-api/versioning-reports/versioning"
"github.com/speakeasy-api/sdk-gen-config/workflow"
config "github.com/speakeasy-api/sdk-gen-config"
"github.com/speakeasy-api/sdk-generation-action/internal/cli"
"github.com/speakeasy-api/sdk-generation-action/internal/environment"
)
type LanguageGenInfo struct {
PackageName string
Version string
}
type GenerationInfo struct {
SpeakeasyVersion string
GenerationVersion string
OpenAPIDocVersion string
Languages map[string]LanguageGenInfo
HasTestingEnabled bool
}
type RunResult struct {
GenInfo *GenerationInfo
OpenAPIChangeSummary string
LintingReportURL string
ChangesReportURL string
VersioningReport *versioning.MergedVersionReport
VersioningInfo versionbumps.VersioningInfo
// key is language, value is release notes
ReleaseNotes map[string]string
}
type Git interface {
CheckDirDirty(dir string, ignoreMap map[string]string) (bool, string, error)
}
func Run(g Git, pr *github.PullRequest, wf *workflow.Workflow) (*RunResult, map[string]string, error) {
workspace := environment.GetWorkspace()
outputs := map[string]string{}
releaseNotes := map[string]string{}
executeSpeakeasyVersion, err := cli.GetSpeakeasyVersion()
if err != nil {
return nil, outputs, fmt.Errorf("failed to get speakeasy version: %w", err)
}
executeGenerationVersion, err := cli.GetGenerationVersion()
if err != nil {
return nil, outputs, fmt.Errorf("failed to get generation version: %w", err)
}
speakeasyVersion := executeSpeakeasyVersion.String()
generationVersion := executeGenerationVersion.String()
langGenerated := map[string]bool{}
globalPreviousGenVersion := ""
langConfigs := map[string]*config.LanguageConfig{}
installationURLs := map[string]string{}
repoURL := getRepoURL()
fmt.Println("INPUT_ENABLE_SDK_CHANGELOG: ", environment.GetSDKChangelog())
repoSubdirectories := map[string]string{}
previousManagementInfos := map[string]config.Management{}
var manualVersioningBump *versioning.BumpType
if versionBump := versionbumps.GetLabelBasedVersionBump(pr); versionBump != "" && versionBump != versioning.BumpNone {
fmt.Println("Using label based version bump: ", versionBump)
manualVersioningBump = &versionBump
}
getDirAndOutputDir := func(target workflow.Target) (string, string) {
dir := "."
if target.Output != nil {
dir = *target.Output
}
dir = filepath.Join(environment.GetWorkingDirectory(), dir)
return dir, path.Join(workspace, "repo", dir)
}
includesTerraform := false
// Load initial configs
for targetID, target := range wf.Targets {
if environment.SpecifiedTarget() != "" && environment.SpecifiedTarget() != "all" && environment.SpecifiedTarget() != targetID {
continue
}
lang := target.Target
dir, outputDir := getDirAndOutputDir(target)
// Load the config so we can get the current version information
loadedCfg, err := config.Load(outputDir)
if err != nil {
return nil, outputs, err
}
previousManagementInfos[targetID] = loadedCfg.LockFile.Management
globalPreviousGenVersion, err = getPreviousGenVersion(loadedCfg.LockFile, lang, globalPreviousGenVersion)
if err != nil {
return nil, outputs, err
}
fmt.Printf("Generating %s SDK in %s\n", lang, outputDir)
installationURL := getInstallationURL(lang, dir)
AddTargetPublishOutputs(target, outputs, &installationURL)
if installationURL != "" {
installationURLs[targetID] = installationURL
}
if dir != "." {
repoSubdirectories[targetID] = filepath.Clean(dir)
} else {
repoSubdirectories[targetID] = ""
}
if lang == "terraform" {
includesTerraform = true
}
}
// Run the workflow
var runRes *cli.RunResults
var changereport *versioning.MergedVersionReport
changereport, runRes, err = versioning.WithVersionReportCapture[*cli.RunResults](context.Background(), func(ctx context.Context) (*cli.RunResults, error) {
return cli.Run(wf.Targets == nil || len(wf.Targets) == 0, installationURLs, repoURL, repoSubdirectories, manualVersioningBump)
})
if err != nil {
return nil, outputs, err
}
if len(changereport.Reports) == 0 {
// Assume it's not yet enabled (e.g. CLI version too old)
changereport = nil
}
if changereport != nil && !changereport.MustGenerate() && !environment.ForceGeneration() && pr == nil {
// no further steps
fmt.Printf("No changes that imply the need for us to automatically regenerate the SDK.\n Use \"Force Generation\" if you want to force a new generation.\n Changes would include:\n-----\n%s", changereport.GetMarkdownSection())
return &RunResult{
GenInfo: nil,
VersioningInfo: versionbumps.VersioningInfo{
VersionReport: changereport,
ManualBump: versionbumps.ManualBumpWasUsed(manualVersioningBump, changereport),
},
OpenAPIChangeSummary: runRes.OpenAPIChangeSummary,
LintingReportURL: runRes.LintingReportURL,
ChangesReportURL: runRes.ChangesReportURL,
}, outputs, nil
}
// For terraform, we also trigger "go generate ./..." to regenerate docs
if includesTerraform {
if err = cli.TriggerGoGenerate(); err != nil {
return nil, outputs, err
}
}
hasTestingEnabled := false
// Legacy logic: check for changes + dirty-check
for targetID, target := range wf.Targets {
if environment.SpecifiedTarget() != "" && environment.SpecifiedTarget() != "all" && environment.SpecifiedTarget() != targetID {
continue
}
lang := target.Target
dir, outputDir := getDirAndOutputDir(target)
// Load the config again so we can compare the versions
loadedCfg, err := config.Load(outputDir)
if err != nil {
return nil, outputs, err
}
currentManagementInfo := loadedCfg.LockFile.Management
langCfg := loadedCfg.Config.Languages[lang]
langConfigs[lang] = &langCfg
if loadedCfg.LockFile.ReleaseNotes != "" {
releaseNotes[lang] = loadedCfg.LockFile.ReleaseNotes
}
outputs[utils.OutputTargetDirectory(lang)] = dir
previousManagementInfo := previousManagementInfos[targetID]
dirty, dirtyMsg, err := g.CheckDirDirty(dir, map[string]string{
previousManagementInfo.ReleaseVersion: currentManagementInfo.ReleaseVersion,
previousManagementInfo.GenerationVersion: currentManagementInfo.GenerationVersion,
previousManagementInfo.ConfigChecksum: currentManagementInfo.ConfigChecksum,
previousManagementInfo.DocVersion: currentManagementInfo.DocVersion,
previousManagementInfo.DocChecksum: currentManagementInfo.DocChecksum,
})
if err != nil {
return nil, outputs, err
}
if dirty {
target.IsPublished()
if target.Testing != nil && target.Testing.Enabled != nil && *target.Testing.Enabled {
hasTestingEnabled = true
}
hasTestingEnabled = true
langGenerated[lang] = true
// Set speakeasy version and generation version to what was used by the CLI
if currentManagementInfo.SpeakeasyVersion != "" {
speakeasyVersion = currentManagementInfo.SpeakeasyVersion
}
if currentManagementInfo.GenerationVersion != "" {
generationVersion = currentManagementInfo.GenerationVersion
}
fmt.Printf("Regenerating %s SDK resulted in significant changes %s\n", lang, dirtyMsg)
} else {
fmt.Printf("Regenerating %s SDK did not result in any changes\n", lang)
}
}
outputs["previous_gen_version"] = globalPreviousGenVersion
regenerated := false
langGenInfo := map[string]LanguageGenInfo{}
for lang := range langGenerated {
outputs[utils.OutputTargetRegenerated(lang)] = "true"
langCfg := langConfigs[lang]
langGenInfo[lang] = LanguageGenInfo{
PackageName: utils.GetPackageName(lang, langCfg),
Version: langCfg.Version,
}
regenerated = true
}
var genInfo *GenerationInfo
if regenerated {
genInfo = &GenerationInfo{
SpeakeasyVersion: speakeasyVersion,
GenerationVersion: generationVersion,
// OpenAPIDocVersion: docVersion, //TODO
Languages: langGenInfo,
HasTestingEnabled: hasTestingEnabled,
}
}
return &RunResult{
GenInfo: genInfo,
VersioningInfo: versionbumps.VersioningInfo{
VersionReport: changereport,
ManualBump: versionbumps.ManualBumpWasUsed(manualVersioningBump, changereport),
},
OpenAPIChangeSummary: runRes.OpenAPIChangeSummary,
LintingReportURL: runRes.LintingReportURL,
ChangesReportURL: runRes.ChangesReportURL,
ReleaseNotes: releaseNotes,
}, outputs, nil
}
func getPreviousGenVersion(lockFile *config.LockFile, lang, globalPreviousGenVersion string) (string, error) {
previousFeatureVersions, ok := lockFile.Features[lang]
if !ok {
return globalPreviousGenVersion, nil
}
if globalPreviousGenVersion != "" {
globalPreviousGenVersion += ";"
}
globalPreviousGenVersion += fmt.Sprintf("%s:", lang)
previousFeatureParts := []string{}
for feature, previousVersion := range previousFeatureVersions {
previousFeatureParts = append(previousFeatureParts, fmt.Sprintf("%s,%s", feature, previousVersion))
}
globalPreviousGenVersion += strings.Join(previousFeatureParts, ",")
return globalPreviousGenVersion, nil
}
func getInstallationURL(lang, subdirectory string) string {
subdirectory = filepath.Clean(subdirectory)
switch lang {
case "cli", "go":
base := fmt.Sprintf("%s/%s", environment.GetGithubServerURL(), environment.GetRepo())
if subdirectory == "." {
return base
}
return base + "/" + subdirectory
case "typescript":
if subdirectory == "." {
return fmt.Sprintf("%s/%s", environment.GetGithubServerURL(), environment.GetRepo())
} else {
return ""
}
case "python":
base := fmt.Sprintf("%s/%s.git", environment.GetGithubServerURL(), environment.GetRepo())
if subdirectory == "." {
return base
}
return base + "#subdirectory=" + subdirectory
case "php":
// PHP doesn't support subdirectories
if subdirectory == "." {
return fmt.Sprintf("%s/%s", environment.GetGithubServerURL(), environment.GetRepo())
}
case "ruby":
base := fmt.Sprintf("%s/%s", environment.GetGithubServerURL(), environment.GetRepo())
if subdirectory == "." {
return base
}
return base + " -d " + subdirectory
}
// Neither Java nor C# support pulling directly from git
return ""
}
func getRepoURL() string {
return fmt.Sprintf("%s/%s.git", environment.GetGithubServerURL(), environment.GetRepo())
}
func AddTargetPublishOutputs(target workflow.Target, outputs map[string]string, installationURL *string) {
lang := target.Target
published := target.IsPublished() || lang == "go"
// TODO: Temporary check to fix Java. We may remove this entirely, pending conversation
if installationURL != nil && *installationURL == "" && lang != "java" {
published = true // Treat as published if we don't have an installation URL
}
// Use OR logic: don't let a non-published target overwrite a previously
// set published=true (e.g. workflow.local.yaml targets without publish blocks
// sharing the same output directory as the main target).
outputKey := utils.OutputTargetPublish(lang)
if existing, ok := outputs[outputKey]; !ok || existing != "true" || published {
outputs[outputKey] = strconv.FormatBool(published)
}
if published && lang == "java" && target.Publishing != nil && target.Publishing.Java != nil {
outputs["use_sonatype_legacy"] = strconv.FormatBool(target.Publishing.Java.UseSonatypeLegacy)
}
if lang == "python" && target.Publishing != nil && target.Publishing.PyPi != nil &&
target.Publishing.PyPi.UseTrustedPublishing != nil && *target.Publishing.PyPi.UseTrustedPublishing {
outputs["use_pypi_trusted_publishing"] = "true"
}
if lang == "mcp-typescript" && target.Publishing != nil && target.Publishing.MCPRegistry != nil &&
target.Publishing.MCPRegistry.Auth != "" {
outputs["publish_mcp_registry"] = "true"
outputs["mcp_registry_auth"] = target.Publishing.MCPRegistry.Auth
}
}