Search in sources :

Example 1 with BuildReleaseMethod

use of io.jpom.model.enums.BuildReleaseMethod 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)

Aggregations

JSONObject (com.alibaba.fastjson.JSONObject)1 BuildInfoModel (io.jpom.model.data.BuildInfoModel)1 RepositoryModel (io.jpom.model.data.RepositoryModel)1 BuildReleaseMethod (io.jpom.model.enums.BuildReleaseMethod)1 ClassFeature (io.jpom.permission.ClassFeature)1 Feature (io.jpom.permission.Feature)1 MethodFeature (io.jpom.permission.MethodFeature)1