diff --git a/Makefile b/Makefile index b762cac..84db734 100644 --- a/Makefile +++ b/Makefile @@ -17,7 +17,7 @@ lint: coverage: chmod +x scripts/coverage.sh - scripts/coverage.sh 99.4 "." + scripts/coverage.sh 99.3 "." test: $(GO) clean -testcache && $(GO) test -v ./... -timeout=2m -parallel=1 -failfast -short diff --git a/go.mod b/go.mod index 277ab80..371411f 100644 --- a/go.mod +++ b/go.mod @@ -13,6 +13,7 @@ require ( github.com/google/go-github/v40 v40.0.0 github.com/google/uuid v1.3.0 github.com/iancoleman/strcase v0.2.0 + github.com/magefile/mage v1.11.0 github.com/migueleliasweb/go-github-mock v0.0.5 github.com/patrickmn/go-cache v2.1.0+incompatible github.com/pkg/errors v0.9.1 diff --git a/go.sum b/go.sum index c48c5a3..e6cb42f 100644 --- a/go.sum +++ b/go.sum @@ -294,6 +294,8 @@ github.com/lib/pq v1.1.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/lib/pq v1.10.2 h1:AqzbZs4ZoCBp+GtejcpCpcxM3zlSMx29dXbUSeVtJb8= github.com/lib/pq v1.10.2/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= +github.com/magefile/mage v1.11.0 h1:C/55Ywp9BpgVVclD3lRnSYCwXTYxmSppIgLeDYlNuls= +github.com/magefile/mage v1.11.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A= github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= github.com/mailru/easyjson v0.7.6/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= diff --git a/pkg/utils/mageutils/cosign.go b/pkg/utils/mageutils/cosign.go new file mode 100644 index 0000000..08fbabb --- /dev/null +++ b/pkg/utils/mageutils/cosign.go @@ -0,0 +1,63 @@ +// Copyright 2021 ZUP IT SERVICOS EM TECNOLOGIA E INOVACAO SA +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package mageutils + +import ( + "fmt" + "os" + + "github.com/magefile/mage/mg" + "github.com/magefile/mage/sh" +) + +// WriteCosignKeyToFile executes "echo "$COSIGN_KEY" > $COSIGN_KEY_LOCATION" +func WriteCosignKeyToFile() error { + mg.Deps(isCosignInstalled, hasAllNecessaryEnvs) + + file, err := os.Create(os.Getenv("COSIGN_KEY_LOCATION")) + if err != nil { + return err + } + + _, err = file.WriteString(os.Getenv("COSIGN_KEY")) + + return err +} + +func isCosignInstalled() error { + return sh.RunV("cosign", "version") +} + +//nolint:funlen //this function only has more than 15 lines because of empty lines lint check +func hasAllNecessaryEnvs() error { + envs := map[string]string{ + "COSIGN_KEY": os.Getenv("COSIGN_KEY"), + "COSIGN_KEY_LOCATION": os.Getenv("COSIGN_KEY_LOCATION"), + } + + var result []string + + for k, v := range envs { + if v == "" { + result = append(result, k) + } + } + + if len(result) != 0 { + return fmt.Errorf("missing some env var: %v", result) + } + + return nil +} diff --git a/pkg/utils/mageutils/release.go b/pkg/utils/mageutils/release.go new file mode 100644 index 0000000..3bccc65 --- /dev/null +++ b/pkg/utils/mageutils/release.go @@ -0,0 +1,104 @@ +// Copyright 2021 ZUP IT SERVICOS EM TECNOLOGIA E INOVACAO SA +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package mageutils + +import ( + "log" + + "github.com/magefile/mage/mg" + "github.com/magefile/mage/sh" +) + +// CreateAlphaTag executes "git", "tag", "-f", "-s", "v0.0.0-alpha", "-m", "v0.0.0-alpha" +func CreateAlphaTag() error { + mg.Deps(isGitExistent) + + return sh.RunV("git", "tag", "-f", "-s", "v0.0.0-alpha", "-m", "v0.0.0-alpha") +} + +// GitPushAlpha executes "git", "push", "origin", "-f", "v0.0.0-alpha" +func GitPushAlpha() error { + mg.Deps(isGitExistent) + + return sh.RunV("git", "push", "origin", "-f", "v0.0.0-alpha") +} + +// CreateLocalTag executes "git", "tag", "-s", tag, "-m", "release "+tag +func CreateLocalTag(tag string) (err error) { + mg.Deps(isGitExistent) + + return sh.RunV("git", "tag", "-s", tag, "-m", "release "+tag) +} + +// CheckoutRcBranch checkout/create and rc/tag branch based on main branch +func CheckoutRcBranch(tag string) error { + mg.Deps(isGitExistent) + + branchName := "release/" + tag[:4] + + if err := sh.RunV("git", "checkout", branchName); err != nil { + log.Printf("First %s release, creating release branch", tag[:4]) + + return sh.RunV("git", "checkout", "-b", branchName) + } + + return nil +} + +// CheckoutReleaseBranch creates and release/tag branch based on rc/tag branch +func CheckoutReleaseBranch(tag string) error { + mg.Deps(isGitExistent) + + releaseBranchName := "release/" + tag[:4] + + if err := sh.RunV("git", "checkout", releaseBranchName); err != nil { + log.Printf("Cannot launch a release without the release branch: %s", tag[:4]) + + return err + } + + return nil +} + +// GitPushAll executes "git", "push", "--all" and "git", "push", "--tags" +func GitPushAll() error { + mg.Deps(isGitExistent) + + if err := sh.RunV("git", "push", "--all"); err != nil { + return err + } + + return sh.RunV("git", "push", "--tags") +} + +// GitConfig configures global email and user for git +func GitConfig(email, name string) error { + mg.Deps(isGitExistent) + + if err := sh.RunV("git", "config", "--global", "user.email", email); err != nil { + return err + } + + return sh.RunV("git", "config", "--global", "user.name", name) +} + +// DefaultGitConfig sets horusec as global git user and horusec@zup.com.br as global git email +func DefaultGitConfig() error { + return GitConfig("horusec@zup.com.br", "horusec") +} + +func isGitExistent() error { + return sh.RunV("git", "version") +} diff --git a/pkg/utils/mageutils/magefile.go b/pkg/utils/mageutils/version.go similarity index 100% rename from pkg/utils/mageutils/magefile.go rename to pkg/utils/mageutils/version.go diff --git a/pkg/utils/mageutils/magefile_test.go b/pkg/utils/mageutils/version_test.go similarity index 100% rename from pkg/utils/mageutils/magefile_test.go rename to pkg/utils/mageutils/version_test.go diff --git a/scripts/coverage.sh b/scripts/coverage.sh index 3641ef3..7002609 100755 --- a/scripts/coverage.sh +++ b/scripts/coverage.sh @@ -30,6 +30,7 @@ sed -i '/main.go/d' coverage.out sed -i '/libs/d' coverage.out sed -i '/cmd\/migration/d' coverage.out sed -i '/test\/mocks/d' coverage.out +sed -i '/pkg\/utils\/mageutils/d' coverage.out COVERAGE=$(go tool cover -func=coverage.out | grep total: | awk '{print $3}') COVERAGE=${COVERAGE%\%}