Skip to content

Commit

Permalink
Tool 1347 log new step version info (#692) (#694)
Browse files Browse the repository at this point in the history
* refactor: extract function

* print resolved version when applicable

* refactor: simplify conditionals

* refactor: simplify conditional

* test cases: version row for major and minor lock

* build correct version info row string

* refactor: remove useless code

* print dotted changelog URL

* refactor: put major and minor test case together

* test case: url cropping

* empty line in box under version info

* dont show update info if latest version fits
lock

* remove development related temp code

* remove unrelated logic from tests

* improve test case name

* refactor: simplify string production

* test case for 1.x and 1.x.x format

* refactor: extract update handling to separate file

* refactor: consolidate update check flow

* handle semver parse error

* remove testing code

* refactor: make naming consistent

* monkey patch dependency

* fallback to repo url when releases url is unknown

* dep update

Co-authored-by: lszucs <[email protected]>
  • Loading branch information
trapacska and lszucs authored Jan 21, 2020
1 parent bc04062 commit 93419bc
Show file tree
Hide file tree
Showing 10 changed files with 670 additions and 224 deletions.
68 changes: 40 additions & 28 deletions bitrise/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/bitrise-io/go-utils/colorstring"
log "github.com/bitrise-io/go-utils/log"
"github.com/bitrise-io/go-utils/stringutil"
"github.com/bitrise-io/go-utils/versions"
stepmanModels "github.com/bitrise-io/stepman/models"
)

