Search in sources :

Example 6 with BuildInfoModel

use of io.jpom.model.data.BuildInfoModel in project Jpom by dromara.

the class BuildInfoController method updateBuild.

/**
 * edit build info
 *
 * @param id            构建ID
 * @param name          构建名称
 * @param repositoryId  仓库ID
 * @param resultDirFile 构建产物目录
 * @param script        构建命令
 * @param releaseMethod 发布方法
 * @param branchName    分支名称
 * @param webhook       webhook
 * @param extraData     构建的其他信息
 * @param autoBuildCron 自动构建表达是
 * @param branchTagName 标签名
 * @return json
 */
@RequestMapping(value = "/build/edit", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@Feature(method = MethodFeature.EDIT)
public String updateBuild(String id, @ValidatorItem(value = ValidatorRule.NOT_BLANK, msg = "构建名称不能为空") String name, @ValidatorItem(value = ValidatorRule.NOT_BLANK, msg = "仓库信息不能为空") String repositoryId, @ValidatorItem(value = ValidatorRule.NOT_BLANK, msg = "构建产物目录不能为空,长度1-200", range = "1:200") String resultDirFile, @ValidatorItem(value = ValidatorRule.NOT_BLANK, msg = "构建命令不能为空") String script, @ValidatorItem(value = ValidatorRule.POSITIVE_INTEGER, msg = "发布方法不正确") int releaseMethod, String branchName, String branchTagName, String webhook, String autoBuildCron, String extraData, String group, @ValidatorItem(value = ValidatorRule.POSITIVE_INTEGER, msg = "构建方式不正确") int buildMode) {
    // 根据 repositoryId 查询仓库信息
    RepositoryModel repositoryModel = repositoryService.getByKey(repositoryId, getRequest());
    Assert.notNull(repositoryModel, "无效的仓库信息");
    // 如果是 GIT 需要检测分支是否存在
    if (RepositoryModel.RepoType.Git.getCode() == repositoryModel.getRepoType()) {
        Assert.hasText(branchName, "请选择分支");
    } else if (RepositoryModel.RepoType.Svn.getCode() == repositoryModel.getRepoType()) {
        // 如果是 SVN
        branchName = "trunk";
    }
    // 
    Assert.state(buildMode == 0 || buildMode == 1, "请选择正确的构建方式");
    if (buildMode == 1) {
        // 验证 dsl 内容
        this.checkDocker(script);
    }
    if (ServerExtConfigBean.getInstance().getBuildCheckDeleteCommand()) {
        // 判断删除命令
        Assert.state(!CommandUtil.checkContainsDel(script), "不能包含删除命令");
    }
    // 查询构建信息
    BuildInfoModel buildInfoModel = buildInfoService.getByKey(id, getRequest());
    buildInfoModel = ObjectUtil.defaultIfNull(buildInfoModel, new BuildInfoModel());
    // 设置参数
    if (StrUtil.isNotEmpty(webhook)) {
        Validator.validateMatchRegex(RegexPool.URL_HTTP, webhook, "WebHooks 地址不合法");
    }
    buildInfoModel.setAutoBuildCron(this.checkCron(autoBuildCron));
    buildInfoModel.setWebhook(webhook);
    buildInfoModel.setRepositoryId(repositoryId);
    buildInfoModel.setName(name);
    buildInfoModel.setBranchName(branchName);
    buildInfoModel.setBranchTagName(branchTagName);
    buildInfoModel.setResultDirFile(resultDirFile);
    buildInfoModel.setScript(script);
    buildInfoModel.setGroup(group);
    buildInfoModel.setBuildMode(buildMode);
    // 发布方式
    BuildReleaseMethod releaseMethod1 = BaseEnum.getEnum(BuildReleaseMethod.class, releaseMethod);
    Assert.notNull(releaseMethod1, "发布方法不正确");
    buildInfoModel.setReleaseMethod(releaseMethod1.getCode());
    // 把 extraData 信息转换成 JSON 字符串
    JSONObject jsonObject = JSON.parseObject(extraData);
    // 验证发布方式 和 extraData 信息
    if (releaseMethod1 == BuildReleaseMethod.Project) {
        this.formatProject(jsonObject);
    } else if (releaseMethod1 == BuildReleaseMethod.Ssh) {
        this.formatSsh(jsonObject);
    } else if (releaseMethod1 == BuildReleaseMethod.Outgiving) {
        String releaseMethodDataId = jsonObject.getString("releaseMethodDataId_1");
        Assert.hasText(releaseMethodDataId, "请选择分发项目");
        jsonObject.put("releaseMethodDataId", releaseMethodDataId);
    } else if (releaseMethod1 == BuildReleaseMethod.LocalCommand) {
        this.formatLocalCommand(jsonObject);
        jsonObject.put("releaseMethodDataId", "LocalCommand");
    } else if (releaseMethod1 == BuildReleaseMethod.DockerImage) {
        // dockerSwarmId default
        String dockerSwarmId = this.formatDocker(jsonObject);
        jsonObject.put("releaseMethodDataId", dockerSwarmId);
    }
    // 检查关联数据ID
    buildInfoModel.setReleaseMethodDataId(jsonObject.getString("releaseMethodDataId"));
    if (buildInfoModel.getReleaseMethod() != BuildReleaseMethod.No.getCode()) {
        Assert.hasText(buildInfoModel.getReleaseMethodDataId(), "没有发布分发对应关联数据ID");
    }
    buildInfoModel.setExtraData(jsonObject.toJSONString());
    // 新增构建信息
    if (StrUtil.isEmpty(id)) {
        // set default buildId
        buildInfoModel.setBuildId(0);
        buildInfoService.insert(buildInfoModel);
        return JsonMessage.getString(200, "添加成功");
    }
    buildInfoService.updateById(buildInfoModel, getRequest());
    return JsonMessage.getString(200, "修改成功");
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) BuildReleaseMethod(io.jpom.model.enums.BuildReleaseMethod) RepositoryModel(io.jpom.model.data.RepositoryModel) BuildInfoModel(io.jpom.model.data.BuildInfoModel) Feature(io.jpom.permission.Feature) MethodFeature(io.jpom.permission.MethodFeature) ClassFeature(io.jpom.permission.ClassFeature)

Example 7 with BuildInfoModel

use of io.jpom.model.data.BuildInfoModel in project Jpom by dromara.

the class BuildExecuteService method start.

/**
 * start build
 *
 * @param buildInfoId      构建Id
 * @param userModel        用户信息
 * @param delay            延迟的时间
 * @param triggerBuildType 触发构建类型
 * @return json
 */
public JsonMessage<Integer> start(String buildInfoId, UserModel userModel, Integer delay, int triggerBuildType, String buildRemark) {
    synchronized (buildInfoId.intern()) {
        BuildInfoModel buildInfoModel = buildService.getByKey(buildInfoId);
        String e = this.checkStatus(buildInfoModel.getStatus());
        Assert.isNull(e, () -> e);
        // set buildId field
        int buildId = ObjectUtil.defaultIfNull(buildInfoModel.getBuildId(), 0);
        {
            BuildInfoModel buildInfoModel1 = new BuildInfoModel();
            buildInfoModel1.setBuildId(buildId + 1);
            buildInfoModel1.setId(buildInfoId);
            buildInfoModel.setBuildId(buildInfoModel1.getBuildId());
            buildService.update(buildInfoModel1);
        }
        // load repository
        RepositoryModel repositoryModel = repositoryService.getByKey(buildInfoModel.getRepositoryId(), false);
        Assert.notNull(repositoryModel, "仓库信息不存在");
        Map<String, String> env = workspaceEnvVarService.getEnv(buildInfoModel.getWorkspaceId());
        BuildExecuteService.TaskData.TaskDataBuilder taskBuilder = BuildExecuteService.TaskData.builder().buildInfoModel(buildInfoModel).repositoryModel(repositoryModel).userModel(userModel).buildRemark(buildRemark).delay(delay).env(env).triggerBuildType(triggerBuildType);
        this.runTask(taskBuilder.build());
        String msg = (delay == null || delay <= 0) ? "开始构建中" : "延迟" + delay + "秒后开始构建";
        return new JsonMessage<>(200, msg, buildInfoModel.getBuildId());
    }
}
Also used : JsonMessage(cn.jiangzeyin.common.JsonMessage) RepositoryModel(io.jpom.model.data.RepositoryModel) BuildInfoModel(io.jpom.model.data.BuildInfoModel)

Example 8 with BuildInfoModel

use of io.jpom.model.data.BuildInfoModel in project Jpom by dromara.

the class BuildInfoManageController method start.

/**
 * 开始构建
 *
 * @param id id
 * @return json
 */
@RequestMapping(value = "/build/manage/start", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@Feature(method = MethodFeature.EXECUTE)
public String start(@ValidatorItem(value = ValidatorRule.NOT_BLANK, msg = "没有数据") String id, String buildRemark, String resultDirFile, String branchName, String branchTagName) {
    BuildInfoModel item = buildInfoService.getByKey(id, getRequest());
    Assert.notNull(item, "没有对应数据");
    // 更新数据
    BuildInfoModel update = new BuildInfoModel();
    if (StrUtil.isNotEmpty(resultDirFile)) {
        update.setResultDirFile(resultDirFile);
    }
    if (StrUtil.isNotEmpty(branchName)) {
        update.setBranchName(branchName);
    }
    if (StrUtil.isNotEmpty(branchTagName)) {
        update.setBranchTagName(branchTagName);
    }
    if (!StrUtil.isAllBlank(resultDirFile, branchName, branchTagName)) {
        update.setId(id);
        buildInfoService.update(update);
    }
    // userModel
    UserModel userModel = getUser();
    // 执行构建
    return buildExecuteService.start(item.getId(), userModel, null, 0, buildRemark).toString();
}
Also used : UserModel(io.jpom.model.data.UserModel) BuildInfoModel(io.jpom.model.data.BuildInfoModel) Feature(io.jpom.permission.Feature) MethodFeature(io.jpom.permission.MethodFeature) ClassFeature(io.jpom.permission.ClassFeature) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 9 with BuildInfoModel

use of io.jpom.model.data.BuildInfoModel in project Jpom by dromara.

the class BuildInfoManageController method reRelease.

/**
 * 重新发布
 *
 * @param logId logId
 * @return json
 */
@RequestMapping(value = "/build/manage/reRelease", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@Feature(method = MethodFeature.EXECUTE)
public String reRelease(@ValidatorConfig(@ValidatorItem(value = ValidatorRule.NOT_BLANK, msg = "没有数据")) String logId) {
    BuildHistoryLog buildHistoryLog = dbBuildHistoryLogService.getByKey(logId, getRequest());
    Objects.requireNonNull(buildHistoryLog, "没有对应构建记录.");
    BuildInfoModel item = buildInfoService.getByKey(buildHistoryLog.getBuildDataId());
    Objects.requireNonNull(item, "没有对应数据");
    String e = buildExecuteService.checkStatus(item.getStatus());
    Assert.isNull(e, () -> e);
    UserModel userModel = getUser();
    BuildExtraModule buildExtraModule = BuildExtraModule.build(buildHistoryLog);
    // new BuildExtraModule();
    // buildExtraModule.updateValue(buildHistoryLog);
    ReleaseManage manage = ReleaseManage.builder().buildExtraModule(buildExtraModule).logId(buildHistoryLog.getId()).userModel(userModel).buildId(buildHistoryLog.getBuildNumberId()).buildExecuteService(buildExecuteService).build();
    // ReleaseManage releaseManage = new ReleaseManage(buildHistoryLog, userModel);
    // 标记发布中
    // releaseManage.updateStatus(BuildStatus.PubIng);
    ThreadUtil.execute(manage);
    return JsonMessage.getString(200, "重新发布中");
}
Also used : UserModel(io.jpom.model.data.UserModel) BuildHistoryLog(io.jpom.model.log.BuildHistoryLog) ReleaseManage(io.jpom.build.ReleaseManage) BuildExtraModule(io.jpom.build.BuildExtraModule) BuildInfoModel(io.jpom.model.data.BuildInfoModel) Feature(io.jpom.permission.Feature) MethodFeature(io.jpom.permission.MethodFeature) ClassFeature(io.jpom.permission.ClassFeature) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 10 with BuildInfoModel

use of io.jpom.model.data.BuildInfoModel in project Jpom by dromara.

the class BuildInfoManageController method cancel.

/**
 * 取消构建
 *
 * @param id id
 * @return json
 */
@RequestMapping(value = "/build/manage/cancel", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@Feature(method = MethodFeature.EXECUTE)
public String cancel(@ValidatorConfig(@ValidatorItem(value = ValidatorRule.NOT_BLANK, msg = "没有数据")) String id) {
    BuildInfoModel item = buildInfoService.getByKey(id, getRequest());
    Objects.requireNonNull(item, "没有对应数据");
    BuildStatus nowStatus = BaseEnum.getEnum(BuildStatus.class, item.getStatus());
    Objects.requireNonNull(nowStatus);
    if (BuildStatus.Ing != nowStatus && BuildStatus.PubIng != nowStatus) {
        return JsonMessage.getString(501, "当前状态不在进行中");
    }
    boolean status = buildExecuteService.cancelTask(item.getId());
    if (!status) {
        /*BuildInfoModel buildInfoModel = new BuildInfoModel();
			buildInfoModel.setId(id);
			buildInfoModel.setStatus(BuildStatus.Cancel.getCode());*/
        buildInfoService.updateStatus(id, BuildStatus.Cancel);
    }
    return JsonMessage.getString(200, "取消成功");
}
Also used : BuildStatus(io.jpom.model.enums.BuildStatus) BuildInfoModel(io.jpom.model.data.BuildInfoModel) Feature(io.jpom.permission.Feature) MethodFeature(io.jpom.permission.MethodFeature) ClassFeature(io.jpom.permission.ClassFeature) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

BuildInfoModel (io.jpom.model.data.BuildInfoModel)22 ClassFeature (io.jpom.permission.ClassFeature)12 Feature (io.jpom.permission.Feature)12 MethodFeature (io.jpom.permission.MethodFeature)12 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)8 File (java.io.File)7 UserModel (io.jpom.model.data.UserModel)5 BuildHistoryLog (io.jpom.model.log.BuildHistoryLog)5 JSONObject (com.alibaba.fastjson.JSONObject)4 JsonMessage (cn.jiangzeyin.common.JsonMessage)3 BuildStatus (io.jpom.model.enums.BuildStatus)3 RepositoryModel (io.jpom.model.data.RepositoryModel)2 Convert (cn.hutool.core.convert.Convert)1 RegexPool (cn.hutool.core.lang.RegexPool)1 Validator (cn.hutool.core.lang.Validator)1 ObjectUtil (cn.hutool.core.util.ObjectUtil)1 StrUtil (cn.hutool.core.util.StrUtil)1 Entity (cn.hutool.db.Entity)1 ServletUtil (cn.hutool.extra.servlet.ServletUtil)1 DefaultSystemLog (cn.jiangzeyin.common.DefaultSystemLog)1