Display Git commit ID in output of odo commands where the version is shown (#7074)

* Display Git commit ID in output of odo commands where the version is displayed

This covers:
- odo init
- odo dev
- odo deploy

Displaying the commit ID (same as in `odo version`) will help quickly pinpoint the exact commit without having to run `odo version`.

See #6131 for more context

* Append the state of the working tree next to the Git commit ID

`git describe` is much more helpful to quickly understand the state of the working tree.

For backward compatibility, we are defaulting to `git rev-parse`,
just in case `git describe` does not work correctly.

* Fix integration tests

* Fix doc automation tests

Strip the Git commit ID from the full odo version string
prior to comparing the outputs.
We still want to compare the tag displayed.
This commit is contained in:
Armel Soro
2023-09-06 20:36:13 +02:00
committed by GitHub
parent adc96994d9
commit 725a64014a
13 changed files with 88 additions and 39 deletions

View File

@@ -56,6 +56,7 @@ var _ = Describe("doc command reference odo init", Label(helper.LabelNoCluster),
got := helper.StripAnsi(out)
got = helper.StripInteractiveQuestion(got)
got = fmt.Sprintf(outputStringFormat, args[1], helper.StripSpinner(got))
got = helper.StripGitCommitFromVersion(got)
file := "interactive_mode_empty_directory_output.mdx"
want := helper.GetMDXContent(filepath.Join(commonPath, file))
diff := cmp.Diff(want, got)
@@ -87,6 +88,7 @@ var _ = Describe("doc command reference odo init", Label(helper.LabelNoCluster),
got := helper.StripAnsi(out)
got = helper.StripInteractiveQuestion(got)
got = fmt.Sprintf(outputStringFormat, args[1], helper.StripSpinner(got))
got = helper.StripGitCommitFromVersion(got)
file := "interactive_mode_directory_with_sources_output.mdx"
want := helper.GetMDXContent(filepath.Join(commonPath, file))
diff := cmp.Diff(want, got)
@@ -100,6 +102,7 @@ var _ = Describe("doc command reference odo init", Label(helper.LabelNoCluster),
args := []string{"init", "--devfile", "go", "--name", "my-go-app", "--devfile-version", "2.0.0"}
out := helper.Cmd("odo", args...).ShouldPass().Out()
got := fmt.Sprintf(outputStringFormat, strings.Join(args, " "), helper.StripSpinner(out))
got = helper.StripGitCommitFromVersion(got)
file := "versioned_devfile_output.mdx"
want := helper.GetMDXContent(filepath.Join(commonPath, file))
diff := cmp.Diff(want, got)
@@ -110,6 +113,7 @@ var _ = Describe("doc command reference odo init", Label(helper.LabelNoCluster),
args := []string{"init", "--devfile", "go", "--name", "my-go-app", "--devfile-version", "latest"}
out := helper.Cmd("odo", args...).ShouldPass().Out()
got := fmt.Sprintf(outputStringFormat, strings.Join(args, " "), helper.StripSpinner(out))
got = helper.StripGitCommitFromVersion(got)
file := "latest_versioned_devfile_output.mdx"
want := helper.GetMDXContent(filepath.Join(commonPath, file))
diff := cmp.Diff(want, got)
@@ -120,6 +124,7 @@ var _ = Describe("doc command reference odo init", Label(helper.LabelNoCluster),
args := []string{"init", "--devfile-path", "https://registry.devfile.io/devfiles/nodejs-angular", "--name", "my-nodejs-app", "--starter", "nodejs-angular-starter"}
out := helper.Cmd("odo", args...).ShouldPass().Out()
got := fmt.Sprintf(outputStringFormat, strings.Join(args, " "), helper.StripSpinner(out))
got = helper.StripGitCommitFromVersion(got)
file := "devfile_from_url_output.mdx"
want := helper.GetMDXContent(filepath.Join(commonPath, file))
diff := cmp.Diff(want, got)
@@ -170,6 +175,7 @@ var _ = Describe("doc command reference odo init", Label(helper.LabelNoCluster),
args := []string{"init", "--name", "my-spring-app", "--devfile", "java-springboot", "--devfile-registry", "DefaultDevfileRegistry", "--starter", "springbootproject"}
out := helper.Cmd("odo", args...).ShouldPass().Out()
got := fmt.Sprintf(outputStringFormat, strings.Join(args, " "), helper.StripSpinner(out))
got = helper.StripGitCommitFromVersion(got)
file := "devfile_from_specific_registry_output.mdx"
want := helper.GetMDXContent(filepath.Join(commonPath, file))
diff := cmp.Diff(want, got)
@@ -196,6 +202,7 @@ var _ = Describe("doc command reference odo init", Label(helper.LabelNoCluster),
args := []string{"init", "--devfile", "nodejs-react", "--name", "my-nr-app"}
out := helper.Cmd("odo", args...).ShouldPass().Out()
got := fmt.Sprintf(outputStringFormat, strings.Join(args, " "), helper.StripSpinner(out))
got = helper.StripGitCommitFromVersion(got)
file := "devfile_from_any_registry_output.mdx"
want := helper.GetMDXContent(filepath.Join(commonPath, file))
diff := cmp.Diff(want, got)
@@ -210,6 +217,7 @@ var _ = Describe("doc command reference odo init", Label(helper.LabelNoCluster),
args := []string{"init", "--devfile-path", "https://registry.devfile.io/devfiles/nodejs-angular", "--name", "my-nodejs-app", "--starter", "nodejs-angular-starter"}
out := helper.Cmd("odo", args...).ShouldPass().Out()
got := fmt.Sprintf(outputStringFormat, strings.Join(args, " "), helper.StripSpinner(out))
got = helper.StripGitCommitFromVersion(got)
file := "devfile_from_url_output.mdx"
want := helper.GetMDXContent(filepath.Join(commonPath, file))
diff := cmp.Diff(want, got)
@@ -220,6 +228,7 @@ var _ = Describe("doc command reference odo init", Label(helper.LabelNoCluster),
args := []string{"init", "--devfile", "go", "--name", "my-go-app", "--run-port", "3456", "--run-port", "9876"}
out := helper.Cmd("odo", args...).ShouldPass().Out()
got := fmt.Sprintf(outputStringFormat, strings.Join(args, " "), helper.StripSpinner(out))
got = helper.StripGitCommitFromVersion(got)
file := "devfile_with_run-port_output.mdx"
want := helper.GetMDXContent(filepath.Join(commonPath, file))
diff := cmp.Diff(want, got)

View File

@@ -83,6 +83,7 @@ var _ = Describe("User guides: Quickstart test", func() {
got := helper.StripAnsi(out)
got = helper.StripInteractiveQuestion(got)
got = fmt.Sprintf(outputStringFormat, "init", helper.StripSpinner(got))
got = helper.StripGitCommitFromVersion(got)
file := filepath.Join(commonNodeJSPath, "nodejs_odo_init_output.mdx")
want := helper.GetMDXContent(file)
diff := cmp.Diff(want, got)
@@ -98,6 +99,7 @@ var _ = Describe("User guides: Quickstart test", func() {
got = helper.ReplaceAllForwardedPorts(got, devSession.Endpoints, map[string]string{"3000": "127.0.0.1:20001", "5858": "127.0.0.1:20002"})
got = strings.ReplaceAll(got, commonVar.Project, namespace)
got = fmt.Sprintf(outputStringFormat, strings.Join(args, " "), helper.StripSpinner(got))
got = helper.StripGitCommitFromVersion(got)
file := filepath.Join(commonNodeJSPath, "nodejs_odo_dev_output.mdx")
want := helper.GetMDXContent(file)
diff := cmp.Diff(want, got)
@@ -131,6 +133,7 @@ var _ = Describe("User guides: Quickstart test", func() {
got := helper.StripAnsi(out)
got = helper.StripInteractiveQuestion(got)
got = fmt.Sprintf(outputStringFormat, "init", helper.StripSpinner(got))
got = helper.StripGitCommitFromVersion(got)
file := filepath.Join(commonGoPath, "go_odo_init_output.mdx")
want := helper.GetMDXContent(file)
diff := cmp.Diff(want, got)
@@ -146,6 +149,7 @@ var _ = Describe("User guides: Quickstart test", func() {
got = helper.ReplaceAllForwardedPorts(got, devSession.Endpoints, map[string]string{"8080": "127.0.0.1:20001"})
got = strings.ReplaceAll(got, commonVar.Project, namespace)
got = fmt.Sprintf(outputStringFormat, strings.Join(args, " "), helper.StripSpinner(got))
got = helper.StripGitCommitFromVersion(got)
file := filepath.Join(commonGoPath, "go_odo_dev_output.mdx")
want := helper.GetMDXContent(file)
diff := cmp.Diff(want, got)
@@ -190,6 +194,7 @@ var _ = Describe("User guides: Quickstart test", func() {
got = helper.StripInteractiveQuestion(got)
got = strings.ReplaceAll(got, commonVar.Project, namespace)
got = fmt.Sprintf(outputStringFormat, "init", helper.StripSpinner(got))
got = helper.StripGitCommitFromVersion(got)
file := filepath.Join(commondotnetPath, "dotnet_odo_init_output.mdx")
want := helper.GetMDXContent(file)
diff := cmp.Diff(want, got)
@@ -205,6 +210,7 @@ var _ = Describe("User guides: Quickstart test", func() {
got = helper.ReplaceAllForwardedPorts(got, devSession.Endpoints, map[string]string{"8080": "127.0.0.1:20001"})
got = strings.ReplaceAll(got, commonVar.Project, namespace)
got = fmt.Sprintf(outputStringFormat, strings.Join(args, " "), helper.StripSpinner(got))
got = helper.StripGitCommitFromVersion(got)
file := filepath.Join(commondotnetPath, "dotnet_odo_dev_output.mdx")
want := helper.GetMDXContent(file)
diff := cmp.Diff(want, got)
@@ -238,6 +244,7 @@ var _ = Describe("User guides: Quickstart test", func() {
got := helper.StripAnsi(out)
got = helper.StripInteractiveQuestion(got)
got = fmt.Sprintf(outputStringFormat, "init", helper.StripSpinner(got))
got = helper.StripGitCommitFromVersion(got)
file := filepath.Join(commonGoPath, "java_odo_init_output.mdx")
want := helper.GetMDXContent(file)
diff := cmp.Diff(want, got)
@@ -256,6 +263,7 @@ var _ = Describe("User guides: Quickstart test", func() {
got = helper.ReplaceAllForwardedPorts(got, devSession.Endpoints, map[string]string{"8080": "127.0.0.1:20001", "5858": "127.0.0.1:20002"})
got = strings.ReplaceAll(got, commonVar.Project, namespace)
got = fmt.Sprintf(outputStringFormat, strings.Join(args, " "), helper.StripSpinner(got))
got = helper.StripGitCommitFromVersion(got)
file := filepath.Join(commonGoPath, "java_odo_dev_output.mdx")
want := helper.GetMDXContent(file)
diff := cmp.Diff(want, got)

View File

@@ -21,6 +21,11 @@ const (
unicodeSpinnerFrames = "◓◐◑◒"
)
var (
// Matches versions like "vX.Y.Z (6721c668b)" or "vX.Y.Z (6721c668b-dirty)" or "vX.Y.Z-rc (6721c668b-broken)"
reOdoVersion = regexp.MustCompile(`(v[0-9]+.[0-9]+.[0-9]+(?:-\w+)?)\s*\(\w+(-\w+)?\)`)
)
// ReplaceAllTimeInString replaces the time taken to download a Devfile or a starter project for an odo command with a custom value;
// this function is helpful because the time value is variable and replacing it with the value in mdx content helps in comparing.
func ReplaceAllTimeInString(docString string, timeString string) string {
@@ -28,6 +33,12 @@ func ReplaceAllTimeInString(docString string, timeString string) string {
return reg.ReplaceAllString(docString, timeString)
}
// StripGitCommitFromVersion removes any git commit hash from the full odo version string.
// For example, given a version string like "vX.Y.Z (6721c668b)", it will simply return "vX.Y.Z"
func StripGitCommitFromVersion(docString string) string {
return reOdoVersion.ReplaceAllString(docString, "$1")
}
// StripSpinner strips the cmd out string of spaces, spinner statements and spinner frames
func StripSpinner(docString string) (returnString string) {
sc := bufio.NewScanner(strings.NewReader(docString))
@@ -105,7 +116,8 @@ func GetMDXContent(filePath string) (mdxContent string) {
// replace all instances of time to [1s], this is also done for cmd out
mdxContent = ReplaceAllTimeInString(mdxContent, staticTimeValue)
return
mdxContent = StripGitCommitFromVersion(mdxContent)
return mdxContent
}
// StripAnsi strips the cmd out of ansi values used for fomatting(underline, colored line, etc.) the cmd out;

View File

@@ -411,3 +411,8 @@ func GetDevfileRegistryURL() string {
}
return registryURL
}
func GetOdoVersion() (version string, gitCommit string) {
odoVersion := Cmd("odo", "version", "--client", "-o", "json").ShouldPass().Out()
return gjson.Get(odoVersion, "version").String(), gjson.Get(odoVersion, "gitCommit").String()
}

View File

@@ -117,7 +117,7 @@ var _ = Describe("odo generic", func() {
Context("executing odo version command", func() {
const (
reOdoVersion = `^odo\s*v[0-9]+.[0-9]+.[0-9]+(?:-\w+)?\s*\(\w+\)`
reOdoVersion = `^odo\s*v[0-9]+.[0-9]+.[0-9]+(?:-\w+)?\s*\(\w+(-\w+)?\)`
reKubernetesVersion = `Kubernetes:\s*v[0-9]+.[0-9]+.[0-9]+((-\w+\.[0-9]+)?\+\w+)?`
rePodmanVersion = `Podman Client:\s*[0-9]+.[0-9]+.[0-9]+((-\w+\.[0-9]+)?\+\w+)?`
reJSONVersion = `^v{0,1}[0-9]+.[0-9]+.[0-9]+((-\w+\.[0-9]+)?\+\w+)?`

View File

@@ -13,14 +13,13 @@ import (
"github.com/devfile/library/v2/pkg/devfile/parser/data/v2/common"
"k8s.io/utils/pointer"
"github.com/redhat-developer/odo/pkg/odo/cli/messages"
"github.com/redhat-developer/odo/pkg/util"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/redhat-developer/odo/pkg/odo/cli/messages"
"github.com/redhat-developer/odo/pkg/util"
odolog "github.com/redhat-developer/odo/pkg/log"
"github.com/redhat-developer/odo/pkg/version"
"github.com/redhat-developer/odo/tests/helper"
)
@@ -413,8 +412,12 @@ var _ = Describe("odo init interactive command tests", func() {
language := "java"
// The first output is welcoming message / paragraph / banner output
welcomingMsgs := strings.Split(odolog.Stitle(messages.InitializingNewComponent, messages.NoSourceCodeDetected, "odo version: "+version.VERSION), "\n")
odoVersion, gitCommit := helper.GetOdoVersion()
welcomingMsgs := strings.Split(
odolog.StitleWithVersion(messages.InitializingNewComponent,
messages.NoSourceCodeDetected,
fmt.Sprintf("odo version: %s (%s)", odoVersion, gitCommit)),
"\n")
output, err := testRunner(language, welcomingMsgs, func(ctx helper.InteractiveContext) {
helper.ExpectString(ctx, "Select architectures")
helper.SendLine(ctx, "")
@@ -455,7 +458,12 @@ var _ = Describe("odo init interactive command tests", func() {
language := "Python"
projectType := "Python"
versionedDevfileName := "python:2.1.0"
welcomingMsgs := strings.Split(odolog.Stitle(messages.InitializingNewComponent, messages.SourceCodeDetected, "odo version: "+version.VERSION), "\n")
odoVersion, gitCommit := helper.GetOdoVersion()
welcomingMsgs := strings.Split(
odolog.StitleWithVersion(messages.InitializingNewComponent,
messages.SourceCodeDetected,
fmt.Sprintf("odo version: %s (%s)", odoVersion, gitCommit)),
"\n")
output, err := testRunner(language, welcomingMsgs, func(ctx helper.InteractiveContext) {
helper.ExpectString(ctx, "Based on the files in the current directory odo detected")