Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(integration): external approval is always created when initiating a ticket #125

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public void approve_approveWithComment_approveSucceed() {

private FlowApprovalInstance createApprovalInstance(boolean startEndPoint, boolean endEndPoint, String name,
String activityId, Long flowInstanceId) {
FlowApprovalInstance instance = new FlowApprovalInstance(1L, flowInstanceId, null, null, 12, startEndPoint,
FlowApprovalInstance instance = new FlowApprovalInstance(1L, flowInstanceId, null, 12, startEndPoint,
endEndPoint, false, flowableAdaptor, taskService, formService, new LocalEventPublisher(),
authenticationFacade, nodeRepository, sequenceRepository, userTaskInstanceRepository);
instance.setName(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public void generateFlowGatewayInstance_createNewOne_returnFlowGatewayInstance()
public void generateFlowApprovalInstance_createNewOne_returnFlowApprovalInstance() {
FlowInstance flowInstance = flowFactory.generateFlowInstance("Test FlowInstance", "Test");
FlowApprovalInstance approvalInstance =
flowFactory.generateFlowApprovalInstance(flowInstance.getId(), true, false, false, 15, null, null);
flowFactory.generateFlowApprovalInstance(flowInstance.getId(), true, false, false, 15, null);

Assert.assertEquals((long) flowInstance.getId(), approvalInstance.getFlowInstanceId());
}
Expand Down Expand Up @@ -190,7 +190,7 @@ private FlowTaskInstance createTaskInstance(Long flowInstanceId, ExecutionStrate

private FlowApprovalInstance createApprovalInstance(Long flowInstanceId) {
return new FlowApprovalInstance(authenticationFacade.currentOrganizationId(), flowInstanceId, null,
null, 10, false, false, false, flowAdaptor, taskService, formService, new LocalEventPublisher(),
10, false, false, false, flowAdaptor, taskService, formService, new LocalEventPublisher(),
authenticationFacade, nodeRepository, sequenceRepository, userTaskInstanceRepository);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ public TestFlowApprovalInstance(@NonNull Long organizationId, @NonNull Long flow
@NonNull NodeInstanceEntityRepository nodeRepository,
@NonNull SequenceInstanceRepository sequenceRepository,
@NonNull UserTaskInstanceRepository userTaskInstanceRepository) {
super(organizationId, flowInstanceId, null, null, expireIntervalSeconds, startEndpoint, endEndPoint, false,
super(organizationId, flowInstanceId, null, expireIntervalSeconds, startEndpoint, endEndPoint, false,
flowableAdaptor, taskService, formService, eventPublisher, authenticationFacade, nodeRepository,
sequenceRepository, userTaskInstanceRepository);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ private FlowTaskInstance createTaskInstance(Long flowInstanceId, TaskType taskTy
}

private FlowApprovalInstance createApprovalInstance(Long flowInstanceId, Integer expireIntervalSeconds) {
return new FlowApprovalInstance(authenticationFacade.currentOrganizationId(), flowInstanceId, null, null,
return new FlowApprovalInstance(authenticationFacade.currentOrganizationId(), flowInstanceId, null,
expireIntervalSeconds, false, false, false, flowableAdaptor, taskService, formService,
new LocalEventPublisher(), authenticationFacade, nodeRepository, sequenceRepository,
userTaskInstanceRepository);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ private FlowTaskInstance createTaskInstance(Long flowInstanceId, ExecutionStrate

private FlowApprovalInstance createApprovalInstance(Long flowInstanceId) {
return new FlowApprovalInstance(authenticationFacade.currentOrganizationId(), flowInstanceId, null,
null, 10, false, false, false, flowAdaptor, taskService, formService, new LocalEventPublisher(),
10, false, false, false, flowAdaptor, taskService, formService, new LocalEventPublisher(),
authenticationFacade, nodeRepository, sequenceRepository, userTaskInstanceRepository);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ private Database getDatabase() {
private FlowApprovalInstance createApprovalInstance(Long flowInstanceId, Integer expireIntervalSeconds,
boolean startEndPoint, boolean endEndPoint) {
return new FlowApprovalInstance(authenticationFacade.currentOrganizationId(), flowInstanceId, null,
null, expireIntervalSeconds, startEndPoint, endEndPoint, false, flowAdaptor, taskService, formService,
expireIntervalSeconds, startEndPoint, endEndPoint, false, flowAdaptor, taskService, formService,
eventPublisher, authenticationFacade, nodeRepository, sequenceRepository, userTaskInstanceRepository);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ private FlowGatewayInstance createGatewayInstance(Long flowInstanceId) {
}

private FlowApprovalInstance createApprovalInstance(Long flowInstanceId) {
return new FlowApprovalInstance(1L, flowInstanceId, null, null, 12, true, true, false, flowableAdaptor,
return new FlowApprovalInstance(1L, flowInstanceId, null, 12, true, true, false, flowableAdaptor,
taskService, formService, new LocalEventPublisher(), authenticationFacade,
nodeRepository, sequenceRepository, userTaskInstanceRepository);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.oceanbase.odc.core.flow.model;

import com.oceanbase.odc.core.flow.builder.ErrorBoundaryEventBuilder;
import com.oceanbase.odc.core.flow.builder.ServiceTaskBuilder;
import com.oceanbase.odc.core.flow.builder.SignalCatchEventBuilder;
import com.oceanbase.odc.core.flow.builder.TimerBoundaryEventBuilder;
import com.oceanbase.odc.core.flow.builder.UserTaskBuilder;
Expand Down Expand Up @@ -64,4 +65,8 @@ public FlowableElement(@NonNull UserTaskBuilder userTaskBuilder) {
this(userTaskBuilder.getGraphId(), userTaskBuilder.getName(), FlowableElementType.USER_TASK);
}

public FlowableElement(@NonNull ServiceTaskBuilder serviceTaskBuilder) {
this(serviceTaskBuilder.getGraphId(), serviceTaskBuilder.getName(), FlowableElementType.SERVICE_TASK);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ List<UserTaskInstanceEntity> findByResourceRoleIdentifierIn(
@Transactional
@Query("update UserTaskInstanceEntity as ut set ut.userTaskId=:#{#param.userTaskId},ut.status=:#{#param.status},"
+ "ut.approved=:#{#param.approved},ut.operatorId=:#{#param.operatorId},ut.comment=:#{#param.comment},"
+ "ut.expireIntervalSeconds=:#{#param.expireIntervalSeconds} where ut.id=:#{#param.id}")
smallsheeeep marked this conversation as resolved.
Show resolved Hide resolved
+ "ut.expireIntervalSeconds=:#{#param.expireIntervalSeconds},ut.externalFlowInstanceId=:#{#param.externalFlowInstanceId}"
+ " where ut.id=:#{#param.id}")
@Modifying
int update(@Param("param") UserTaskInstanceEntity entity);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ public class FlowInstanceService {
private final List<Consumer<ShadowTableComparingUpdateEvent>> shadowTableComparingTaskHooks = new ArrayList<>();
private static final long MAX_EXPORT_OBJECT_COUNT = 10000;
private static final String ODC_SITE_URL = "odc.site.url";
private static final String INVALID_EXTERNAL_INSTANCE_ID = "N/A";

@PostConstruct
public void init() {
Expand Down Expand Up @@ -270,11 +269,6 @@ public List<FlowInstanceDetailResp> create(@NotNull @Valid CreateFlowInstanceReq
PreConditions.maxLength(taskParameters.getSqlContent(), "sql content",
flowTaskProperties.getSqlContentMaxLength());
}

Long connId = createReq.getConnectionId();
ConnectionConfig conn = connectionService.getForConnectionSkipPermissionCheck(connId);

// acquire export masking policy
if (createReq.getTaskType() == TaskType.EXPORT) {
DataTransferConfig dataTransferConfig = (DataTransferConfig) createReq.getParameters();
if (dataTransferConfig.getExportDbObjects().size() > MAX_EXPORT_OBJECT_COUNT) {
Expand All @@ -285,6 +279,7 @@ public List<FlowInstanceDetailResp> create(@NotNull @Valid CreateFlowInstanceReq
}
List<RiskLevel> riskLevels = riskLevelService.list();
Verify.notEmpty(riskLevels, "riskLevels");
ConnectionConfig conn = connectionService.getForConnectionSkipPermissionCheck(createReq.getConnectionId());
return Collections.singletonList(buildFlowInstance(riskLevels, createReq, conn));
}

Expand Down Expand Up @@ -686,8 +681,8 @@ private FlowInstanceDetailResp buildFlowInstance(List<RiskLevel> riskLevels,
flowInstance.newFlowInstance().next(riskDetectInstance).next(riskLevelGateway);
for (int i = 0; i < riskLevels.size(); i++) {
FlowInstanceConfigurer targetConfigurer = buildConfigurer(riskLevels.get(i).getApprovalFlowConfig(),
flowInstance, flowInstanceReq.getTaskType(), connectionConfig,
taskEntity.getId(), flowInstanceReq.getParameters(), flowInstanceReq);
flowInstance, flowInstanceReq.getTaskType(), taskEntity.getId(),
flowInstanceReq.getParameters(), flowInstanceReq);
startConfigurer.route(
String.format("${%s == %d}", RuntimeTaskConstants.RISKLEVEL, riskLevels.get(i).getLevel()),
targetConfigurer);
Expand Down Expand Up @@ -726,7 +721,6 @@ private FlowInstanceConfigurer buildConfigurer(
@NonNull ApprovalFlowConfig approvalFlowConfig,
@NonNull FlowInstance flowInstance,
@NonNull TaskType taskType,
@NonNull ConnectionConfig connectionConfig,
@NonNull Long targetTaskId,
@NonNull TaskParameters parameters,
@NonNull CreateFlowInstanceReq flowInstanceReq) {
Expand All @@ -737,23 +731,10 @@ private FlowInstanceConfigurer buildConfigurer(
FlowInstanceConfigurer configurer;
ApprovalNodeConfig nodeConfig = nodeConfigs.get(nodeSequence);
Long resourceRoleId = nodeConfig.getResourceRoleId();
String externalFlowInstanceId = null;
Long externalApprovalId = nodeConfig.getExternalApprovalId();
if (Objects.nonNull(externalApprovalId)) {
IntegrationConfig config = integrationService.detailWithoutPermissionCheck(externalApprovalId);
ApprovalProperties properties = ApprovalProperties.from(config);
TemplateVariables variables = buildTemplateVariables(flowInstanceReq, connectionConfig);
try {
externalFlowInstanceId = approvalClient.start(properties, variables);
} catch (Exception e) {
externalFlowInstanceId = INVALID_EXTERNAL_INSTANCE_ID;
log.warn("Create external approval instance failed, the instance will be force closed!");
}
}
FlowApprovalInstance approvalInstance = flowFactory.generateFlowApprovalInstance(flowInstance.getId(),
false, false,
nodeConfig.getAutoApproval(), approvalFlowConfig.getApprovalExpirationIntervalSeconds(),
externalApprovalId, externalFlowInstanceId);
nodeConfig.getExternalApprovalId());
if (Objects.nonNull(resourceRoleId)) {
approvalPermissionService.setCandidateResourceRole(approvalInstance.getId(),
StringUtils.join(flowInstanceReq.getProjectId(), ":", resourceRoleId));
Expand Down Expand Up @@ -854,11 +835,10 @@ private TemplateVariables buildTemplateVariables(CreateFlowInstanceReq flowInsta
variables.setAttribute(Variable.TASK_TYPE, taskType.getLocalizedMessage());
variables.setAttribute(Variable.TASK_DETAILS, JsonUtils.toJson(flowInstanceReq.getParameters()));
// set connection related variables
ConnectionConfig connection = connectionService.getWithoutPermissionCheck(flowInstanceReq.getConnectionId());
if (Objects.nonNull(connection)) {
variables.setAttribute(Variable.CONNECTION_NAME, connection.getName());
variables.setAttribute(Variable.CONNECTION_TENANT, connection.getTenantName());
for (Entry<String, String> entry : connection.getProperties().entrySet()) {
if (Objects.nonNull(config)) {
variables.setAttribute(Variable.CONNECTION_NAME, config.getName());
variables.setAttribute(Variable.CONNECTION_TENANT, config.getTenantName());
for (Entry<String, String> entry : config.getProperties().entrySet()) {
variables.setAttribute(Variable.CONNECTION_PROPERTIES, entry.getKey(), entry.getValue());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,10 @@ public FlowGatewayInstance generateFlowGatewayInstance(@NonNull Long flowInstanc
}

public FlowApprovalInstance generateFlowApprovalInstance(@NonNull Long flowInstanceId, boolean isStartEndPoint,
boolean isEndEndPoint, boolean autoApprove, int expireIntervalSeconds, Long externalApprovalId,
String externalFlowInstanceId) {
boolean isEndEndPoint, boolean autoApprove, int expireIntervalSeconds, Long externalApprovalId) {
Verify.verify(expireIntervalSeconds > 0, "ApprovalExpirationInterval can not be negative");
return new FlowApprovalInstance(authenticationFacade.currentOrganizationId(), flowInstanceId,
externalApprovalId, externalFlowInstanceId, expireIntervalSeconds, isStartEndPoint, isEndEndPoint,
externalApprovalId, expireIntervalSeconds, isStartEndPoint, isEndEndPoint,
autoApprove, flowableAdaptor, flowableTaskService, formService, eventPublisher, authenticationFacade,
nodeRepository, sequenceRepository, userTaskInstanceRepository);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

/**
Expand All @@ -61,7 +62,8 @@ public class FlowApprovalInstance extends BaseFlowUserTaskInstance {
private boolean approved;
private final Integer expireIntervalSeconds;
private final boolean autoApprove;
private final String externalFlowInstanceId;
@Setter
private String externalFlowInstanceId;
private final Long externalApprovalId;
private boolean waitForConfirm;

Expand Down Expand Up @@ -94,7 +96,7 @@ public FlowApprovalInstance(@NonNull UserTaskInstanceEntity entity, @NonNull Flo
}

public FlowApprovalInstance(@NonNull Long organizationId, @NonNull Long flowInstanceId,
Long externalApprovalId, String externalFlowInstanceId,
Long externalApprovalId,
@NonNull Integer expireIntervalSeconds, boolean startEndpoint, boolean endEndPoint, boolean autoApprove,
@NonNull FlowableAdaptor flowableAdaptor, @NonNull TaskService taskService,
@NonNull FormService formService,
Expand All @@ -109,7 +111,6 @@ public FlowApprovalInstance(@NonNull Long organizationId, @NonNull Long flowInst
this.authenticationFacade = authenticationFacade;
this.expireIntervalSeconds = expireIntervalSeconds;
this.autoApprove = autoApprove;
this.externalFlowInstanceId = externalFlowInstanceId;
this.externalApprovalId = externalApprovalId;
alloc();
create();
Expand All @@ -135,7 +136,6 @@ public FlowApprovalInstance(@NonNull Long organizationId, @NonNull Long flowInst
this.expireIntervalSeconds = expireIntervalSeconds;
this.autoApprove = autoApprove;
this.waitForConfirm = waitForConfirm;
this.externalFlowInstanceId = null;
this.externalApprovalId = null;
alloc();
create();
Expand Down Expand Up @@ -188,6 +188,7 @@ public void update() {
entity.setOperatorId(getOperatorId());
entity.setComment(getComment());
entity.setExpireIntervalSeconds(getExpireIntervalSeconds());
entity.setExternalFlowInstanceId(getExternalFlowInstanceId());
int affectRows = userTaskInstanceRepository.update(entity);
log.info("Update approval task instance successfully, affectRows={}, approvalTask={}", affectRows, entity);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.time.Duration;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Supplier;
Expand Down Expand Up @@ -54,6 +55,8 @@
import com.oceanbase.odc.service.flow.model.FlowNodeType;
import com.oceanbase.odc.service.flow.model.FlowTaskExecutionStrategy;
import com.oceanbase.odc.service.flow.task.BaseRuntimeFlowableDelegate;
import com.oceanbase.odc.service.flow.task.CreateExternalApprovalTask;
import com.oceanbase.odc.service.flow.task.model.RuntimeTaskConstants;

import lombok.Getter;
import lombok.NonNull;
Expand Down Expand Up @@ -208,10 +211,28 @@ protected FlowInstanceConfigurer next(@NonNull FlowApprovalInstance nextNode,
userTaskConsumer.accept(builder);
return builder;
});
targetExecution.next(userTaskBuilder);
if (Objects.nonNull(nextNode.getExternalApprovalId())) {
String serviceTaskName = "create_external_approval_task_" + nextNode.getId();
smallsheeeep marked this conversation as resolved.
Show resolved Hide resolved
ServiceTaskBuilder serviceTaskBuilder = nullSafeGetNodeBuilder(serviceTaskName, nextNode,
() -> new ServiceTaskBuilder(serviceTaskName, CreateExternalApprovalTask.class));
if (this.requiresActivityIdAndName) {
flowableAdaptor.setFlowableElement(nextNode, new FlowableElement(serviceTaskBuilder));
}
String gatewayName = "create_external_approval_gateway_" + nextNode.getId();
smallsheeeep marked this conversation as resolved.
Show resolved Hide resolved
ExclusiveGatewayBuilder gatewayBuilder = nullSafeGetNodeBuilder(gatewayName, nextNode,
() -> new ExclusiveGatewayBuilder(gatewayName));
targetExecution.next(serviceTaskBuilder).next(gatewayBuilder);
String expr = RuntimeTaskConstants.SUCCESS_CREATE_EXT_INS + "_" + nextNode.getId();
targetExecution.route(String.format("${!%s}", expr), this.targetProcessBuilder.endProcess());
targetExecution.next(userTaskBuilder, new ConditionSequenceFlowBuilder(
gatewayBuilder.getGraphId() + " -> " + serviceTaskBuilder.getGraphId(),
String.format("${%s}", expr)));
} else {
targetExecution.next(userTaskBuilder);
}
if (log.isDebugEnabled()) {
log.debug(
"Successfully set up the approval task node instance, instanceId={}, intanceType={}, activityId={}, name={}",
"Successfully set up the approval task node instance, instanceId={}, instanceType={}, activityId={}, name={}",
nextNode.getId(), nextNode.getNodeType(), userTaskBuilder.getGraphId(), userTaskBuilder.getName());
}
return next(userTaskBuilder, nextNode);
Expand Down
Loading