Search in sources :

Example 1 with RepositoryModel

use of io.jpom.model.data.RepositoryModel 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 2 with RepositoryModel

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

the class BuildInfoController method branchList.

/**
 * 获取分支信息
 *
 * @param repositoryId 仓库id
 * @return json
 * @throws Exception 异常
 */
@RequestMapping(value = "/build/branch-list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@Feature(method = MethodFeature.LIST)
public String branchList(@ValidatorConfig(@ValidatorItem(value = ValidatorRule.NOT_BLANK, msg = "仓库ID不能为空")) String repositoryId) throws Exception {
    // 根据 repositoryId 查询仓库信息
    RepositoryModel repositoryModel = repositoryService.getByKey(repositoryId, false);
    Assert.notNull(repositoryModel, "无效的仓库信息");
    // 
    Assert.state(repositoryModel.getRepoType() == 0, "只有 GIT 仓库才有分支信息");
    IPlugin plugin = PluginFactory.getPlugin("git-clone");
    Map<String, Object> map = repositoryModel.toMap();
    Tuple branchAndTagList = (Tuple) plugin.execute("branchAndTagList", map);
    Assert.notNull(branchAndTagList, "没有任何分支");
    Object[] members = branchAndTagList.getMembers();
    return JsonMessage.getString(200, "ok", members);
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) RepositoryModel(io.jpom.model.data.RepositoryModel) Tuple(cn.hutool.core.lang.Tuple) Feature(io.jpom.permission.Feature) MethodFeature(io.jpom.permission.MethodFeature) ClassFeature(io.jpom.permission.ClassFeature)

Example 3 with RepositoryModel

use of io.jpom.model.data.RepositoryModel 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 4 with RepositoryModel

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

the class RepositoryController method editRepository.

/**
 * edit
 *
 * @param repositoryModelReq 仓库实体
 * @return json
 */
@PostMapping(value = "/build/repository/edit")
@Feature(method = MethodFeature.EDIT)
public Object editRepository(RepositoryModel repositoryModelReq) {
    this.checkInfo(repositoryModelReq);
    // 检查 rsa 私钥
    boolean andUpdateSshKey = this.checkAndUpdateSshKey(repositoryModelReq);
    Assert.state(andUpdateSshKey, "rsa 私钥文件不存在或者有误");
    if (repositoryModelReq.getRepoType() == RepositoryModel.RepoType.Git.getCode()) {
        RepositoryModel repositoryModel = repositoryService.getByKey(repositoryModelReq.getId(), false);
        if (repositoryModel != null) {
            repositoryModelReq.setRsaPrv(StrUtil.emptyToDefault(repositoryModelReq.getRsaPrv(), repositoryModel.getRsaPrv()));
            repositoryModelReq.setPassword(StrUtil.emptyToDefault(repositoryModelReq.getPassword(), repositoryModel.getPassword()));
        }
        // 验证 git 仓库信息
        try {
            IPlugin plugin = PluginFactory.getPlugin("git-clone");
            Map<String, Object> map = repositoryModelReq.toMap();
            Tuple branchAndTagList = (Tuple) plugin.execute("branchAndTagList", map);
        // Tuple tuple = GitUtil.getBranchAndTagList(repositoryModelReq);
        } catch (JpomRuntimeException jpomRuntimeException) {
            throw jpomRuntimeException;
        } catch (Exception e) {
            DefaultSystemLog.getLog().warn("获取仓库分支失败", e);
            return JsonMessage.toJson(500, "无法连接此仓库," + e.getMessage());
        }
    }
    if (StrUtil.isEmpty(repositoryModelReq.getId())) {
        // insert data
        repositoryService.insert(repositoryModelReq);
    } else {
        // update data
        // repositoryModelReq.setWorkspaceId(repositoryService.getCheckUserWorkspace(getRequest()));
        repositoryService.updateById(repositoryModelReq, getRequest());
    }
    return JsonMessage.toJson(200, "操作成功");
}
Also used : JpomRuntimeException(io.jpom.system.JpomRuntimeException) JSONObject(com.alibaba.fastjson.JSONObject) RepositoryModel(io.jpom.model.data.RepositoryModel) Tuple(cn.hutool.core.lang.Tuple) JpomRuntimeException(io.jpom.system.JpomRuntimeException) PostMapping(org.springframework.web.bind.annotation.PostMapping) Feature(io.jpom.permission.Feature) MethodFeature(io.jpom.permission.MethodFeature) ClassFeature(io.jpom.permission.ClassFeature)

Example 5 with RepositoryModel

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

the class RepositoryController method restHideField.

/**
 * edit
 *
 * @param id 仓库信息
 * @return json
 */
@PostMapping(value = "/build/repository/rest_hide_field")
@Feature(method = MethodFeature.EDIT)
public Object restHideField(@ValidatorItem String id) {
    RepositoryModel repositoryModel = new RepositoryModel();
    repositoryModel.setId(id);
    repositoryModel.setPassword(StrUtil.EMPTY);
    repositoryModel.setRsaPrv(StrUtil.EMPTY);
    repositoryModel.setRsaPub(StrUtil.EMPTY);
    repositoryModel.setWorkspaceId(repositoryService.getCheckUserWorkspace(getRequest()));
    repositoryService.updateById(repositoryModel);
    return JsonMessage.toJson(200, "操作成功");
}
Also used : RepositoryModel(io.jpom.model.data.RepositoryModel) PostMapping(org.springframework.web.bind.annotation.PostMapping) Feature(io.jpom.permission.Feature) MethodFeature(io.jpom.permission.MethodFeature) ClassFeature(io.jpom.permission.ClassFeature)

Aggregations

RepositoryModel (io.jpom.model.data.RepositoryModel)5 ClassFeature (io.jpom.permission.ClassFeature)4 Feature (io.jpom.permission.Feature)4 MethodFeature (io.jpom.permission.MethodFeature)4 JSONObject (com.alibaba.fastjson.JSONObject)3 Tuple (cn.hutool.core.lang.Tuple)2 BuildInfoModel (io.jpom.model.data.BuildInfoModel)2 PostMapping (org.springframework.web.bind.annotation.PostMapping)2 JsonMessage (cn.jiangzeyin.common.JsonMessage)1 BuildReleaseMethod (io.jpom.model.enums.BuildReleaseMethod)1 JpomRuntimeException (io.jpom.system.JpomRuntimeException)1