github.com/freiheit-com/kuberpult@v1.24.2-0.20240328135542-315d5630abe6/services/cd-service/pkg/service/release_train_prognosis.go (about)

     1  /*This file is part of kuberpult.
     2  
     3  Kuberpult is free software: you can redistribute it and/or modify
     4  it under the terms of the Expat(MIT) License as published by
     5  the Free Software Foundation.
     6  
     7  Kuberpult is distributed in the hope that it will be useful,
     8  but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    10  MIT License for more details.
    11  
    12  You should have received a copy of the MIT License
    13  along with kuberpult. If not, see <https://directory.fsf.org/wiki/License:Expat>.
    14  
    15  Copyright 2023 freiheit.com*/
    16  
    17  package service
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  
    23  	api "github.com/freiheit-com/kuberpult/pkg/api/v1"
    24  	"github.com/freiheit-com/kuberpult/pkg/auth"
    25  	rp "github.com/freiheit-com/kuberpult/services/cd-service/pkg/repository"
    26  )
    27  
    28  type ReleaseTrainPrognosisServer struct {
    29  	Repository rp.Repository
    30  	RBACConfig auth.RBACConfig
    31  }
    32  
    33  func (s *ReleaseTrainPrognosisServer) GetReleaseTrainPrognosis(ctx context.Context, in *api.ReleaseTrainRequest) (*api.GetReleaseTrainPrognosisResponse, error) {
    34  	t := &rp.ReleaseTrain{
    35  		Authentication: rp.Authentication{
    36  			RBACConfig: s.RBACConfig,
    37  		},
    38  		Target:          in.Target,
    39  		Team:            in.Team,
    40  		CommitHash:      in.CommitHash,
    41  		WriteCommitData: false,
    42  		Repo:            s.Repository,
    43  	}
    44  
    45  	prognosis := t.Prognosis(ctx, s.Repository.State())
    46  
    47  	if prognosis.Error != nil {
    48  		return nil, prognosis.Error
    49  	}
    50  
    51  	ret := &api.GetReleaseTrainPrognosisResponse{
    52  		EnvsPrognoses: make(map[string]*api.ReleaseTrainEnvPrognosis),
    53  	}
    54  
    55  	for envName, envPrognosis := range prognosis.EnvironmentPrognoses {
    56  		//exhaustruct:ignore
    57  		retEnvPrognosis := &api.ReleaseTrainEnvPrognosis{}
    58  		switch {
    59  		case envPrognosis.SkipCause != nil:
    60  			retEnvPrognosis.Outcome = envPrognosis.SkipCause
    61  		case envPrognosis.Error != nil:
    62  			// this case should never be reached since an error in the environment prognosis is propagated to the release train prognosis
    63  			return nil, fmt.Errorf("error in an environment release train, environment: %s, error: %w", envName, envPrognosis.Error)
    64  		case envPrognosis.AppsPrognoses != nil:
    65  			retEnvPrognosis.Outcome = &api.ReleaseTrainEnvPrognosis_AppsPrognoses{
    66  				AppsPrognoses: &api.ReleaseTrainEnvPrognosis_AppsPrognosesWrapper{
    67  					Prognoses: make(map[string]*api.ReleaseTrainAppPrognosis),
    68  				},
    69  			}
    70  			for appName, appPrognosis := range envPrognosis.AppsPrognoses {
    71  				//exhaustruct:ignore
    72  				retAppPrognosis := &api.ReleaseTrainAppPrognosis{}
    73  				if appPrognosis.SkipCause != nil {
    74  					retAppPrognosis.Outcome = appPrognosis.SkipCause
    75  				} else {
    76  					retAppPrognosis.Outcome = &api.ReleaseTrainAppPrognosis_DeployedVersion{
    77  						DeployedVersion: appPrognosis.Version,
    78  					}
    79  				}
    80  				retEnvPrognosis.GetAppsPrognoses().Prognoses[appName] = retAppPrognosis
    81  			}
    82  		}
    83  		ret.EnvsPrognoses[envName] = retEnvPrognosis
    84  	}
    85  
    86  	return ret, nil
    87  }