// ShouldTrigger decides whether a given application should be triggered or not.
ShouldTrigger decides whether a given application should be triggered or not.
// ShouldTrigger decides whether a given application should be triggered or not .
func (d *OnCommitDeterminer) ShouldTrigger(ctx context.Context, app *model.Application, appCfg *config.GenericDeploymentSpec) (bool, error) {
	logger := d.logger.With(
		zap.String("app", app.Name),
		zap.String("app-id", app.Id),
		zap.String("target-commit", d.targetCommit),
	)
	if appCfg.Trigger.OnCommit.Disabled {
		logger.Info(fmt.Sprintf("auto trigger deployment disabled for application, hash: %s", d.targetCommit))
		return false, nil
	}
	preCommit, err := d.commitGetter.Get(ctx, app.Id)
	if err != nil {
		logger.Error("failed to get last triggered commit", zap.Error(err))
		return false, err
	}
	if preCommit == "" {
		logger.Info("no previously triggered deployment was found")
		return true, nil
	}
	if preCommit == d.targetCommit {
		logger.Info(fmt.Sprintf("no update to sync for application, hash: %s", d.targetCommit))
		return false, nil
	}
	changedFiles, err := d.repo.ChangedFiles(ctx, preCommit, d.targetCommit)
	if err != nil {
		return false, err
	}
	checkingPaths := make([]string, 0, len(appCfg.Trigger.OnCommit.Paths)+len(appCfg.TriggerPaths))
	for _, p := range appCfg.Trigger.OnCommit.Paths {
		if p != "" {
			checkingPaths = append(checkingPaths, p)
		}
	}
	for _, p := range appCfg.TriggerPaths {
		if p != "" {
			checkingPaths = append(checkingPaths, p)
		}
	}
	touched, err := isTouchedByChangedFiles(app.GitPath.Path, checkingPaths, changedFiles)
	if err != nil {
		return false, err
	}
	if !touched {
		logger.Info("application was not touched by any new commits", zap.String("last-triggered-commit", preCommit))
		return false, nil
	}
	return true, nil
}
func ( d * OnCommitDeterminer ) ShouldTrigger ( ctx context . Context , app * model . Application , appCfg * config . GenericDeploymentSpec ) ( bool , error ) { logger := d . logger . With ( zap . String ( " " , app . Name ) , zap . String ( " " , app . Id ) , zap . String ( " " , d . targetCommit ) , ) if appCfg . Trigger . OnCommit . Disabled { logger . Info ( fmt . Sprintf ( " " , d . targetCommit ) ) return false , nil } preCommit , err := d . commitGetter . Get ( ctx , app . Id ) if err != nil { logger . Error ( " " , zap . Error ( err ) ) return false , err } if preCommit == " " { logger . Info ( " " ) return true , nil } if preCommit == d . targetCommit { logger . Info ( fmt . Sprintf ( " " , d . targetCommit ) ) return false , nil } changedFiles , err := d . repo . ChangedFiles ( ctx , preCommit , d . targetCommit ) if err != nil { return false , err } checkingPaths := make ( [ ] string , 0 , len ( appCfg . Trigger . OnCommit . Paths ) + len ( appCfg . TriggerPaths ) ) for _ , p := range appCfg . Trigger . OnCommit . Paths { if p != " " { checkingPaths = append ( checkingPaths , p ) } } for _ , p := range appCfg . TriggerPaths { if p != " " { checkingPaths = append ( checkingPaths , p ) } } touched , err := isTouchedByChangedFiles ( app . GitPath . Path , checkingPaths , changedFiles ) if err != nil { return false , err } if ! touched { logger . Info ( " " , zap . String ( " " , preCommit ) ) return false , nil } return true , nil }
ShouldTrigger decides whether a given application should be triggered or not.
ShouldTrigger decides whether a given application should be triggered or not .
// Not trigger in case users disable auto trigger deploy on change and the user config is unignorable. // There is no previous deployment so we don't need to check anymore. // Just do it. // Check whether the most recently applied one is the target commit or not. // If so, nothing to do for this time. // List the changed files between those two commits and // determine whether this application was touch by those changed files. // TODO: Remove deprecated `appCfg.TriggerPaths` configuration. // Note: appCfg.TriggerPaths or appCfg.Trigger.OnCommit.Paths may contain "" (empty string) // in case users use one of them without the other, that cause unexpected "" path in the checkingPaths list // leads to always trigger deployment since "" path matched all other paths. // The below logic is to remove that "" path from checking path list, will remove after remove the // deprecated appCfg.TriggerPaths.