-
-
Notifications
You must be signed in to change notification settings - Fork 629
feat: Add build information #1443
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
Open
PythonGermany
wants to merge
25
commits into
TwiN:master
Choose a base branch
from
PythonGermany:add-build-information
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 12 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
9397efe
feat: Add build information
PythonGermany 3b14c65
refactor: Rename buildinfo package
PythonGermany db16de0
refactor: Make build info names consistent
PythonGermany 42d6c32
feat(metrics): Expose build information
PythonGermany 4834898
docs: Add build info
PythonGermany a97547d
fix: Only set BuildVersion if showing it in the UI is enabled
PythonGermany fca1c0e
test(buildinfo): Add default test
PythonGermany 5419779
fix: Hide tag not found error output
PythonGermany 5e45497
ci: Use tag as version build arg
PythonGermany a11ca1a
chore: Remove release link to keep it simple
PythonGermany 15d1733
chore(ui): Regenerate static assets
PythonGermany 73d4c3d
feat: Use embedded build info
PythonGermany a7126d6
fix: Get commit timestamp correctly
PythonGermany 3a41a74
refactor: Rename buildinfo date to revision date
PythonGermany e802f99
docs: Add comment explaining special default case
PythonGermany a2a657b
Merge remote-tracking branch 'TwiN/master' into add-build-information
PythonGermany 0bc9375
refactor(ui): Rename ShowBuildInfo to ShowVersion
PythonGermany 12e2601
chore(ui): Change ShowVersion default to false
PythonGermany 4743298
test: Add missing config parameters
PythonGermany 3776343
fix(docker): Outdated parameter name
PythonGermany 12d5d3a
refactor(ui): Simplify show version condition
PythonGermany c62b2d5
Merge remote-tracking branch 'TwiN/master' into add-build-information
PythonGermany 5843575
chore(ui): Regenerate static assets
PythonGermany 2291d8d
Merge branch 'master' into add-build-information
TwiN 9b96014
Merge remote-tracking branch 'TwiN/master' into add-build-information
PythonGermany File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| package buildinfo | ||
|
|
||
| import ( | ||
| "runtime" | ||
| "runtime/debug" | ||
| ) | ||
|
|
||
| const ( | ||
| defaultVersion = "development" | ||
| defaultRevision = "unknown" | ||
| defaultDate = "unknown" | ||
| ) | ||
|
|
||
| var ( | ||
| version string | ||
| revision string | ||
| date string | ||
|
|
||
| buildInfo = SetBuildInfo(GetDefault()) | ||
| ) | ||
|
|
||
| type BuildInfo struct { | ||
| Version string | ||
| Revision string | ||
| Date string | ||
| GoVersion string | ||
| Dirty bool | ||
| } | ||
|
|
||
| func SetEmbedded(info *BuildInfo) { | ||
| rawInfo, ok := debug.ReadBuildInfo() | ||
| if !ok { | ||
| info.Revision += "-buildinfo-err" | ||
| } | ||
| for _, s := range rawInfo.Settings { | ||
| switch s.Key { | ||
| case "vcs.revision": | ||
| info.Revision = s.Value | ||
| case "vcs.time": | ||
| info.Date = s.Value | ||
| case "vcs.modified": | ||
| info.Dirty = s.Value == "true" | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func SetLdflags(info *BuildInfo) { | ||
| if len(version) > 0 { | ||
| info.Version = version | ||
| } | ||
| if len(revision) > 0 { | ||
| info.Revision = revision | ||
| } | ||
| if len(date) > 0 { | ||
| info.Date = date | ||
| } | ||
| if len(info.GoVersion) == 0 { | ||
| info.GoVersion = runtime.Version() | ||
| } | ||
| } | ||
|
|
||
| func GetDefault() BuildInfo { | ||
| return BuildInfo{ | ||
| Version: defaultVersion, | ||
| Revision: defaultRevision, | ||
| Date: defaultDate, | ||
| GoVersion: runtime.Version(), | ||
| Dirty: false, | ||
| } | ||
| } | ||
|
|
||
| func SetBuildInfo(info BuildInfo) BuildInfo { | ||
| SetEmbedded(&info) | ||
| SetLdflags(&info) | ||
|
|
||
| if info.Dirty { | ||
| info.Revision += "-dirty" | ||
| } | ||
| return info | ||
| } | ||
|
|
||
| func Get() BuildInfo { | ||
| return buildInfo | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| package buildinfo | ||
|
|
||
| import ( | ||
| "runtime" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestBuildInfo_GetDefaults(t *testing.T) { | ||
| info := GetDefault() | ||
| if info.Version != defaultVersion { | ||
| t.Errorf("Expected default Version '%s', got '%s'", defaultVersion, info.Version) | ||
| } | ||
| if info.Revision != defaultRevision { | ||
| t.Errorf("Expected default Revision '%s', got '%s'", defaultRevision, info.Revision) | ||
| } | ||
| if info.Date != defaultDate { | ||
| t.Errorf("Expected default Date '%s', got '%s'", defaultDate, info.Date) | ||
| } | ||
| if info.GoVersion != runtime.Version() { | ||
| t.Errorf("Expected GoVersion '%s', got '%s'", runtime.Version(), info.GoVersion) | ||
| } | ||
| if info.Dirty != false { | ||
| t.Errorf("Expected Dirty 'false', got '%v'", info.Dirty) | ||
| } | ||
| } | ||
|
|
||
| func TestBuildInfo_SetLdflags(t *testing.T) { | ||
| t.Run("NoLdflags", func(t *testing.T) { | ||
| info := GetDefault() | ||
| SetLdflags(&info) | ||
|
|
||
| // Since we are not setting ldflags during the test, the values largely remain defaults | ||
| if info.Version != defaultVersion { | ||
| t.Errorf("Expected Version '%s', got '%s'", defaultVersion, info.Version) | ||
| } | ||
| if info.Revision != defaultRevision { | ||
| t.Errorf("Expected Revision '%s', got '%s'", defaultRevision, info.Revision) | ||
| } | ||
| if info.Date != defaultDate { | ||
| t.Errorf("Expected Date '%s', got '%s'", defaultDate, info.Date) | ||
| } | ||
| if info.GoVersion != runtime.Version() { | ||
| t.Errorf("Expected GoVersion '%s', got '%s'", runtime.Version(), info.GoVersion) | ||
| } | ||
| if info.Dirty != false { | ||
| t.Errorf("Expected Dirty 'false', got '%v'", info.Dirty) | ||
| } | ||
| }) | ||
| t.Run("WithLdflags", func(t *testing.T) { | ||
| // Simulate ldflags being set | ||
| originalVersion := version | ||
| originalRevision := revision | ||
| originalDate := date | ||
| defer func() { | ||
| version = originalVersion | ||
| revision = originalRevision | ||
| date = originalDate | ||
| }() | ||
| version = "test-version" | ||
| revision = "test-revision" | ||
| date = "test-date" | ||
|
|
||
| info := GetDefault() | ||
| SetLdflags(&info) | ||
|
|
||
| if info.Version != "test-version" { | ||
| t.Errorf("Expected Version 'test-version', got '%s'", info.Version) | ||
| } | ||
| if info.Revision != "test-revision" { | ||
| t.Errorf("Expected Revision 'test-revision', got '%s'", info.Revision) | ||
| } | ||
| if info.Date != "test-date" { | ||
| t.Errorf("Expected Date ''test-date', got '%s'", info.Date) | ||
| } | ||
| if info.GoVersion != runtime.Version() { | ||
| t.Errorf("Expected GoVersion '%s', got '%s'", runtime.Version(), info.GoVersion) | ||
| } | ||
| if info.Dirty != false { | ||
| t.Errorf("Expected Dirty 'false', got '%v'", info.Dirty) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| func TestBuildInfo_SetEmbedded(t *testing.T) { | ||
| info := GetDefault() | ||
| SetEmbedded(&info) | ||
|
|
||
| if info.Version != defaultVersion { | ||
| t.Errorf("Expected Version '%s', got '%s'", defaultVersion, info.Version) | ||
| } | ||
| if info.Revision != defaultRevision { | ||
| t.Errorf("Expected Revision '%s', got '%s'", defaultRevision, info.Revision) | ||
| } | ||
| if info.Date != defaultDate { | ||
| t.Errorf("Expected Date '%s', got '%s'", defaultDate, info.Date) | ||
| } | ||
| if info.GoVersion != runtime.Version() { | ||
| t.Errorf("Expected GoVersion '%s', got '%s'", runtime.Version(), info.GoVersion) | ||
| } | ||
| if info.Dirty != false { | ||
| t.Errorf("Expected Dirty 'false', got '%v'", info.Dirty) | ||
| } | ||
| } | ||
|
|
||
| func TestBuildInfo_SetBuildInfo(t *testing.T) { | ||
| t.Run("TestEmbeddOverwriteLdflags", func(t *testing.T) { | ||
| // Only change one ldflag at a time to test their effects | ||
| originalVersion := version | ||
| defer func() { | ||
| version = originalVersion | ||
| }() | ||
| version = "test-version" | ||
|
|
||
| defaultInfo := GetDefault() | ||
| info := SetBuildInfo(defaultInfo) | ||
| if info.Version != "test-version" { | ||
| t.Errorf("Expected Version 'test-version', got '%s'", info.Version) | ||
| } | ||
| if info.Revision != defaultRevision { | ||
| t.Errorf("Expected Revision '%s', got '%s'", defaultRevision, info.Revision) | ||
| } | ||
| if info.Date != defaultDate { | ||
| t.Errorf("Expected Date '%s', got '%s'", defaultDate, info.Date) | ||
| } | ||
| if info.GoVersion != runtime.Version() { | ||
| t.Errorf("Expected GoVersion '%s', got '%s'", runtime.Version(), info.GoVersion) | ||
| } | ||
| if info.Dirty != false { | ||
| t.Errorf("Expected Dirty 'false', got '%v'", info.Dirty) | ||
| } | ||
|
|
||
| // Change another ldflag | ||
| originalRevision := revision | ||
| defer func() { | ||
| revision = originalRevision | ||
| }() | ||
| revision = "test-rev" | ||
|
|
||
| info = SetBuildInfo(GetDefault()) | ||
| if info.Revision != "test-rev" { | ||
| t.Errorf("Expected Revision 'test-rev', got '%s'", info.Revision) | ||
| } | ||
|
|
||
| // Change the last ldflag | ||
| originalDate := date | ||
| defer func() { | ||
| date = originalDate | ||
| }() | ||
| date = "test-date" | ||
|
|
||
| info = SetBuildInfo(defaultInfo) | ||
| if info.Date != "test-date" { | ||
| t.Errorf("Expected Date 'test-date', got '%s'", info.Date) | ||
| } | ||
| }) | ||
| t.Run("TestDirtyFlag", func(t *testing.T) { | ||
| info := GetDefault() | ||
| info.Dirty = true // Simulate the embedded info setting Dirty to true | ||
| info = SetBuildInfo(info) | ||
| if info.Dirty != true { | ||
| t.Errorf("Expected Dirty 'true', got '%v'", info.Dirty) | ||
| } | ||
| if info.Revision != defaultRevision+"-dirty" { | ||
| t.Errorf("Expected Revision '%s-dirty', got '%s'", defaultRevision, info.Revision) | ||
| } | ||
| }) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.