Skip to content

Commit

Permalink
[CI-217] Step started event (#825)
Browse files Browse the repository at this point in the history
* Add step started event log generation

* Add logger implementation

* Use new step started logging

* Add to the integration test

* Use human readable titles if available

* Move the title logic out of logging

* Fix if statement
  • Loading branch information
tothszabi committed Oct 25, 2022
1 parent ea4a8c2 commit c4945d2
Show file tree
Hide file tree
Showing 12 changed files with 335 additions and 207 deletions.
35 changes: 35 additions & 0 deletions _tests/integration/log_format_test.go
Expand Up @@ -64,6 +64,20 @@ func restoreConsoleLog(t *testing.T, log []byte) string {
}

func convertEventLog(line []byte) (string, error) {
logLine, err := convertBitriseStartedEventLog(line)
if err == nil {
return logLine, nil
}

logLine, err = convertStepStartedEventLog(line)
if err == nil {
return logLine, nil
}

return "", fmt.Errorf("unknown event log")
}

func convertBitriseStartedEventLog(line []byte) (string, error) {
type EventLog struct {
Timestamp string `json:"timestamp"`
MessageType string `json:"type"`
Expand All @@ -88,6 +102,27 @@ func convertEventLog(line []byte) (string, error) {
return buf.String(), nil
}

func convertStepStartedEventLog(line []byte) (string, error) {
type EventLog struct {
Timestamp string `json:"timestamp"`
MessageType string `json:"type"`
EventType string `json:"event_type"`
Content log.StepStartedParams `json:"content"`
}

var eventLog EventLog
err := json.Unmarshal(line, &eventLog)
if err != nil {
return "", err
}

var buf bytes.Buffer
logger := log.NewLogger(log.LoggerOpts{LoggerType: log.ConsoleLogger, Writer: &buf})
logger.PrintStepStartedEvent(eventLog.Content)

return buf.String(), nil
}

func convertMessageLog(line []byte) (string, error) {
type MessageLog struct {
Message string `json:"message"`
Expand Down
138 changes: 0 additions & 138 deletions bitrise/print.go
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/bitrise-io/bitrise/log"
"github.com/bitrise-io/bitrise/models"
"github.com/bitrise-io/bitrise/toolkits"
"github.com/bitrise-io/go-utils/colorstring"
"github.com/bitrise-io/go-utils/stringutil"
stepmanModels "github.com/bitrise-io/stepman/models"
Expand Down Expand Up @@ -76,131 +75,6 @@ func getTrimmedStepName(stepRunResult models.StepRunResultsModel) string {
return trimTitle(title, suffix, titleBoxWidth)
}

func getRunningStepHeaderMainSection(stepInfo stepmanModels.StepInfoModel, idx int) string {
title := ""
if stepInfo.Step.Title != nil && *stepInfo.Step.Title != "" {
title = *stepInfo.Step.Title
}

content := fmt.Sprintf("| (%d) %s |", idx, title)
charDiff := len(content) - stepRunSummaryBoxWidthInChars

if charDiff < 0 {
// shorter than desired - fill with space
content = fmt.Sprintf("| (%d) %s%s |", idx, title, strings.Repeat(" ", -charDiff))
} else if charDiff > 0 {
// longer than desired - trim title
trimmedTitleWidth := len(title) - charDiff
if trimmedTitleWidth < 4 {
log.Errorf("Step title too long, can't present title at all! : %s", title)
} else {
content = fmt.Sprintf("| (%d) %s |", idx, stringutil.MaxFirstCharsWithDots(title, trimmedTitleWidth))
}
}
return content
}

func getRunningStepHeaderSubSection(step stepmanModels.StepModel, stepInfo stepmanModels.StepInfoModel) string {

idRow := ""
{
id := stepInfo.ID
idRow = fmt.Sprintf("| id: %s |", id)
charDiff := len(idRow) - stepRunSummaryBoxWidthInChars
if charDiff < 0 {
// shorter than desired - fill with space
idRow = fmt.Sprintf("| id: %s%s |", id, strings.Repeat(" ", -charDiff))
} else if charDiff > 0 {
// longer than desired - trim title
trimmedWidth := len(id) - charDiff
if trimmedWidth < 4 {
log.Errorf("Step id too long, can't present id at all! : %s", id)
} else {
idRow = fmt.Sprintf("| id: %s |", stringutil.MaxFirstCharsWithDots(id, trimmedWidth))
}
}
}

versionRow := ""
{
version := stepInfo.Version
versionRow = fmt.Sprintf("| version: %s |", version)
charDiff := len(versionRow) - stepRunSummaryBoxWidthInChars
if charDiff < 0 {
// shorter than desired - fill with space
versionRow = fmt.Sprintf("| version: %s%s |", version, strings.Repeat(" ", -charDiff))
} else if charDiff > 0 {
// longer than desired - trim title
trimmedWidth := len(version) - charDiff
if trimmedWidth < 4 {
log.Errorf("Step version too long, can't present version at all! : %s", version)
} else {
versionRow = fmt.Sprintf("| id: %s |", stringutil.MaxFirstCharsWithDots(version, trimmedWidth))
}
}
}

collectionRow := ""
{
collection := stepInfo.Library
collectionRow = fmt.Sprintf("| collection: %s |", collection)
charDiff := len(collectionRow) - stepRunSummaryBoxWidthInChars
if charDiff < 0 {
// shorter than desired - fill with space
collectionRow = fmt.Sprintf("| collection: %s%s |", collection, strings.Repeat(" ", -charDiff))
} else if charDiff > 0 {
// longer than desired - trim title
trimmedWidth := len(collection) - charDiff
if trimmedWidth < 4 {
log.Errorf("Step collection too long, can't present collection at all! : %s", collection)
} else {
collectionRow = fmt.Sprintf("| collection: %s |", stringutil.MaxLastCharsWithDots(collection, trimmedWidth))
}
}
}

toolkitRow := ""
{
toolkitForStep := toolkits.ToolkitForStep(step)
toolkitName := toolkitForStep.ToolkitName()
toolkitRow = fmt.Sprintf("| toolkit: %s |", toolkitName)
charDiff := len(toolkitRow) - stepRunSummaryBoxWidthInChars
if charDiff < 0 {
// shorter than desired - fill with space
toolkitRow = fmt.Sprintf("| toolkit: %s%s |", toolkitName, strings.Repeat(" ", -charDiff))
} else if charDiff > 0 {
// longer than desired - trim title
trimmedWidth := len(toolkitName) - charDiff
if trimmedWidth < 4 {
log.Errorf("Step toolkitName too long, can't present toolkitName at all! : %s", toolkitName)
} else {
toolkitRow = fmt.Sprintf("| toolkit: %s |", stringutil.MaxLastCharsWithDots(toolkitName, trimmedWidth))
}
}
}

timeRow := ""
{
logTime := time.Now().Format(time.RFC3339)
timeRow = fmt.Sprintf("| time: %s |", logTime)
charDiff := len(timeRow) - stepRunSummaryBoxWidthInChars
if charDiff < 0 {
// shorter than desired - fill with space
timeRow = fmt.Sprintf("| time: %s%s |", logTime, strings.Repeat(" ", -charDiff))
} else if charDiff > 0 {
// longer than desired - trim title
trimmedWidth := len(logTime) - charDiff
if trimmedWidth < 4 {
log.Errorf("Time too long, can't present time at all! : %s", logTime)
} else {
timeRow = fmt.Sprintf("| time: %s |", stringutil.MaxFirstCharsWithDots(logTime, trimmedWidth))
}
}
}

return fmt.Sprintf("%s\n%s\n%s\n%s\n%s", idRow, versionRow, collectionRow, toolkitRow, timeRow)
}

func getRunningStepFooterMainSection(stepRunResult models.StepRunResultsModel) string {
iconBoxWidth := len(" ")
timeBoxWidth := len(" time (s) ")
Expand Down Expand Up @@ -538,18 +412,6 @@ func getRunningStepFooterSubSection(stepRunResult models.StepRunResultsModel) st
return content
}

// PrintRunningStepHeader ...
func PrintRunningStepHeader(stepInfo stepmanModels.StepInfoModel, step stepmanModels.StepModel, idx int) {
sep := fmt.Sprintf("+%s+", strings.Repeat("-", stepRunSummaryBoxWidthInChars-2))

log.Print(sep)
log.Print(getRunningStepHeaderMainSection(stepInfo, idx))
log.Print(sep)
log.Print(getRunningStepHeaderSubSection(step, stepInfo))
log.Print(sep)
log.Print("|" + strings.Repeat(" ", stepRunSummaryBoxWidthInChars-2) + "|")
}

// PrintRunningStepFooter ..
func PrintRunningStepFooter(stepRunResult models.StepRunResultsModel, isLastStepInWorkflow bool) {
iconBoxWidth := len(" ")
Expand Down
49 changes: 0 additions & 49 deletions bitrise/print_test.go
Expand Up @@ -105,32 +105,6 @@ func TestGetTrimmedStepName(t *testing.T) {
}
}

func TestGetRunningStepHeaderMainSection(t *testing.T) {
stepInfo := stepmanModels.StepInfoModel{
Step: stepmanModels.StepModel{
Title: pointers.NewStringPtr(longStr),
},
Version: longStr,
}

actual := getRunningStepHeaderMainSection(stepInfo, 0)
expected := "| (0) This is a very long string, this is a very long string, this is a ver... |"
require.Equal(t, expected, actual)
}

func TestGetRunningStepHeaderSubSection(t *testing.T) {
stepInfo := stepmanModels.StepInfoModel{
ID: longStr,
Step: stepmanModels.StepModel{
Title: pointers.NewStringPtr(longStr),
},
Version: longStr,
}

actual := getRunningStepHeaderSubSection(stepmanModels.StepModel{}, stepInfo)
require.NotEqual(t, "", actual)
}

func Test_getRunningStepFooterMainSection(t *testing.T) {
noTitleInfo := stepmanModels.StepInfoModel{
Step: stepmanModels.StepModel{
Expand Down Expand Up @@ -418,29 +392,6 @@ func TestPrintRunningWorkflow(t *testing.T) {
PrintRunningWorkflow(longStr)
}

func TestPrintRunningStepHeader(t *testing.T) {
stepInfo := stepmanModels.StepInfoModel{
Step: stepmanModels.StepModel{
Title: pointers.NewStringPtr(""),
},
Version: "",
}
step := stepmanModels.StepModel{}
PrintRunningStepHeader(stepInfo, step, 0)

stepInfo.Step.Title = pointers.NewStringPtr(longStr)
stepInfo.Version = ""
PrintRunningStepHeader(stepInfo, step, 0)

stepInfo.Step.Title = pointers.NewStringPtr("")
stepInfo.Version = longStr
PrintRunningStepHeader(stepInfo, step, 0)

stepInfo.Step.Title = pointers.NewStringPtr(longStr)
stepInfo.Version = longStr
PrintRunningStepHeader(stepInfo, step, 0)
}

func TestPrintRunningStepFooter(t *testing.T) {
stepInfo := stepmanModels.StepInfoModel{
Step: stepmanModels.StepModel{
Expand Down
3 changes: 2 additions & 1 deletion cli/build_run_result_collector.go
Expand Up @@ -27,6 +27,7 @@ func newBuildRunResultCollector(tracker analytics.Tracker) buildRunResultCollect

func (r buildRunResultCollector) registerStepRunResults(
buildRunResults *models.BuildRunResultsModel,
stepExecutionId string,
stepStartTime time.Time,
step stepmanModels.StepModel,
stepInfoPtr stepmanModels.StepInfoModel,
Expand Down Expand Up @@ -87,7 +88,7 @@ func (r buildRunResultCollector) registerStepRunResults(
}
}
if printStepHeader {
bitrise.PrintRunningStepHeader(stepInfoPtr, step, stepIdxPtr)
logStepStarted(stepInfoPtr, step, stepIdxPtr, stepExecutionId, stepStartTime)
}

errStr := ""
Expand Down

0 comments on commit c4945d2

Please sign in to comment.