Skip to content

Commit

Permalink
Display Git commit ID in output of odo commands where the version i…
Browse files Browse the repository at this point in the history
…s 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.
  • Loading branch information
rm3l authored Sep 6, 2023
1 parent adc9699 commit 725a640
Show file tree
Hide file tree
Showing 13 changed files with 88 additions and 39 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 25 additions & 11 deletions pkg/log/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.)
Expand Down
12 changes: 5 additions & 7 deletions pkg/odo/cli/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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)

Expand Down
5 changes: 1 addition & 4 deletions pkg/odo/cli/dev/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
7 changes: 3 additions & 4 deletions pkg/odo/cli/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions pkg/odo/genericclioptions/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
},
Expand Down
2 changes: 1 addition & 1 deletion scripts/rpm-prepare.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
14 changes: 13 additions & 1 deletion tests/helper/helper_documentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,24 @@ 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 {
reg := regexp.MustCompile(timePatternInOdo)
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))
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions tests/helper/helper_generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
2 changes: 1 addition & 1 deletion tests/integration/generic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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+)?`
Expand Down
Loading

0 comments on commit 725a640

Please sign in to comment.