Skip to content

Commit

Permalink
patch: print plan summary upfront to executing it
Browse files Browse the repository at this point in the history
This change will print a toc at debug level upfront to running
a workflow. It will help prepare outputs for (e.g. UI) before
executing and parsing the log output.

- fix: handle nil steps
- refactor: prepare for improved logging update

Co-authored-by: Philipp Hinrichsen <philipp.hinrichsen@new-work.se>
Co-authored-by: Björn Brauer <bjoern.brauer@new-work.se>
  • Loading branch information
3 people authored and github-actions committed May 19, 2022
1 parent 6a4d2aa commit 981a701
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 3 deletions.
69 changes: 69 additions & 0 deletions pkg/runner/boundaries.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package runner

import (
"context"
"fmt"
"strings"

"github.com/nektos/act/pkg/common"
"github.com/nektos/act/pkg/model"
log "github.com/sirupsen/logrus"
)

func escapeID(id string) string {
id = strings.ReplaceAll(id, "|", "||")
id = strings.ReplaceAll(id, "\n", "\\n")
return id
}

func printPlan(plan *model.Plan) {
log.Debugln("################################################################################")
log.Debugf("# %s", plan.Stages[0].Runs[0].Workflow.Name)
for _, stage := range plan.Stages {
for _, run := range stage.Runs {
log.Debugf("## %s | %s", escapeID(run.JobID), run.Job().Name)
for n, step := range run.Job().Steps {
if step == nil {
continue
}
id := step.ID
if id == "" {
id = fmt.Sprint(n)
}

log.Debugf("### %s | %s", escapeID(id), step)
}
}
}
log.Debugln("################################################################################")
}

func (rc *RunContext) logJobBoundaries(executor common.Executor) common.Executor {
id := escapeID(rc.Run.JobID)
jobName := escapeID(rc.JobName)

return common.NewDebugExecutor("@@ job | start | %s | %s @@", id, jobName).
Then(executor).
Finally(func(ctx context.Context) error {
jobStatus := rc.getJobContext().Status
return common.NewDebugExecutor("@@ job | stop | %s | %s | %s @@", id, jobName, jobStatus)(ctx)
})
}

func (rc *RunContext) logStepBoundaries(step *model.Step, executor common.Executor) common.Executor {
id := escapeID(step.ID)
stepIdentifier := escapeID(step.String())

return common.NewDebugExecutor("@@ step | start | %s | %s @@", id, stepIdentifier).
Then(executor).
Finally(func(ctx context.Context) error {
result := rc.StepResults[step.ID]
var stepStatus string
if result != nil {
stepStatus = result.Conclusion.String()
} else {
stepStatus = "unknown"
}
return common.NewDebugExecutor("@@ step | stop | %s | %s | %s @@", id, stepIdentifier, stepStatus)(ctx)
})
}
4 changes: 2 additions & 2 deletions pkg/runner/job_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func newJobExecutor(info jobInfo, sf stepFactory, rc *RunContext) common.Executo
preSteps = append(preSteps, useStepLogger(rc, stepModel, step.pre()))

stepExec := step.main()
steps = append(steps, useStepLogger(rc, stepModel, func(ctx context.Context) error {
steps = append(steps, rc.logStepBoundaries(stepModel, useStepLogger(rc, stepModel, func(ctx context.Context) error {
logger := common.Logger(ctx)
err := stepExec(ctx)
if err != nil {
Expand All @@ -70,7 +70,7 @@ func newJobExecutor(info jobInfo, sf stepFactory, rc *RunContext) common.Executo
common.SetJobError(ctx, ctx.Err())
}
return nil
}))
})))

// run the post exector in reverse order
if postExecutor != nil {
Expand Down
4 changes: 3 additions & 1 deletion pkg/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ func New(runnerConfig *Config) (Runner, error) {
// NewPlanExecutor ...
//nolint:gocyclo
func (runner *runnerImpl) NewPlanExecutor(plan *model.Plan) common.Executor {
printPlan(plan)

maxJobNameLen := 0

stagePipeline := make([]common.Executor, 0)
Expand Down Expand Up @@ -165,7 +167,7 @@ func (runner *runnerImpl) NewPlanExecutor(plan *model.Plan) common.Executor {
stageExecutor = append(stageExecutor, func(ctx context.Context) error {
logger := common.Logger(ctx)
jobName := fmt.Sprintf("%-*s", maxJobNameLen, rc.String())
return rc.Executor().Finally(func(ctx context.Context) error {
return rc.logJobBoundaries(rc.Executor()).Finally(func(ctx context.Context) error {
isLastRunningContainer := func(currentStage int, currentRun int) bool {
return currentStage == len(plan.Stages)-1 && currentRun == len(stage.Runs)-1
}
Expand Down

0 comments on commit 981a701

Please sign in to comment.