// convertSteps converts [non-root] steps.
// Mutates p.steps.
343709
func (p *stepConverter) convertSteps(c context.Context, bbSteps *[]*pb.Step, ann *annotpb.Step, stepPrefix string) (*pb.Step, error) {
	// Set up Buildbucket v2 step.
	bb := &pb.Step{
		StartTime: ann.Started,
		EndTime:   ann.Ended,
	}

	// Annotee is known to produce startTime>endTime if they are very close.
	// Correct that here.
	if bb.StartTime != nil && bb.EndTime != nil && cmpTs(bb.StartTime, bb.EndTime) > 0 {
		if bb.StartTime.Seconds-bb.EndTime.Seconds > 1 {
			return nil, fmt.Errorf(
				"step %q start time %q is much greater than end time %q",
				ann.Name, bb.StartTime, bb.EndTime)
		}
		// Swap them. They are close enough.
		bb.StartTime, bb.EndTime = bb.EndTime, bb.StartTime
	}

	// Unlike annotation step names, buildbucket step names must be unique.
	// Choose a name.
	stripPrefix := strings.Replace(strings.TrimSuffix(stepPrefix, StepSep), "|", ".", -1) + "."
	baseName := stepPrefix + strings.TrimPrefix(ann.Name, stripPrefix)
	bb.Name = baseName
	for i := 2; p.steps[bb.Name] != nil; i++ {
		bb.Name = fmt.Sprintf("%s (%d)", baseName, i)
	}
	p.steps[bb.Name] = bb

	// summary stores a slice of paragraphs, not individual lines.
	var summary []string

	// Get any failure details.
	if fd := ann.GetFailureDetails(); fd != nil && fd.Text != "" {
		summary = append(summary, fd.Text)
	}

	// Handle text. Below description copied from
	// https://cs.chromium.org/chromium/infra/appengine/cr-buildbucket/annotations.py?l=85&rcl=ec46b5a76fd9948d43a9116051435cbccafa12f1.
	// Although annotation.proto says each line in step_text is a consecutive
	// line and should not contain newlines, in practice they are in HTML format
	// may have <br>s, Buildbot joins them with " " and treats the result
	// as safe HTML.
	// https://cs.chromium.org/chromium/build/third_party/buildbot_8_4p1/buildbot/status/web/build.py?sq=package:chromium&rcl=83e20043dedd1db6977c6aa818e66c1f82ff31e1&l=130
	// Preserve this semantics (except, it is not safe).
	// HTML is valid Markdown, so use it as is.
	if len(ann.Text) > 0 {
		escaped := make([]string, len(ann.Text))
		for i, text := range ann.Text {
			escaped[i] = markdownEscaper.Replace(text)
		}
		summary = append(summary, strings.Join(escaped, " "))
	}

	// Handle logs.
	logs, lines := p.convertLinks(c, ann)
	bb.Logs = logs
	if len(lines) > 0 {
		summary = append(summary, strings.Join(lines, "\n"))
	}

	// Put completed summary into current step.
	bb.SummaryMarkdown = strings.Join(summary, "\n\n")

	// Put step into list of steps.
	*bbSteps = append(*bbSteps, bb)

	// Handle substeps.
	bbSubsteps, err := p.convertSubsteps(c, bbSteps, ann.Substep, bb.Name+StepSep)
	if err != nil {
		return nil, err
	}

	// Ensure parent step start/end time is not after/before of its children.
	for _, ss := range bbSubsteps {
		if ss.StartTime != nil && (bb.StartTime == nil || cmpTs(bb.StartTime, ss.StartTime) > 0) {
			bb.StartTime = ss.StartTime
		}
		switch {
		case ss.EndTime == nil:
			bb.EndTime = nil
		case bb.EndTime != nil && cmpTs(bb.EndTime, ss.EndTime) < 0:
			bb.EndTime = ss.EndTime
		}
	}

	// Determine status.
	switch {
	// First of all, honor start/end times.

	case bb.StartTime == nil:
		bb.Status = pb.Status_SCHEDULED

	case bb.EndTime == nil:
		bb.Status = pb.Status_STARTED

	// Secondly, honor current status.

	case ann.Status == annotpb.Status_SUCCESS:
		bb.Status = pb.Status_SUCCESS

	case ann.Status == annotpb.Status_FAILURE:
		if fd := ann.GetFailureDetails(); fd != nil {
			switch fd.Type {
			case annotpb.FailureDetails_GENERAL:
				bb.Status = pb.Status_FAILURE
			case annotpb.FailureDetails_CANCELLED:
				bb.Status = pb.Status_CANCELED
			default:
				bb.Status = pb.Status_INFRA_FAILURE
			}
		} else {
			bb.Status = pb.Status_FAILURE
		}

	default:
		return nil, errors.Reason("step %q has end time, but status is not terminal", bb.Name).Err()
	}

	// When parent step finishes running, compute its final status as worst
	// status, as determined by statusPrecedence map below, among direct children
	// and its own status.
	if protoutil.IsEnded(bb.Status) {
		for _, bbSubstep := range bbSubsteps {
			substepStatusPrecedence, ok := statusPrecedence[bbSubstep.Status]
			if ok && substepStatusPrecedence < statusPrecedence[bb.Status] {
				bb.Status = bbSubstep.Status
			}
		}
	}

	maybeCloneTimestamp(&bb.StartTime)
	maybeCloneTimestamp(&bb.EndTime)
	return bb, nil
}
270793
func translateToStepGroups(fc *flow.Flow) []*StepGroup {
	// use array instead of map to ensure consistent ordering
	stepId2StepGroup := make([]*StepGroup, len(fc.Steps))
	for _, step := range fc.Steps {
		// println("step:", step.Name, step.Id, "starting...")
		ancestorStepId, foundStepId := findAncestorStepId(step)
		if !foundStepId {
			println("step:", step.Id, "Not found ancestorStepId.")
			continue
		}
		// println("step:", step.Name, step.Id, "ancestorStepId", ancestorStepId)
		if stepId2StepGroup[ancestorStepId] == nil {
			stepId2StepGroup[ancestorStepId] = NewStepGroup()
			for _, ds := range step.InputDatasets {
				parentDsId, hasParentIdId := findAncestorStepId(ds.Step)
				if !hasParentIdId {
					// since we add steps following the same order as the code
					log.Panic("parent StepGroup should already be in the map")
				}
				parentSg := stepId2StepGroup[parentDsId]
				if parentSg == nil {
					// since we add steps following the same order as the code
					log.Panic("parent StepGroup should already be in the map")
				}
				stepId2StepGroup[ancestorStepId].AddParent(parentSg)
			}
		}
		stepId2StepGroup[ancestorStepId].AddStep(step)
	}
	// shrink
	var ret []*StepGroup
	for _, stepGroup := range stepId2StepGroup {
		if stepGroup == nil || len(stepGroup.Steps) == 0 {
			continue
		}
		// println("add step group started by", stepGroup.Steps[0].Name, "with", len(stepGroup.Steps), "steps")
		ret = append(ret, stepGroup)
	}

	return ret
}
712963
func stepsFor24() []Step {
	return []Step{
		&upgradeStep{
			description: "Install the service file in Standard location '/lib/systemd'",
			targets:     []Target{AllMachines},
			run:         installServiceFile,
		},
	}
}
1004849
func (s *Range) SetStep(v int64) *Range {
	s.Step = &v
	return s
}
1088804
func (b *BiMapper) ConvertDown(up []interface{}) []interface{} {
	if len(up) != len(b.UpTypes) {
		panic(fmt.Sprintf("Expected %d values != %d", len(b.UpTypes), len(up)))
	}
	upVals := make([]reflect.Value, len(b.UpTypes))
	for i, val := range up {
		if val == nil {
			upVals[i] = reflect.Zero(b.UpTypes[i])
		} else {
			upVals[i] = reflect.ValueOf(val)
		}
	}
	downVals := b.Downstream.Call(upVals)
	down := make([]interface{}, len(downVals))
	for i, downVal := range downVals {
		down[i] = downVal.Interface()
	}

	return down
}
1270602
func (b *B) Step(str string) {
	if !AnalysisOn {
		return
	}

	b.stopTimer()
	fmt.Println(str + ":")
	fmt.Println(b.results().toString())

	b.resetTimer()
	b.startTimer()
}
521716
func (b *PlanGraphBuilder) Steps() []GraphTransformer {
	b.once.Do(b.init)

	concreteResourceInstanceDeposed := func(a *NodeAbstractResourceInstance, key states.DeposedKey) dag.Vertex {
		return &NodePlanDeposedResourceInstanceObject{
			NodeAbstractResourceInstance: a,
			DeposedKey:                   key,
		}
	}

	steps := []GraphTransformer{
		// Creates all the resources represented in the config
		&ConfigTransformer{
			Concrete: b.ConcreteResource,
			Config:   b.Config,
		},

		// Add the local values
		&LocalTransformer{Config: b.Config},

		// Add the outputs
		&OutputTransformer{Config: b.Config},

		// Add orphan resources
		&OrphanResourceInstanceTransformer{
			Concrete: b.ConcreteResourceOrphan,
			State:    b.State,
			Config:   b.Config,
		},

		// We also need nodes for any deposed instance objects present in the
		// state, so we can plan to destroy them. (This intentionally
		// skips creating nodes for _current_ objects, since ConfigTransformer
		// created nodes that will do that during DynamicExpand.)
		&StateTransformer{
			ConcreteDeposed: concreteResourceInstanceDeposed,
			State:           b.State,
		},

		// Create orphan output nodes
		&OrphanOutputTransformer{
			Config: b.Config,
			State:  b.State,
		},

		// Attach the configuration to any resources
		&AttachResourceConfigTransformer{Config: b.Config},

		// Attach the state
		&AttachStateTransformer{State: b.State},

		// Add root variables
		&RootVariableTransformer{Config: b.Config},

		&MissingProvisionerTransformer{Provisioners: b.Components.ResourceProvisioners()},
		&ProvisionerTransformer{},

		// Add module variables
		&ModuleVariableTransformer{
			Config: b.Config,
		},

		TransformProviders(b.Components.ResourceProviders(), b.ConcreteProvider, b.Config),

		// Remove modules no longer present in the config
		&RemovedModuleTransformer{Config: b.Config, State: b.State},

		// Must attach schemas before ReferenceTransformer so that we can
		// analyze the configuration to find references.
		&AttachSchemaTransformer{Schemas: b.Schemas},

		// Connect so that the references are ready for targeting. We'll
		// have to connect again later for providers and so on.
		&ReferenceTransformer{},

		// Add the node to fix the state count boundaries
		&CountBoundaryTransformer{
			Config: b.Config,
		},

		// Target
		&TargetsTransformer{
			Targets: b.Targets,

			// Resource nodes from config have not yet been expanded for
			// "count", so we must apply targeting without indices. Exact
			// targeting will be dealt with later when these resources
			// DynamicExpand.
			IgnoreIndices: true,
		},

		// Detect when create_before_destroy must be forced on for a particular
		// node due to dependency edges, to avoid graph cycles during apply.
		&ForcedCBDTransformer{},

		// Close opened plugin connections
		&CloseProviderTransformer{},
		&CloseProvisionerTransformer{},

		// Single root
		&RootTransformer{},
	}

	if !b.DisableReduce {
		// Perform the transitive reduction to make our graph a bit
		// more sane if possible (it usually is possible).
		steps = append(steps, &TransitiveReductionTransformer{})
	}

	return steps
}
147449
func (wo *WriteOperations) AddStep(js json.RawMessage) error {
	t, err := GetElementType(js)
	if err != nil {
		return err
	}
	switch t {
	case "tee":
		ts, err := NewTeeFromJSON(js)
		if err != nil {
			return err
		}
		wo.Steps = append(wo.Steps, ts)
	case "encoder":
		step, err := EncoderFromJSON(js)
		if err != nil {
			return err
		}
		wo.Steps = append(wo.Steps, step)
	case "output":
		return errors.New("element of type 'output' should appear only once as the last element of a writer")
	default:
		return fmt.Errorf("element of type '%s' is not available inside a writer", t)
	}
	return nil
}
1121092
func (m *postgresMigrator) step(fn MigrationFunc) error {
	if fn == nil {
		panic("step called but no function!")
	}
	tx, err := m.db.Begin()
	if err != nil {
		return err
	}
	if err := fn(tx); err != nil {
		fmt.Printf("got an error trying to run a step: %v", err)
		if err := tx.Rollback(); err != nil {
			panic(fmt.Sprintf("unable to rollback migration transaction:%v", err))
		}
		return err
	}
	if err := tx.Commit(); err != nil {
		panic(fmt.Sprintf("unable to commit migration transaction:%v", err))
	}
	return nil
}
542324
func (ac *annotationConverter) step(c context.Context, src *miloProto.Step) (*buildbot.Step, error) {
	// This implementation is based on
	// https://chromium.googlesource.com/infra/luci/luci-go/+/7ad046489c578e339b873886d6973abbe43cc137/milo/buildsource/rawpresentation/logDogBuild.go#47

	res := &buildbot.Step{
		Name:       src.Name,
		IsStarted:  true,
		IsFinished: src.Ended != nil,
	}

	// Convert step result.
	switch {
	case src.Status == miloProto.Status_SUCCESS:
		res.Results.Result = buildbot.Success

	case src.Status == miloProto.Status_FAILURE:
		res.Results.Result = buildbot.Failure
		if fd := src.GetFailureDetails(); fd != nil {
			if fd.Type != miloProto.FailureDetails_GENERAL {
				res.Results.Result = buildbot.Exception
			}
			if fd.Text != "" {
				res.Text = append(res.Text, fd.Text)
			}
		}

	case !ac.buildCompletedTime.IsZero():
		res.Results.Result = buildbot.Exception

	default:
		res.Results.Result = buildbot.NoResult
	}

	// annotee never initializes src.Link
	var allLinks []*miloProto.Link
	if src.StdoutStream != nil {
		allLinks = append(allLinks, &miloProto.Link{
			Label: "stdout",
			Value: &miloProto.Link_LogdogStream{src.StdoutStream},
		})
	}
	if src.StderrStream != nil {
		allLinks = append(allLinks, &miloProto.Link{
			Label: "stderr",
			Value: &miloProto.Link_LogdogStream{src.StdoutStream},
		})
	}
	allLinks = append(allLinks, src.OtherLinks...)
	for _, l := range allLinks {
		log, err := ac.log(l)
		if err != nil {
			logging.WithError(err).Errorf(c, "could not convert link %q to buildbot", l.Label)
			continue
		}
		res.Logs = append(res.Logs, *log)
	}

	// Timestamps
	if src.Started != nil {
		var err error
		res.Times.Start.Time, err = ptypes.Timestamp(src.Started)
		if err != nil {
			return nil, errors.Annotate(err, "invalid start time").Err()
		}
		if src.Ended != nil {
			res.Times.Finish.Time, err = ptypes.Timestamp(src.Ended)
			if err != nil {
				return nil, errors.Annotate(err, "invalid end time").Err()
			}
		} else {
			res.Times.Finish.Time = ac.buildCompletedTime
		}
	}

	for _, line := range src.Text {
		// src.Text may contain <br> ;(
		res.Text = append(res.Text, reLineBreak.Split(line, -1)...)
	}
	return res, nil
}
1037465
func (f *FakeClock) Step(d time.Duration) {
	f.lock.Lock()
	defer f.lock.Unlock()
	f.setTimeLocked(f.time.Add(d))
}
1259321
func StepmanRawStepList(collection string) (string, error) {
	logLevel := log.GetLevel().String()
	args := []string{"--loglevel", logLevel, "step-list", "--collection", collection, "--format", "raw"}
	return command.RunCommandAndReturnCombinedStdoutAndStderr("stepman", args...)
}
388040
func (b *ImportGraphBuilder) Steps() []GraphTransformer {
	// Get the module. If we don't have one, we just use an empty tree
	// so that the transform still works but does nothing.
	config := b.Config
	if config == nil {
		config = configs.NewEmptyConfig()
	}

	// Custom factory for creating providers.
	concreteProvider := func(a *NodeAbstractProvider) dag.Vertex {
		return &NodeApplyableProvider{
			NodeAbstractProvider: a,
		}
	}

	steps := []GraphTransformer{
		// Create all our resources from the configuration and state
		&ConfigTransformer{Config: config},

		// Add the import steps
		&ImportStateTransformer{Targets: b.ImportTargets},

		// Add root variables
		&RootVariableTransformer{Config: b.Config},

		TransformProviders(b.Components.ResourceProviders(), concreteProvider, config),

		// This validates that the providers only depend on variables
		&ImportProviderValidateTransformer{},

		// Add the local values
		&LocalTransformer{Config: b.Config},

		// Add the outputs
		&OutputTransformer{Config: b.Config},

		// Add module variables
		&ModuleVariableTransformer{Config: b.Config},

		// Must attach schemas before ReferenceTransformer so that we can
		// analyze the configuration to find references.
		&AttachSchemaTransformer{Schemas: b.Schemas},

		// Connect so that the references are ready for targeting. We'll
		// have to connect again later for providers and so on.
		&ReferenceTransformer{},

		// Close opened plugin connections
		&CloseProviderTransformer{},

		// Single root
		&RootTransformer{},

		// Optimize
		&TransitiveReductionTransformer{},
	}

	return steps
}
272209
func convertLevel(l Level) logging.Level {
	switch l {
	case DEBUG:
		return logging.DEBUG
	case WARNING:
		return logging.WARNING
	case ERROR:
		return logging.ERROR
	case FATAL:
		return logging.CRITICAL
	default:
		return logging.INFO
	}
}
70377
func (woc *wfOperationCtx) expandStep(step wfv1.WorkflowStep) ([]wfv1.WorkflowStep, error) {
	stepBytes, err := json.Marshal(step)
	if err != nil {
		return nil, errors.InternalWrapError(err)
	}
	fstTmpl := fasttemplate.New(string(stepBytes), "{{", "}}")
	expandedStep := make([]wfv1.WorkflowStep, 0)
	var items []wfv1.Item
	if len(step.WithItems) > 0 {
		items = step.WithItems
	} else if step.WithParam != "" {
		err = json.Unmarshal([]byte(step.WithParam), &items)
		if err != nil {
			return nil, errors.Errorf(errors.CodeBadRequest, "withParam value could not be parsed as a JSON list: %s", strings.TrimSpace(step.WithParam))
		}
	} else if step.WithSequence != nil {
		items, err = expandSequence(step.WithSequence)
		if err != nil {
			return nil, err
		}
	} else {
		// this should have been prevented in expandStepGroup()
		return nil, errors.InternalError("expandStep() was called with withItems and withParam empty")
	}

	for i, item := range items {
		var newStep wfv1.WorkflowStep
		newStepName, err := processItem(fstTmpl, step.Name, i, item, &newStep)
		if err != nil {
			return nil, err
		}
		newStep.Name = newStepName
		newStep.Template = step.Template
		expandedStep = append(expandedStep, newStep)
	}
	return expandedStep, nil
}
1365843
func (a *Automaton) step(state, label int) int {
	assert(state >= 0)
	assert(label >= 0)
	if 2*state >= len(a.states) {
		return -1 // invalid state
	}
	trans := a.states[2*state]
	limit := trans + 3*a.states[2*state+1]
	// TODO binary search
	for trans < limit {
		dest, min, max := a.transitions[trans], a.transitions[trans+1], a.transitions[trans+2]
		if min <= label && label <= max {
			return dest
		}
		trans += 3
	}
	return -1
}
1360847
func (p *Pipeline) step() {
	if p.head == p.tail {
		go func() {
			p.done <- nil
		}()
	} else {
		go func() {
			p.head = p.head.next
			p.next <- nil
		}()
	}
}