Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add a common execution id into CI events #93

Merged
merged 13 commits into from
Mar 11, 2024
7 changes: 5 additions & 2 deletions .github/workflows/build-docker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}

GOPRIVATE: "github.com/speakeasy-api"
ThomasRooney marked this conversation as resolved.
Show resolved Hide resolved
jobs:
build-and-push-image:
runs-on: ubuntu-latest
Expand All @@ -17,9 +17,12 @@ jobs:
packages: write

steps:
- name: Configure git for private modules
env:
GIT_AUTH_TOKEN: ${{ secrets.BOT_REPO_TOKEN }}
run: git config --global url."https://speakeasybot:${GIT_AUTH_TOKEN}@github.com".insteadOf "https://github.com"
- name: Checkout repository
uses: actions/checkout@v3

- name: Log in to the Container registry
uses: docker/login-action@v2.1.0
with:
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ jobs:
id-token: "write"
actions: "read"
steps:
- name: Configure git for private modules
env:
GIT_AUTH_TOKEN: ${{ secrets.BOT_REPO_TOKEN }}
run: git config --global url."https://speakeasybot:${GIT_AUTH_TOKEN}@github.com".insteadOf "https://github.com"
- name: Set up Go 1.x
uses: actions/setup-go@v3
with:
Expand Down
41 changes: 25 additions & 16 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,55 @@ module github.com/speakeasy-api/sdk-generation-action
go 1.21

require (
github.com/go-git/go-git/v5 v5.4.2
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.13.8
github.com/speakeasy-api/git-diff-parser v0.0.3
github.com/speakeasy-api/sdk-gen-config v1.5.1
github.com/speakeasy-api/speakeasy-client-sdk-go/v3 v3.4.1
github.com/speakeasy-api/speakeasy-core v0.3.2
github.com/stretchr/testify v1.8.4
golang.org/x/exp v0.0.0-20230811145659-89c5cff77bcb
golang.org/x/oauth2 v0.11.0
gopkg.in/yaml.v3 v3.0.1
)

