Skip to content
This repository was archived by the owner on Jul 31, 2025. It is now read-only.

Commit 3e3c6f7

Browse files
author
Surya Gaddipati
committed
Add support for adding deploykeys to repo
Implements https://developer.github.com/v3/repos/keys/
1 parent 3097378 commit 3e3c6f7

3 files changed

Lines changed: 80 additions & 0 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package org.kohsuke.github;
2+
3+
import java.io.IOException;
4+
5+
import org.apache.commons.lang.builder.ToStringBuilder;
6+
7+
public class GHDeployKey {
8+
9+
protected String url, key, title;
10+
protected boolean verified;
11+
protected int id;
12+
private GHRepository owner;
13+
14+
public int getId() {
15+
return id;
16+
}
17+
18+
public String getKey() {
19+
return key;
20+
}
21+
22+
public String getTitle() {
23+
return title;
24+
}
25+
26+
public String getUrl() {
27+
return url;
28+
}
29+
30+
public boolean isVerified() {
31+
return verified;
32+
}
33+
34+
public GHDeployKey wrap(GHRepository repo) {
35+
this.owner = repo;
36+
return this;
37+
}
38+
39+
public String toString() {
40+
return new ToStringBuilder(this).append("title",title).append("id",id).append("key",key).toString();
41+
}
42+
43+
public void delete() throws IOException {
44+
new Requester(owner.root).method("DELETE").to(String.format("/repos/%s/%s/keys/%d", owner.getOwnerName(), owner.getName(), id));
45+
}
46+
}

src/main/java/org/kohsuke/github/GHRepository.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,22 @@ public GHMilestone createMilestone(String title, String description) throws IOEx
907907
return new Requester(root)
908908
.with("title", title).with("description", description).method("POST").to(getApiTailUrl("milestones"), GHMilestone.class).wrap(this);
909909
}
910+
911+
public GHDeployKey addDeployKey(String title,String key) throws IOException {
912+
return new Requester(root)
913+
.with("title", title).with("key", key).method("POST").to(getApiTailUrl("keys"), GHDeployKey.class).wrap(this);
914+
915+
}
916+
917+
public List<GHDeployKey> getDeployKeys() throws IOException{
918+
List<GHDeployKey> list = new ArrayList<GHDeployKey>(Arrays.asList(
919+
root.retrieve().to(String.format("/repos/%s/%s/keys", owner.login, name), GHDeployKey[].class)));
920+
for (GHDeployKey h : list)
921+
h.wrap(this);
922+
return list;
923+
}
924+
925+
910926

911927
@Override
912928
public String toString() {

src/test/java/org/kohsuke/github/AppTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.IOException;
44
import java.net.URL;
55
import java.util.ArrayList;
6+
import java.util.Collection;
67
import java.util.List;
78
import java.util.Map;
89
import java.util.Set;
@@ -20,6 +21,9 @@
2021
import org.kohsuke.github.GHCommit.File;
2122
import org.kohsuke.github.GHOrganization.Permission;
2223

24+
import com.google.common.base.Predicate;
25+
import com.google.common.collect.Iterables;
26+
2327
import java.util.Date;
2428

2529
/**
@@ -541,6 +545,20 @@ public void directoryListing() throws IOException {
541545
}
542546
}
543547
}
548+
549+
@Test
550+
public void testAddDeployKey() throws IOException {
551+
GHRepository myRepository = Iterables.get(gitHub.getMyself().getRepositories().values(),0);
552+
final GHDeployKey newDeployKey = myRepository.addDeployKey("test", "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC55wA5wHqTFMk3OkyHqtmgSAmIanREVP4ukMrPZFzYfRBaKYPCbBRxu7ddzF3oZ+i6ZV8+rH8hvhQTYl5LtOIxLUppsVVNSB9YKXQv37LLaWul9WoJPdXHGWfR3wlhRXsg1sMPpbgu60lXAl7xvx729FEjKEEHRMGkPbcIeHkov/tlEg9oQdqFC1Pqnv/lCsZ5UKRPLHY3V9pmSaEplwmwb//HppNtEYr9t6VNvOMjqbUrbhsilKu0t6qa3G7Kb47kvfJwMn+DKD2XJMYHYHMyHtHcFK8RIOSX8I+Bu4yeVmvcooSL65FBCIrmVoejkI7gZWDfgWVRboQ9RyB+VeXL [email protected]");
553+
assertNotNull(newDeployKey.getId());
554+
555+
Iterables.find( myRepository.getDeployKeys(),new Predicate<GHDeployKey>() {
556+
public boolean apply(GHDeployKey deployKey) {
557+
return newDeployKey.getId() == deployKey.getId() ;
558+
}
559+
});
560+
newDeployKey.delete();
561+
}
544562

545563
private void kohsuke() {
546564
String login = getUser().getLogin();

0 commit comments

Comments
 (0)