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

Commit

Permalink
feat: tag with latest
Browse files Browse the repository at this point in the history
This new input variable is a boolean, however it accepts 3 different values:
false, true or not present. If present it will always take precendence over
the tag with ref input variable.

Signed-off-by: Felipe Santos <felipecassiors@gmail.com>
  • Loading branch information
felipecrs committed Jun 19, 2020
1 parent 0f18e2a commit 2071a1b
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 11 deletions.
9 changes: 9 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=827846 to learn about workspace recommendations.
// Extension identifier format: ${publisher}.${name}. Example: vscode.csharp

// List of extensions which should be recommended for users of this workspace.
"recommendations": ["golang.go"],
// List of extensions recommended by VS Code that should not be recommended for users of this workspace.
"unwantedRecommendations": []
}
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

0 comments on commit 2071a1b

Please sign in to comment.