diff options
| author | Kevin Sawicki | 2011-04-13 18:23:44 +0000 |
|---|---|---|
| committer | Chris Aniszczyk | 2011-04-14 14:58:29 +0000 |
| commit | 57d357dc53cb589abbf66db64ec55bcdc52679e7 (patch) | |
| tree | 7709cadb62dbefbe8ff265be5df8fa6856544f94 | |
| parent | 546cb834e9ed9fd541167fb4f6abd86d3b2453bc (diff) | |
| download | egit-github-57d357dc53cb589abbf66db64ec55bcdc52679e7.tar.gz egit-github-57d357dc53cb589abbf66db64ec55bcdc52679e7.tar.xz egit-github-57d357dc53cb589abbf66db64ec55bcdc52679e7.zip | |
Migrate gist handler and job to use new gist service
Change-Id: I250b3811435e65ce8e0f7030cb526218763d9cce
Signed-off-by: Kevin Sawicki <kevin@github.com>
Signed-off-by: Chris Aniszczyk <caniszczyk@gmail.com>
3 files changed, 48 insertions, 29 deletions
diff --git a/org.eclipse.mylyn.github.ui/src/org/eclipse/mylyn/github/ui/internal/CreateGistHandler.java b/org.eclipse.mylyn.github.ui/src/org/eclipse/mylyn/github/ui/internal/CreateGistHandler.java index 5a46a720..637413b9 100644 --- a/org.eclipse.mylyn.github.ui/src/org/eclipse/mylyn/github/ui/internal/CreateGistHandler.java +++ b/org.eclipse.mylyn.github.ui/src/org/eclipse/mylyn/github/ui/internal/CreateGistHandler.java @@ -23,9 +23,11 @@ import org.eclipse.core.runtime.CoreException; import org.eclipse.jface.text.ITextSelection; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.mylyn.commons.net.AuthenticationCredentials; +import org.eclipse.mylyn.commons.net.AuthenticationType; +import org.eclipse.mylyn.github.internal.GistService; import org.eclipse.mylyn.github.internal.GitHub; -import org.eclipse.mylyn.github.internal.GitHubCredentials; -import org.eclipse.mylyn.github.internal.GitHubService; +import org.eclipse.mylyn.github.internal.GitHubClient; import org.eclipse.mylyn.tasks.core.TaskRepository; import org.eclipse.mylyn.tasks.ui.TasksUi; import org.eclipse.ui.IEditorInput; @@ -62,11 +64,20 @@ public class CreateGistHandler extends AbstractHandler { } private void createGistJob(String name, String extension, String contents) { - Set<TaskRepository> repositories = TasksUi.getRepositoryManager().getRepositories(GitHub.CONNECTOR_KIND); + Set<TaskRepository> repositories = TasksUi.getRepositoryManager() + .getRepositories(GitHub.CONNECTOR_KIND); TaskRepository repository = repositories.iterator().next(); - GitHubService service = new GitHubService(); - GitHubCredentials credentials = GitHubCredentials.create(repository); - CreateGistJob job = new CreateGistJob("Creating Gist", name, extension, contents, credentials, service); + GitHubClient client = new GitHubClient(); + AuthenticationCredentials credentials = repository + .getCredentials(AuthenticationType.REPOSITORY); + String userName = null; + if (credentials != null) { + userName = credentials.getUserName(); + client.setCredentials(userName, credentials.getPassword()); + } + GistService service = new GistService(client); + CreateGistJob job = new CreateGistJob("Creating Gist", name, contents, + service, userName); job.setSystem(true); job.schedule(); } @@ -77,10 +88,9 @@ public class CreateGistHandler extends AbstractHandler { br = new BufferedReader(new InputStreamReader(file.getContents())); String line; StringBuilder result = new StringBuilder(); - while ((line = br.readLine()) != null) { - result.append(line); - result.append('\n'); - } + while ((line = br.readLine()) != null) + result.append(line).append('\n'); + String contents = result.toString(); createGistJob(file.getName(), file.getFileExtension(), contents); } catch (CoreException e) { diff --git a/org.eclipse.mylyn.github.ui/src/org/eclipse/mylyn/github/ui/internal/CreateGistJob.java b/org.eclipse.mylyn.github.ui/src/org/eclipse/mylyn/github/ui/internal/CreateGistJob.java index ffe6edaf..409f16a5 100644 --- a/org.eclipse.mylyn.github.ui/src/org/eclipse/mylyn/github/ui/internal/CreateGistJob.java +++ b/org.eclipse.mylyn.github.ui/src/org/eclipse/mylyn/github/ui/internal/CreateGistJob.java @@ -11,47 +11,57 @@ package org.eclipse.mylyn.github.ui.internal; import java.io.IOException; +import java.util.Collections; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Platform; import org.eclipse.core.runtime.Status; import org.eclipse.core.runtime.jobs.Job; -import org.eclipse.mylyn.github.internal.GitHubCredentials; -import org.eclipse.mylyn.github.internal.GitHubService; -import org.eclipse.mylyn.github.internal.GitHubServiceException; +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.User; import org.eclipse.swt.widgets.Display; +import org.eclipse.ui.PlatformUI; public class CreateGistJob extends Job { private String title; private String extension; private String content; - private GitHubCredentials credentials; - private GitHubService service; + private GistService service; + private String user; - public CreateGistJob(String name, String title, String extension, String content, GitHubCredentials credentials, GitHubService service) { + public CreateGistJob(String name, String title, String content, + GistService service, String user) { super(name); this.title = title; - this.extension = extension; this.content = content; - this.credentials = credentials; this.service = service; + this.user = user; } @Override protected IStatus run(IProgressMonitor monitor) { try { - final String url = service.createGist(title, extension, content, credentials); - Display.getDefault().asyncExec(new Runnable() { + Gist gist = new Gist().setPublic(true); + if (user != null) + gist.setAuthor(new User().setLogin(user)); + gist.setDescription(title); + GistFile file = new GistFile().setContent(content); + gist.setFiles(Collections.singletonMap(title, file)); + final Gist created = service.createGist(gist); + PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() { @SuppressWarnings("restriction") public void run() { - GistNotificationPopup popup = new GistNotificationPopup(Display.getDefault(), url, title); + GistNotificationPopup popup = new GistNotificationPopup( + PlatformUI.getWorkbench().getDisplay(), created + .getId(), title); popup.create(); popup.open(); } }); - } catch (GitHubServiceException e) { - GitHubUi.logError(e); } catch (IOException e) { GitHubUi.logError(e); } diff --git a/org.eclipse.mylyn.github.ui/src/org/eclipse/mylyn/github/ui/internal/GistNotificationPopup.java b/org.eclipse.mylyn.github.ui/src/org/eclipse/mylyn/github/ui/internal/GistNotificationPopup.java index 2d5fac8a..4cc84448 100644 --- a/org.eclipse.mylyn.github.ui/src/org/eclipse/mylyn/github/ui/internal/GistNotificationPopup.java +++ b/org.eclipse.mylyn.github.ui/src/org/eclipse/mylyn/github/ui/internal/GistNotificationPopup.java @@ -26,13 +26,13 @@ import org.eclipse.swt.widgets.Link; @SuppressWarnings("restriction") public class GistNotificationPopup extends AbstractNotificationPopup { - private String gistURL; + private String id; private String title; - public GistNotificationPopup(Display display, String gistURL, String title) { + public GistNotificationPopup(Display display, String id, String title) { super(display); - this.gistURL = gistURL; + this.id = id; this.title = title; } @@ -42,15 +42,14 @@ public class GistNotificationPopup extends AbstractNotificationPopup { Label label = new Label(composite, SWT.NONE); label.setText("Title: " + title); Link link = new Link(composite, SWT.WRAP); - String number = gistURL.split("https://gist.github.com/")[1]; - link.setText("Created Gist: <a>" + number + "</a>"); + link.setText("Created Gist: <a>" + id + "</a>"); link.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); link.setBackground(composite.getBackground()); link.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { - Program.launch(gistURL); + Program.launch("https://gist.github.com/" + id); } }); } |
