Skip to content

Commit

Permalink
Merge pull request #258 from buildtool/promote_message
Browse files Browse the repository at this point in the history
Change promote message
  • Loading branch information
peter-svensson authored Sep 21, 2022
2 parents 59c7908 + a25703b commit 39ba61e
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 34 deletions.
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ require (
sigs.k8s.io/yaml v1.3.0 // indirect
)

require k8s.io/cli-runtime v0.23.5
require (
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6
k8s.io/cli-runtime v0.23.5
)

require (
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1431,6 +1431,7 @@ golang.org/x/exp v0.0.0-20191129062945-2f5052295587/go.mod h1:2RIsYlXP63K8oxa1u0
golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6 h1:QE6XYQK6naiK1EPAe1g/ILLxN5RBoH5xkJk3CqlMI/Y=
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
Expand Down
12 changes: 10 additions & 2 deletions pkg/promote/promote.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
"golang.org/x/exp/utf8string"

"github.com/buildtool/build-tools/pkg/file"

Expand All @@ -32,6 +33,7 @@ type Args struct {
PrivateKey string `name:"key" help:"private key for Git access (defaults to ~/.ssh/id_rsa)" default:""`
Password string `name:"password" help:"password for private key" default:""`
Out string `name:"out" short:"o" help:"write output to specified file instead of committing and pushing to Git" default:""`
shortSha string
}

func DoPromote(dir string, info version.Info, osArgs ...string) int {
Expand Down Expand Up @@ -63,12 +65,19 @@ func DoPromote(dir string, info version.Info, osArgs ...string) int {
target.Path = "/"
}
currentCI := cfg.CurrentCI()
promoteArgs.shortSha = promoteArgs.Tag
if promoteArgs.Tag == "" {
if !ci.IsValid(currentCI) {
log.Errorf("Commit and/or branch information is <red>missing</red>. Perhaps your not in a Git repository or forgot to set environment variables?\n")
return -3
}
promoteArgs.Tag = currentCI.Commit()
shortSha := utf8string.NewString(promoteArgs.Tag)
if shortSha.RuneCount() > 7 {
promoteArgs.shortSha = shortSha.Slice(0, 7)
} else {
promoteArgs.shortSha = shortSha.String()
}
} else {
log.Infof("Using passed tag <green>%s</green> to promote\n", promoteArgs.Tag)
}
Expand Down Expand Up @@ -153,9 +162,8 @@ func commitAndPush(target *config.Gitops, keys *ssh.PublicKeys, name string, buf
if err != nil {
return err
}

hash, err := worktree.Commit(
fmt.Sprintf("ci: promoting %s commit %s to %s", normalized, args.Tag, args.Target),
fmt.Sprintf("ci: promoting %s to %s, commit %s", normalized, args.Target, args.shortSha),
&git.CommitOptions{
Author: &object.Signature{
Name: defaultIfEmpty(gitConfig.Name, "Buildtools"),
Expand Down
87 changes: 56 additions & 31 deletions pkg/promote/promote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ import (

func TestDoPromote(t *testing.T) {
tests := []struct {
name string
config string
descriptor string
args []string
env map[string]string
want int
wantLogged []string
wantCommits int
name string
config string
descriptor string
args []string
env map[string]string
want int
wantLogged []string
wantCommitMessage *string
}{
{
name: "invalid argument",
Expand Down Expand Up @@ -102,10 +102,10 @@ gitops:
name: "no deployment descriptors",
config: `
gitops:
dummy:
target:
url: "{{.repo}}"
`,
args: []string{"dummy"},
args: []string{"target"},
env: map[string]string{
"CI_COMMIT_SHA": "abc123",
"CI_PROJECT_NAME": "dummy",
Expand All @@ -121,7 +121,7 @@ git:
name: Some User
email: [email protected]
gitops:
dummy:
target:
url: "{{.repo}}"
`,
descriptor: `
Expand All @@ -132,9 +132,9 @@ metadata:
data:
BASE_URL: https://example.org
`,
args: []string{"dummy"},
args: []string{"target"},
env: map[string]string{
"CI_COMMIT_SHA": "abc123",
"CI_COMMIT_SHA": "abc12345678",
"CI_PROJECT_NAME": "dummy",
"CI_COMMIT_REF_NAME": "master",
},
Expand All @@ -143,7 +143,7 @@ data:
"info: generating...",
"^info: pushing commit [0-9a-f]+ to .*\n$",
},
wantCommits: 1,
wantCommitMessage: strPointer("ci: promoting dummy to target, commit abc1234"),
},
{
name: "build name is normalized",
Expand All @@ -152,7 +152,7 @@ git:
name: Some User
email: [email protected]
gitops:
dummy:
target:
url: "{{.repo}}"
`,
descriptor: `
Expand All @@ -163,7 +163,7 @@ metadata:
data:
BASE_URL: https://example.org
`,
args: []string{"dummy"},
args: []string{"target"},
env: map[string]string{
"CI_COMMIT_SHA": "abc123",
"CI_PROJECT_NAME": "dummy_repo",
Expand All @@ -174,13 +174,13 @@ data:
"info: generating...",
"^info: pushing commit [0-9a-f]+ to .*git-repo.*\\/dummy-repo\n$",
},
wantCommits: 1,
wantCommitMessage: strPointer("ci: promoting dummy-repo to target, commit abc123"),
},
{
name: "other repo, path and tag",
config: `
gitops:
dummy:
target:
url: "{{.repo}}"
`,
descriptor: `
Expand All @@ -191,7 +191,7 @@ metadata:
data:
BASE_URL: https://example.org
`,
args: []string{"dummy", "--url", "{{.other}}", "--path", "test/path", "--tag", "testing"},
args: []string{"target", "--url", "{{.other}}", "--path", "test/path", "--tag", "testing"},
env: map[string]string{
"CI_COMMIT_SHA": "abc123",
"CI_PROJECT_NAME": "dummy",
Expand All @@ -203,13 +203,12 @@ data:
"info: generating...\n",
"^info: pushing commit [0-9a-f]+ to .*other-repo.*\\/test\\/path/dummy\n$",
},
wantCommits: 0,
},
{
name: "other ssh key from config",
config: `
gitops:
dummy:
target:
url: "{{.repo}}"
git:
key: ~/other/id_rsa
Expand All @@ -222,24 +221,25 @@ metadata:
data:
BASE_URL: https://example.org
`,
args: []string{"dummy"},
args: []string{"target", "--tag", "providedlongtag"},
env: map[string]string{
"CI_COMMIT_SHA": "abc123",
"CI_PROJECT_NAME": "dummy",
"CI_COMMIT_REF_NAME": "master",
},
want: 0,
wantLogged: []string{
"info: Using passed tag <green>providedlongtag</green> to promote\n",
"info: generating...",
"^info: pushing commit [0-9a-f]+ to .*git-repo.*\\/dummy\n$",
},
wantCommits: 1,
wantCommitMessage: strPointer("ci: promoting dummy to target, commit providedlongtag"),
},
{
name: "clone error",
config: `
gitops:
dummy:
target:
url: "{{.repo}}"
`,
descriptor: `
Expand All @@ -250,7 +250,7 @@ metadata:
data:
BASE_URL: https://example.org
`,
args: []string{"dummy", "--url", "/missing/repo"},
args: []string{"target", "--url", "/missing/repo"},
env: map[string]string{
"CI_COMMIT_SHA": "abc123",
"CI_PROJECT_NAME": "dummy",
Expand All @@ -261,13 +261,12 @@ data:
"info: generating...",
"error: repository not found",
},
wantCommits: 0,
},
{
name: "missing SSH key",
config: `
gitops:
dummy:
target:
url: "{{.repo}}"
`,
descriptor: `
Expand All @@ -278,7 +277,7 @@ metadata:
data:
BASE_URL: https://example.org
`,
args: []string{"dummy", "--key", "/missing/key"},
args: []string{"target", "--key", "/missing/key"},
env: map[string]string{
"CI_COMMIT_SHA": "abc123",
"CI_PROJECT_NAME": "dummy",
Expand All @@ -289,7 +288,6 @@ data:
"info: generating...",
"error: ssh key: open /missing/key: no such file or directory",
},
wantCommits: 0,
},
}
for _, tt := range tests {
Expand Down Expand Up @@ -348,8 +346,12 @@ data:
}
CheckLogged(t, tt.wantLogged, logMock.Logged)

gotCommits := CountCommits(t, repo)
assert.Equal(t, tt.wantCommits, gotCommits)
commits := GetCommits(t, repo)
if tt.wantCommitMessage != nil {
assert.Equal(t, *tt.wantCommitMessage, commits[0].Message)
} else {
assert.Equal(t, 1, len(commits))
}
})
}
}
Expand Down Expand Up @@ -446,6 +448,25 @@ func InitRepo(t *testing.T, prefix string) (string, plumbing.Hash) {
return repo, hash
}

func GetCommits(t *testing.T, repo string) []*object.Commit {
gitrepo, err := git.PlainOpen(repo)
assert.NoError(t, err)
iter, err := gitrepo.Log(&git.LogOptions{})
assert.NoError(t, err)
var result []*object.Commit
for {
commit, err := iter.Next()
if err != nil {
if errors.Is(err, io.EOF) {
break
}
assert.NoError(t, err)
}
result = append(result, commit)
}
return result
}

func CountCommits(t *testing.T, repo string) int {
gitrepo, err := git.PlainOpen(repo)
assert.NoError(t, err)
Expand Down Expand Up @@ -476,3 +497,7 @@ func Template(t *testing.T, text, repo, otherrepo string) *bytes.Buffer {
assert.NoError(t, err)
return buff
}

func strPointer(s string) *string {
return &s
}

0 comments on commit 39ba61e

Please sign in to comment.