diff options
author | Kevin Sawicki | 2011-05-24 17:35:12 +0000 |
---|---|---|
committer | Kevin Sawicki | 2011-05-24 17:35:12 +0000 |
commit | 917a6928bf371db1fddacdffddb3883d838516f9 (patch) | |
tree | 43b5523a3079a4630872bd72a654c90a43896958 | |
parent | be9cd4654e45954307b8f99e22f294bdf0bec8e2 (diff) | |
download | egit-github-917a6928bf371db1fddacdffddb3883d838516f9.tar.gz egit-github-917a6928bf371db1fddacdffddb3883d838516f9.tar.xz egit-github-917a6928bf371db1fddacdffddb3883d838516f9.zip |
Add support for creating GitHub repositories.
Change-Id: I2a217ad041e8d481316ed01544b4f72c675b5681
Signed-off-by: Kevin Sawicki <kevin@github.com>
4 files changed, 113 insertions, 2 deletions
diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/live/RepositoryTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/live/RepositoryTest.java index 808273d2..2455b508 100644 --- a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/live/RepositoryTest.java +++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/live/RepositoryTest.java @@ -47,4 +47,24 @@ public class RepositoryTest extends LiveTest { } } + /** + * Test creating a repository + * + * @throws IOException + */ + public void testCreate() throws IOException { + assertNotNull("Client user is required", client.getUser()); + RepositoryService service = new RepositoryService( + createClient(IGitHubConstants.URL_API_V2)); + Repository repository = new Repository(client.getUser(), "test-create-" + + System.currentTimeMillis()); + repository.setPrivate(true); + Repository created = service.createRepository(repository); + assertNotNull(created); + assertNotSame(repository, created); + assertTrue(created.isPrivate()); + assertEquals(repository.getOwner(), created.getOwner()); + assertEquals(repository.getName(), created.getName()); + } + } diff --git a/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/Repository.java b/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/Repository.java index 7ea72d5d..42775414 100644 --- a/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/Repository.java +++ b/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/Repository.java @@ -169,6 +169,13 @@ public class Repository { } /** + * @param name + */ + public void setName(String name) { + this.name = name; + } + + /** * @return fork */ public boolean isFork() { @@ -204,6 +211,13 @@ public class Repository { } /** + * @param isPrivate + */ + public void setPrivate(boolean isPrivate) { + this.isPrivate = isPrivate; + } + + /** * @return createdAt */ public Date getCreatedAt() { @@ -226,6 +240,13 @@ public class Repository { } /** + * @param description + */ + public void setDescription(String description) { + this.description = description; + } + + /** * @return homepage */ public String getHomepage() { @@ -233,6 +254,13 @@ public class Repository { } /** + * @param homepage + */ + public void setHomepage(String homepage) { + this.homepage = homepage; + } + + /** * @return language */ public String getLanguage() { diff --git a/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/client/IGitHubConstants.java b/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/client/IGitHubConstants.java index 7b0decef..66375c78 100644 --- a/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/client/IGitHubConstants.java +++ b/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/client/IGitHubConstants.java @@ -71,6 +71,11 @@ public interface IGitHubConstants { String SEGMENT_USERS = "/users"; //$NON-NLS-1$ /** + * SEGMENT_CREATE + */ + String SEGMENT_CREATE = "/create"; //$NON-NLS-1$ + + /** * SEGMENT_V2_API */ String SEGMENT_V2_API = "/api/v2/json"; //$NON-NLS-1$ diff --git a/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/service/RepositoryService.java b/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/service/RepositoryService.java index 94cb74db..51711212 100644 --- a/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/service/RepositoryService.java +++ b/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/service/RepositoryService.java @@ -11,8 +11,11 @@ package org.eclipse.egit.github.core.service; import java.io.IOException; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import org.eclipse.egit.github.core.Assert; import org.eclipse.egit.github.core.IResourceProvider; import org.eclipse.egit.github.core.ListResourceCollector; import org.eclipse.egit.github.core.Repository; @@ -22,11 +25,29 @@ import org.eclipse.egit.github.core.client.PagedRequest; /** * Repository service class. - * - * @author Kevin Sawicki (kevin@github.com) */ public class RepositoryService extends GitHubService { + /** + * FIELD_NAME + */ + public static final String FIELD_NAME = "name"; //$NON-NLS-1$ + + /** + * FIELD_DESCRIPTION + */ + public static final String FIELD_DESCRIPTION = "description"; //$NON-NLS-1$ + + /** + * FIELD_HOMEPAGE + */ + public static final String FIELD_HOMEPAGE = "homepage"; //$NON-NLS-1$ + + /** + * FIELD_PUBLIC + */ + public static final String FIELD_PUBLIC = "public"; //$NON-NLS-1$ + private static class RepositoryContainer implements IResourceProvider<Repository> { @@ -41,6 +62,12 @@ public class RepositoryService extends GitHubService { } + private static class RepositoryWrapper { + + Repository repository; + + } + /** * Create repository service * @@ -92,4 +119,35 @@ public class RepositoryService extends GitHubService { getAll(request); return collector.getResources(); } + + /** + * Create a new repository + * + * @param repository + * @return created repository + * @throws IOException + */ + public Repository createRepository(Repository repository) + throws IOException { + Assert.notNull("Repository cannot be null", repository); //$NON-NLS-1$ + StringBuilder uri = new StringBuilder(IGitHubConstants.SEGMENT_V2_API); + uri.append(IGitHubConstants.SEGMENT_REPOS).append( + IGitHubConstants.SEGMENT_CREATE); + + // Name is required, all others are optional + Map<String, String> params = new HashMap<String, String>(); + params.put(FIELD_NAME, repository.getName()); + String desc = repository.getDescription(); + if (desc != null) + params.put(FIELD_DESCRIPTION, desc); + String homepage = repository.getHomepage(); + if (homepage != null) + params.put(FIELD_HOMEPAGE, homepage); + params.put(FIELD_PUBLIC, repository.isPrivate() ? Integer.toString(0) + : Integer.toString(1)); + + RepositoryWrapper wrapper = client.post(uri.toString(), params, + RepositoryWrapper.class); + return wrapper.repository; + } } |