Skip to content

Commit

Permalink
mageutils: feature - adding basic git commands and cosignWriteFile
Browse files Browse the repository at this point in the history
Signed-off-by: Ian Cardoso <ian.cardoso@zup.com.br>
  • Loading branch information
iancardosozup committed Nov 17, 2021
1 parent 8071a05 commit 72d5833
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 1 deletion.
4 changes: 4 additions & 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 All @@ -34,13 +35,15 @@ require (
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bmatcuk/doublestar/v4 v4.0.2 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.19.6 // indirect
github.com/go-openapi/spec v0.20.3 // indirect
github.com/go-openapi/swag v0.19.15 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/addlicense v1.0.0 // indirect
github.com/google/go-github/v37 v37.0.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/gorilla/mux v1.8.0 // indirect
Expand All @@ -65,6 +68,7 @@ require (
github.com/swaggo/files v0.0.0-20210815190702-a29dd2bc99b2 // indirect
github.com/swaggo/swag v1.7.3 // indirect
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/tools v0.1.0 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/bmatcuk/doublestar/v4 v4.0.2 h1:X0krlUVAVmtr2cRoTqR8aDMrDqnB36ht8wpWTiQ3jsA=
github.com/bmatcuk/doublestar/v4 v4.0.2/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc=
github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
Expand Down Expand Up @@ -166,6 +168,8 @@ github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/addlicense v1.0.0 h1:cqvo5suPWlsk6r6o42Fs2K66xYCl2tnhVPUYoP3EnO4=
github.com/google/addlicense v1.0.0/go.mod h1:Sm/DHu7Jk+T5miFHHehdIjbi4M5+dJDRS3Cq0rncIxA=
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
Expand Down Expand Up @@ -294,6 +298,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 Expand Up @@ -516,6 +522,8 @@ golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down
60 changes: 60 additions & 0 deletions pkg/utils/mageutils/cosign.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// 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"))
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
}
109 changes: 109 additions & 0 deletions pkg/utils/mageutils/release.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// 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)
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
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// 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 (
Expand Down Expand Up @@ -173,7 +187,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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// 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 (
Expand Down

0 comments on commit 72d5833

Please sign in to comment.