Skip to content

Commit

Permalink
sort tags in desc order, add ability to process sub-actions
Browse files Browse the repository at this point in the history
  • Loading branch information
amenocal committed Dec 14, 2023
1 parent 1396aee commit e1cacc3
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Currently supported formats:

- `<repo>/<action>@<version>`
- `<repos>/<action>@<branchName>`
- `<repo>/<action>/sub-action@<version>`
- `<repo>/<action>/sub-action@<branchName>`

## Installation

Expand Down
3 changes: 2 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func GetActionHashByVersion(repository string, version string) (string, string,
var tagVersion string
var std_err bytes.Buffer
var err error

repository = pkg.ExtractOwnerRepo(repository)
// Remove 'v' from the version string if sent through command line
version = strings.TrimPrefix(version, "v")
// Check to see if value received is latest version or a specific version
Expand Down Expand Up @@ -159,6 +159,7 @@ func GetLatestPatchVersion(repository string, version string) (string, error) {
}

func GetBranchHash(repository string, branch string) (string, error) {
repository = pkg.ExtractOwnerRepo(repository)
cliArgs := ".commit.sha"
cliOptions := fmt.Sprintf("repos/%s/branches/%s", repository, branch)
shaCommit, std_err, err := gh.Exec("api", cliOptions, "--jq", cliArgs)
Expand Down
10 changes: 10 additions & 0 deletions pkg/action_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,13 @@ func SplitActionString(action string, delimiter string) (string, string, error)
branchOrVersion := actionSplit[1]
return repoWithOwner, branchOrVersion, nil
}

func ExtractOwnerRepo(repository string) string {
if strings.Count(repository, "/") > 1 {
parts := strings.Split(repository, "/")
if len(parts) > 2 {
repository = parts[0] + "/" + parts[1]
}
}
return repository
}
19 changes: 19 additions & 0 deletions pkg/action_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,22 @@ func TestSplitActionString(t *testing.T) {
}
}
}
func TestExtractOwnerRepo(t *testing.T) {
tests := []struct {
repository string
expected string
}{
{"owner/repo", "owner/repo"},
{"owner/repo/sub", "owner/repo"},
{"owner/repo/sub/sub", "owner/repo"},
{"repo", "repo"},
{"", ""},
}

for _, test := range tests {
result := ExtractOwnerRepo(test.repository)
if result != test.expected {
t.Errorf("Unexpected result for repository %s: got %s, want %s", test.repository, result, test.expected)
}
}
}
18 changes: 16 additions & 2 deletions pkg/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pkg

import (
"fmt"
"sort"
"strconv"
"strings"
)
Expand Down Expand Up @@ -39,6 +40,17 @@ func ParseSemver(version string) (Semver, error) {

func FindHighestPatchVersion(tags []string, version string) (string, error) {
var semverVersion Semver
// Remove the last element if it's an empty string
if tags[len(tags)-1] == "" {
tags = tags[:len(tags)-1]
}

//sort tags in descending order
sort.Strings(tags)
sort.Slice(tags, func(i, j int) bool {
return tags[i] > tags[j]
})

for _, tag := range tags {

tagVersion, err := ParseSemver(tag)
Expand All @@ -48,12 +60,14 @@ func FindHighestPatchVersion(tags []string, version string) (string, error) {

if strings.Contains(version, ".") {
requestedMajorMinor := fmt.Sprintf("%d.%d", tagVersion.Major, tagVersion.Minor)
if requestedMajorMinor == version && tagVersion.Patch >= semverVersion.Patch {
if requestedMajorMinor == version {
semverVersion = tagVersion
break
}
} else {
if fmt.Sprintf("%d", tagVersion.Major) == version && tagVersion.Minor >= semverVersion.Minor {
if fmt.Sprintf("%d", tagVersion.Major) == version {
semverVersion = tagVersion
break
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestParseSemver(t *testing.T) {
}

func TestFindHighestPatchVersion(t *testing.T) {
tags := []string{"v1.2.3", "v2.0.0", "v1.0.0-alpha", "v1.2", "v1.3.1", "v3.5.0", "v3.4.0"}
tags := []string{"v1.2.3", "v2.0.0", "v1.0.0-alpha", "v1.2", "v1.3.1", "v3.5.0", "v3.4.0", "v3.5.2", "v3.5.1"}

tests := []struct {
version string
Expand All @@ -41,7 +41,7 @@ func TestFindHighestPatchVersion(t *testing.T) {
{"2", "v2.0.0"},
{"1.3", "v1.3.1"},
{"1", "v1.3.1"},
{"3", "v3.5.0"},
{"3", "v3.5.2"},
}

for _, test := range tests {
Expand Down

0 comments on commit e1cacc3

Please sign in to comment.