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
2 changes: 0 additions & 2 deletions .github/workflows/build-docker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ on:
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}

jobs:
build-and-push-image:
runs-on: ubuntu-latest
Expand All @@ -19,7 +18,6 @@ jobs:
steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Log in to the Container registry
uses: docker/login-action@v2.1.0
with:
Expand Down
2 changes: 0 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ on:
- reopened
- synchronize
- ready_for_review
env:
GOPRIVATE: "github.com/speakeasy-api"
jobs:
build:
name: Build
Expand Down
40 changes: 24 additions & 16 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,54 @@ 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/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
180 changes: 102 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
}
12 changes: 11 additions & 1 deletion internal/cli/speakeasy.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type Git interface {
GetDownloadLink(version string) (string, string, error)
}

func Download(pinnedVersion string, g Git) (string, error) {
func GetVersion(pinnedVersion string) string {
if pinnedVersion == "" {
pinnedVersion = "latest"
}
Expand All @@ -36,11 +36,21 @@ func Download(pinnedVersion string, g Git) (string, error) {
}
}

return version
}

func Download(pinnedVersion string, g Git) (string, error) {
version := GetVersion(pinnedVersion)

link, version, err := g.GetDownloadLink(version)
if err != nil {
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
3 changes: 1 addition & 2 deletions internal/environment/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type Mode string

const (
ModeDirect Mode = "direct"
ModePR Mode = "pr"
ModePR Mode = "pr"
ThomasRooney marked this conversation as resolved.
Show resolved Hide resolved
)

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

func GetMaxValidationWarnings() (int, error) {
maxVal := os.Getenv("INPUT_MAX_VALIDATION_WARNINGS")
if maxVal == "" {
Expand Down
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
132 changes: 132 additions & 0 deletions internal/telemetry/telemetry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package telemetry

import (
"context"
"fmt"
"github.com/google/uuid"
speakeasy "github.com/speakeasy-api/speakeasy-client-sdk-go/v3"
"github.com/speakeasy-api/speakeasy-client-sdk-go/v3/pkg/models/operations"
"github.com/speakeasy-api/speakeasy-client-sdk-go/v3/pkg/models/shared"
"os"
"strings"
"time"
)

type ContextKey string

const ExecutionKeyEnvironmentVariable = "SPEAKEASY_EXECUTION_ID"
const SpeakeasySDKKey ContextKey = "speakeasy.SDK"
const WorkspaceIDKey ContextKey = "speakeasy.workspaceID"
const AccountTypeKey ContextKey = "speakeasy.accountType"
// a random UUID. Change this to fan-out executions with the same gh run id.
const speakeasyGithubActionNamespace = "360D564A-5583-4EF6-BC2B-99530BF036CC"

func NewContextWithSDK(ctx context.Context, apiKey string) (context.Context, *speakeasy.Speakeasy, string, error) {
security := shared.Security{APIKey: &apiKey}
sdk := speakeasy.New(speakeasy.WithSecurity(security))
validated, err := sdk.Auth.ValidateAPIKey(ctx)
if err != nil {
return ctx, nil, "", err
}
sdkWithWorkspace := speakeasy.New(speakeasy.WithSecurity(security), speakeasy.WithWorkspaceID(validated.APIKeyDetails.WorkspaceID))
ctx = context.WithValue(ctx, SpeakeasySDKKey, sdkWithWorkspace)
ctx = context.WithValue(ctx, WorkspaceIDKey, validated.APIKeyDetails.WorkspaceID)
ctx = context.WithValue(ctx, AccountTypeKey, validated.APIKeyDetails.AccountType)
return ctx, sdkWithWorkspace, validated.APIKeyDetails.WorkspaceID, err
}

func GetApiKey() string {
return os.Getenv("SPEAKEASY_API_KEY")
}

func EnrichEventWithEnvironmentVariables(event *shared.CliEvent) {
if os.Getenv("GITHUB_ACTIONS") != "true" {
return
}
ghActionOrg := os.Getenv("GITHUB_REPOSITORY_OWNER")
ghActionRepoOrg := os.Getenv("GITHUB_REPOSITORY")
event.GhActionOrganization = &ghActionOrg
repo := strings.TrimPrefix(ghActionRepoOrg, ghActionOrg+"/")
event.GhActionRepository = &repo
runLink := fmt.Sprintf("%s/%s/actions/runs/%s", os.Getenv("GITHUB_SERVER_URL"), ghActionRepoOrg, os.Getenv("GITHUB_RUN_ID"))
event.GhActionRunLink = &runLink

ghActionVersion := os.Getenv("GH_ACTION_VERSION")
if ghActionVersion != "" {
event.GhActionVersion = &ghActionVersion
}
}

func enrichHostName(event *shared.CliEvent) {
hostname, err := os.Hostname()
if err != nil {
return
}
event.Hostname = &hostname
}

func Track(ctx context.Context, exec shared.InteractionType, fn func(ctx context.Context, event *shared.CliEvent) error) error {
// Generate a unique ID for this event
id, err := uuid.NewV7()
if err != nil {
return err
}

runID := os.Getenv("GITHUB_RUN_ID")
if runID == "" {
return fmt.Errorf("no GITHUB_RUN_ID provided")
}
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, sdk, workspaceID, err := NewContextWithSDK(ctx, apiKey)
if err != nil {
return err
}
executionID := uuid.NewSHA1(namespace, []byte(executionKey)).String()
_ = os.Setenv(ExecutionKeyEnvironmentVariable, executionID)

// Prepare the initial CliEvent
runEvent := &shared.CliEvent{
CreatedAt: time.Now(),
ExecutionID: executionID,
ID: id.String(),
WorkspaceID: workspaceID,
InteractionType: exec,
LocalStartedAt: time.Now(),
SpeakeasyVersion: fmt.Sprintf(os.Getenv("GH_ACTION_VERSION")),
Success: false,
}
runEvent.WorkspaceID = workspaceID

EnrichEventWithEnvironmentVariables(runEvent)
enrichHostName(runEvent)

// Execute the provided function, capturing any error
err = fn(ctx, runEvent)

// Update the event with completion details
curTime := time.Now()
runEvent.LocalCompletedAt = &curTime
duration := runEvent.LocalCompletedAt.Sub(runEvent.LocalStartedAt).Milliseconds()
runEvent.DurationMs = &duration
runEvent.Success = err == nil
currentIntegrationEnvironment := "GITHUB_ACTIONS"
runEvent.ContinuousIntegrationEnvironment = &currentIntegrationEnvironment

// Attempt to flush any stored events (swallow errors)
sdk.Events.PostWorkspaceEvents(ctx, operations.PostWorkspaceEventsRequest{
RequestBody: []shared.CliEvent{*runEvent},
WorkspaceID: &workspaceID,
})

return err

}
Loading
Loading