Skip to content

fix: Material-info-overview git details #4869

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions api/bean/AppView.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ type DeploymentDetailContainer struct {
IsVirtualEnvironment bool `json:"isVirtualEnvironment"`
HelmPackageName string `json:"helmPackageName"`
HelmReleaseInstallStatus string `json:"-"`
DeployedBy int32 `json:"deployedBy"`
}

type AppDetailContainer struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2011,7 +2011,7 @@ func (handler *PipelineConfigRestHandlerImpl) GetImageTaggingData(w http.Respons
}
//RBAC

resp, err := handler.imageTaggingService.GetTagsData(ciPipelineId, appId, artifactId, externalCi)
resp, err := handler.imageTaggingService.GetTagsData(ciPipelineId, appId, artifactId, externalCi, 0)
if err != nil {
handler.Logger.Errorw("error occurred in fetching GetTagsData for artifact ", "err", err, "artifactId", artifactId, "ciPipelineId", ciPipelineId, "externalCi", externalCi, "appId", appId)
common.WriteJsonResp(w, err, resp, http.StatusInternalServerError)
Expand Down
3 changes: 2 additions & 1 deletion internal/sql/repository/AppListingRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,8 @@ func (impl AppListingRepositoryImpl) FetchMinDetailOtherEnvironment(appId int) (
func (impl AppListingRepositoryImpl) DeploymentDetailByArtifactId(ciArtifactId int, envId int) (bean.DeploymentDetailContainer, error) {
impl.Logger.Debug("reached at AppListingRepository:")
var deploymentDetail bean.DeploymentDetailContainer
query := "SELECT env.id AS environment_id, env.environment_name, env.default, pco.created_on as last_deployed_time, a.app_name" +
query := "SELECT env.id AS environment_id, env.environment_name, env.default, pco.created_on as last_deployed_time, a.app_name, " +
" pco.created_by as deployed_by" +
" FROM pipeline_config_override pco" +
" INNER JOIN pipeline p on p.id = pco.pipeline_id" +
" INNER JOIN environment env ON env.id=p.environment_id" +
Expand Down
7 changes: 5 additions & 2 deletions internal/sql/repository/CiArtifactRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (

type credentialsSource = string
type ArtifactsSourceType = string
type ciMaterialType = string

const (
GLOBAL_CONTAINER_REGISTRY credentialsSource = "global_container_registry"
Expand All @@ -50,6 +51,8 @@ const (
// deprecated; Handled for backward compatibility
EXT ArtifactsSourceType = "ext"
// PRE_CI is not a valid DataSource for an artifact
MATERIAL_TYPE_GIT ciMaterialType = "git"
MATERIAL_TYPE_SCM ciMaterialType = "scm"
)

type CiArtifactWithExtraData struct {
Expand Down Expand Up @@ -515,9 +518,9 @@ func (artifact *CiArtifact) ParseMaterialInfo() (map[string]string, error) {
scmMap := map[string]string{}
for _, material := range ciMaterials {
var url string
if material.Material.Type == "git" {
if material.Material.Type == MATERIAL_TYPE_GIT {
url = material.Material.GitConfiguration.URL
} else if material.Material.Type == "scm" {
} else if material.Material.Type == MATERIAL_TYPE_SCM {
url = material.Material.ScmConfiguration.URL
} else {
return nil, fmt.Errorf("unknown material type:%s ", material.Material.Type)
Expand Down
209 changes: 133 additions & 76 deletions pkg/pipeline/CiHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ const Starting = "Starting"
const POD_DELETED_MESSAGE = "pod deleted"
const TERMINATE_MESSAGE = "workflow shutdown with strategy: Terminate"
const ABORT_MESSAGE_AFTER_STARTING_STAGE = "workflow shutdown with strategy: Force Abort"
const commitTimeLayout = "2006-01-02T15:04:05Z07:00"

func (impl *CiHandlerImpl) CheckAndReTriggerCI(workflowStatus v1alpha1.WorkflowStatus) error {

Expand Down Expand Up @@ -1468,105 +1469,47 @@ func (impl *CiHandlerImpl) FetchMaterialInfoByArtifactId(ciArtifactId int, envId

ciArtifact, err := impl.ciArtifactRepository.Get(ciArtifactId)
if err != nil {
impl.Logger.Errorw("err", "ciArtifactId", ciArtifactId, "err", err)
impl.Logger.Errorw("error in getting CiArtifact Details", "ciArtifactId", ciArtifactId, "err", err)
return &types.GitTriggerInfoResponse{}, err
}

ciPipeline, err := impl.ciPipelineRepository.FindByIdIncludingInActive(ciArtifact.PipelineId)
deployDetail, err := impl.appListingRepository.DeploymentDetailByArtifactId(ciArtifactId, envId)
if err != nil {
impl.Logger.Errorw("err", "ciArtifactId", ciArtifactId, "err", err)
impl.Logger.Errorw("error in getting deploy detail", "ciArtifactId", ciArtifactId, "envId", envId, "err", err)
return &types.GitTriggerInfoResponse{}, err
}

ciMaterials, err := impl.ciPipelineMaterialRepository.GetByPipelineId(ciPipeline.Id)
if err != nil {
impl.Logger.Errorw("err", "err", err)
var triggeredByUserEmailId string
triggeredByUserEmailId, err = impl.userService.GetEmailById(deployDetail.DeployedBy)
if err != nil && !util.IsErrNoRows(err) {
impl.Logger.Errorw("error in getting the user email Id ", "userId", deployDetail.DeployedBy, "err", err)
return &types.GitTriggerInfoResponse{}, err
}

deployDetail, err := impl.appListingRepository.DeploymentDetailByArtifactId(ciArtifactId, envId)
ciMaterials, err := repository.GetCiMaterialInfo(ciArtifact.MaterialInfo, ciArtifact.DataSource)
if err != nil {
impl.Logger.Errorw("err", "err", err)
impl.Logger.Info("error in unmarshalling material info", ciArtifact.MaterialInfo, "err", err)
return &types.GitTriggerInfoResponse{}, err
}

ciMaterialsArr := make([]pipelineConfig.CiPipelineMaterialResponse, 0)
var triggeredByUserEmailId string
//check workflow data only for non external builds
if !ciPipeline.IsExternal {
var workflow *pipelineConfig.CiWorkflow
if ciArtifact.ParentCiArtifact > 0 {
workflow, err = impl.ciWorkflowRepository.FindLastTriggeredWorkflowByArtifactId(ciArtifact.ParentCiArtifact)
if err != nil {
impl.Logger.Errorw("err", "ciArtifactId", ciArtifact.ParentCiArtifact, "err", err)
return &types.GitTriggerInfoResponse{}, err
}
} else {
workflow, err = impl.ciWorkflowRepository.FindLastTriggeredWorkflowByArtifactId(ciArtifactId)
if err != nil {
impl.Logger.Errorw("err", "ciArtifactId", ciArtifactId, "err", err)
return &types.GitTriggerInfoResponse{}, err
}
}

triggeredByUserEmailId, err = impl.userService.GetEmailById(workflow.TriggeredBy)
if err != nil && !util.IsErrNoRows(err) {
impl.Logger.Errorw("err", "err", err)
return &types.GitTriggerInfoResponse{}, err
}
ciMaterialsArr := impl.parseCiMaterials(ciMaterials)

for _, m := range ciMaterials {
var history []*gitSensor.GitCommit
_gitTrigger := workflow.GitTriggers[m.Id]

// ignore git trigger which have commit and webhook both data nil
if len(_gitTrigger.Commit) == 0 && _gitTrigger.WebhookData.Id == 0 {
continue
}

_gitCommit := &gitSensor.GitCommit{
Message: _gitTrigger.Message,
Author: _gitTrigger.Author,
Date: _gitTrigger.Date,
Changes: _gitTrigger.Changes,
Commit: _gitTrigger.Commit,
}

// set webhook data
_webhookData := _gitTrigger.WebhookData
if _webhookData.Id > 0 {
_gitCommit.WebhookData = &gitSensor.WebhookData{
Id: _webhookData.Id,
EventActionType: _webhookData.EventActionType,
Data: _webhookData.Data,
}
}

history = append(history, _gitCommit)

res := pipelineConfig.CiPipelineMaterialResponse{
Id: m.Id,
GitMaterialId: m.GitMaterialId,
GitMaterialName: m.GitMaterial.Name[strings.Index(m.GitMaterial.Name, "-")+1:],
Type: string(m.Type),
Value: m.Value,
Active: m.Active,
Url: m.GitMaterial.Url,
History: history,
}
ciMaterialsArr = append(ciMaterialsArr, res)
}
ciPipelineId, appId, isExternalCi, cdPipelineId, err := impl.fetchVariablesForImageTagging(ciArtifact.DataSource, ciArtifact.PipelineId, ciArtifact.ExternalCiPipelineId, ciArtifact.ComponentId)
if err != nil {
impl.Logger.Errorw("error in fetching variables for Image Tagging ", "PipelineId", ciArtifact.PipelineId, "ciArtifactId", ciArtifactId, "dataSource", ciArtifact.DataSource, "externalCiPipelineId", ciArtifact.ExternalCiPipelineId, "componentId", ciArtifact.ComponentId, "err", err)
return &types.GitTriggerInfoResponse{}, err
}
imageTaggingData, err := impl.imageTaggingService.GetTagsData(ciPipeline.Id, ciPipeline.AppId, ciArtifactId, false)
imageTaggingData, err := impl.imageTaggingService.GetTagsData(ciPipelineId, appId, ciArtifactId, isExternalCi, cdPipelineId)
if err != nil {
impl.Logger.Errorw("error in fetching imageTaggingData", "err", err, "ciPipelineId", ciPipeline.Id, "appId", ciPipeline.AppId, "ciArtifactId", ciArtifactId)
impl.Logger.Errorw("error in fetching imageTaggingData", "ciPipelineId", ciPipelineId, "appId", appId, "ciArtifactId", ciArtifactId, "cdPipelineId", cdPipelineId, "isExternalCi", isExternalCi, "err", err)
return &types.GitTriggerInfoResponse{}, err
}

gitTriggerInfoResponse := &types.GitTriggerInfoResponse{
//GitTriggers: workflow.GitTriggers,
CiMaterials: ciMaterialsArr,
TriggeredByEmail: triggeredByUserEmailId,
AppId: ciPipeline.AppId,
AppId: appId,
AppName: deployDetail.AppName,
EnvironmentId: deployDetail.EnvironmentId,
EnvironmentName: deployDetail.EnvironmentName,
Expand All @@ -1578,6 +1521,120 @@ func (impl *CiHandlerImpl) FetchMaterialInfoByArtifactId(ciArtifactId int, envId
return gitTriggerInfoResponse, nil
}

func (impl *CiHandlerImpl) parseCiMaterials(ciMaterials []repository.CiMaterialInfo) []pipelineConfig.CiPipelineMaterialResponse {
ciMaterialsArr := make([]pipelineConfig.CiPipelineMaterialResponse, 0)
for _, m := range ciMaterials {

var history []*gitSensor.GitCommit

var modification repository.Modification
if len(m.Modifications) > 0 {
modification = m.Modifications[0]
} else {
continue
}

if len(modification.Revision) == 0 && modification.WebhookData.Id == 0 {
continue
}

commitDate, err := time.Parse(commitTimeLayout, modification.ModifiedTime)
if err != nil {
impl.Logger.Errorw("error in parsing commit time", "commitTimeString", commitDate, "err", err)
}
gitCommit := &gitSensor.GitCommit{
Message: modification.Message,
Author: modification.Author,
Date: commitDate,
Commit: modification.Revision,
}
if modification.WebhookData.Id > 0 {
gitCommit.WebhookData = &gitSensor.WebhookData{
Id: modification.WebhookData.Id,
EventActionType: modification.WebhookData.EventActionType,
Data: modification.WebhookData.Data,
}
}
history = append(history, gitCommit)

var url string
if m.Material.Type == repository.MATERIAL_TYPE_GIT {
url = m.Material.GitConfiguration.URL
} else if m.Material.Type == repository.MATERIAL_TYPE_SCM {
url = m.Material.ScmConfiguration.URL
} else {
continue
}

// urlFormat := https://github.com/foo/<repoName>.git, https://github.com/foo/<repoName>
var gitMaterialName string
urlSplit := strings.Split(url, "/")
repoName := urlSplit[len(urlSplit)-1]
repoNameSplit := strings.Split(repoName, ".")
gitMaterialName = repoNameSplit[0]

res := pipelineConfig.CiPipelineMaterialResponse{
GitMaterialName: gitMaterialName,
Type: m.Material.Type,
Value: modification.Branch,
Url: url,
History: history,
}
ciMaterialsArr = append(ciMaterialsArr, res)
}

return ciMaterialsArr
}

func (impl *CiHandlerImpl) fetchVariablesForImageTagging(dataSource string, ciPipelineId int, externalCiPipelineId int, componentId int) (int, int, bool, int, error) {

isExternalCi := false
var appId int
var cdPipelineId int

//CI_RUNNER,EXTERNAL,post_ci,pre_cd,post_cd
switch dataSource {
case repository.CI_RUNNER:
ciPipeline, err := impl.ciPipelineRepository.FindByIdIncludingInActive(ciPipelineId)
if err != nil {
impl.Logger.Errorw("err in fetching the ciPipeline", "ciPipelineId", ciPipelineId, "err", err)
return 0, 0, isExternalCi, 0, err
}
ciPipelineId = ciPipeline.Id
appId = ciPipeline.AppId

case repository.POST_CI:
ciPipeline, err := impl.ciPipelineRepository.FindByIdIncludingInActive(componentId)
if err != nil {
impl.Logger.Errorw("err in fetching the ciPipeline", "ciPipelineId", ciPipelineId, "componentId", componentId, "err", err)
return 0, 0, isExternalCi, 0, err
}
ciPipelineId = ciPipeline.Id
appId = ciPipeline.AppId
case repository.PRE_CD, repository.POST_CD:
cdPipeline, err := impl.cdPipelineRepository.FindById(componentId)
if err != nil {
impl.Logger.Errorw("err in fetching the cdPipeline", "ciPipelineId", ciPipelineId, "componentId", componentId, "err", err)
return 0, 0, isExternalCi, 0, err
}
cdPipelineId = cdPipeline.Id
appId = cdPipeline.AppId
case repository.WEBHOOK:
externalCiPipeline, err := impl.ciPipelineRepository.FindExternalCiById(externalCiPipelineId)
if err != nil {
impl.Logger.Errorw("err in fetching the external ciPipeline", "ciPipelineId", ciPipelineId, "externalCiPipelineId", externalCiPipelineId, "err", err)
return 0, 0, isExternalCi, 0, err
}
ciPipelineId = externalCiPipeline.Id
appId = externalCiPipeline.AppId
isExternalCi = true
default:
return 0, 0, false, 0, errors.New("invalid data source")
}
return ciPipelineId, appId, isExternalCi, cdPipelineId, nil

}

func (impl *CiHandlerImpl) UpdateCiWorkflowStatusFailure(timeoutForFailureCiBuild int) error {
ciWorkflows, err := impl.ciWorkflowRepository.FindByStatusesIn([]string{Starting, Running})
if err != nil {
Expand Down
9 changes: 6 additions & 3 deletions pkg/pipeline/ImageTaggingService.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type ImageTaggingService interface {
//AppReleaseTags -> all the tags of the given appId,
//imageComment -> comment of the given artifactId,
// ProdEnvExists -> implies the existence of prod environment in any workflow of given ciPipelineId or its child ciPipelineRequest's
GetTagsData(ciPipelineId, appId, artifactId int, externalCi bool) (*types.ImageTaggingResponseDTO, error)
GetTagsData(ciPipelineId, appId, artifactId int, externalCi bool, cdPipelineId int) (*types.ImageTaggingResponseDTO, error)
CreateOrUpdateImageTagging(ciPipelineId, appId, artifactId, userId int, imageTaggingRequest *types.ImageTaggingRequestDTO) (*types.ImageTaggingResponseDTO, error)
GetProdEnvFromParentAndLinkedWorkflow(ciPipelineId int) (bool, error)
GetProdEnvByCdPipelineId(pipelineId int) (bool, error)
Expand Down Expand Up @@ -100,7 +100,7 @@ func (impl ImageTaggingServiceImpl) GetImageTaggingServiceConfig() ImageTaggingS
// AppReleaseTags -> all the tags of the given appId,
// imageComment -> comment of the given artifactId,
// ProdEnvExists -> implies the existence of prod environment in any workflow of given ciPipelineId or its child ciPipelineRequest's
func (impl ImageTaggingServiceImpl) GetTagsData(ciPipelineId, appId, artifactId int, externalCi bool) (*types.ImageTaggingResponseDTO, error) {
func (impl ImageTaggingServiceImpl) GetTagsData(ciPipelineId, appId, artifactId int, externalCi bool, cdPipelineId int) (*types.ImageTaggingResponseDTO, error) {
resp := &types.ImageTaggingResponseDTO{}
imageComment, err := impl.imageTaggingRepo.GetImageComment(artifactId)
if err != nil && err != pg.ErrNoRows {
Expand All @@ -120,9 +120,12 @@ func (impl ImageTaggingServiceImpl) GetTagsData(ciPipelineId, appId, artifactId
prodEnvExists := false
if externalCi {
prodEnvExists, err = impl.FindProdEnvExists(externalCi, []int{ciPipelineId})
} else if cdPipelineId > 0 {
prodEnvExists, err = impl.GetProdEnvByCdPipelineId(cdPipelineId)
} else {
prodEnvExists, err = impl.GetProdEnvFromParentAndLinkedWorkflow(ciPipelineId)
}

if err != nil {
impl.logger.Errorw("error in finding prodEnvExists value", "err", err, "ciPipelineId", ciPipelineId, "externalCi", externalCi)
return resp, err
Expand Down Expand Up @@ -311,7 +314,7 @@ func (impl ImageTaggingServiceImpl) CreateOrUpdateImageTagging(ciPipelineId, app
impl.logger.Errorw("error in committing transaction", "err", err, "ciPipelineId", ciPipelineId, "appId", appId, "artifactId", artifactId, "userId", userId, "imageTaggingRequest", imageTaggingRequest)
return nil, err
}
return impl.GetTagsData(ciPipelineId, appId, artifactId, imageTaggingRequest.ExternalCi)
return impl.GetTagsData(ciPipelineId, appId, artifactId, imageTaggingRequest.ExternalCi, 0)
}

func (impl ImageTaggingServiceImpl) performTagOperationsAndGetAuditList(tx *pg.Tx, appId, artifactId, userId int, imageTaggingRequest *types.ImageTaggingRequestDTO) ([]*repository.ImageTaggingAudit, error) {
Expand Down
Loading