Skip to content

Commit

Permalink
feat: add publishing CLI events support python (#123)
Browse files Browse the repository at this point in the history
* feat: add publishing CLI events

* feat: update for testing

* feat: update
  • Loading branch information
ryan-timothy-albert authored May 9, 2024
1 parent 9b0f991 commit f86aa1a
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 19 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/sdk-publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,18 @@ jobs:
run: |
python setup.py sdist bdist_wheel
twine upload dist/*
- id: publish-event
uses: speakeasy-api/sdk-generation-action@v15
if: always()
with:
github_access_token: ${{ secrets.github_access_token }}
action: publish-event
speakeasy_api_key: ${{ secrets.speakeasy_api_key }}
speakeasy_server_url: ${{ inputs.speakeasy_server_url }}
registry_name: "pypi"
env:
GH_ACTION_RESULT: ${{ job.status }}
GH_ACTION_VERSION: "v15"
- uses: ravsamhq/notify-slack-action@v2
if: always() && env.SLACK_WEBHOOK_URL != ''
with:
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ outputs:
description: "The version of the previous generation"
openapi_doc:
description: "The location of the OpenAPI document used for generation"
registry_name:
description: "The name of the publishing registry"
runs:
using: "docker"
image: "docker://ghcr.io/speakeasy-api/sdk-generation-action:v15"
Expand All @@ -165,3 +167,5 @@ runs:
- ${{ inputs.openapi_doc_auth_token }}
- ${{ inputs.target }}
- ${{ inputs.registry_tags }}
- ${{ inputs.registry_name }}

4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ module github.com/speakeasy-api/sdk-generation-action
go 1.21.0

require (
github.com/go-git/go-billy/v5 v5.5.0
github.com/go-git/go-git/v5 v5.11.0
github.com/google/go-github/v54 v54.0.0
github.com/google/uuid v1.6.0
github.com/hashicorp/go-version v1.6.0
github.com/pb33f/libopenapi v0.14.0
github.com/speakeasy-api/git-diff-parser v0.0.3
github.com/speakeasy-api/sdk-gen-config v1.7.4
github.com/speakeasy-api/speakeasy-client-sdk-go/v3 v3.4.1
github.com/speakeasy-api/speakeasy-client-sdk-go/v3 v3.7.1
github.com/stretchr/testify v1.8.4
golang.org/x/exp v0.0.0-20231127185646-65229373498e
golang.org/x/oauth2 v0.11.0
Expand All @@ -33,7 +34,6 @@ require (
github.com/ericlagergren/decimal v0.0.0-20240305081647-93d586550569 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/go-git/go-billy/v5 v5.5.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-querystring v1.1.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ github.com/speakeasy-api/git-diff-parser v0.0.3 h1:LL12d+HMtSyj6O/hQqIn/lgDPYI6c
github.com/speakeasy-api/git-diff-parser v0.0.3/go.mod h1:P46HmmVVmwA9P8h2wa0fDpmRM8/grbVQ+uKhWDtpkIY=
github.com/speakeasy-api/sdk-gen-config v1.7.4 h1:hk3GaKiL6zxx3SulnxdunvU2V7bUSQ4YviXtIiUmWuo=
github.com/speakeasy-api/sdk-gen-config v1.7.4/go.mod h1:4R+8FTyM6UdLHltOVAigIoR5D2UfPsGMmEFzPOP1yCs=
github.com/speakeasy-api/speakeasy-client-sdk-go/v3 v3.4.1 h1:7bnCFOsWDhce7m0Btgsxjq30ZGlluXDm21hI6qj2BPc=
github.com/speakeasy-api/speakeasy-client-sdk-go/v3 v3.4.1/go.mod h1:b4fiZ1Wid0JHwwiYqhaPifDwjmC15uiN7A8Cmid+9kw=
github.com/speakeasy-api/speakeasy-client-sdk-go/v3 v3.7.1 h1:8cfPRFXn9a7IMBAQFhQ0N4rKCHMqWclVBGTEmXKXrZ4=
github.com/speakeasy-api/speakeasy-client-sdk-go/v3 v3.7.1/go.mod h1:b4fiZ1Wid0JHwwiYqhaPifDwjmC15uiN7A8Cmid+9kw=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
Expand Down
109 changes: 109 additions & 0 deletions internal/actions/publishEvent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package actions

import (
"context"
"fmt"
"os"
"strings"

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

func PublishEvent() error {
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
fmt.Println("REGISTRY NAME")
fmt.Println(registryName)
}

workingDir, err := os.Getwd() // in publishing working dir is the SDK output directory
if err != nil {
return err
}

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

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

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

var processingErr error
switch os.Getenv("INPUT_REGISTRY_NAME") {
case "pypy":
processingErr = processPyPI(loadedCfg, event, workingDir, version)
}

if processingErr != nil {
return processingErr
}

if !strings.Contains(strings.ToLower(os.Getenv("GH_ACTION_RESULT")), "success") {
return fmt.Errorf("failure in publishing: %s", os.Getenv("GH_ACTION_RESULT"))
}

return nil
})
}

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

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

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

if packageName != "" {
event.PublishPackageName = &packageName
fmt.Println("PACKAGE NAME")
fmt.Println(packageName)
}

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

return nil

}

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

if lockFile.Management.ReleaseVersion != "" {
event.PublishPackageVersion = &lockFile.Management.ReleaseVersion
fmt.Println("RELEASE VERSION")
fmt.Println(lockFile.Management.ReleaseVersion)
}

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

return lockFile.Management.SpeakeasyVersion
}
1 change: 1 addition & 0 deletions internal/environment/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const (
ActionFinalizeSuggestion Action = "finalize-suggestion"
ActionRelease Action = "release"
ActionLog Action = "log-result"
ActionPublishEvent Action = "publish-event"
)

const (
Expand Down
32 changes: 17 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package main
import (
"context"
"fmt"
"github.com/speakeasy-api/sdk-generation-action/internal/telemetry"
"github.com/speakeasy-api/speakeasy-client-sdk-go/v3/pkg/models/shared"
"os"
"strings"

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

"github.com/speakeasy-api/sdk-generation-action/internal/actions"
"github.com/speakeasy-api/sdk-generation-action/internal/environment"
"golang.org/x/exp/slices"
Expand Down Expand Up @@ -38,22 +39,23 @@ func main() {

err = telemetry.Track(context.Background(), shared.InteractionTypeCiExec, func(ctx context.Context, event *shared.CliEvent) error {
switch environment.GetAction() {
case environment.ActionSuggest:
return actions.Suggest()
case environment.ActionRunWorkflow:
return actions.RunWorkflow()
case environment.ActionFinalizeSuggestion:
return actions.FinalizeSuggestion()
case environment.ActionRelease:
return actions.Release()
case environment.ActionLog:
return actions.LogActionResult()
default:
return fmt.Errorf("unknown action: %s", environment.GetAction())
case environment.ActionSuggest:
return actions.Suggest()
case environment.ActionRunWorkflow:
return actions.RunWorkflow()
case environment.ActionFinalizeSuggestion:
return actions.FinalizeSuggestion()
case environment.ActionRelease:
return actions.Release()
case environment.ActionLog:
return actions.LogActionResult()
case environment.ActionPublishEvent:
return actions.PublishEvent()
default:
return fmt.Errorf("unknown action: %s", environment.GetAction())
}
})


if err != nil {
fmt.Printf("::error title=failed::%v\n", err)
os.Exit(1)
Expand Down

0 comments on commit f86aa1a

Please sign in to comment.