Skip to content

Commit

Permalink
refactor: 实例化 logger
Browse files Browse the repository at this point in the history
  • Loading branch information
liuruibin committed Aug 28, 2024
1 parent 5773c6f commit 1f2fe80
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
11 changes: 6 additions & 5 deletions src/main/java/io/metersphere/v3/MeterSphereBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
public class MeterSphereBuilder extends Builder implements SimpleBuildStep, Serializable {

private static final String LOG_PREFIX = "[MeterSphere] ";
private MeterSphereUtils meterSphereUtils;

private final String msEndpoint;
private final String msAccessKey;
Expand All @@ -52,7 +53,7 @@ public MeterSphereBuilder(String msEndpoint, String msAccessKey, String msSecret
@Override
public void perform(@Nonnull Run<?, ?> run, @Nonnull FilePath workspace, @Nonnull Launcher launcher,
@Nonnull TaskListener listener) throws InterruptedException, IOException {
MeterSphereUtils.logger = listener.getLogger();
this.meterSphereUtils = new MeterSphereUtils(listener.getLogger());
listener.getLogger().println("workspace=" + workspace);
listener.getLogger().println("number=" + run.getNumber());
listener.getLogger().println("url=" + run.getUrl());
Expand Down Expand Up @@ -80,16 +81,16 @@ public void perform(@Nonnull Run<?, ?> run, @Nonnull FilePath workspace, @Nonnul

List<TestPlanDTO> testPlans = client.getTestPlanIds(realProjectId);
Optional<TestPlanDTO> first = testPlans.stream()
.filter(plan -> StringUtils.equals(testPlanName, MeterSphereUtils.handleTestPlanName(plan.getId(), plan.getNum()))
|| StringUtils.equals(testPlanName, MeterSphereUtils.handleTestPlanName(plan.getName(), plan.getNum())))
.filter(plan -> StringUtils.equals(testPlanName, meterSphereUtils.handleTestPlanName(plan.getId(), plan.getNum()))
|| StringUtils.equals(testPlanName, meterSphereUtils.handleTestPlanName(plan.getName(), plan.getNum())))
.findFirst();

if (!first.isPresent()) {
log("测试计划不存在");
run.setResult(Result.FAILURE);
return;
}
result = MeterSphereUtils.runTestPlan(run, client, first.get(), organizationId, realProjectId, msEndpoint);
result = meterSphereUtils.runTestPlan(run, client, first.get(), organizationId, realProjectId, msEndpoint);
// 使用case的结果
run.setResult(result ? Result.SUCCESS : Result.FAILURE);
} catch (Exception e) {
Expand Down Expand Up @@ -243,7 +244,7 @@ public DescriptorImpl getDescriptor() {


private void log(String msg) {
MeterSphereUtils.logger.println(LOG_PREFIX + msg);
meterSphereUtils.log(LOG_PREFIX + msg);
}

@DataBoundSetter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,19 @@
import java.util.Map;

public class MeterSphereUtils {
public static PrintStream logger;
public PrintStream logger;
private static final String LOG_PREFIX = "[MeterSphere,代码测试]";

private static void log(String msg) {
public MeterSphereUtils(PrintStream logger) {
this.logger = logger;
}

public void log(String msg) {
logger.println(LOG_PREFIX + msg);
}


public static boolean runTestPlan(Run<?, ?> run, MeterSphereClient meterSphereClient, TestPlanDTO testPlan, String organizationId, String projectId, String endpoint) throws InterruptedException {
public boolean runTestPlan(Run<?, ?> run, MeterSphereClient meterSphereClient, TestPlanDTO testPlan, String organizationId, String projectId, String endpoint) throws InterruptedException {
log("测试计划开始执行");
String id = meterSphereClient.exeTestPlan(testPlan.getId());
log("生成测试报告id: " + id + ",测试计划: " + testPlan.getName() + ",类型: " + testPlan.getType());
Expand Down Expand Up @@ -64,7 +68,7 @@ public static boolean runTestPlan(Run<?, ?> run, MeterSphereClient meterSphereCl
}


public static String handleTestPlanName(String name, String num) {
public String handleTestPlanName(String name, String num) {
return "[" + num + "] " + name;
}

Expand Down

0 comments on commit 1f2fe80

Please sign in to comment.