Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for adding deploykeys to repo #94

Merged
merged 1 commit into from
Jun 5, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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<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 @@ -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 [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