diff options
Diffstat (limited to 'org.eclipse.mylyn.github.core/src/org')
6 files changed, 235 insertions, 3 deletions
diff --git a/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/internal/github/core/gist/GistAttachmentHandler.java b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/internal/github/core/gist/GistAttachmentHandler.java new file mode 100644 index 00000000..40b11168 --- /dev/null +++ b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/internal/github/core/gist/GistAttachmentHandler.java @@ -0,0 +1,125 @@ +/******************************************************************************* + * Copyright (c) 2011 GitHub Inc. + * 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: + * Kevin Sawicki (GitHub Inc.) - initial API and implementation + *******************************************************************************/ +package org.eclipse.mylyn.internal.github.core.gist; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Collections; + +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.mylyn.commons.net.AuthenticationCredentials; +import org.eclipse.mylyn.commons.net.AuthenticationType; +import org.eclipse.mylyn.github.internal.Gist; +import org.eclipse.mylyn.github.internal.GistFile; +import org.eclipse.mylyn.github.internal.GistService; +import org.eclipse.mylyn.github.internal.GitHub; +import org.eclipse.mylyn.github.internal.GitHubClient; +import org.eclipse.mylyn.tasks.core.ITask; +import org.eclipse.mylyn.tasks.core.TaskRepository; +import org.eclipse.mylyn.tasks.core.data.AbstractTaskAttachmentHandler; +import org.eclipse.mylyn.tasks.core.data.AbstractTaskAttachmentSource; +import org.eclipse.mylyn.tasks.core.data.TaskAttribute; + +/** + * Handles Gist attatchments + */ +public class GistAttachmentHandler extends AbstractTaskAttachmentHandler { + + /** + * @see org.eclipse.mylyn.tasks.core.data.AbstractTaskAttachmentHandler#canGetContent(org.eclipse.mylyn.tasks.core.TaskRepository, + * org.eclipse.mylyn.tasks.core.ITask) + */ + public boolean canGetContent(TaskRepository repository, ITask task) { + return true; + } + + /** + * @see org.eclipse.mylyn.tasks.core.data.AbstractTaskAttachmentHandler#canPostContent(org.eclipse.mylyn.tasks.core.TaskRepository, + * org.eclipse.mylyn.tasks.core.ITask) + */ + public boolean canPostContent(TaskRepository repository, ITask task) { + return true; + } + + /** + * @see org.eclipse.mylyn.tasks.core.data.AbstractTaskAttachmentHandler#getContent(org.eclipse.mylyn.tasks.core.TaskRepository, + * org.eclipse.mylyn.tasks.core.ITask, + * org.eclipse.mylyn.tasks.core.data.TaskAttribute, + * org.eclipse.core.runtime.IProgressMonitor) + */ + public InputStream getContent(TaskRepository repository, ITask task, + TaskAttribute attachmentAttribute, IProgressMonitor monitor) + throws CoreException { + GitHubClient client = new GitHubClient(); + AuthenticationCredentials credentials = repository + .getCredentials(AuthenticationType.REPOSITORY); + if (credentials != null) + client.setCredentials(credentials.getUserName(), + credentials.getPassword()); + + TaskAttribute urlAttribute = attachmentAttribute + .getAttribute(GistAttribute.RAW_FILE_URL.getId()); + try { + return client.getStream(urlAttribute.getValue(), null); + } catch (IOException e) { + throw new CoreException(GitHub.createErrorStatus(e)); + } + } + + /** + * @see org.eclipse.mylyn.tasks.core.data.AbstractTaskAttachmentHandler#postContent(org.eclipse.mylyn.tasks.core.TaskRepository, + * org.eclipse.mylyn.tasks.core.ITask, + * org.eclipse.mylyn.tasks.core.data.AbstractTaskAttachmentSource, + * java.lang.String, org.eclipse.mylyn.tasks.core.data.TaskAttribute, + * org.eclipse.core.runtime.IProgressMonitor) + */ + public void postContent(TaskRepository repository, ITask task, + AbstractTaskAttachmentSource source, String comment, + TaskAttribute attachmentAttribute, IProgressMonitor monitor) + throws CoreException { + Gist gist = new Gist().setRepo(task.getTaskId()); + gist.setDescription(attachmentAttribute.getParentAttribute() + .getAttribute(GistAttribute.DESCRIPTION.getId()).getValue()); + GistFile file = new GistFile(); + file.setFilename(source.getName()); + gist.setFiles(Collections.singletonMap(file.getFilename(), file)); + + GitHubClient client = new GitHubClient(); + AuthenticationCredentials credentials = repository + .getCredentials(AuthenticationType.REPOSITORY); + if (credentials != null) + client.setCredentials(credentials.getUserName(), + credentials.getPassword()); + + GistService service = new GistService(client); + InputStream input = source.createInputStream(monitor); + try { + byte[] buffer = new byte[8192]; + ByteArrayOutputStream output = new ByteArrayOutputStream(); + int read; + while ((read = input.read(buffer)) != -1) + output.write(buffer, 0, read); + file.setContent(output.toString()); + service.updateGist(gist); + } catch (IOException e) { + throw new CoreException(GitHub.createErrorStatus(e)); + } finally { + try { + input.close(); + } catch (IOException ignore) { + // Ignored + } + } + + } +} diff --git a/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/internal/github/core/gist/GistAttribute.java b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/internal/github/core/gist/GistAttribute.java index d3f2fa56..7055093b 100644 --- a/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/internal/github/core/gist/GistAttribute.java +++ b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/internal/github/core/gist/GistAttribute.java @@ -58,6 +58,18 @@ public enum GistAttribute { TaskAttribute.TYPE_URL, true), /** + * RAW_FILE_URL + */ + RAW_FILE_URL("github.gist.file.url", Messages.GistAttribute_LabelFileUrl, TaskAttribute.TYPE_URL, //$NON-NLS-1$ + true), + + /** + * SUMMARY + */ + SUMMARY(TaskAttribute.SUMMARY, Messages.GistAttribute_LabelSummary, + TaskAttribute.TYPE_SHORT_RICH_TEXT, true), + + /** * Gist description */ DESCRIPTION(TaskAttribute.DESCRIPTION, diff --git a/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/internal/github/core/gist/GistConnector.java b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/internal/github/core/gist/GistConnector.java index f8a42fd7..ef116a3d 100644 --- a/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/internal/github/core/gist/GistConnector.java +++ b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/internal/github/core/gist/GistConnector.java @@ -26,6 +26,7 @@ import org.eclipse.mylyn.tasks.core.AbstractRepositoryConnector; import org.eclipse.mylyn.tasks.core.IRepositoryQuery; import org.eclipse.mylyn.tasks.core.ITask; import org.eclipse.mylyn.tasks.core.TaskRepository; +import org.eclipse.mylyn.tasks.core.data.AbstractTaskAttachmentHandler; import org.eclipse.mylyn.tasks.core.data.AbstractTaskDataHandler; import org.eclipse.mylyn.tasks.core.data.TaskAttributeMapper; import org.eclipse.mylyn.tasks.core.data.TaskData; @@ -45,6 +46,8 @@ public class GistConnector extends AbstractRepositoryConnector { private GistTaskDataHandler dataHandler = new GistTaskDataHandler(); + private GistAttachmentHandler attachmentHandler = new GistAttachmentHandler(); + /** * @see org.eclipse.mylyn.tasks.core.AbstractRepositoryConnector#getTaskDataHandler() */ @@ -53,6 +56,13 @@ public class GistConnector extends AbstractRepositoryConnector { } /** + * @see org.eclipse.mylyn.tasks.core.AbstractRepositoryConnector#getTaskAttachmentHandler() + */ + public AbstractTaskAttachmentHandler getTaskAttachmentHandler() { + return this.attachmentHandler; + } + + /** * Create client for repository * * @param repository diff --git a/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/internal/github/core/gist/GistTaskDataHandler.java b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/internal/github/core/gist/GistTaskDataHandler.java index 901e9d91..6f290da3 100644 --- a/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/internal/github/core/gist/GistTaskDataHandler.java +++ b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/internal/github/core/gist/GistTaskDataHandler.java @@ -10,20 +10,30 @@ *******************************************************************************/ package org.eclipse.mylyn.internal.github.core.gist; +import java.io.IOException; import java.util.List; +import java.util.Map; import java.util.Set; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.mylyn.commons.net.AuthenticationCredentials; +import org.eclipse.mylyn.commons.net.AuthenticationType; import org.eclipse.mylyn.github.internal.Comment; import org.eclipse.mylyn.github.internal.Gist; +import org.eclipse.mylyn.github.internal.GistFile; +import org.eclipse.mylyn.github.internal.GistService; +import org.eclipse.mylyn.github.internal.GitHub; +import org.eclipse.mylyn.github.internal.GitHubClient; import org.eclipse.mylyn.github.internal.GitHubTaskAttributeMapper; import org.eclipse.mylyn.github.internal.User; import org.eclipse.mylyn.tasks.core.IRepositoryPerson; import org.eclipse.mylyn.tasks.core.ITaskMapping; import org.eclipse.mylyn.tasks.core.RepositoryResponse; import org.eclipse.mylyn.tasks.core.TaskRepository; +import org.eclipse.mylyn.tasks.core.RepositoryResponse.ResponseKind; import org.eclipse.mylyn.tasks.core.data.AbstractTaskDataHandler; +import org.eclipse.mylyn.tasks.core.data.TaskAttachmentMapper; import org.eclipse.mylyn.tasks.core.data.TaskAttribute; import org.eclipse.mylyn.tasks.core.data.TaskAttributeMapper; import org.eclipse.mylyn.tasks.core.data.TaskCommentMapper; @@ -106,11 +116,11 @@ public class GistTaskDataHandler extends AbstractTaskDataHandler { TaskAttribute url = GistAttribute.URL.create(data); url.setValue(getGistUrl(data.getRepositoryUrl(), gist)); + IRepositoryPerson reporterPerson = null; User user = gist.getUser(); if (user != null) { TaskAttribute reporter = GistAttribute.AUTHOR.create(data); - IRepositoryPerson reporterPerson = repository.createPerson(user - .getLogin()); + reporterPerson = repository.createPerson(user.getLogin()); reporterPerson.setName(user.getName()); mapper.setRepositoryPerson(reporter, reporterPerson); @@ -118,6 +128,27 @@ public class GistTaskDataHandler extends AbstractTaskDataHandler { mapper.setValue(gravatar, user.getGravatarUrl()); } + Map<String, GistFile> files = gist.getFiles(); + if (files != null && !files.isEmpty()) { + int count = 1; + for (GistFile file : files.values()) { + TaskAttachmentMapper attachmentMapper = new TaskAttachmentMapper(); + attachmentMapper.setFileName(file.getFilename()); + attachmentMapper.setReplaceExisting(true); + attachmentMapper.setLength((long) file.getSize()); + attachmentMapper.setPatch(false); + attachmentMapper.setAuthor(reporterPerson); + TaskAttribute attribute = data.getRoot().createAttribute( + TaskAttribute.PREFIX_ATTACHMENT + count); + attachmentMapper.applyTo(attribute); + + GistAttribute.RAW_FILE_URL.create(attribute).setValue( + file.getRawUrl()); + + count++; + } + } + GistAttribute.COMMENT_NEW.create(data); return data; @@ -131,7 +162,47 @@ public class GistTaskDataHandler extends AbstractTaskDataHandler { public RepositoryResponse postTaskData(TaskRepository repository, TaskData taskData, Set<TaskAttribute> oldAttributes, IProgressMonitor monitor) throws CoreException { - return null; + RepositoryResponse response = null; + + Gist gist = new Gist(); + GitHubClient client = new GitHubClient(); + AuthenticationCredentials credentials = repository + .getCredentials(AuthenticationType.REPOSITORY); + if (credentials != null) { + client.setCredentials(credentials.getUserName(), + credentials.getPassword()); + gist.setUser(new User().setLogin(credentials.getUserName())); + } + + GistService service = new GistService(client); + TaskAttribute root = taskData.getRoot(); + gist.setRepo(taskData.getTaskId()); + gist.setDescription(root + .getAttribute(GistAttribute.DESCRIPTION.getId()).getValue()); + + if (taskData.isNew()) { + try { + gist = service.createGist(gist); + } catch (IOException e) { + throw new CoreException(GitHub.createErrorStatus(e)); + } + response = new RepositoryResponse(ResponseKind.TASK_CREATED, + gist.getRepo()); + } else { + try { + String newComment = root.getAttribute( + GistAttribute.COMMENT_NEW.getId()).getValue(); + if (newComment.length() > 0) + service.createComment(taskData.getTaskId(), newComment); + + service.updateGist(gist); + } catch (IOException e) { + throw new CoreException(GitHub.createErrorStatus(e)); + } + response = new RepositoryResponse(ResponseKind.TASK_UPDATED, + taskData.getTaskId()); + } + return response; } /** @@ -143,6 +214,12 @@ public class GistTaskDataHandler extends AbstractTaskDataHandler { public boolean initializeTaskData(TaskRepository repository, TaskData data, ITaskMapping initializationData, IProgressMonitor monitor) throws CoreException { + TaskAttributeMapper mapper = data.getAttributeMapper(); + + TaskAttribute summary = GistAttribute.SUMMARY.create(data); + mapper.setValue(summary, "New Gist"); + GistAttribute.DESCRIPTION.create(data); + return true; } diff --git a/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/internal/github/core/gist/Messages.java b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/internal/github/core/gist/Messages.java index 26e2e102..4ba2416c 100644 --- a/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/internal/github/core/gist/Messages.java +++ b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/internal/github/core/gist/Messages.java @@ -32,12 +32,18 @@ public class Messages extends NLS { public static String GistAttribute_LabelDescription; /** */ + public static String GistAttribute_LabelFileUrl; + + /** */ public static String GistAttribute_LabelKey; /** */ public static String GistAttribute_LabelNewComment; /** */ + public static String GistAttribute_LabelSummary; + + /** */ public static String GistAttribute_LabelUrl; /** */ diff --git a/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/internal/github/core/gist/messages.properties b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/internal/github/core/gist/messages.properties index 9321b0bf..3d788508 100644 --- a/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/internal/github/core/gist/messages.properties +++ b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/internal/github/core/gist/messages.properties @@ -2,7 +2,9 @@ GistAttribute_LabelAuthor=Author: GistAttribute_LabelAuthorGravatar=Author GistAttribute_LabelCreated=Created: GistAttribute_LabelDescription=Description: +GistAttribute_LabelFileUrl=File url GistAttribute_LabelKey=Key GistAttribute_LabelNewComment=New Comment +GistAttribute_LabelSummary=Summary GistAttribute_LabelUrl=Url GistConnector_LabelConnector=Github Gists |