Skip to content
This repository has been archived by the owner on Jun 22, 2021. It is now read-only.

feat: tag with latest #20

Closed
wants to merge 2 commits into from
Closed
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
3 changes: 3 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"recommendations": ["golang.go"]
}
13 changes: 13 additions & 0 deletions internal/options/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,16 @@ func readBoolOption(key string) (bool, error) {
}
return strconv.ParseBool(o)
}

// OptionalBool enum
type OptionalBool int

const (
notPresent OptionalBool = iota
presentAndFalse
presentAndTrue
)

func (d OptionalBool) String() string {
return [...]string{"notPresent", "presentAndFalse", "presentAndTrue"}[d]
}
28 changes: 25 additions & 3 deletions internal/options/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (
)

var (
errTagWithRefParse = errors.New("tag_with_ref input must be a valid boolean value")
errTagWithShaParse = errors.New("tag_with_sha input must be a valid boolean value")
errTagWithRefParse = errors.New("tag_with_ref input must be a valid boolean value")
errTagWithShaParse = errors.New("tag_with_sha input must be a valid boolean value")
errTagWithLatestParse = errors.New("tag_with_latest input must be a valid boolean value")
)

func tagWithRef() (bool, error) {
Expand All @@ -28,6 +29,21 @@ func tagWithSha() (bool, error) {
return b, nil
}

func tagWithLatest() (OptionalBool, error) {
var key = "INPUT_TAG_WITH_LATEST"
if _, present := os.LookupEnv(key); !present {
return notPresent, nil
}
b, err := readBoolOption(key)
if err != nil {
return notPresent, errTagWithLatestParse
}
if b {
return presentAndTrue, nil
}
return presentAndFalse, nil
}

func dockerRepo(github GitHub) string {
if repo := os.Getenv("INPUT_REPOSITORY"); repo != "" {
return repo
Expand Down Expand Up @@ -57,12 +73,18 @@ func GetTags(registry string, github GitHub) ([]string, error) {
for _, t := range staticTags() {
tags = append(tags, toFullTag(registry, repo, t))
}
withLatest, err := tagWithLatest()
if err != nil {
return nil, err
} else if withLatest == presentAndTrue {
tags = append(tags, toFullTag(registry, repo, "latest"))
}
if withRef, err := tagWithRef(); err != nil {
return nil, err
} else if withRef {
switch github.Reference.Type {
case GitRefHead:
if github.Reference.Name == "master" {
if withLatest == notPresent && github.Reference.Name == "master" {
tags = append(tags, toFullTag(registry, repo, "latest"))
} else {
tags = appendGitRefTag(tags, registry, repo, github.Reference.Name)
Expand Down
55 changes: 47 additions & 8 deletions internal/options/tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ import (

func TestGetTags(t *testing.T) {
testCases := []struct {
name string
tagWithRef bool
tagWithSha bool
tags string
ref GitReference
registry string
expected []string
sha string
name string
tagWithRef bool
tagWithSha bool
tagWithLatest OptionalBool
tags string
ref GitReference
registry string
expected []string
sha string
}{
{
name: "no-standard-tags",
Expand Down Expand Up @@ -46,6 +47,36 @@ func TestGetTags(t *testing.T) {
expected: []string{"my/repo:tag1", "my/repo:tag2", "my/repo:latest"},
ref: GitReference{GitRefHead, "master"},
},
{
name: "latest-true",
tags: "tag1,tag2",
tagWithLatest: presentAndTrue,
expected: []string{"my/repo:tag1", "my/repo:tag2", "my/repo:latest"},
ref: GitReference{GitRefHead, "master"},
},
{
name: "latest-false",
tags: "tag1,tag2",
tagWithLatest: presentAndFalse,
expected: []string{"my/repo:tag1", "my/repo:tag2"},
ref: GitReference{GitRefHead, "master"},
},
{
name: "master-branch-latest-true",
tagWithRef: true,
tags: "tag1,tag2",
tagWithLatest: presentAndTrue,
expected: []string{"my/repo:tag1", "my/repo:tag2", "my/repo:latest", "my/repo:master"},
ref: GitReference{GitRefHead, "master"},
},
{
name: "master-branch-latest-false",
tagWithRef: true,
tags: "tag1,tag2",
tagWithLatest: presentAndFalse,
expected: []string{"my/repo:tag1", "my/repo:tag2", "my/repo:master"},
ref: GitReference{GitRefHead, "master"},
},
{
name: "different-branch",
tagWithRef: true,
Expand Down Expand Up @@ -99,10 +130,18 @@ func TestGetTags(t *testing.T) {
defer os.Unsetenv("INPUT_REPOSITORY")
defer os.Unsetenv("INPUT_TAG_WITH_REF")
defer os.Unsetenv("INPUT_TAG_WITH_SHA")
defer os.Unsetenv("INPUT_TAG_WITH_LATEST")
_ = os.Setenv("INPUT_TAGS", tc.tags)
_ = os.Setenv("INPUT_REPOSITORY", "my/repo")
_ = os.Setenv("INPUT_TAG_WITH_REF", fmt.Sprint(tc.tagWithRef))
_ = os.Setenv("INPUT_TAG_WITH_SHA", fmt.Sprint(tc.tagWithSha))
if tc.tagWithLatest == presentAndTrue || tc.tagWithLatest == presentAndFalse {
if tc.tagWithLatest == presentAndTrue {
_ = os.Setenv("INPUT_TAG_WITH_LATEST", fmt.Sprint(true))
} else {
_ = os.Setenv("INPUT_TAG_WITH_LATEST", fmt.Sprint(false))
}
}

tags, err := GetTags(
tc.registry,
Expand Down