Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mageutils:feature - add git basic operations and cosignKeyWriter #132

Merged
merged 1 commit into from
Nov 18, 2021
Merged
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
63 changes: 63 additions & 0 deletions pkg/utils/mageutils/cosign.go
Original file line number Diff line number Diff line change
@@ -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
}
104 changes: 104 additions & 0 deletions pkg/utils/mageutils/release.go
Original file line number Diff line number Diff line change
@@ -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")
}
File renamed without changes.
1 change: 1 addition & 0 deletions scripts/coverage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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%\%}
Expand Down