All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.carousell.testrails.CreateRunMojo.orig Maven / Gradle / Ivy

package io.github.carousell.testrails;

import com.codepine.api.testrail.TestRail;
import com.codepine.api.testrail.TestRailException;
import com.codepine.api.testrail.model.Plan.Entry;
import java.util.ArrayList;
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;

<<<<<<< HEAD
=======
import java.util.ArrayList;
import java.util.List;


>>>>>>> 51ddaea716a8d315e450dc92a7ca45a37246ace0
/** 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(readonly = true)
  private int testRunId;

  @Parameter(readonly = true)
  private String entryId;

  @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")
  private String[] tags;

  private FeatureParser featureParser = new FeatureParser();

  public void execute() throws MojoExecutionException, MojoFailureException {
<<<<<<< HEAD
    TestRail testRail = TestRail.builder(url, username, password).build();
    Entry entry = null;

    // Check configuration
    if ((testRunId > 0 && (entryId == null || entryId.isEmpty()))
        || (testRunId == 0 && entryId != null && entryId.isEmpty())) {
      LOG.error("Either both runId and entryId must be set or none of them. Aborting!");
      return;
    }

    // Existing run: Verify that it exists!
    else if (testRunId > 0 && entryId != null && !entryId.isEmpty()) {
      try {
        LOG.info("Using existing test run testRunId: {}, entryId: {}", testRunId, entryId);
        testRail.runs().get(testRunId).execute();
        if (!testRail
            .plans()
            .get(planId)
            .execute()
            .getEntries()
            .stream()
            .anyMatch(existingEntry -> existingEntry.getId().equals(entryId))) {
          LOG.error(
              "Error reading existing test run ["
                  + testRunId
                  + "], entry ["
                  + entryId
                  + "]. Aborting!");
          return;
        }
      } catch (TestRailException testRailException) {
        LOG.error(
            "Error reading existing test run ["
                + testRunId
                + "], entry ["
                + entryId
                + "]. Aborting!",
            testRailException);
        return;
      }
    }
    // New run: create it
    else {
      LOG.info("Creating testrail run");
      entry =
          testRail
              .plans()
              .addEntry(
                  planId,
                  new Entry()
                      .setSuiteId(suiteId)
                      .setName(testRunName)
                      .setIncludeAll(false)
                      .setCaseIds(
                          new ArrayList() {
                            {
                              add(1);
                            }
                          }))
              .execute();

      testRunId = entry.getRuns().get(0).getId();
    }
=======

    LOG.info("Get parameters from command...");
    LOG.info("Project Id : {}", projectId);
    LOG.info("Suite Id : {}", suiteId);
    LOG.info("Plan Id : {}", planId);
    LOG.info("Test Run Name : {}", testRunName);
    LOG.info("Tags : {}", tags);

    LOG.info("Start to Parse Case Ids from Feature Files...");
    featureParser.parseFeatures(featuresSourceDirectory, tags);
    List caseIdList = featureParser.getCaseIdList().size() != 0
            ? featureParser.getCaseIdList() : new ArrayList(){{add(1);}};
    LOG.info("These Test Case ID will be added into TestRun : {}", caseIdList);

    LOG.info("Create a TestRun Name {} with case ids under Plan Id {} on TestRail",
            testRunName, planId);

    TestRail testRail =
        TestRail.builder(url, username, password).applicationName("playground").build();

    Entry entry = testRail
            .plans()
            .addEntry(planId, new Entry().setSuiteId(suiteId)
                                            .setName(testRunName)
                                            .setIncludeAll(false)
                                            .setCaseIds(caseIdList))
            .execute();
    int runId = entry.getRuns().get(0).getId();
>>>>>>> 51ddaea716a8d315e450dc92a7ca45a37246ace0

    LOG.info("Setting entryId {} as Maven property", entry.getId());
    project.getProperties().put("entryId", entry.getId());
    LOG.info("Setting testRunId {} as Maven property", testRunId);
    project.getProperties().put("testRunId", Integer.toString(testRunId));
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy