package cn.reghao.bnt.web.devops.build.model.provider;

import cn.reghao.bnt.web.devops.build.model.po.RepoAuthConfig;
import cn.reghao.bnt.web.devops.builder.model.constant.RepoAuthType;
import org.hibernate.validator.spi.group.DefaultGroupSequenceProvider;

import java.util.ArrayList;
import java.util.List;

/**
 * @author reghao
 * @date 2023-03-10 20:43:47
 */
public class RepoAuthGroupSequenceProvider implements DefaultGroupSequenceProvider<RepoAuthConfig> {
    @Override
    public List<Class<?>> getValidationGroups(RepoAuthConfig repoAuth) {
        List<Class<?>> defaultGroupSequence = new ArrayList<>();
        // 这一步不能省,否则Default分组都不会执行了，会抛错的
        defaultGroupSequence.add(RepoAuthConfig.class);

        if (repoAuth != null) {
            String authType = repoAuth.getAuthType();
            if (authType.equals(RepoAuthType.none.getName())) {
                defaultGroupSequence.add(RepoAuthConfig.NoneRepoAuth.class);
            } else if (authType.equals(RepoAuthType.http.getName())) {
                defaultGroupSequence.add(RepoAuthConfig.HttpRepoAuth.class);
            } else if (authType.equals(RepoAuthType.ssh.getName())) {
                defaultGroupSequence.add(RepoAuthConfig.SshRepoAuth.class);
            }
        }

        return defaultGroupSequence;
    }
}
