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: Read release info from build info #704

Merged
merged 3 commits into from
Sep 1, 2023
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
19 changes: 19 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"encoding/json"
"fmt"
"os"
"runtime/debug"
"strings"
"time"

Expand Down Expand Up @@ -66,6 +67,13 @@
}
}

if info, ok := debug.ReadBuildInfo(); ok {
buildInfoVcsRevision := revisionFromBuildInfo(info)
if len(buildInfoVcsRevision) > 0 {
return buildInfoVcsRevision
}

Check warning on line 74 in util.go

View check run for this annotation

Codecov / codecov/patch

util.go#L70-L74

Added lines #L70 - L74 were not covered by tests
}

// Derive a version string from Git. Example outputs:
// v1.0.1-0-g9de4
// v2.0-8-g77df-dirty
Expand All @@ -89,3 +97,14 @@
Logger.Printf("Using release from Git: %s", release)
return release
}

func revisionFromBuildInfo(info *debug.BuildInfo) string {
for _, setting := range info.Settings {
if setting.Key == "vcs.revision" && setting.Value != "" {
Logger.Printf("Using release from debug info: %s", setting.Value)
return setting.Value
}
}

return ""
}
64 changes: 64 additions & 0 deletions util_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sentry

import (
"runtime/debug"
"testing"
)

Expand All @@ -27,3 +28,66 @@ func TestFileExistsReturnsFalseForNonExistingFiles(t *testing.T) {
assertEqual(t, fileExists(("util_nope.go")), false)
assertEqual(t, fileExists(("util_nope_test.go")), false)
}

func TestDefaultReleaseSentryReleaseEnvvar(t *testing.T) {
releaseVersion := "1.2.3"
t.Setenv("SENTRY_RELEASE", releaseVersion)

assertEqual(t, defaultRelease(), releaseVersion)
}

func TestDefaultReleaseSentryReleaseEnvvarPrecedence(t *testing.T) {
releaseVersion := "1.2.3"
t.Setenv("SOURCE_VERSION", "3.2.1")
t.Setenv("SENTRY_RELEASE", releaseVersion)

assertEqual(t, defaultRelease(), releaseVersion)
}

func TestRevisionFromBuildInfo(t *testing.T) {
releaseVersion := "deadbeef"

info := &debug.BuildInfo{
Main: debug.Module{
Path: "my/module",
Version: "(devel)",
},
Deps: []*debug.Module{
{
Path: "github.com/getsentry/sentry-go",
Version: "v0.23.1",
Replace: &debug.Module{
Path: "pkg/sentry",
},
},
},
Settings: []debug.BuildSetting{
{
Key: "vcs.revision",
Value: releaseVersion,
},
},
}

assertEqual(t, revisionFromBuildInfo(info), releaseVersion)
}

func TestRevisionFromBuildInfoNoVcsInformation(t *testing.T) {
info := &debug.BuildInfo{
Main: debug.Module{
Path: "my/module",
Version: "(devel)",
},
Deps: []*debug.Module{
{
Path: "github.com/getsentry/sentry-go",
Version: "v0.23.1",
Replace: &debug.Module{
Path: "pkg/sentry",
},
},
},
}

assertEqual(t, revisionFromBuildInfo(info), "")
}