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

use new build version feature #397

Merged
merged 10 commits into from
Feb 20, 2025
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 .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
go-version-file: go.mod

- name: Build
run: go build -v ./...
run: go version && go build -v ./...

- name: Test
# -count=2 ensures that test fixtures cleanup after themselves
Expand Down
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ Use any of the following for a pain-free installation:
```
The benefit of this method is that re-running the command will always update to the latest version.
* You can download a pre-built binary from the [releases] page.
* If you have the [`gh`][gh] command available, you can install the latest release
of `dependabot` using the following command ([gist source](https://gist.github.com/mattt/e09e1ecd76d5573e0517a7622009f06f)):
```shell
gh gist view --raw e09e1ecd76d5573e0517a7622009f06f | bash
```
* On Mac, you can run `brew install dependabot`

## Requirements

Expand Down
4 changes: 2 additions & 2 deletions cmd/dependabot/dependabot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func TestDependabot(t *testing.T) {
err := exec.Command("go", "build", "dependabot.go").Run()
err := exec.Command("go", "build", ".").Run()
Copy link
Member Author

Choose a reason for hiding this comment

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

When you build with a file, the version info isn't injected.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, I remember hitting something like this at a prior employer. It was a royal pain to workaround.

if err != nil {
t.Fatal("failed to build dependabot")
}
Expand All @@ -24,7 +24,7 @@ func TestDependabot(t *testing.T) {
engine := &script.Engine{
Conds: scripttest.DefaultConds(),
Cmds: Commands(),
Quiet: !testing.Verbose(),
Quiet: false,
Copy link
Member Author

Choose a reason for hiding this comment

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

Having it always output the failing test is helpful, making it permanent.

}
env := []string{
"PATH=" + os.Getenv("PATH"),
Expand Down
32 changes: 7 additions & 25 deletions cmd/dependabot/internal/cmd/version.go
Original file line number Diff line number Diff line change
@@ -1,42 +1,24 @@
package cmd

import (
"fmt"
"regexp"
"log"
"runtime/debug"
)

// ldflags inserts the version here on release
var version string

var timestampRegex = regexp.MustCompile("[^a-zA-Z0-9]+")

func Version() string {
if version == "" {
version = "0.0.0-dev"
commit := ""
timestamp := ""
modified := false

info, _ := debug.ReadBuildInfo()
for _, entry := range info.Settings {
if entry.Key == "vcs.revision" && len(entry.Value) >= 7 {
commit = entry.Value[:7] // short ref
}

if entry.Key == "vcs.modified" {
modified = entry.Value == "true"
}

if entry.Key == "vcs.time" {
timestamp = timestampRegex.ReplaceAllString(entry.Value, "")
}
info, ok := debug.ReadBuildInfo()
if !ok {
log.Println("debug.ReadBuildInfo failed")
return version
}

if modified && timestamp != "" {
return fmt.Sprintf("%s+%s", version, timestamp)
} else if commit != "" {
return fmt.Sprintf("%s+%s", version, commit)
if info.Main.Version != "" {
version = info.Main.Version
}
}

Expand Down
5 changes: 5 additions & 0 deletions testdata/scripts/version.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Run the dependabot command
dependabot --version

# example: dependabot version v1.57.1-0.20250218140044-2f3fdc69ebfd
stdout 'dependabot version v\d+.\d+.\d+-(\d+\.)?\d{14}-[a-f0-9]{12}'