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

@@ -2,7 +2,7 @@ PROJECT := github.com/redhat-developer/odo
ifdef GITCOMMIT
GITCOMMIT := $(GITCOMMIT)
else
GITCOMMIT := $(shell git rev-parse --short HEAD 2>/dev/null)
GITCOMMIT := $(shell git describe --no-match --always --abbrev=9 --dirty --broken 2>/dev/null || git rev-parse --short HEAD 2>/dev/null)
endif
COMMON_GOFLAGS := -mod=vendor

View File

@@ -36,6 +36,7 @@ import (
"golang.org/x/term"
"github.com/redhat-developer/odo/pkg/log/fidget"
"github.com/redhat-developer/odo/pkg/version"
)
// Spacing for logging
@@ -339,28 +340,41 @@ More details on https://odo.dev/docs/user-guides/advanced/experimental-mode
}
}
// Title Prints the logo as well as the first line being BLUE (indicator of the command information)
// the second and third lines are optional and provide information with regards to what is being ran
// Title Prints the logo as well as the first line being BLUE (indicator of the command information);
// the second line is optional and provides information in regard to what is being run.
// The last line displays information about the current odo version.
//
// __
// / \__ **First line**
// \__/ \ Second line
// / \__/ Third line
// \__/
func Title(firstLine, secondLine, thirdLine string) {
// __
// / \__ **First line**
// \__/ \ Second line
// / \__/ odo version: <VERSION>
// \__/
func Title(firstLine, secondLine string) {
if !IsJSON() {
fmt.Fprint(GetStdout(), Stitle(firstLine, secondLine, thirdLine))
fmt.Fprint(GetStdout(), Stitle(firstLine, secondLine))
}
}
// Stitle is the same as Title but returns the string instead
func Stitle(firstLine, secondLine, thirdLine string) string {
func Stitle(firstLine, secondLine string) string {
var versionMsg string
if version.VERSION != "" {
versionMsg = "odo version: " + version.VERSION
}
if version.GITCOMMIT != "" {
versionMsg += " (" + version.GITCOMMIT + ")"
}
return StitleWithVersion(firstLine, secondLine, versionMsg)
}
// StitleWithVersion is the same as Stitle, but it allows to customize the version message line
func StitleWithVersion(firstLine, secondLine, versionLine string) string {
blue := color.New(color.FgBlue).SprintFunc()
return fmt.Sprintf(` __
/ \__ %s
\__/ \ %s
/ \__/ %s
\__/%s`, blue(firstLine), secondLine, thirdLine, "\n")
\__/%s`, blue(firstLine), secondLine, versionLine, "\n")
}
// Sectionf outputs a title in BLUE and underlined for separating a section (such as building a container, deploying files, etc.)

View File

@@ -8,6 +8,9 @@ import (
"github.com/redhat-developer/odo/pkg/kclient"
"github.com/spf13/cobra"
"k8s.io/kubectl/pkg/util/templates"
"github.com/redhat-developer/odo/pkg/component"
"github.com/redhat-developer/odo/pkg/log"
"github.com/redhat-developer/odo/pkg/odo/cli/messages"
@@ -19,10 +22,6 @@ import (
"github.com/redhat-developer/odo/pkg/odo/util"
odoutil "github.com/redhat-developer/odo/pkg/odo/util"
scontext "github.com/redhat-developer/odo/pkg/segment/context"
"github.com/redhat-developer/odo/pkg/version"
"github.com/spf13/cobra"
"k8s.io/kubectl/pkg/util/templates"
)
// RecommendedCommandName is the recommended command name
@@ -87,9 +86,8 @@ func (o *DeployOptions) Run(ctx context.Context) error {
scontext.SetProjectType(ctx, devfileObj.Data.GetMetadata().ProjectType)
scontext.SetDevfileName(ctx, devfileName)
// Output what the command is doing / information
log.Title("Running the application in Deploy mode using "+devfileName+" Devfile",
"Namespace: "+namespace,
"odo version: "+version.VERSION)
log.Title("Running the application in Deploy mode using the \""+devfileName+"\" Devfile",
"Namespace: "+namespace)
genericclioptions.WarnIfDefaultNamespace(namespace, o.clientset.KubernetesClient)

View File

@@ -38,7 +38,6 @@ import (
scontext "github.com/redhat-developer/odo/pkg/segment/context"
"github.com/redhat-developer/odo/pkg/state"
"github.com/redhat-developer/odo/pkg/util"
"github.com/redhat-developer/odo/pkg/version"
)
// RecommendedCommandName is the recommended command name
@@ -219,9 +218,7 @@ func (o *DevOptions) Run(ctx context.Context) (err error) {
}
// Output what the command is doing / information
log.Title("Developing using the \""+componentName+"\" Devfile",
dest,
"odo version: "+version.VERSION)
log.Title("Developing using the \""+componentName+"\" Devfile", dest)
if platform == commonflags.PlatformCluster {
genericclioptions.WarnIfDefaultNamespace(odocontext.GetNamespace(ctx), o.clientset.KubernetesClient)
}

View File

@@ -12,6 +12,8 @@ import (
"github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/devfile/library/v2/pkg/devfile/parser"
"k8s.io/kubectl/pkg/util/templates"
"github.com/redhat-developer/odo/pkg/api"
"github.com/redhat-developer/odo/pkg/component"
"github.com/redhat-developer/odo/pkg/devfile"
@@ -30,9 +32,6 @@ import (
"github.com/redhat-developer/odo/pkg/odo/util"
odoutil "github.com/redhat-developer/odo/pkg/odo/util"
scontext "github.com/redhat-developer/odo/pkg/segment/context"
"github.com/redhat-developer/odo/pkg/version"
"k8s.io/kubectl/pkg/util/templates"
)
// RecommendedCommandName is the recommended command name
@@ -212,7 +211,7 @@ func (o *InitOptions) run(ctx context.Context) (devfileObj parser.DevfileObj, pa
} else if len(o.flags) == 0 {
infoOutput = messages.SourceCodeDetected
}
log.Title(messages.InitializingNewComponent, infoOutput, "odo version: "+version.VERSION)
log.Title(messages.InitializingNewComponent, infoOutput)
log.Println()
if len(o.flags) == 0 {
log.Info(messages.InteractiveModeEnabled)

View File

@@ -14,7 +14,6 @@ import (
"github.com/redhat-developer/odo/pkg/odo/cmdline"
"github.com/redhat-developer/odo/pkg/odo/genericclioptions/clientset"
scontext "github.com/redhat-developer/odo/pkg/segment/context"
"github.com/redhat-developer/odo/pkg/version"
)
// runPreInit executes the Init command before running the main command
@@ -33,7 +32,7 @@ func runPreInit(ctx context.Context, workingDir string, deps *clientset.Clientse
func(interactiveMode bool) {
scontext.SetInteractive(cmdline.Context(), interactiveMode)
if interactiveMode {
log.Title(msg, messages.SourceCodeDetected, "odo version: "+version.VERSION)
log.Title(msg, messages.SourceCodeDetected)
log.Info("\n" + messages.InteractiveModeEnabled)
}
},

View File

@@ -8,7 +8,7 @@ echo "Reading ODO_VERSION, ODO_RELEASE and GIT_COMMIT env, if they are set"
export ODO_VERSION=${ODO_VERSION:=3.14.0}
export ODO_RELEASE=${ODO_RELEASE:=1}
export GIT_COMMIT=${GIT_COMMIT:=$(git rev-parse --short HEAD 2>/dev/null)}
export GIT_COMMIT=${GIT_COMMIT:=$(git describe --no-match --always --abbrev=9 --dirty --broken 2>/dev/null || git rev-parse --short HEAD 2>/dev/null)}
ODO_RPM_VERSION=$(echo $ODO_VERSION | tr '-' '~')
export ODO_RPM_VERSION

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")