require (
dario.cat/mergo v1.0.0 // indirect
github.com/AlekSi/pointer v1.2.0 // indirect
github.com/Microsoft/go-winio v0.5.2 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8 // indirect
github.com/acomagu/bufpipe v1.0.3 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/cloudflare/circl v1.3.3 // indirect
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dprotaso/go-yit v0.0.0-20220510233725-9ba8df137936 // indirect
github.com/emirpasic/gods v1.12.0 // indirect
github.com/go-git/gcfg v1.5.0 // indirect
github.com/go-git/go-billy/v5 v5.3.1 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/ericlagergren/decimal v0.0.0-20240305081647-93d586550569 // 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
github.com/google/uuid v1.5.0 // indirect
github.com/imdario/mergo v0.3.15 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sergi/go-diff v1.2.0 // indirect
github.com/skeema/knownhosts v1.2.1 // indirect
github.com/vmware-labs/yaml-jsonpath v0.3.2 // indirect
github.com/xanzy/ssh-agent v0.3.0 // indirect
golang.org/x/crypto v0.12.0 // indirect
golang.org/x/net v0.14.0 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
golang.org/x/crypto v0.16.0 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/tools v0.13.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
Expand Down
176 changes: 98 additions & 78 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion internal/actions/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ func Generate() error {
}()

genInfo, outputs, err := generate.Generate(g)
outputs["resolved_speakeasy_version"] = resolvedVersion
if err != nil {
if err := setOutputs(outputs); err != nil {
logging.Debug("failed to set outputs: %v", err)
}
return err
}
outputs["resolved_speakeasy_version"] = resolvedVersion

if genInfo != nil {
docVersion := genInfo.OpenAPIDocVersion
Expand Down
12 changes: 6 additions & 6 deletions internal/actions/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ type logProxyEntry struct {
Tags map[string]interface{} `json:"tags"`
}

func LogActionResult() {
func LogActionResult() error {
key := os.Getenv("SPEAKEASY_API_KEY")
if key == "" {
fmt.Print("no SPEAKEASY_API_KEY provided.")
return
return nil
}

logLevel := logProxyLevelInfo
Expand Down Expand Up @@ -90,7 +90,7 @@ func LogActionResult() {
body, err := json.Marshal(&request)
if err != nil {
fmt.Print("failure sending log to speakeasy.")
return
return nil
}

baseURL := os.Getenv("SPEAKEASY_SERVER_URL")
Expand All @@ -101,7 +101,7 @@ func LogActionResult() {
req, err := http.NewRequest("POST", baseURL+"/v1/log/proxy", bytes.NewBuffer(body))
if err != nil {
fmt.Print("failure sending log to speakeasy.")
return
return nil
}

req.Header.Set("Content-Type", "application/json")
Expand All @@ -113,7 +113,7 @@ func LogActionResult() {
resp, err := client.Do(req)
if err != nil {
fmt.Print("failure sending log to speakeasy.")
return
return nil
}

defer resp.Body.Close()
Expand All @@ -122,5 +122,5 @@ func LogActionResult() {
fmt.Printf("failure sending log to speakeasy with status %s.", resp.Status)
}

return
return nil
}
4 changes: 4 additions & 0 deletions internal/cli/speakeasy.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ func Download(pinnedVersion string, g Git) (string, error) {
return version, err
}

if _, err := os.Stat(filepath.Join(environment.GetBaseDir(), "bin", "speakeasy")); err == nil {
return version, nil
}

fmt.Println("Downloading speakeasy cli version: ", version)

downloadPath := filepath.Join(os.TempDir(), "speakeasy"+path.Ext(link))
Expand Down
39 changes: 38 additions & 1 deletion internal/environment/environment.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package environment

import (
"context"
"fmt"
"github.com/google/uuid"
"github.com/speakeasy-api/speakeasy-client-sdk-go/v3/pkg/models/shared"
"github.com/speakeasy-api/speakeasy-core/auth"
"github.com/speakeasy-api/speakeasy-core/events"
"os"
"strconv"
"strings"
Expand All @@ -12,7 +17,9 @@ type Mode string

const (
ModeDirect Mode = "direct"
ModePR Mode = "pr"
ModePR Mode = "pr"
ThomasRooney marked this conversation as resolved.
Show resolved Hide resolved
// a random UUID. Change this to fan-out executions with the same gh run id.
speakeasyGithubActionNamespace = "360D564A-5583-4EF6-BC2B-99530BF036CC"
)

type Action string
Expand Down Expand Up @@ -81,6 +88,9 @@ func GetPinnedSpeakeasyVersion() string {
func GetMaxSuggestions() string {
return os.Getenv("INPUT_MAX_SUGGESTIONS")
}
func GetApiKey() string {
return os.Getenv("SPEAKEASY_API_KEY")
}

func GetMaxValidationWarnings() (int, error) {
maxVal := os.Getenv("INPUT_MAX_VALIDATION_WARNINGS")
Expand Down Expand Up @@ -219,3 +229,30 @@ func GetWorkspace() string {
func ShouldOutputTests() bool {
return os.Getenv("INPUT_OUTPUT_TESTS") == "true"
}

func Telemetry(f func() error) error {
runID := os.Getenv("GITHUB_RUN_ID")
if runID == "" {
return f()
}
executionKey := fmt.Sprintf("GITHUB_RUN_ID_%s", runID)
namespace, err := uuid.Parse(speakeasyGithubActionNamespace)
if err != nil {
return err
}

apiKey := GetApiKey()
if apiKey == "" {
return fmt.Errorf("no SPEAKEASY_API_KEY secret provided")
}
ctx, err := auth.NewContextWithSDK(context.Background(), apiKey)
if err != nil {
return err
}
executionID := uuid.NewSHA1(namespace, []byte(executionKey))
_ = os.Setenv(events.ExecutionKeyEnvironmentVariable, executionID.String())

return events.Telemetry(ctx, shared.InteractionTypeCiExec, func(ctx context.Context, event *shared.CliEvent) error {
return f()
})
}
7 changes: 7 additions & 0 deletions internal/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ func (g *Git) CloneRepo() error {

workspace := environment.GetWorkspace()

// Remove the repo if it exists
// Flow is useful when testing locally, but we're usually in a fresh image so unnecessary most of the time
repoDir := path.Join(workspace, "repo")
if err := os.RemoveAll(repoDir); err != nil {
return err
}

r, err := git.PlainClone(path.Join(workspace, "repo"), false, &git.CloneOptions{
URL: repoPath,
Progress: os.Stdout,
Expand Down
37 changes: 21 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,27 @@ func main() {
}

var err error
switch environment.GetAction() {
case environment.ActionValidate:
err = actions.Validate()
case environment.ActionSuggest:
err = actions.Suggest()
case environment.ActionGenerate:
err = actions.Generate()
case environment.ActionFinalize:
err = actions.Finalize()
case environment.ActionFinalizeSuggestion:
err = actions.FinalizeSuggestion()
case environment.ActionRelease:
err = actions.Release()
case environment.ActionLog:
actions.LogActionResult()
}

err = environment.Telemetry(func() error {
switch environment.GetAction() {
case environment.ActionValidate:
return actions.Validate()
case environment.ActionSuggest:
return actions.Suggest()
case environment.ActionGenerate:
return actions.Generate()
case environment.ActionFinalize:
return actions.Finalize()
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())
}
})

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