func FindPreviousStageName()

in controllers/stage/chain/util/util.go [35:59]


func FindPreviousStageName(ctx context.Context, k8sClient client.Client, stage *cdPipeApi.Stage) (string, error) {
	if stage.IsFirst() {
		return "", errors.New("can't get previous stage from first stage")
	}

	stages := &cdPipeApi.StageList{}
	if err := k8sClient.List(
		ctx,
		stages,
		client.InNamespace(stage.Namespace),
		client.MatchingLabels{cdPipeApi.StageCdPipelineLabelName: stage.Spec.CdPipeline},
	); err != nil {
		return "", fmt.Errorf("failed to list stage names: %w", err)
	}

	//nolint
	//refactor
	for _, val := range stages.Items {
		if val.Spec.CdPipeline == stage.Spec.CdPipeline && val.Spec.Order == (stage.Spec.Order-1) {
			return val.Spec.Name, nil
		}
	}

	return "", errors.New("previous stage not found")
}