Skip to content

Commit

Permalink
use jirarestclient instead of directly rest access see rtcTo#10
Browse files Browse the repository at this point in the history
  • Loading branch information
WtfJoke committed Jul 29, 2015
1 parent 4e73e7b commit a450787
Showing 1 changed file with 24 additions and 33 deletions.
57 changes: 24 additions & 33 deletions src/main/java/to/rtc/rtc2jira/exporter/jira/JiraExporter.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
package to.rtc.rtc2jira.exporter.jira;

import java.util.List;

import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import java.io.IOException;

import to.rtc.rtc2jira.Settings;
import to.rtc.rtc2jira.exporter.Exporter;
import to.rtc.rtc2jira.exporter.jira.entities.Project;
import to.rtc.rtc2jira.exporter.jira.entities.ProjectOverview;
import to.rtc.rtc2jira.exporter.jira.rest.AutoClosableRestClient;
import to.rtc.rtc2jira.exporter.jira.rest.RestClientFactory;
import to.rtc.rtc2jira.storage.StorageEngine;

import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.ClientResponse.Status;
import com.sun.jersey.api.client.GenericType;
import com.atlassian.jira.rest.client.api.IssueRestClient;
import com.atlassian.jira.rest.client.api.JiraRestClient;
import com.atlassian.jira.rest.client.api.RestClientException;
import com.atlassian.jira.rest.client.api.domain.Attachment;
import com.atlassian.jira.rest.client.api.domain.BasicProject;
import com.atlassian.jira.rest.client.api.domain.Issue;
import com.atlassian.util.concurrent.Promise;

public class JiraExporter implements Exporter {

private StorageEngine engine;
private Settings settings;
private JiraRestAccess restAccess;
private AutoClosableRestClient restAccess;

@Override
public void initialize(Settings settings, StorageEngine engine) {
Expand All @@ -31,35 +32,25 @@ public void initialize(Settings settings, StorageEngine engine) {
public boolean isConfigured() {
boolean isConfigured = false;
if (settings.hasJiraProperties()) {
restAccess =
new JiraRestAccess(settings.getJiraUrl(), settings.getJiraUser(),
settings.getJiraPassword());
ClientResponse response = restAccess.getResponse("/project");
isConfigured = response.getStatus() == Status.OK.getStatusCode();
try (JiraRestClient client = RestClientFactory.create(settings)) {
client.getSessionClient().getCurrentSession();
} catch (RestClientException | IOException e) {
System.err.println("Problem while connecting to session: " + e);
isConfigured = false;
}
}
return isConfigured;
}

@Override
public void export() throws Exception {
List<ProjectOverview> projects =
restAccess.get("/project", new GenericType<List<ProjectOverview>>() {});
Project project = restAccess.get("/project/10001", Project.class);

JSONObject data = createIssueData(project);
String response = restAccess.post("/issue/", data.toString(), String.class);
try (JiraRestClient client = RestClientFactory.create(settings)) {
IssueRestClient issueClient = client.getIssueClient();
Promise<Issue> issuePromise = issueClient.getIssue("WOR-2");
Issue issue = issuePromise.get();
Iterable<Attachment> attachments = issue.getAttachments();
Iterable<BasicProject> projectsIterable = client.getProjectClient().getAllProjects().get();
}
}

private JSONObject createIssueData(Project project) throws JSONException {
JSONObject data = new JSONObject();
JSONObject fields = new JSONObject();
fields.put("summary", "Test REST");
fields.put("description",
"Creating of an issue using project keys and issue type names using the REST API");
fields.put("project", new JSONObject().put("id", project.getId()));
fields.put("issuetype", new JSONObject().put("name", "Task"));

data.put("fields", fields);
return data;
}
}

0 comments on commit a450787

Please sign in to comment.