Skip to content

Commit

Permalink
支持nacos configset
Browse files Browse the repository at this point in the history
  • Loading branch information
Bruce.wu committed Sep 11, 2024
1 parent bcd8d5a commit e1b86cf
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,23 @@
import io.jenkins.plugins.configops.model.dto.NacosConfigDTO;
import java.io.Serializable;
import java.util.List;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

/**
* @author Bruce.Wu
* @date 2024-08-30
*/
@Setter
@Getter
@ToString
public class NacosApplyChangeSetReq implements Serializable {
private static final long serialVersionUID = -801656933583501739L;

private String nacosId;

private String changeSetId;
private List<String> changeSetIds;

private List<NacosConfigDTO> changes;

public String getNacosId() {
return nacosId;
}

public void setNacosId(String nacosId) {
this.nacosId = nacosId;
}

public String getChangeSetId() {
return changeSetId;
}

public void setChangeSetId(String changeSetId) {
this.changeSetId = changeSetId;
}

public List<NacosConfigDTO> getChanges() {
return changes;
}

public void setChanges(List<NacosConfigDTO> changes) {
this.changes = changes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ public class NacosGetChangeSetReq implements Serializable {
private String nacosId;

private String changeLogFile;

private Integer count;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@ public class NacosGetChangeSetResp implements Serializable {

private String comment;

private List<String> ids;

private List<NacosConfigDTO> changes;
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,19 @@ public class NacosChangeSetApplyStep extends Step implements Serializable {

private final String toolUrl;

private final String changeSetId;
private final List<String> changeSetIds;

private final List<NacosConfigModifyDTO> items;

@DataBoundConstructor
public NacosChangeSetApplyStep(
@NonNull String nacosId, String toolUrl, String changeSetId, @NonNull List<NacosConfigModifyDTO> items) {
@NonNull String nacosId,
String toolUrl,
@NonNull List<String> changeSetIds,
@NonNull List<NacosConfigModifyDTO> items) {
this.nacosId = nacosId;
this.toolUrl = StringUtils.defaultIfBlank(toolUrl, Constants.DEFAULT_TOOL_URL);
this.changeSetId = changeSetId;
this.changeSetIds = changeSetIds;
this.items = items;
}

Expand Down Expand Up @@ -118,11 +121,11 @@ protected Map<String, Object> run() throws Exception {
.collect(Collectors.toList());
VirtualChannel channel = Utils.getChannel(launcher);
logger.log(
"Applying change log config. toolUrl:%s, nacosId:%s, changeSetId:%s",
step.getToolUrl(), step.getNacosId(), step.getChangeSetId());
"Applying change log config. toolUrl:%s, nacosId:%s, changeSetIds:%s",
step.getToolUrl(), step.getNacosId(), step.getChangeSetIds());

channel.call(
new RemoteExecutionCallable(step.getToolUrl(), step.getNacosId(), step.getChangeSetId(), changes));
new RemoteExecutionCallable(step.getToolUrl(), step.getNacosId(), step.getChangeSetIds(), changes));

return Collections.emptyMap();
}
Expand All @@ -133,14 +136,14 @@ public static class RemoteExecutionCallable implements Callable<String, Exceptio
private static final long serialVersionUID = 4711346178005514552L;
private final String toolUrl;
private final String nacosId;
private final String changeSetId;
private final List<String> changeSetIds;
private final List<NacosConfigDTO> changes;

public RemoteExecutionCallable(
String toolUrl, String nacosId, String changeSetId, List<NacosConfigDTO> changes) {
String toolUrl, String nacosId, List<String> changeSetIds, List<NacosConfigDTO> changes) {
this.toolUrl = toolUrl;
this.nacosId = nacosId;
this.changeSetId = changeSetId;
this.changeSetIds = changeSetIds;
this.changes = changes;
}

Expand All @@ -149,7 +152,7 @@ public String call() throws Exception {
ConfigOpsClient client = new ConfigOpsClient(toolUrl);
NacosApplyChangeSetReq req = new NacosApplyChangeSetReq();
req.setNacosId(nacosId);
req.setChangeSetId(changeSetId);
req.setChangeSetIds(changeSetIds);
req.setChanges(changes);
return client.applyChangeSet(req);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.jenkinsci.plugins.workflow.steps.StepExecution;
import org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;

@Setter
@Getter
Expand All @@ -44,13 +45,20 @@ public class NacosChangeSetGetStep extends Step implements Serializable {

private final String changeLogFile;

private Integer count;

@DataBoundConstructor
public NacosChangeSetGetStep(String nacosId, String toolUrl, String changeLogFile) {
this.nacosId = nacosId;
this.toolUrl = StringUtils.defaultIfBlank(toolUrl, Constants.DEFAULT_TOOL_URL);
this.changeLogFile = changeLogFile;
}

@DataBoundSetter
public void setCount(Integer count) {
this.count = count;
}

@Override
public StepExecution start(StepContext context) throws Exception {
return new StepExecutionImpl(context, this);
Expand Down Expand Up @@ -79,7 +87,8 @@ protected NacosGetChangeSetResp run() throws Exception {
if (!changeLog.exists()) {
throw new IllegalArgumentException("Change log file not found");
}
NacosGetChangeSetResp resp = changeLog.act(new RemoteCallable(step.getToolUrl(), step.getNacosId()));
NacosGetChangeSetResp resp =
changeLog.act(new RemoteCallable(step.getToolUrl(), step.getNacosId(), step.getCount()));
logger.log("Get ChangeSet from file: %s", step.getChangeLogFile());
if (CollectionUtils.isNotEmpty(resp.getChanges())) {
for (NacosConfigDTO nc : resp.getChanges()) {
Expand All @@ -99,16 +108,21 @@ private static class RemoteCallable extends MasterToSlaveFileCallable<NacosGetCh
private final String toolUrl;
private final String nacosId;

private RemoteCallable(String toolUrl, String nacosId) {
private final Integer count;

private RemoteCallable(String toolUrl, String nacosId, Integer count) {
this.toolUrl = toolUrl;
this.nacosId = nacosId;
this.count = count;
}

@Override
public NacosGetChangeSetResp invoke(File f, VirtualChannel channel) throws IOException, InterruptedException {
ConfigOpsClient client = new ConfigOpsClient(toolUrl);
NacosGetChangeSetReq nacosGetChangeSetReq =
new NacosGetChangeSetReq().setNacosId(nacosId).setChangeLogFile(f.getAbsolutePath());
NacosGetChangeSetReq nacosGetChangeSetReq = new NacosGetChangeSetReq()
.setNacosId(nacosId)
.setChangeLogFile(f.getAbsolutePath())
.setCount(count);
return client.getChangeSet(nacosGetChangeSetReq);
}
}
Expand Down

0 comments on commit e1b86cf

Please sign in to comment.