Skip to content

Commit

Permalink
feat:流水线变量语法支持两种风格 #10576
Browse files Browse the repository at this point in the history
  • Loading branch information
mingshewhe committed Sep 25, 2024
1 parent b1e730e commit e24a210
Showing 1 changed file with 8 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import com.tencent.devops.common.api.util.JsonUtil;
import com.tencent.devops.common.api.util.ReflectUtil;
import com.tencent.devops.common.pipeline.utils.ExprReplacementUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashSet;
import java.util.Iterator;
Expand All @@ -40,6 +42,7 @@

public class ExprReplaceEnvVarUtil {

private static final Logger logger = LoggerFactory.getLogger(ExprReplaceEnvVarUtil.class);
public static Object replaceEnvVar(Object obj, Map<String, String> envMap) {
return replaceEnvVar(obj,
new ExprReplacementOptions(envMap, false, null, null, null)
Expand All @@ -60,6 +63,7 @@ public static Object replaceEnvVar(Object obj, ExprReplacementOptions options) {
Set<Map.Entry<String, Object>> entrySet = ((Map) obj).entrySet();
for (Map.Entry entry : entrySet) {
Object value = entry.getValue();
logger.info("replace map entry:{}", value);
if (!isNormalReplaceEnvVar(value)) {
entry.setValue(replaceEnvVar(value, options));
} else {
Expand All @@ -71,6 +75,7 @@ public static Object replaceEnvVar(Object obj, ExprReplacementOptions options) {
List dataList = (List) obj;
for (int i = 0; i < dataList.size(); i++) {
Object value = dataList.get(i);
logger.info("replace list entry:{}", value);
if (!isNormalReplaceEnvVar(value)) {
dataList.set(i, replaceEnvVar(value, options));
} else {
Expand Down Expand Up @@ -117,6 +122,7 @@ private static Object handleNormalEnvVar(Object obj, ExprReplacementOptions opti
if (objStr.startsWith("{") && objStr.endsWith("}") && JsonSchemaUtil.INSTANCE.validateJson(objStr)) {
try {
Object dataObj = JsonUtil.INSTANCE.to((String) obj, Map.class);
logger.info("replace string map:{}", dataObj);
// string能正常转换成map,则说明是json串,那么把dataObj进行递归替换变量后再转成json串
dataObj = replaceEnvVar(dataObj, options);
obj = JsonUtil.INSTANCE.toJson(dataObj, true);
Expand All @@ -129,10 +135,12 @@ private static Object handleNormalEnvVar(Object obj, ExprReplacementOptions opti
} else if (objStr.startsWith("[") && objStr.endsWith("]") && JsonSchemaUtil.INSTANCE.validateJson(objStr)) {
try {
Object dataObj = JsonUtil.INSTANCE.to((String) obj, List.class);
logger.info("replace string list:{}", dataObj);
// string能正常转成list,说明是json串,把dataObj进行递归替换变量后再转成json串
dataObj = replaceEnvVar(dataObj, options);
obj = JsonUtil.INSTANCE.toJson(dataObj, true);
} catch (Throwable e1) {
logger.error("replace error", e1);
// 转换不了list的字符串对象则直接替换
obj = ExprReplacementUtil.INSTANCE.parseExpression(
JsonUtil.INSTANCE.toJson(obj, true), options
Expand Down

0 comments on commit e24a210

Please sign in to comment.