package cn.reghao.autodop.dmaster.app.model.po.log;

import cn.reghao.jdkutil.result.Result;
import cn.reghao.autodop.dmaster.app.model.po.config.AppConfig;
import cn.reghao.autodop.dmaster.app.service.bd.tools.repo.CommitInfo;
import cn.reghao.autodop.dmaster.app.model.vo.BuildConfig;
import cn.reghao.autodop.dmaster.rbac.UserContext;
import cn.reghao.autodop.dmaster.util.db.BaseDocument;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.data.mongodb.core.mapping.Document;

/**
 * 应用构建日志
 *
 * @author reghao
 * @date 2020-03-10 11:22:05
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Document("BuildLog")
public class BuildLog extends BaseDocument {
    private String appId;
    private String appType;
    private String env;
    private String repoBranch;
    private BuildConfig buildConfig;
    private String packScript;
    private String packagePath;

    private CommitInfo commitInfo;
    private BuildTime buildTime;
    private Result result;
    private String buildBy;

    public static BuildLog from(AppConfig app) {
        BuildLog buildLog = new BuildLog();
        buildLog.setAppId(app.getAppId());
        buildLog.setAppType(app.getAppType());
        buildLog.setEnv(app.getEnv());
        buildLog.setRepoBranch(app.getRepoBranch());
        buildLog.setBuildConfig(
                new BuildConfig(app.getRepoAuthConfig(), app.getCompilerConfig(), app.getPackerConfig()));
        buildLog.setPackScript(app.getPackScript());

        buildLog.setBuildTime(new BuildTime());
        buildLog.setResult(new Result());
        buildLog.setBuildBy(UserContext.currentUser().getNickname());
        return buildLog;
    }
}
