Open Jacobvu84 opened 3 years ago
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.rcarz.jiraclient.BasicCredentials;
import net.rcarz.jiraclient.Field;
import net.rcarz.jiraclient.Issue;
import net.rcarz.jiraclient.JiraClient;
import net.rcarz.jiraclient.JiraException;
public class CreateSubtask {
private static final Logger LOGGER = LoggerFactory.getLogger(CreateSubtask.class);
public static SubtaskBuilder withParent(String ticketId) {
return new SubtaskBuilder(ticketId);
}
public static class SubtaskBuilder {
private String ticketId;
private List<String> summaries = new ArrayList<String>();
public SubtaskBuilder(String ticketId) {
this.ticketId = ticketId;
}
public SubtaskBuilder withSummary(String summary) {
this.summaries.add(summary);
return this;
}
public void execution() {
BasicCredentials creds = new BasicCredentials("Jacob.vu", "xyz123");
JiraClient jira = new JiraClient("https://jira.company.com/", creds);
try {
Issue issue = jira.getIssue(ticketId);
List<Issue> subtasks = issue.getSubtasks();
for (String summary : summaries) {
Boolean flag = false;
for (Issue subtask : subtasks) {
if (subtask.getSummary().equals(summary)) {
flag = true;
}
}
if (flag) {
LOGGER.info(summary + ". This ticket already existed");
} else {
String keyIssue = issue.createSubtask().field(Field.SUMMARY, summary).execute().getKey();
Issue subissue = jira.getIssue(keyIssue);
LOGGER.info(keyIssue + ": " + subissue.getSummary() + " is created");
}
}
} catch (JiraException ex) {
System.err.println(ex.getMessage());
if (ex.getCause() != null)
System.err.println(ex.getCause().getMessage());
}
}
}
}
Cách dùng:
public class JiraClientTest {
public static void main(String[] args) {
CreateSubtask
.withParent("JIRA-312")
.withSummary("[Auto] Created by jira-client: Jacob")
.withSummary("[Auto] Created by jira-client: Cony")
.execution();
}
}
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import net.rcarz.jiraclient.Issue;
import net.rcarz.jiraclient.JiraException;
import net.rcarz.jiraclient.RestException;
import net.sf.json.JSON;
import net.sf.json.JSONObject;
import net.thucydides.core.model.TestOutcome;
public class CreateTestCase extends JiraBase {
private static final Logger LOGGER = LoggerFactory.getLogger(CreateTestCase.class);
public static Issue template(TestOutcome testOutcome) {
JSON result = null;
JsonObject jsonObject = null;
String key;
if (!isTestCaseTemplate(testOutcome)) {
List<JSONObject> stepsRows = new ArrayList<JSONObject>();
testOutcome.getTestSteps().forEach(stepsRow -> {
JSONObject groupRow = new JSONObject()
.element("isGroup", true)
.element("cells",Arrays.asList(stepsRow.getDescription()));
stepsRows.add(groupRow);
stepsRow.getChildren().forEach(action -> {
JSONObject step = new JSONObject()
.element("isGroup", false)
.element("cells", Arrays.asList(action.getDescription(), "", "", ""));
stepsRows.add(step);
});
});
JSONObject customfield_500247427 = new JSONObject().element("stepsRows", stepsRows);
JSONObject fields = new JSONObject()
.element("project", project)
.element("summary", testOutcome.getTitle())
.element("description", "-")
.element("issuetype", issuetype)
.element("customfield_500247427", customfield_500247427);
JSONObject tct = new JSONObject().element("fields", fields);
try {
result = JIRA.getRestClient().post("/rest/api/latest/issue", tct);
jsonObject = new JsonParser().parse(result.toString()).getAsJsonObject();
key = jsonObject.get("key").getAsString();
LOGGER.info(key + " is created!");
return JIRA.getIssue(key);
} catch (JiraException | RestException | IOException | URISyntaxException e) {
e.printStackTrace();
}
} else {
try {
// TODO: may be failure if not set the @Issue
Update.theDescription(testOutcome);
return JIRA.getIssue(testOutcome.getIssues().get(0));
} catch (JiraException e) {
e.printStackTrace();
}
}
return null;
}
private static boolean isTestCaseTemplate(TestOutcome testOutcome) {
try {
if (Objects.isNull(JiraUtils.checkIssue(JIRA, testOutcome.getIssues().get(0))))
return false;
else
return true;
} catch (IndexOutOfBoundsException e) {
LOGGER.info(e.getStackTrace().toString());
}
return false;
}
}
Thêm vào pom.xml