Skip to content

Commit

Permalink
Merge pull request #1446 from maddymanu/public-keys-maddymanu
Browse files Browse the repository at this point in the history
Adding Secrets + Public Key to Repository
  • Loading branch information
bitwiseman committed Jun 21, 2022
2 parents 38918c4 + 1fa164c commit aa7f342
Show file tree
Hide file tree
Showing 15 changed files with 889 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/main/java/org/kohsuke/github/GHRepository.java
Expand Up @@ -3184,6 +3184,20 @@ public GHWorkflowJob getWorkflowJob(long id) throws IOException {
.wrapUp(this);
}

/**
* Gets the public key for the given repo
*
* @return the public key
* @throws IOException
* the io exception
*/
public GHRepositoryPublicKey getPublicKey() throws IOException {
return root().createRequest()
.withUrlPath(getApiTailUrl("/actions/secrets/public-key"))
.fetch(GHRepositoryPublicKey.class)
.wrapUp(this);
}

// Only used within listTopics().
private static class Topics {
public List<String> names;
Expand Down Expand Up @@ -3223,6 +3237,27 @@ public void setTopics(List<String> topics) throws IOException {
.send();
}

/**
* Set/Update a repository secret
* "https://docs.github.com/rest/reference/actions#create-or-update-a-repository-secret"
*
* @param secretName
* the name of the secret
* @param encryptedValue
* The encrypted value for this secret
* @param publicKeyId
* The id of the Public Key used to encrypt this secret
* @throws IOException
* the io exception
*/
public void createSecret(String secretName, String encryptedValue, String publicKeyId) throws IOException {
root().createRequest()
.method("PUT")
.with("encrypted_value", encryptedValue)
.with("key_id", publicKeyId)
.withUrlPath(getApiTailUrl("actions/secrets") + "/" + secretName)
.send();
}
/**
* Create a tag. See https://developer.github.com/v3/git/tags/#create-a-tag-object
*
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/org/kohsuke/github/GHRepositoryPublicKey.java
@@ -0,0 +1,38 @@
package org.kohsuke.github;

import com.fasterxml.jackson.annotation.JsonIgnore;

import java.io.IOException;
import java.net.URL;

/**
* A public key for the given repository
*
* @author Aditya Bansal
*/
public class GHRepositoryPublicKey extends GHObject {
// Not provided by the API.
@JsonIgnore
private GHRepository owner;

private String keyId;
private String key;

@Override
public URL getHtmlUrl() throws IOException {
return null;
}

public String getKeyId() {
return keyId;
}

public String getKey() {
return key;
}

GHRepositoryPublicKey wrapUp(GHRepository owner) {
this.owner = owner;
return this;
}
}
14 changes: 14 additions & 0 deletions src/test/java/org/kohsuke/github/GHRepositoryTest.java
Expand Up @@ -753,6 +753,15 @@ public void getRefs() throws Exception {
assertThat(refs[0].getRef(), equalTo("refs/heads/main"));
}

@Test
public void getPublicKey() throws Exception {
GHRepository repo = getTempRepository();
GHRepositoryPublicKey publicKey = repo.getPublicKey();
assertThat(publicKey, notNullValue());
assertThat(publicKey.getKey(), equalTo("test-key"));
assertThat(publicKey.getKeyId(), equalTo("key-id"));
}

@Test
public void getRefsHeads() throws Exception {
GHRepository repo = getTempRepository();
Expand Down Expand Up @@ -1099,4 +1108,9 @@ public void createDispatchEventWithClientPayload() throws Exception {
repository.dispatch("test", clientPayload);
}

@Test
public void createSecret() throws Exception {
GHRepository repo = getTempRepository();
repo.createSecret("secret", "encrypted", "public");
}
}
@@ -0,0 +1,41 @@
{
"login": "hub4j-test-org",
"id": 7544739,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=",
"url": "https://api.github.com/orgs/hub4j-test-org",
"repos_url": "https://api.github.com/orgs/hub4j-test-org/repos",
"events_url": "https://api.github.com/orgs/hub4j-test-org/events",
"hooks_url": "https://api.github.com/orgs/hub4j-test-org/hooks",
"issues_url": "https://api.github.com/orgs/hub4j-test-org/issues",
"members_url": "https://api.github.com/orgs/hub4j-test-org/members{/member}",
"public_members_url": "https://api.github.com/orgs/hub4j-test-org/public_members{/member}",
"avatar_url": "https://avatars3.githubusercontent.com/u/7544739?v=4",
"description": null,
"is_verified": false,
"has_organization_projects": true,
"has_repository_projects": true,
"public_repos": 10,
"public_gists": 0,
"followers": 0,
"following": 0,
"html_url": "https://github.com/hub4j-test-org",
"created_at": "2014-05-10T19:39:11Z",
"updated_at": "2015-04-20T00:42:30Z",
"type": "Organization",
"total_private_repos": 0,
"owned_private_repos": 0,
"private_gists": 0,
"disk_usage": 147,
"collaborators": 0,
"billing_email": "kk@kohsuke.org",
"default_repository_permission": "none",
"members_can_create_repositories": false,
"two_factor_requirement_enabled": false,
"plan": {
"name": "free",
"space": 976562499,
"private_repos": 0,
"filled_seats": 11,
"seats": 0
}
}

0 comments on commit aa7f342

Please sign in to comment.