From 3e3c6f70ba4cac97ed8bcdf19288579f3b0ddaa8 Mon Sep 17 00:00:00 2001 From: Surya Gaddipati Date: Tue, 3 Jun 2014 15:26:04 -0500 Subject: [PATCH] Add support for adding deploykeys to repo Implements https://developer.github.com/v3/repos/keys/ --- .../java/org/kohsuke/github/GHDeployKey.java | 46 +++++++++++++++++++ .../java/org/kohsuke/github/GHRepository.java | 16 +++++++ src/test/java/org/kohsuke/github/AppTest.java | 18 ++++++++ 3 files changed, 80 insertions(+) create mode 100644 src/main/java/org/kohsuke/github/GHDeployKey.java diff --git a/src/main/java/org/kohsuke/github/GHDeployKey.java b/src/main/java/org/kohsuke/github/GHDeployKey.java new file mode 100644 index 0000000000..6e4ed496a6 --- /dev/null +++ b/src/main/java/org/kohsuke/github/GHDeployKey.java @@ -0,0 +1,46 @@ +package org.kohsuke.github; + +import java.io.IOException; + +import org.apache.commons.lang.builder.ToStringBuilder; + +public class GHDeployKey { + + protected String url, key, title; + protected boolean verified; + protected int id; + private GHRepository owner; + + public int getId() { + return id; + } + + public String getKey() { + return key; + } + + public String getTitle() { + return title; + } + + public String getUrl() { + return url; + } + + public boolean isVerified() { + return verified; + } + + public GHDeployKey wrap(GHRepository repo) { + this.owner = repo; + return this; + } + + public String toString() { + return new ToStringBuilder(this).append("title",title).append("id",id).append("key",key).toString(); + } + + public void delete() throws IOException { + new Requester(owner.root).method("DELETE").to(String.format("/repos/%s/%s/keys/%d", owner.getOwnerName(), owner.getName(), id)); + } +} diff --git a/src/main/java/org/kohsuke/github/GHRepository.java b/src/main/java/org/kohsuke/github/GHRepository.java index 7a9fd28eb6..10c87665e1 100644 --- a/src/main/java/org/kohsuke/github/GHRepository.java +++ b/src/main/java/org/kohsuke/github/GHRepository.java @@ -907,6 +907,22 @@ public GHMilestone createMilestone(String title, String description) throws IOEx return new Requester(root) .with("title", title).with("description", description).method("POST").to(getApiTailUrl("milestones"), GHMilestone.class).wrap(this); } + + public GHDeployKey addDeployKey(String title,String key) throws IOException { + return new Requester(root) + .with("title", title).with("key", key).method("POST").to(getApiTailUrl("keys"), GHDeployKey.class).wrap(this); + + } + + public List getDeployKeys() throws IOException{ + List list = new ArrayList(Arrays.asList( + root.retrieve().to(String.format("/repos/%s/%s/keys", owner.login, name), GHDeployKey[].class))); + for (GHDeployKey h : list) + h.wrap(this); + return list; + } + + @Override public String toString() { diff --git a/src/test/java/org/kohsuke/github/AppTest.java b/src/test/java/org/kohsuke/github/AppTest.java index f333d26204..10cc712aea 100644 --- a/src/test/java/org/kohsuke/github/AppTest.java +++ b/src/test/java/org/kohsuke/github/AppTest.java @@ -3,6 +3,7 @@ import java.io.IOException; import java.net.URL; import java.util.ArrayList; +import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Set; @@ -20,6 +21,9 @@ import org.kohsuke.github.GHCommit.File; import org.kohsuke.github.GHOrganization.Permission; +import com.google.common.base.Predicate; +import com.google.common.collect.Iterables; + import java.util.Date; /** @@ -541,6 +545,20 @@ public void directoryListing() throws IOException { } } } + + @Test + public void testAddDeployKey() throws IOException { + GHRepository myRepository = Iterables.get(gitHub.getMyself().getRepositories().values(),0); + final GHDeployKey newDeployKey = myRepository.addDeployKey("test", "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC55wA5wHqTFMk3OkyHqtmgSAmIanREVP4ukMrPZFzYfRBaKYPCbBRxu7ddzF3oZ+i6ZV8+rH8hvhQTYl5LtOIxLUppsVVNSB9YKXQv37LLaWul9WoJPdXHGWfR3wlhRXsg1sMPpbgu60lXAl7xvx729FEjKEEHRMGkPbcIeHkov/tlEg9oQdqFC1Pqnv/lCsZ5UKRPLHY3V9pmSaEplwmwb//HppNtEYr9t6VNvOMjqbUrbhsilKu0t6qa3G7Kb47kvfJwMn+DKD2XJMYHYHMyHtHcFK8RIOSX8I+Bu4yeVmvcooSL65FBCIrmVoejkI7gZWDfgWVRboQ9RyB+VeXL example@example.com"); + assertNotNull(newDeployKey.getId()); + + Iterables.find( myRepository.getDeployKeys(),new Predicate() { + public boolean apply(GHDeployKey deployKey) { + return newDeployKey.getId() == deployKey.getId() ; + } + }); + newDeployKey.delete(); + } private void kohsuke() { String login = getUser().getLogin();