Search in sources :

Example 1 with ReleaseCompareResult

use of com.ctrip.framework.apollo.portal.entity.vo.ReleaseCompareResult in project apollo by ctripcorp.

the class ConfigPublishEmailBuilder method renderDiffModule.

private String renderDiffModule(String bodyTemplate, Env env, ReleaseHistoryBO releaseHistory) {
    String appId = releaseHistory.getAppId();
    String namespaceName = releaseHistory.getNamespaceName();
    AppNamespace appNamespace = appNamespaceService.findByAppIdAndName(appId, namespaceName);
    if (appNamespace == null) {
        appNamespace = appNamespaceService.findPublicAppNamespace(namespaceName);
    }
    // don't show diff content if namespace's format is file
    if (appNamespace == null || !appNamespace.getFormat().equals(ConfigFileFormat.Properties.getValue())) {
        return bodyTemplate.replaceAll(EMAIL_CONTENT_DIFF_MODULE, "<br><h4>变更内容请点击链接到Apollo上查看</h4>");
    }
    ReleaseCompareResult result = getReleaseCompareResult(env, releaseHistory);
    if (!result.hasContent()) {
        return bodyTemplate.replaceAll(EMAIL_CONTENT_DIFF_MODULE, "<br><h4>无配置变更</h4>");
    }
    List<Change> changes = result.getChanges();
    StringBuilder changesHtmlBuilder = new StringBuilder();
    for (Change change : changes) {
        String key = change.getEntity().getFirstEntity().getKey();
        String oldValue = change.getEntity().getFirstEntity().getValue();
        String newValue = change.getEntity().getSecondEntity().getValue();
        newValue = newValue == null ? "" : newValue;
        changesHtmlBuilder.append("<tr>");
        changesHtmlBuilder.append("<td width=\"10%\">").append(change.getType().toString()).append("</td>");
        changesHtmlBuilder.append("<td width=\"20%\">").append(cutOffString(key)).append("</td>");
        changesHtmlBuilder.append("<td width=\"35%\">").append(cutOffString(oldValue)).append("</td>");
        changesHtmlBuilder.append("<td width=\"35%\">").append(cutOffString(newValue)).append("</td>");
        changesHtmlBuilder.append("</tr>");
    }
    String diffContent = Matcher.quoteReplacement(changesHtmlBuilder.toString());
    String diffModuleTemplate = getDiffModuleTemplate();
    String diffModuleRenderResult = diffModuleTemplate.replaceAll(EMAIL_CONTENT_FIELD_DIFF_CONTENT, diffContent);
    return bodyTemplate.replaceAll(EMAIL_CONTENT_DIFF_MODULE, diffModuleRenderResult);
}
Also used : AppNamespace(com.ctrip.framework.apollo.common.entity.AppNamespace) Change(com.ctrip.framework.apollo.portal.entity.vo.Change) ReleaseCompareResult(com.ctrip.framework.apollo.portal.entity.vo.ReleaseCompareResult)

Example 2 with ReleaseCompareResult

use of com.ctrip.framework.apollo.portal.entity.vo.ReleaseCompareResult in project apollo by ctripcorp.

the class ReleaseService method compare.

public ReleaseCompareResult compare(ReleaseDTO baseRelease, ReleaseDTO toCompareRelease) {
    Map<String, String> baseReleaseConfiguration = baseRelease == null ? new HashMap<>() : gson.fromJson(baseRelease.getConfigurations(), GsonType.CONFIG);
    Map<String, String> toCompareReleaseConfiguration = toCompareRelease == null ? new HashMap<>() : gson.fromJson(toCompareRelease.getConfigurations(), GsonType.CONFIG);
    ReleaseCompareResult compareResult = new ReleaseCompareResult();
    // added and modified in firstRelease
    for (Map.Entry<String, String> entry : baseReleaseConfiguration.entrySet()) {
        String key = entry.getKey();
        String firstValue = entry.getValue();
        String secondValue = toCompareReleaseConfiguration.get(key);
        // added
        if (secondValue == null) {
            compareResult.addEntityPair(ChangeType.DELETED, new KVEntity(key, firstValue), new KVEntity(key, null));
        } else if (!Objects.equal(firstValue, secondValue)) {
            compareResult.addEntityPair(ChangeType.MODIFIED, new KVEntity(key, firstValue), new KVEntity(key, secondValue));
        }
    }
    // deleted in firstRelease
    for (Map.Entry<String, String> entry : toCompareReleaseConfiguration.entrySet()) {
        String key = entry.getKey();
        String value = entry.getValue();
        if (baseReleaseConfiguration.get(key) == null) {
            compareResult.addEntityPair(ChangeType.ADDED, new KVEntity(key, ""), new KVEntity(key, value));
        }
    }
    return compareResult;
}
Also used : KVEntity(com.ctrip.framework.apollo.portal.entity.bo.KVEntity) HashMap(java.util.HashMap) Map(java.util.Map) ReleaseCompareResult(com.ctrip.framework.apollo.portal.entity.vo.ReleaseCompareResult)

Aggregations

ReleaseCompareResult (com.ctrip.framework.apollo.portal.entity.vo.ReleaseCompareResult)2 AppNamespace (com.ctrip.framework.apollo.common.entity.AppNamespace)1 KVEntity (com.ctrip.framework.apollo.portal.entity.bo.KVEntity)1 Change (com.ctrip.framework.apollo.portal.entity.vo.Change)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1