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

feat: track GitHub PRs in CLI events #177

Merged
merged 5 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/pb33f/libopenapi v0.15.14
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.7.1
github.com/speakeasy-api/speakeasy-client-sdk-go/v3 v3.14.1
github.com/speakeasy-api/versioning-reports v0.6.0
github.com/stretchr/testify v1.9.0
golang.org/x/exp v0.0.0-20240213143201-ec583247a57a
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,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.7.1 h1:8cfPRFXn9a7IMBAQFhQ0N4rKCHMqWclVBGTEmXKXrZ4=
github.com/speakeasy-api/speakeasy-client-sdk-go/v3 v3.7.1/go.mod h1:b4fiZ1Wid0JHwwiYqhaPifDwjmC15uiN7A8Cmid+9kw=
github.com/speakeasy-api/speakeasy-client-sdk-go/v3 v3.14.1 h1:85zERgXt3pNq1EX11j+2qNW8LqC1B5dsqoYjqOoMpoI=
github.com/speakeasy-api/speakeasy-client-sdk-go/v3 v3.14.1/go.mod h1:b4fiZ1Wid0JHwwiYqhaPifDwjmC15uiN7A8Cmid+9kw=
github.com/speakeasy-api/versioning-reports v0.6.0 h1:oLokEQ7xnDXqWAQk60Sk+ifwYaRbq3BrLX2KyT8gWxE=
github.com/speakeasy-api/versioning-reports v0.6.0/go.mod h1:LW5FABrvi5SBbeiD3HJYw0JZYe6Rw2Xna59pFJ2BmLI=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
10 changes: 10 additions & 0 deletions internal/actions/runWorkflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package actions
import (
"context"
"fmt"
"os"
"strings"

"github.com/google/go-github/v63/github"
Expand Down Expand Up @@ -61,6 +62,10 @@ func RunWorkflow() error {
if err != nil {
return err
}

if pr != nil {
RogWilco marked this conversation as resolved.
Show resolved Hide resolved
os.Setenv("GH_PULL_REQUEST", *pr.URL)
}
}

// We want to stay on main if we're pushing code samples because we want to tag the code samples with `main`
Expand Down Expand Up @@ -253,6 +258,11 @@ func finalize(inputs finalizeInputs) error {
}); err != nil {
return err
}

if pr != nil {
RogWilco marked this conversation as resolved.
Show resolved Hide resolved
os.Setenv("GH_PULL_REQUEST", *pr.URL)
}

case environment.ModeDirect:
var releaseInfo *releases.ReleasesInfo
if !inputs.SourcesOnly {
Expand Down
13 changes: 10 additions & 3 deletions internal/telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func NewContextWithSDK(ctx context.Context, apiKey string) (context.Context, *sp
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)
ctx = context.WithValue(ctx, AccountTypeKey, validated.APIKeyDetails.AccountTypeV2)
RogWilco marked this conversation as resolved.
Show resolved Hide resolved
return ctx, sdkWithWorkspace, validated.APIKeyDetails.WorkspaceID, err
}

Expand Down Expand Up @@ -124,6 +124,13 @@ func Track(ctx context.Context, exec shared.InteractionType, fn func(ctx context
// Execute the provided function, capturing any error
err = fn(ctx, runEvent)

// Populate event with pull request env var (available only after run)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not super excited about having to move this outside EnrichEventWithEnvironmentVariables which otherwise was the perfect place to do this, but short of a more involved refactor this should do the trick.

ghPullRequest := os.Getenv("GH_PULL_REQUEST")

if ghPullRequest != "" {
runEvent.GhPullRequest = &ghPullRequest
}

// Update the event with completion details
curTime := time.Now()
runEvent.LocalCompletedAt = &curTime
Expand All @@ -138,9 +145,9 @@ func Track(ctx context.Context, exec shared.InteractionType, fn func(ctx context
runEvent.ContinuousIntegrationEnvironment = &currentIntegrationEnvironment

// Attempt to flush any stored events (swallow errors)
sdk.Events.PostWorkspaceEvents(ctx, operations.PostWorkspaceEventsRequest{
sdk.Events.Post(ctx, operations.PostWorkspaceEventsRequest{
ryan-timothy-albert marked this conversation as resolved.
Show resolved Hide resolved
RequestBody: []shared.CliEvent{*runEvent},
WorkspaceID: &workspaceID,
WorkspaceID: workspaceID,
})

return err
Expand Down
Loading