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

Support for object deployment payloads #920

Merged
merged 2 commits into from
Aug 7, 2020
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
23 changes: 22 additions & 1 deletion src/main/java/org/kohsuke/github/GHDeployment.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

/**
* Represents a deployment
Expand Down Expand Up @@ -60,14 +61,34 @@ public String getTask() {
}

/**
* Gets payload.
* Gets payload. <b>NOTE:</b> only use this method if you can guarantee the payload will be a simple string,
* otherwise use {@link #getPayloadObject()}.
*
* @return the payload
*/
public String getPayload() {
return (String) payload;
}

/**
* Gets payload. <b>NOTE:</b> only use this method if you can guarantee the payload will be a JSON object (Map),
* otherwise use {@link #getPayloadObject()}.
*
* @return the payload
*/
public Map<String, Object> getPayloadMap() {
return (Map<String, Object>) payload;
}

/**
* Gets payload without assuming its type. It could be a String or a Map.
*
* @return the payload
*/
public Object getPayloadObject() {
return payload;
}

/**
* Gets environment.
*
Expand Down
35 changes: 31 additions & 4 deletions src/test/java/org/kohsuke/github/GHDeploymentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,51 @@
import org.junit.Test;

import java.io.IOException;
import java.util.Arrays;
import java.util.Map;

/**
* @author Martin van Zijl
*/
public class GHDeploymentTest extends AbstractGitHubWireMockTest {

@Test
public void testGetDeploymentById() throws IOException {
GHRepository repo = getRepository();
GHDeployment deployment = repo.getDeployment(178653229);
public void testGetDeploymentByIdStringPayload() throws IOException {
final GHRepository repo = getRepository();
final GHDeployment deployment = repo.getDeployment(178653229);
assertNotNull(deployment);
assertEquals(178653229, deployment.getId());
assertEquals("production", deployment.getEnvironment());
assertEquals("custom", deployment.getPayload());
assertEquals("custom", deployment.getPayloadObject());
assertEquals("master", deployment.getRef());
assertEquals("3a09d2de4a9a1322a0ba2c3e2f54a919ca8fe353", deployment.getSha());
assertEquals("deploy", deployment.getTask());
}

@Test
public void testGetDeploymentByIdObjectPayload() throws IOException {
final GHRepository repo = getRepository();
final GHDeployment deployment = repo.getDeployment(178653229);
assertNotNull(deployment);
assertEquals(178653229, deployment.getId());
assertEquals("production", deployment.getEnvironment());
assertEquals("master", deployment.getRef());
assertEquals("3a09d2de4a9a1322a0ba2c3e2f54a919ca8fe353", deployment.getSha());
assertEquals("deploy", deployment.getTask());
final Map<String, Object> payload = deployment.getPayloadMap();
assertEquals(4, payload.size());
assertEquals(1, payload.get("custom1"));
assertEquals("two", payload.get("custom2"));
assertEquals(Arrays.asList("3", 3, "three"), payload.get("custom3"));
assertNull(payload.get("custom4"));
}

protected GHRepository getRepository() throws IOException {
return getRepository(gitHub);
}

private GHRepository getRepository(GitHub gitHub) throws IOException {
private GHRepository getRepository(final GitHub gitHub) throws IOException {
return gitHub.getOrganization("hub4j-test-org").getRepository("github-api");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"url": "https://api.github.com/repos/hub4j-test-org/github-api/deployments/178653229",
"id": 178653229,
"node_id": "MDEwOkRlcGxveW1lbnQxNzg2NTMyMjk=",
"sha": "3a09d2de4a9a1322a0ba2c3e2f54a919ca8fe353",
"ref": "master",
"task": "deploy",
"payload": {
"custom1": 1,
"custom2": "two",
"custom3": ["3", 3, "three"],
"custom4": null
},
"original_environment": "production",
"environment": "production",
"description": null,
"creator": {
"login": "martinvanzijl",
"id": 24422213,
"node_id": "MDQ6VXNlcjI0NDIyMjEz",
"avatar_url": "https://avatars0.githubusercontent.com/u/24422213?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/martinvanzijl",
"html_url": "https://github.com/martinvanzijl",
"followers_url": "https://api.github.com/users/martinvanzijl/followers",
"following_url": "https://api.github.com/users/martinvanzijl/following{/other_user}",
"gists_url": "https://api.github.com/users/martinvanzijl/gists{/gist_id}",
"starred_url": "https://api.github.com/users/martinvanzijl/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/martinvanzijl/subscriptions",
"organizations_url": "https://api.github.com/users/martinvanzijl/orgs",
"repos_url": "https://api.github.com/users/martinvanzijl/repos",
"events_url": "https://api.github.com/users/martinvanzijl/events{/privacy}",
"received_events_url": "https://api.github.com/users/martinvanzijl/received_events",
"type": "User",
"site_admin": false
},
"created_at": "2019-10-30T00:03:34Z",
"updated_at": "2019-10-30T00:03:34Z",
"statuses_url": "https://api.github.com/repos/hub4j-test-org/github-api/deployments/178653229/statuses",
"repository_url": "https://api.github.com/repos/hub4j-test-org/github-api"
}
Original file line number Diff line number Diff line change
@@ -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": 132,
"collaborators": 0,
"billing_email": "[email protected]",
"default_repository_permission": "none",
"members_can_create_repositories": false,
"two_factor_requirement_enabled": false,
"plan": {
"name": "free",
"space": 976562499,
"private_repos": 0,
"filled_seats": 7,
"seats": 0
}
}
Loading