Skip to content

Commit

Permalink
Convert build number to int
Browse files Browse the repository at this point in the history
  • Loading branch information
jradtilbrook committed May 6, 2024
1 parent 7ee4829 commit 7d60c16
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions internal/build/resolver/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"regexp"
"strconv"

"github.com/buildkite/cli/v3/internal/build"
)
Expand All @@ -13,21 +14,31 @@ func ResolveFromURL(args []string) BuildResolverFn {
if len(args) != 1 {
return nil, fmt.Errorf("Incorrect number of arguments, expected 1, got %d", len(args))
}
resolvedURL := splitBuildURL(args[0])
resolvedBuild := splitBuildURL(args[0])

org, name, buildNumber := resolvedURL.Organization, resolvedURL.Pipeline, resolvedURL.BuildNumber
if resolvedBuild == nil {
return nil, fmt.Errorf("Unable to resolve build from URL: %s", args[0])
}

return &build.Build{Pipeline: name, Organization: org, BuildNumber: buildNumber}, nil
return resolvedBuild, nil
}
}

func splitBuildURL(url string) build.Build {
func splitBuildURL(url string) *build.Build {
re := regexp.MustCompile(`https://buildkite.com/([^/]+)/([^/]+)/builds/(\d+)$`)
matches := re.FindStringSubmatch(url)
if matches == nil || len(matches) != 4 {
return nil
}

num, err := strconv.Atoi(matches[3])
if err != nil {
return nil
}

return build.Build{
return &build.Build{
Organization: matches[1],
Pipeline: matches[2],
BuildNumber: matches[3],
BuildNumber: num,
}
}

0 comments on commit 7d60c16

Please sign in to comment.