Skip to content

Commit

Permalink
feat: fire go publishing events (#141)
Browse files Browse the repository at this point in the history
* feat: fire go publishing events

* feat: fire go publishing events

* feat: fire go publishing events

* feat: update

* feat: fix circular dependency

* feat: test

* feat: test
  • Loading branch information
ryan-timothy-albert authored Jun 25, 2024
1 parent 746bf1c commit 5e5b2a4
Show file tree
Hide file tree
Showing 4 changed files with 385 additions and 310 deletions.
314 changes: 5 additions & 309 deletions internal/actions/publishEvent.go
Original file line number Diff line number Diff line change
@@ -1,331 +1,27 @@
package actions

import (
"context"
"fmt"
"os"
"path/filepath"
"strings"

config "github.com/speakeasy-api/sdk-gen-config"
"github.com/speakeasy-api/sdk-generation-action/internal/environment"
"github.com/speakeasy-api/sdk-generation-action/internal/telemetry"
"github.com/speakeasy-api/speakeasy-client-sdk-go/v3/pkg/models/shared"
)

func PublishEvent() error {
func PublishEventAction() error {
g, err := initAction()
if err != nil {
return err
}

workspace := environment.GetWorkspace()
path := filepath.Join(workspace, "repo")
path = filepath.Join(path, os.Getenv("INPUT_TARGET_DIRECTORY"))

return telemetry.Track(context.Background(), shared.InteractionTypePublish, func(ctx context.Context, event *shared.CliEvent) error {
registryName := os.Getenv("INPUT_REGISTRY_NAME")
if registryName != "" {
event.PublishPackageRegistryName = &registryName
}

loadedCfg, err := config.Load(path)
if err != nil {
return err
}

if loadedCfg.LockFile == nil {
return fmt.Errorf("empty lock file for python language target in directory %s", path)
}

version := processLockFile(*loadedCfg.LockFile, event)

if strings.Contains(strings.ToLower(os.Getenv("GH_ACTION_RESULT")), "success") {
version, err := telemetry.TriggerPublishingEvent(os.Getenv("INPUT_TARGET_DIRECTORY"), os.Getenv("GH_ACTION_RESULT"), os.Getenv("INPUT_REGISTRY_NAME"))
if version != "" {
if strings.Contains(os.Getenv("GH_ACTION_RESULT"), "success") {
if err = g.SetReleaseToPublished(version); err != nil {
fmt.Println("Failed to set release to published %w", err)
}
}

var processingErr error
switch os.Getenv("INPUT_REGISTRY_NAME") {
case "pypi":
processingErr = processPyPI(loadedCfg, event, path, version)
case "npm":
processingErr = processNPM(loadedCfg, event, path, version)
case "packagist":
processingErr = processPackagist(loadedCfg, event, path)
case "nuget":
processingErr = processNuget(loadedCfg, event, path, version)
case "gems":
processingErr = processGems(loadedCfg, event, path, version)
case "sonatype":
processingErr = processSonatype(loadedCfg, event, path, version)
case "terraform":
processingErr = processTerraform(loadedCfg, event, path, version)
}

if processingErr != nil {
return processingErr
}

event.Success = strings.Contains(strings.ToLower(os.Getenv("GH_ACTION_RESULT")), "success")

return nil
})
}

func processPyPI(cfg *config.Config, event *shared.CliEvent, path string, version string) error {
lang := "python"
if cfg.Config == nil {
return fmt.Errorf("empty config for %s language target in directory %s", lang, path)
}

langCfg, ok := cfg.Config.Languages[lang]
if !ok {
return fmt.Errorf("no %s config in directory %s", lang, path)
}

event.GenerateTarget = &lang

var packageName string
if name, ok := langCfg.Cfg["packageName"]; ok {
if strName, ok := name.(string); ok {
packageName = strName
}
}

if packageName != "" {
event.PublishPackageName = &packageName
}

if packageName != "" && version != "" {
publishURL := fmt.Sprintf("https://pypi.org/project/%s/%s", packageName, version)
event.PublishPackageURL = &publishURL
}

return nil
}

func processNPM(cfg *config.Config, event *shared.CliEvent, path string, version string) error {
lang := "typescript"
if cfg.Config == nil {
return fmt.Errorf("empty config for %s language target in directory %s", lang, path)
}

langCfg, ok := cfg.Config.Languages[lang]
if !ok {
return fmt.Errorf("no %s config in directory %s", lang, path)
}

event.GenerateTarget = &lang

var packageName string
if name, ok := langCfg.Cfg["packageName"]; ok {
if strName, ok := name.(string); ok {
packageName = strName
}
}

if packageName != "" {
event.PublishPackageName = &packageName
}

if packageName != "" && version != "" {
publishURL := fmt.Sprintf("https://www.npmjs.com/package/%s/v/%s", packageName, version)
event.PublishPackageURL = &publishURL
}

return nil
}

func processPackagist(cfg *config.Config, event *shared.CliEvent, path string) error {
lang := "php"
if cfg.Config == nil {
return fmt.Errorf("empty config for %s language target in directory %s", lang, path)
}

langCfg, ok := cfg.Config.Languages[lang]
if !ok {
return fmt.Errorf("no %s config in directory %s", lang, path)
}

event.GenerateTarget = &lang

var packageName string
if name, ok := langCfg.Cfg["packageName"]; ok {
if strName, ok := name.(string); ok {
packageName = strName
}
}

if packageName != "" {
event.PublishPackageName = &packageName
}

if packageName != "" {
publishURL := fmt.Sprintf("https://packagist.org/packages/%s", packageName)
event.PublishPackageURL = &publishURL
}

return nil
}

func processNuget(cfg *config.Config, event *shared.CliEvent, path string, version string) error {
lang := "csharp"
if cfg.Config == nil {
return fmt.Errorf("empty config for %s language target in directory %s", lang, path)
}

langCfg, ok := cfg.Config.Languages[lang]
if !ok {
return fmt.Errorf("no %s config in directory %s", lang, path)
}

event.GenerateTarget = &lang

var packageName string
if name, ok := langCfg.Cfg["packageName"]; ok {
if strName, ok := name.(string); ok {
packageName = strName
}
}

if packageName != "" {
event.PublishPackageName = &packageName
}

if packageName != "" && version != "" {
publishURL := fmt.Sprintf("https://www.nuget.org/packages/%s/%s", packageName, version)
event.PublishPackageURL = &publishURL
}

return nil
}

func processGems(cfg *config.Config, event *shared.CliEvent, path string, version string) error {
lang := "ruby"
if cfg.Config == nil {
return fmt.Errorf("empty config for %s language target in directory %s", lang, path)
}

langCfg, ok := cfg.Config.Languages[lang]
if !ok {
return fmt.Errorf("no %s config in directory %s", lang, path)
}

event.GenerateTarget = &lang

var packageName string
if name, ok := langCfg.Cfg["packageName"]; ok {
if strName, ok := name.(string); ok {
packageName = strName
}
}

if packageName != "" {
event.PublishPackageName = &packageName
}

if packageName != "" && version != "" {
publishURL := fmt.Sprintf("https://rubygems.org/gems/%s/%s", packageName, version)
event.PublishPackageURL = &publishURL
}

return nil
}

func processSonatype(cfg *config.Config, event *shared.CliEvent, path string, version string) error {
lang := "java"
if cfg.Config == nil {
return fmt.Errorf("empty config for %s language target in directory %s", lang, path)
}

langCfg, ok := cfg.Config.Languages[lang]
if !ok {
return fmt.Errorf("no %s config in directory %s", lang, path)
}

event.GenerateTarget = &lang

var groupID string
if name, ok := langCfg.Cfg["groupID"]; ok {
if strName, ok := name.(string); ok {
groupID = strName
}
}

var artifactID string
if name, ok := langCfg.Cfg["artifactID"]; ok {
if strName, ok := name.(string); ok {
artifactID = strName
}
}

// TODO: Figure out how to represent java legacy publish URL
if groupID != "" && artifactID != "" {
combinedPackage := fmt.Sprintf("%s/%s", groupID, artifactID)
event.PublishPackageName = &combinedPackage
}

if groupID != "" && artifactID != "" && version != "" {
publishURL := fmt.Sprintf("https://central.sonatype.com/artifact/%s/%s/%s", groupID, artifactID, version)
event.PublishPackageURL = &publishURL
}

return nil
}

func processTerraform(cfg *config.Config, event *shared.CliEvent, path string, version string) error {
lang := "terraform"
if cfg.Config == nil {
return fmt.Errorf("empty config for %s language target in directory %s", lang, path)
}

langCfg, ok := cfg.Config.Languages[lang]
if !ok {
return fmt.Errorf("no %s config in directory %s", lang, path)
}

event.GenerateTarget = &lang

var packageName string
if name, ok := langCfg.Cfg["packageName"]; ok {
if strName, ok := name.(string); ok {
packageName = strName
}
}

var author string
if name, ok := langCfg.Cfg["author"]; ok {
if strName, ok := name.(string); ok {
author = strName
}
}

if packageName != "" {
event.PublishPackageName = &packageName
}

if packageName != "" && author != "" && version != "" {
publishURL := fmt.Sprintf("https://registry.terraform.io/providers/%s/%s/%s", author, packageName, version)
event.PublishPackageURL = &publishURL
}

return nil
}

func processLockFile(lockFile config.LockFile, event *shared.CliEvent) string {
if lockFile.ID != "" {
event.GenerateGenLockID = &lockFile.ID
}

if lockFile.Management.ReleaseVersion != "" {
event.PublishPackageVersion = &lockFile.Management.ReleaseVersion
}

if lockFile.Management.SpeakeasyVersion != "" {
event.SpeakeasyVersion = lockFile.Management.SpeakeasyVersion
}

return lockFile.Management.ReleaseVersion
return err
}
15 changes: 15 additions & 0 deletions internal/git/releases.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/google/go-github/v54/github"
"github.com/speakeasy-api/sdk-generation-action/internal/environment"
"github.com/speakeasy-api/sdk-generation-action/internal/telemetry"
"github.com/speakeasy-api/sdk-generation-action/pkg/releases"
)

Expand Down Expand Up @@ -94,6 +95,7 @@ func (g *Git) CreateRelease(releaseInfo releases.ReleasesInfo, outputs map[strin
Name: github.String(fmt.Sprintf("%s - %s - %s", lang, tag, environment.GetInvokeTime().Format("2006-01-02 15:04:05"))),
Body: github.String(fmt.Sprintf(`# Generated by Speakeasy CLI%s`, releaseInfo)),
})

if err != nil {
if release, _, err := g.client.Repositories.GetReleaseByTag(context.Background(), os.Getenv("GITHUB_REPOSITORY_OWNER"), getRepo(), *tagName); err == nil && release != nil {
if release.Body != nil && strings.Contains(*release.Body, "Publishing Completed") {
Expand All @@ -106,8 +108,21 @@ func (g *Git) CreateRelease(releaseInfo releases.ReleasesInfo, outputs map[strin
// TODO: Consider deleting and recreating the release if we are moving forward with publishing
return nil
}
// Go has no publishing job, so we publish a CLI event on github release here
if lang == "go" {
if _, publishEventErr := telemetry.TriggerPublishingEvent(info.Path, "failed", "go"); publishEventErr != nil {
fmt.Printf("failed to write publishing event: %v\n", publishEventErr)
}
}

return fmt.Errorf("failed to create release for tag %s: %w", *tagName, err)
} else {
// Go has no publishing job, so we publish a CLI event on github release here
if lang == "go" {
if _, publishEventErr := telemetry.TriggerPublishingEvent(info.Path, "success", "go"); publishEventErr != nil {
fmt.Printf("failed to write publishing event: %v\n", publishEventErr)
}
}
}
}
}
Expand Down
Loading

0 comments on commit 5e5b2a4

Please sign in to comment.