Skip to content

Commit

Permalink
feat: Automatic Release detection (#335)
Browse files Browse the repository at this point in the history
Attempts to guess the release info, either by getting the last git
commit hash or by looking up some known environment variables that tracks
release identification.
  • Loading branch information
AramisHM committed Jun 8, 2021
1 parent f7d425a commit 790558d
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 6 deletions.
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func NewClient(options ClientOptions) (*Client, error) {
}

if options.Release == "" {
options.Release = os.Getenv("SENTRY_RELEASE")
options.Release = defaultRelease()
}

if options.Environment == "" {
Expand Down
4 changes: 3 additions & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
pkgErrors "github.com/pkg/errors"
)

Expand Down Expand Up @@ -281,7 +282,8 @@ func TestCaptureEvent(t *testing.T) {
},
}
got := transport.lastEvent
if diff := cmp.Diff(want, got); diff != "" {
opts := cmp.Options{cmpopts.IgnoreFields(Event{}, "Release")}
if diff := cmp.Diff(want, got, opts); diff != "" {
t.Errorf("Event mismatch (-want +got):\n%s", diff)
}
}
Expand Down
2 changes: 1 addition & 1 deletion fasthttp/sentryfasthttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func TestIntegration(t *testing.T) {
cmpopts.IgnoreFields(
sentry.Event{},
"Contexts", "EventID", "Extra", "Platform",
"Sdk", "ServerName", "Tags", "Timestamp",
"Release", "Sdk", "ServerName", "Tags", "Timestamp",
),
cmpopts.IgnoreMapEntries(func(k string, v string) bool {
// fasthttp changed Content-Length behavior in
Expand Down
2 changes: 1 addition & 1 deletion http/sentryhttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func TestIntegration(t *testing.T) {
cmpopts.IgnoreFields(
sentry.Event{},
"Contexts", "EventID", "Extra", "Platform",
"Sdk", "ServerName", "Tags", "Timestamp",
"Release", "Sdk", "ServerName", "Tags", "Timestamp",
),
cmpopts.IgnoreFields(
sentry.Request{},
Expand Down
4 changes: 2 additions & 2 deletions tracing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func TestStartSpan(t *testing.T) {
opts := cmp.Options{
cmpopts.IgnoreFields(Event{},
"Contexts", "EventID", "Level", "Platform",
"Sdk", "ServerName",
"Release", "Sdk", "ServerName",
),
cmpopts.EquateEmpty(),
}
Expand Down Expand Up @@ -210,7 +210,7 @@ func TestStartChild(t *testing.T) {
opts := cmp.Options{
cmpopts.IgnoreFields(Event{},
"EventID", "Level", "Platform",
"Sdk", "ServerName", "Timestamp", "StartTime",
"Release", "Sdk", "ServerName", "Timestamp", "StartTime",
),
cmpopts.IgnoreMapEntries(func(k string, v interface{}) bool {
return k != "trace"
Expand Down
43 changes: 43 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package sentry

import (
"bytes"
"crypto/rand"
"encoding/hex"
"encoding/json"
"fmt"
"os"
"os/exec"
"strings"
"time"
)

Expand Down Expand Up @@ -37,3 +40,43 @@ func prettyPrint(data interface{}) {
dbg, _ := json.MarshalIndent(data, "", " ")
fmt.Println(string(dbg))
}

// attempts to guess a default release.
func defaultRelease() string {
// Search environment variables (EV) known to hold release info.
envs := []string{
"SENTRY_RELEASE",
"HEROKU_SLUG_COMMIT",
"SOURCE_VERSION",
"CODEBUILD_RESOLVED_SOURCE_VERSION",
"CIRCLE_SHA1",
"GAE_DEPLOYMENT_ID",
"GITHUB_SHA", // GitHub Actions - https://help.github.com/en/actions
"COMMIT_REF", // Netlify - https://docs.netlify.com/
"VERCEL_GIT_COMMIT_SHA", // Vercel - https://vercel.com/
"ZEIT_GITHUB_COMMIT_SHA", // Zeit (now known as Vercel)
"ZEIT_GITLAB_COMMIT_SHA",
"ZEIT_BITBUCKET_COMMIT_SHA"}
for _, e := range envs {
if val := os.Getenv(e); val != "" {
return val // Stop at first non-empty variable.
}
}

// No EV's, attempt to get the last commit hash with git.
var stdout bytes.Buffer
cmd := exec.Command("git", "rev-parse", "HEAD")
cmd.Stdout = &stdout
err := cmd.Run()
if err != nil {
Logger.Println("Failed attempt to run git rev-parse.")
} else {
shastr := strings.TrimSpace(stdout.String())
if len(shastr) == 40 { // sha1 hash length
return shastr
}
}

// Not able to find a release name at all.
return ""
}

0 comments on commit 790558d

Please sign in to comment.