Skip to content

Commit

Permalink
Merge pull request #94 from suryagaddipati/master
Browse files Browse the repository at this point in the history
Add support for adding deploykeys to repo
  • Loading branch information
kohsuke committed Jun 5, 2014
2 parents 134ece9 + 3e3c6f7 commit 08eafc4
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/main/java/org/kohsuke/github/GHDeployKey.java
Original file line number Diff line number Diff line change
@@ -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));
}
}
16 changes: 16 additions & 0 deletions src/main/java/org/kohsuke/github/GHRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,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<GHDeployKey> getDeployKeys() throws IOException{
List<GHDeployKey> list = new ArrayList<GHDeployKey>(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() {
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/org/kohsuke/github/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand Down Expand Up @@ -547,6 +551,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 [email protected]");
assertNotNull(newDeployKey.getId());

Iterables.find( myRepository.getDeployKeys(),new Predicate<GHDeployKey>() {
public boolean apply(GHDeployKey deployKey) {
return newDeployKey.getId() == deployKey.getId() ;
}
});
newDeployKey.delete();
}

private void kohsuke() {
String login = getUser().getLogin();
Expand Down

0 comments on commit 08eafc4

Please sign in to comment.