
io.github.carousell.testrails.CreateRunMojo Maven / Gradle / Ivy
package io.github.carousell.testrails;
import com.codepine.api.testrail.TestRail;
import com.codepine.api.testrail.model.Plan.Entry;
import com.codepine.api.testrail.model.Test;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/** Mojo for creating a Testrail run */
@Mojo(name = "createrun")
public class CreateRunMojo extends AbstractMojo {
private static final Logger LOG = LoggerFactory.getLogger(CreateRunMojo.class);
@Parameter(defaultValue = "${project}", readonly = true, required = true)
private MavenProject project;
@Parameter(defaultValue = "${project.basedir}/src/test/resources/features")
private String featuresSourceDirectory;
@Parameter(required = true, readonly = true)
private String url;
@Parameter(required = true, readonly = true)
private int projectId;
@Parameter(required = true, readonly = true)
private int suiteId;
@Parameter(required = true, readonly = true)
private int planId;
@Parameter(required = true, readonly = true)
private String username;
@Parameter(required = true, readonly = true)
private String password;
@Parameter(defaultValue = "generated by testrail-maven-plugin")
private String testRunName;
@Parameter(property = "tags", readonly = true)
private String[] tags;
@Parameter(property = "runId", readonly = true)
private int runId;
@Parameter(property = "entryId", readonly = true)
private String entryId;
private FeatureParser featureParser = new FeatureParser();
private boolean isNewTestRun = true;
public void execute() throws MojoExecutionException, MojoFailureException {
LOG.info("[execute] Get Parameters from command...");
LOG.info("[execute] Project Id : {}", projectId);
LOG.info("[execute] Suite Id : {}", suiteId);
LOG.info("[execute] Plan Id : {}", planId);
LOG.info("[execute] Test Run Name : {}", testRunName);
LOG.info("[execute] Tags : {}", tags);
LOG.info("[execute] Run Id : {}", runId);
LOG.info("[execute] Entry Id : {}", entryId);
TestRail testRail =
TestRail.builder(url, username, password).applicationName("playground").build();
List preLoadCaseIdList = new ArrayList();
LOG.info("[execute] Start to Preload Existed Test Case Id from Feature Files ...");
if ((runId != 0) && (entryId != null)) {
LOG.info("[execute] Need to preload existed test cases in test entry , because runId != {} and entryId != {}"
, runId
, entryId);
try {
isNewTestRun = false;
LOG.info("[execute] This is not a new Test Run, need to update existed Test Run , set isNewTestRun = {}"
, isNewTestRun);
List tests = testRail.tests().list(runId).execute();
LOG.info("[execute] Start to preload Test Case Id from TestRail ...");
for (Test test : tests) {
LOG.debug("[execute] Add Existed Case Id : {}", test.getCaseId());
preLoadCaseIdList.add(test.getCaseId());
}
} catch (com.codepine.api.testrail.TestRailException e) {
LOG.error("[execute] Throw Exception: {}", e.toString());
}
} else {
LOG.info("[execute] Skip preload existed test cases in test entry , because runId = {} and entryId = {}"
, runId
, entryId);
}
LOG.info("[execute] Show Preload Test Case Id Size : {} List : {}"
, preLoadCaseIdList.size()
, preLoadCaseIdList);
LOG.info("[execute] Start to Parse New Case Id from Feature Files ...");
featureParser.parseFeatures(featuresSourceDirectory, tags);
List newCaseIdList = featureParser.getCaseIdList().size() != 0
? featureParser.getCaseIdList() : new ArrayList(){{add(1);}};
LOG.info("[execute] Show New Test Case Id Size : {}, List : {}"
, newCaseIdList.size()
, preLoadCaseIdList);
LOG.info("[execute] Start to Merge Preload Test Case Id list and New Test Case Id List");
List finalCaseIdList = new ArrayList();
finalCaseIdList.addAll(preLoadCaseIdList);
finalCaseIdList.addAll(newCaseIdList);
finalCaseIdList = finalCaseIdList.stream().distinct().collect(Collectors.toList());
LOG.info("[execute] Show Final Test Case Id Size : {}, List : {}"
, finalCaseIdList.size()
, finalCaseIdList);
Entry entry = null;
if (isNewTestRun) {
LOG.info("[execute] Create a New TestRun with Name {} with cases in Plan Id {}",
testRunName, planId);
entry = testRail
.plans()
.addEntry(planId, new Entry()
.setSuiteId(suiteId)
.setName(testRunName)
.setIncludeAll(false)
.setCaseIds(finalCaseIdList))
.execute();
} else {
LOG.info("[execute] Update an Existed TestRun {} with cases by Entry {} in Plan Id {}",
runId, entryId, planId);
entry = testRail
.plans()
.updateEntry(planId, new Entry()
.setId(entryId)
.setName(testRunName)
.setIncludeAll(false)
.setCaseIds(finalCaseIdList))
.execute();
}
int runId = entry.getRuns().get(0).getId();
LOG.info("[execute] Setting entryId {} as Maven property", entry.getId());
project.getProperties().put("entryId", entry.getId());
LOG.info("[execute] Setting testRunId {} as Maven property", runId);
project.getProperties().put("testRunId", Integer.toString(runId));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy