diff options
author | Frank Becker | 2015-05-20 16:06:31 +0000 |
---|---|---|
committer | Gerrit Code Review @ Eclipse.org | 2015-11-26 07:16:58 +0000 |
commit | d384ba5675c9353af165a3145aae3d011032e546 (patch) | |
tree | 645f6b20d16b9e52280dd6c6e66a03f4137f24ec | |
parent | f653f7f0ce4a5fb7b84c0e99fa9634ef28bbc260 (diff) | |
download | org.eclipse.mylyn.tasks-d384ba5675c9353af165a3145aae3d011032e546.tar.gz org.eclipse.mylyn.tasks-d384ba5675c9353af165a3145aae3d011032e546.tar.xz org.eclipse.mylyn.tasks-d384ba5675c9353af165a3145aae3d011032e546.zip |
414360: add get Comments for the new REST API
Change-Id: Ic122013c93594556221883d5c87f73daf8ea95b1
Task-Url: https://bugs.eclipse.org/bugs/show_bug.cgi?id=414360
3 files changed, 166 insertions, 5 deletions
diff --git a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core.tests/src/org/eclipse/mylyn/internal/bugzilla/rest/core/tests/BugzillaRestClientTest.java b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core.tests/src/org/eclipse/mylyn/internal/bugzilla/rest/core/tests/BugzillaRestClientTest.java index 501dc8e68..0865a81f8 100644 --- a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core.tests/src/org/eclipse/mylyn/internal/bugzilla/rest/core/tests/BugzillaRestClientTest.java +++ b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core.tests/src/org/eclipse/mylyn/internal/bugzilla/rest/core/tests/BugzillaRestClientTest.java @@ -512,20 +512,22 @@ public class BugzillaRestClientTest { //Act RepositoryResponse reposonse = connector.getClient(actualFixture.repository()).postTaskData(taskDataGet, changed, null); - + assertNotNull(reposonse); + assertNotNull(reposonse.getReposonseKind()); + assertThat(reposonse.getReposonseKind(), is(ResponseKind.TASK_UPDATED)); //Assert TaskData taskDataUpdate = harness.getTaskFromServer(taskId); - attribute = taskDataGet.getRoot() + attribute = taskDataUpdate.getRoot() .getMappedAttribute(BugzillaRestCreateTaskSchema.getDefault().PRODUCT.getKey()); assertThat(attribute.getValue(), is("Product with Spaces")); - attribute = taskDataGet.getRoot() + attribute = taskDataUpdate.getRoot() .getMappedAttribute(BugzillaRestCreateTaskSchema.getDefault().COMPONENT.getKey()); assertThat(attribute.getValue(), is("Component 1")); - attribute = taskDataGet.getRoot() + attribute = taskDataUpdate.getRoot() .getMappedAttribute(BugzillaRestCreateTaskSchema.getDefault().VERSION.getKey()); assertThat(attribute.getValue(), is("b")); - attribute = taskDataGet.getRoot() + attribute = taskDataUpdate.getRoot() .getAttribute(BugzillaRestCreateTaskSchema.getDefault().TARGET_MILESTONE.getKey()); assertThat(attribute.getValue(), is("M3.0")); attribute = taskDataUpdate.getRoot().getAttribute("cf_dropdown"); @@ -534,4 +536,38 @@ public class BugzillaRestClientTest { assertThat(attribute.getValues(), is(Arrays.asList("Red", "Yellow"))); } + @Test + public void testAddComment() throws Exception { + String taskId = harness.getTaksId4TestProduct(); + TaskData taskDataGet = harness.getTaskFromServer(taskId); + + Set<TaskAttribute> changed = new HashSet<TaskAttribute>(); + + TaskAttribute attribute = taskDataGet.getRoot() + .getAttribute(BugzillaRestTaskSchema.getDefault().NEW_COMMENT.getKey()); + attribute.setValue("The Comment"); + changed.add(attribute); + + //Act + RepositoryResponse reposonse = connector.getClient(actualFixture.repository()).postTaskData(taskDataGet, + changed, null); + assertNotNull(reposonse); + assertNotNull(reposonse.getReposonseKind()); + assertThat(reposonse.getReposonseKind(), is(ResponseKind.TASK_UPDATED)); + //Assert + TaskData taskDataUpdate = harness.getTaskFromServer(taskId); + + attribute = taskDataUpdate.getRoot().getMappedAttribute(TaskAttribute.PREFIX_COMMENT + "1"); + assertNotNull(attribute); + TaskAttribute commentAttribute = attribute.getMappedAttribute(TaskAttribute.COMMENT_TEXT); + assertNotNull(commentAttribute); + assertThat(commentAttribute.getValue(), is("The Comment")); + commentAttribute = attribute.getMappedAttribute(TaskAttribute.COMMENT_NUMBER); + assertNotNull(commentAttribute); + assertThat(commentAttribute.getValue(), is("1")); + commentAttribute = attribute.getMappedAttribute(TaskAttribute.COMMENT_ISPRIVATE); + assertNotNull(commentAttribute); + assertThat(commentAttribute.getValue(), is("false")); + } + } diff --git a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestClient.java b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestClient.java index 1eb602ab2..88ac8e619 100644 --- a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestClient.java +++ b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestClient.java @@ -159,6 +159,7 @@ public class BugzillaRestClient { List<TaskData> taskDataArray = new BugzillaRestGetTaskData(client, connector, urlIDList, taskRepository) .run(monitor); for (TaskData taskData : taskDataArray) { + new BugzillaRestGetTaskComments(getClient(), taskData).run(monitor); config.updateProductOptions(taskData); config.addValidOperations(taskData); collector.accept(taskData); diff --git a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestGetTaskComments.java b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestGetTaskComments.java new file mode 100644 index 000000000..6e37fdb2b --- /dev/null +++ b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestGetTaskComments.java @@ -0,0 +1,124 @@ +/******************************************************************************* + * Copyright (c) 2015 Frank Becker and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Frank Becker - initial API and implementation + *******************************************************************************/ + +package org.eclipse.mylyn.internal.bugzilla.rest.core; + +import java.io.InputStreamReader; +import java.lang.reflect.Type; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.Locale; +import java.util.Map.Entry; +import java.util.TimeZone; + +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.mylyn.tasks.core.IRepositoryPerson; +import org.eclipse.mylyn.tasks.core.data.TaskAttribute; +import org.eclipse.mylyn.tasks.core.data.TaskCommentMapper; +import org.eclipse.mylyn.tasks.core.data.TaskData; + +import com.google.gson.GsonBuilder; +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParseException; +import com.google.gson.reflect.TypeToken; + +public class BugzillaRestGetTaskComments extends BugzillaRestAuthenticatedGetRequest<ArrayList<TaskAttribute>> { + private final TaskData taskData; + + public BugzillaRestGetTaskComments(BugzillaRestHttpClient client, TaskData taskData) { + super(client, "/bug/" + taskData.getTaskId() + "/comment?", null); //$NON-NLS-1$ //$NON-NLS-2$ + this.taskData = taskData; + } + + @Override + protected ArrayList<TaskAttribute> parseFromJson(InputStreamReader in) { + TypeToken<ArrayList<TaskAttribute>> type = new TypeToken<ArrayList<TaskAttribute>>() { + }; + return new GsonBuilder().registerTypeAdapter(type.getType(), new JSonTaskDataDeserializer()) + .create() + .fromJson(in, type.getType()); + } + + BugzillaRestTaskSchema taskSchema = BugzillaRestTaskSchema.getDefault(); + + private class JSonTaskDataDeserializer implements JsonDeserializer<ArrayList<TaskAttribute>> { + + @Override + public ArrayList<TaskAttribute> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) + throws JsonParseException { + ArrayList<TaskAttribute> response = new ArrayList<TaskAttribute>(); + for (Entry<String, JsonElement> commentEntry : ((JsonObject) json.getAsJsonObject().get("bugs")) //$NON-NLS-1$ + .entrySet()) { + for (JsonElement jsonElement : ((JsonObject) commentEntry.getValue()).get("comments") //$NON-NLS-1$ + .getAsJsonArray()) { + JsonObject comment = (JsonObject) jsonElement; + int count = comment.get("count").getAsInt(); //$NON-NLS-1$ + if (count == 0) { + TaskAttribute desc = taskData.getRoot().getMappedAttribute(taskSchema.DESCRIPTION.getKey()); + desc.setValue(comment.get("text").getAsString()); //$NON-NLS-1$ + TaskAttribute cid = desc.getAttribute(taskSchema.COMMENT_NUMBER.getKey()); + cid.setValue(comment.get("id").getAsString()); //$NON-NLS-1$ + TaskAttribute cidp = desc.getAttribute(taskSchema.COMMENT_ISPRIVATE.getKey()); + cidp.setValue(comment.get("is_private").getAsBoolean() ? "1" : "0"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ + response.add(desc); + } else { + TaskAttribute attribute = taskData.getRoot() + .createAttribute(TaskAttribute.PREFIX_COMMENT + count); + TaskCommentMapper taskComment = TaskCommentMapper.createFrom(attribute); + taskComment.setCommentId(comment.get("id").getAsString()); //$NON-NLS-1$ + taskComment.setNumber(count); + taskComment.setUrl(taskData.getRepositoryUrl() + "/show_bug.cgi?id=" //$NON-NLS-1$ + + taskData.getTaskId() + "#c" + count); //$NON-NLS-1$ + IRepositoryPerson author = taskData.getAttributeMapper() + .getTaskRepository() + .createPerson(comment.get("creator").getAsString()); //$NON-NLS-1$ + author.setName(comment.get("creator").getAsString()); //$NON-NLS-1$ + taskComment.setAuthor(author); + JsonElement isPrivate = comment.get("is_private"); //$NON-NLS-1$ + if (isPrivate != null) { + taskComment.setIsPrivate(isPrivate.getAsBoolean()); + } else { + taskComment.setIsPrivate(null); + } + try { + SimpleDateFormat iso8601Format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", //$NON-NLS-1$ + Locale.US); + iso8601Format.setTimeZone(TimeZone.getTimeZone("UTC")); //$NON-NLS-1$ + Date tempDate = iso8601Format.parse(comment.get("creation_time").getAsString()); //$NON-NLS-1$ + taskComment.setCreationDate(tempDate); + } catch (ParseException e) { + com.google.common.base.Throwables.propagate(new CoreException(new Status(IStatus.ERROR, + BugzillaRestCore.ID_PLUGIN, + "Can not parse Date (" + comment.get("creation_time").getAsString() + ")"))); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ + } + + if (comment.get("text").getAsString() != null) { //$NON-NLS-1$ + String commentText = comment.get("text").getAsString().trim(); //$NON-NLS-1$ + taskComment.setText(commentText); + } + taskComment.applyTo(attribute); + response.add(attribute); + } + } + } + return response; + } + + } + +}
\ No newline at end of file |