Expand All @@ -24,19 +23,6 @@ const (
// Util methods
//------------------------------

func isUpdateAvailable(stepInfo stepmanModels.StepInfoModel) bool {
if stepInfo.LatestVersion == "" {
return false
}

res, err := versions.CompareVersions(stepInfo.Version, stepInfo.LatestVersion)
if err != nil {
log.Errorf("Failed to compare versions, err: %s", err)
}

return (res == 1)
}

func getTrimmedStepName(stepRunResult models.StepRunResultsModel) string {
iconBoxWidth := len(" ")
timeBoxWidth := len(" time (s) ")
Expand Down Expand Up @@ -361,6 +347,38 @@ func getDeprecateNotesRows(notes string) string {
return formattedNote
}

func getRow(str string) string {
str = stringutil.MaxLastCharsWithDots(str, stepRunSummaryBoxWidthInChars-4)
return fmt.Sprintf("| %s |", str+strings.Repeat(" ", stepRunSummaryBoxWidthInChars-len(str)-4))
}

func getUpdateRow(stepInfo stepmanModels.StepInfoModel, width int) string {
vstr := fmt.Sprintf("%s -> %s", stepInfo.Version, stepInfo.LatestVersion)
if stepInfo.Version != stepInfo.EvaluatedVersion {
vstr = fmt.Sprintf("%s (%s) -> %s", stepInfo.Version, stepInfo.EvaluatedVersion, stepInfo.LatestVersion)
}

updateRow := fmt.Sprintf("| Update available: %s |", vstr)
charDiff := len(updateRow) - width

if charDiff == 0 {
return updateRow
}

// shorter than desired - fill with space
updateRow = fmt.Sprintf("| Update available: %s%s |", vstr, strings.Repeat(" ", -charDiff))

if charDiff > 0 {
// longer than desired - trim title
updateRow = fmt.Sprintf("| Update available: -> %s%s |", stepInfo.LatestVersion, strings.Repeat(" ", -len("| Update available: -> %s |")-width))
if charDiff > 6 {
updateRow = fmt.Sprintf("| Update available!%s |", strings.Repeat(" ", -len("| Update available! |")-width))
}
}

return updateRow
}

func getRunningStepFooterSubSection(stepRunResult models.StepRunResultsModel) string {
stepInfo := stepRunResult.StepInfo

Expand All @@ -385,19 +403,7 @@ func getRunningStepFooterSubSection(stepRunResult models.StepRunResultsModel) st
isUpdateAvailable := isUpdateAvailable(stepRunResult.StepInfo)
updateRow := ""
if isUpdateAvailable {
updateRow = fmt.Sprintf("| Update available: %s -> %s |", stepInfo.Version, stepInfo.LatestVersion)
charDiff := len(updateRow) - stepRunSummaryBoxWidthInChars
if charDiff < 0 {
// shorter than desired - fill with space
updateRow = fmt.Sprintf("| Update available: %s -> %s%s |", stepInfo.Version, stepInfo.LatestVersion, strings.Repeat(" ", -charDiff))
} else if charDiff > 0 {
// longer than desired - trim title
if charDiff > 6 {
updateRow = fmt.Sprintf("| Update available!%s |", strings.Repeat(" ", -len("| Update available! |")-stepRunSummaryBoxWidthInChars))
} else {
updateRow = fmt.Sprintf("| Update available: -> %s%s |", stepInfo.LatestVersion, strings.Repeat(" ", -len("| Update available: -> %s |")-stepRunSummaryBoxWidthInChars))
}
}
updateRow = getUpdateRow(stepInfo, stepRunSummaryBoxWidthInChars)
}

issueRow := ""
Expand Down Expand Up @@ -475,7 +481,13 @@ func getRunningStepFooterSubSection(stepRunResult models.StepRunResultsModel) st
// Update available
content := ""
if isUpdateAvailable {
content = fmt.Sprintf("%s", updateRow)
content = updateRow
if stepInfo.Step.SourceCodeURL != nil && *stepInfo.Step.SourceCodeURL != "" {
content += "\n" + getRow("")
releasesURL := repoReleasesURL(*stepInfo.Step.SourceCodeURL)
content += "\n" + getRow("Release notes are available below")
content += "\n" + getRow(releasesURL)
}
}

// Support URL
Expand Down
139 changes: 137 additions & 2 deletions bitrise/print_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,9 @@ func TestGetRunningStepFooterSubSection(t *testing.T) {
Step: stepmanModels.StepModel{
Title: pointers.NewStringPtr(longStr),
},
Version: "1.0.0",
LatestVersion: "1.1.0",
Version: "1.0.0",
LatestVersion: "1.1.0",
EvaluatedVersion: "1.0.0",
}

result := models.StepRunResultsModel{
Expand All @@ -258,6 +259,140 @@ func TestGetRunningStepFooterSubSection(t *testing.T) {
require.Equal(t, expected, actual)
}

t.Log("Update available, major/minor lock, with changelog URL cropping")
{
stepInfo := stepmanModels.StepInfoModel{
Step: stepmanModels.StepModel{
Title: pointers.NewStringPtr(longStr),
SourceCodeURL: pointers.NewStringPtr("https://github.com/test-organization/very-long-test-repository-name-exceeding-max-width"),
},
Version: "1",
LatestVersion: "2.1.0",
EvaluatedVersion: "1.0.1",
}

result := models.StepRunResultsModel{
StepInfo: stepInfo,
Status: models.StepRunStatusCodeSuccess,
Idx: 0,
RunTime: 10000000,
}

actual := getRunningStepFooterSubSection(result)
expected := "| Update available: 1 (1.0.1) -> 2.1.0 |" + "\n" +
"| |" + "\n" +
"| Release notes are available below |" + "\n" +
"| ...-organization/very-long-test-repository-name-exceeding-max-width/releases |"
require.Equal(t, expected, actual)

result.StepInfo.Version = "1.x.x"
actual = getRunningStepFooterSubSection(result)
expected = "| Update available: 1.x.x (1.0.1) -> 2.1.0 |" + "\n" +
"| |" + "\n" +
"| Release notes are available below |" + "\n" +
"| ...-organization/very-long-test-repository-name-exceeding-max-width/releases |"
require.Equal(t, expected, actual)

result.StepInfo.Version = "1.0"
actual = getRunningStepFooterSubSection(result)
expected = "| Update available: 1.0 (1.0.1) -> 2.1.0 |" + "\n" +
"| |" + "\n" +
"| Release notes are available below |" + "\n" +
"| ...-organization/very-long-test-repository-name-exceeding-max-width/releases |"
require.Equal(t, expected, actual)

result.StepInfo.Version = "1.0.x"
actual = getRunningStepFooterSubSection(result)
expected = "| Update available: 1.0.x (1.0.1) -> 2.1.0 |" + "\n" +
"| |" + "\n" +
"| Release notes are available below |" + "\n" +
"| ...-organization/very-long-test-repository-name-exceeding-max-width/releases |"
require.Equal(t, expected, actual)

}

t.Log("Update available, major/minor lock, without changelog URL cropping")
{
stepInfo := stepmanModels.StepInfoModel{
Step: stepmanModels.StepModel{
Title: pointers.NewStringPtr(longStr),
SourceCodeURL: pointers.NewStringPtr("https://github.com/bitrise-steplib/steps-script"),
},
Version: "1",
LatestVersion: "2.1.0",
EvaluatedVersion: "1.0.1",
}

result := models.StepRunResultsModel{
StepInfo: stepInfo,
Status: models.StepRunStatusCodeSuccess,
Idx: 0,
RunTime: 10000000,
}

actual := getRunningStepFooterSubSection(result)
expected := "| Update available: 1 (1.0.1) -> 2.1.0 |" + "\n" +
"| |" + "\n" +
"| Release notes are available below |" + "\n" +
"| https://github.com/bitrise-steplib/steps-script/releases |"
require.Equal(t, expected, actual)

result.StepInfo.Version = "1.x.x"
actual = getRunningStepFooterSubSection(result)
expected = "| Update available: 1.x.x (1.0.1) -> 2.1.0 |" + "\n" +
"| |" + "\n" +
"| Release notes are available below |" + "\n" +
"| https://github.com/bitrise-steplib/steps-script/releases |"
require.Equal(t, expected, actual)

result.StepInfo.Version = "1.0"
actual = getRunningStepFooterSubSection(result)
expected = "| Update available: 1.0 (1.0.1) -> 2.1.0 |" + "\n" +
"| |" + "\n" +
"| Release notes are available below |" + "\n" +
"| https://github.com/bitrise-steplib/steps-script/releases |"
require.Equal(t, expected, actual)

result.StepInfo.Version = "1.0.x"
actual = getRunningStepFooterSubSection(result)
expected = "| Update available: 1.0.x (1.0.1) -> 2.1.0 |" + "\n" +
"| |" + "\n" +
"| Release notes are available below |" + "\n" +
"| https://github.com/bitrise-steplib/steps-script/releases |"
require.Equal(t, expected, actual)

}

t.Log("Update available, nothing is printed if latest version is within major/minor lock range")
{
stepInfo := stepmanModels.StepInfoModel{
Step: stepmanModels.StepModel{
Title: pointers.NewStringPtr(longStr),
SourceCodeURL: pointers.NewStringPtr("https://github.com/bitrise-steplib/steps-script"),
},
Version: "1",
LatestVersion: "1.0.1",
EvaluatedVersion: "1.0.1",
}

result := models.StepRunResultsModel{
StepInfo: stepInfo,
Status: models.StepRunStatusCodeSuccess,
Idx: 0,
RunTime: 10000000,
}

actual := getRunningStepFooterSubSection(result)
expected := ""
require.Equal(t, expected, actual)

result.StepInfo.Version = "1.0"
actual = getRunningStepFooterSubSection(result)
expected = ""
require.Equal(t, expected, actual)

}

t.Log("support url row length's chardiff = 0")
{
paddingCharCnt := 4
Expand Down
51 changes: 51 additions & 0 deletions bitrise/updates.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package bitrise

import (
"regexp"
"strings"

"github.com/bitrise-io/go-utils/log"
stepmanModels "github.com/bitrise-io/stepman/models"
ver "github.com/hashicorp/go-version"
)

func isUpdateAvailable(stepInfo stepmanModels.StepInfoModel) bool {
if stepInfo.LatestVersion == "" {
return false
}

re := regexp.MustCompile(`\d+`)
components := re.FindAllString(stepInfo.Version, -1)
normalized := strings.Join(components, ".")
locked, err := ver.NewSemver(normalized)

if err != nil {
log.Warnf("Error processing version (%s): normalized version (%s) not in semver format: %s", stepInfo.Version, normalized, err)
return false
}

latest, err := ver.NewSemver(stepInfo.LatestVersion)
if err != nil {
log.Warnf("Error processing latest version (%s): %s", stepInfo.LatestVersion, err)
return false
}

switch len(components) {
case 1:
return locked.Segments()[0] < latest.Segments()[0]
case 2:
return locked.Segments()[0] < latest.Segments()[0] || locked.Segments()[1] < latest.Segments()[1]
case 3:
return locked.LessThan(latest)
default:
return false
}
}

func repoReleasesURL(repoURL string) string {
if strings.Contains(repoURL, "github") || strings.Contains(repoURL, "gitlab") {
return repoURL + "/releases"
}

return repoURL
}
Loading

0 comments on commit 93419bc

Please sign in to comment.