diff --git a/go.mod b/go.mod index 277ab804..371411fb 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 c48c5a3c..e6cb42f7 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 00000000..eb0aee49 --- /dev/null +++ b/pkg/utils/mageutils/cosign.go @@ -0,0 +1,46 @@ +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")) + if err != nil { + return err + } + return nil +} + +func isCosignInstalled() error { + if err := sh.RunV("cosign", "version"); err != nil { + return err + } + return nil +} +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 00000000..f526f726 --- /dev/null +++ b/pkg/utils/mageutils/release.go @@ -0,0 +1,95 @@ +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) + if err := sh.RunV("git", "tag", "-f", "-s", "v0.0.0-alpha", "-m", "v0.0.0-alpha"); err != nil { + return err + } + return nil +} + +// GitPushAlpha executes "git", "push", "origin", "-f", "v0.0.0-alpha" +func GitPushAlpha() error { + mg.Deps(isGitExistent) + if err := sh.RunV("git", "push", "origin", "-f", "v0.0.0-alpha"); err != nil { + return err + } + return nil +} + +// CreateLocalTag executes "git", "tag", "-s", tag, "-m", "release "+tag +func CreateLocalTag(tag string) (err error) { + mg.Deps(isGitExistent) + if err := sh.RunV("git", "tag", "-s", tag, "-m", "release "+tag); err != nil { + return err + } + return nil +} + +// 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]) + if err := sh.RunV("git", "checkout", "-b", branchName); err != nil { + return err + } + } + 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 + } + if err := sh.RunV("git", "push", "--tags"); err != nil { + return err + } + return nil +} + +// 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 + } + if err := sh.RunV("git", "config", "--global", "user.name", name); err != nil { + return err + } + return nil +} + +// 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 { + if err := sh.RunV("git", "version"); err != nil { + return err + } + return nil +} diff --git a/pkg/utils/mageutils/magefile.go b/pkg/utils/mageutils/version.go similarity index 99% rename from pkg/utils/mageutils/magefile.go rename to pkg/utils/mageutils/version.go index 198e6ff0..b2a2187d 100644 --- a/pkg/utils/mageutils/magefile.go +++ b/pkg/utils/mageutils/version.go @@ -173,7 +173,6 @@ func (u *upVersions) listTags() ([]*github.RepositoryTag, error) { Page: 1, PerPage: 50, } - tags, resp, err := u.githubClient.Repositories.ListTags(u.ctx, u.githubOrg, u.githubRepo, listOptions) if github.CheckResponse(resp.Response) != nil { return nil, err 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