Skip to content

Commit

Permalink
Merge pull request #1248 from mxandeco/add_repository_dispatch_event
Browse files Browse the repository at this point in the history
Add repository dispatch event
  • Loading branch information
bitwiseman authored Oct 4, 2021
2 parents 9e4b709 + aefe8d3 commit 8f3a100
Show file tree
Hide file tree
Showing 12 changed files with 669 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/main/java/org/kohsuke/github/GHRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import java.util.WeakHashMap;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import static java.util.Arrays.asList;
import static java.util.Objects.requireNonNull;
Expand Down Expand Up @@ -3211,6 +3212,26 @@ public <T> T readTar(InputStreamFunction<T> streamFunction, String ref) throws I
return downloadArchive("tar", ref, streamFunction);
}

/**
* Create a repository dispatch event, which can be used to start a workflow/action from outside github, as
* described on https://docs.github.com/en/rest/reference/repos#create-a-repository-dispatch-event
*
* @param eventType
* the eventType
* @param clientPayload
* a custom payload , can be nullable
* @throws IOException
* the io exception
*/
public <T> void dispatch(String eventType, @Nullable T clientPayload) throws IOException {
root().createRequest()
.method("POST")
.withUrlPath(getApiTailUrl("dispatches"))
.with("event_type", eventType)
.with("client_payload", clientPayload)
.send();
}

private <T> T downloadArchive(@Nonnull String type,
@CheckForNull String ref,
@Nonnull InputStreamFunction<T> streamFunction) throws IOException {
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/org/kohsuke/github/GHRepositoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -987,4 +988,19 @@ public void getCommitsBetweenPaged() throws Exception {
assertThat(mockGitHub.getRequestCount(), equalTo(startingCount + 4));
}

@Test
public void createDispatchEventWithoutClientPayload() throws Exception {
GHRepository repository = getTempRepository();
repository.dispatch("test", null);
}

@Test
public void createDispatchEventWithClientPayload() throws Exception {
GHRepository repository = getTempRepository();
Map<String, Object> clientPayload = new HashMap<>();
clientPayload.put("name", "joe.doe");
clientPayload.put("list", new ArrayList<>());
repository.dispatch("test", clientPayload);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
{
"id": 412057727,
"node_id": "R_kgDOGI-Afw",
"name": "temp-createDispatchEventWithClientPayload",
"full_name": "hub4j-test-org/temp-createDispatchEventWithClientPayload",
"private": false,
"owner": {
"login": "hub4j-test-org",
"id": 7544739,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=",
"avatar_url": "https://avatars.githubusercontent.com/u/7544739?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/hub4j-test-org",
"html_url": "https://github.com/hub4j-test-org",
"followers_url": "https://api.github.com/users/hub4j-test-org/followers",
"following_url": "https://api.github.com/users/hub4j-test-org/following{/other_user}",
"gists_url": "https://api.github.com/users/hub4j-test-org/gists{/gist_id}",
"starred_url": "https://api.github.com/users/hub4j-test-org/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/hub4j-test-org/subscriptions",
"organizations_url": "https://api.github.com/users/hub4j-test-org/orgs",
"repos_url": "https://api.github.com/users/hub4j-test-org/repos",
"events_url": "https://api.github.com/users/hub4j-test-org/events{/privacy}",
"received_events_url": "https://api.github.com/users/hub4j-test-org/received_events",
"type": "Organization",
"site_admin": false
},
"html_url": "https://github.com/hub4j-test-org/temp-createDispatchEventWithClientPayload",
"description": "A test repository for testing the github-api project: temp-createDispatchEventWithClientPayload",
"fork": false,
"url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload",
"forks_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/forks",
"keys_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/keys{/key_id}",
"collaborators_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/collaborators{/collaborator}",
"teams_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/teams",
"hooks_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/hooks",
"issue_events_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/issues/events{/number}",
"events_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/events",
"assignees_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/assignees{/user}",
"branches_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/branches{/branch}",
"tags_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/tags",
"blobs_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/git/blobs{/sha}",
"git_tags_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/git/tags{/sha}",
"git_refs_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/git/refs{/sha}",
"trees_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/git/trees{/sha}",
"statuses_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/statuses/{sha}",
"languages_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/languages",
"stargazers_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/stargazers",
"contributors_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/contributors",
"subscribers_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/subscribers",
"subscription_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/subscription",
"commits_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/commits{/sha}",
"git_commits_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/git/commits{/sha}",
"comments_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/comments{/number}",
"issue_comment_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/issues/comments{/number}",
"contents_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/contents/{+path}",
"compare_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/compare/{base}...{head}",
"merges_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/merges",
"archive_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/{archive_format}{/ref}",
"downloads_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/downloads",
"issues_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/issues{/number}",
"pulls_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/pulls{/number}",
"milestones_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/milestones{/number}",
"notifications_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/notifications{?since,all,participating}",
"labels_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/labels{/name}",
"releases_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/releases{/id}",
"deployments_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/deployments",
"created_at": "2021-09-30T12:34:44Z",
"updated_at": "2021-09-30T12:34:44Z",
"pushed_at": "2021-09-30T12:34:45Z",
"git_url": "git://github.com/hub4j-test-org/temp-createDispatchEventWithClientPayload.git",
"ssh_url": "[email protected]:hub4j-test-org/temp-createDispatchEventWithClientPayload.git",
"clone_url": "https://github.com/hub4j-test-org/temp-createDispatchEventWithClientPayload.git",
"svn_url": "https://github.com/hub4j-test-org/temp-createDispatchEventWithClientPayload",
"homepage": "http://github-api.kohsuke.org/",
"size": 0,
"stargazers_count": 0,
"watchers_count": 0,
"language": null,
"has_issues": true,
"has_projects": true,
"has_downloads": true,
"has_wiki": true,
"has_pages": false,
"forks_count": 0,
"mirror_url": null,
"archived": false,
"disabled": false,
"open_issues_count": 0,
"license": null,
"allow_forking": true,
"forks": 0,
"open_issues": 0,
"watchers": 0,
"default_branch": "main",
"permissions": {
"admin": true,
"maintain": true,
"push": true,
"triage": true,
"pull": true
},
"temp_clone_token": "",
"allow_squash_merge": true,
"allow_merge_commit": true,
"allow_rebase_merge": true,
"allow_auto_merge": false,
"delete_branch_on_merge": false,
"organization": {
"login": "hub4j-test-org",
"id": 7544739,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=",
"avatar_url": "https://avatars.githubusercontent.com/u/7544739?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/hub4j-test-org",
"html_url": "https://github.com/hub4j-test-org",
"followers_url": "https://api.github.com/users/hub4j-test-org/followers",
"following_url": "https://api.github.com/users/hub4j-test-org/following{/other_user}",
"gists_url": "https://api.github.com/users/hub4j-test-org/gists{/gist_id}",
"starred_url": "https://api.github.com/users/hub4j-test-org/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/hub4j-test-org/subscriptions",
"organizations_url": "https://api.github.com/users/hub4j-test-org/orgs",
"repos_url": "https://api.github.com/users/hub4j-test-org/repos",
"events_url": "https://api.github.com/users/hub4j-test-org/events{/privacy}",
"received_events_url": "https://api.github.com/users/hub4j-test-org/received_events",
"type": "Organization",
"site_admin": false
},
"network_count": 0,
"subscribers_count": 15
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"login": "mxandeco",
"id": 42962662,
"node_id": "MDQ6VXNlcjQyOTYyNjYy",
"avatar_url": "https://avatars.githubusercontent.com/u/42962662?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/mxandeco",
"html_url": "https://github.com/mxandeco",
"followers_url": "https://api.github.com/users/mxandeco/followers",
"following_url": "https://api.github.com/users/mxandeco/following{/other_user}",
"gists_url": "https://api.github.com/users/mxandeco/gists{/gist_id}",
"starred_url": "https://api.github.com/users/mxandeco/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/mxandeco/subscriptions",
"organizations_url": "https://api.github.com/users/mxandeco/orgs",
"repos_url": "https://api.github.com/users/mxandeco/repos",
"events_url": "https://api.github.com/users/mxandeco/events{/privacy}",
"received_events_url": "https://api.github.com/users/mxandeco/received_events",
"type": "User",
"site_admin": false,
"name": "Max Xandeco",
"company": null,
"blog": "",
"location": null,
"email": null,
"hireable": null,
"bio": null,
"twitter_username": null,
"public_repos": 18,
"public_gists": 1,
"followers": 0,
"following": 0,
"created_at": "2018-09-04T09:13:09Z",
"updated_at": "2021-09-06T20:36:46Z",
"private_gists": 0,
"total_private_repos": 8,
"owned_private_repos": 4,
"disk_usage": 90,
"collaborators": 1,
"two_factor_authentication": true,
"plan": {
"name": "free",
"space": 976562499,
"collaborators": 0,
"private_repos": 10000
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"id": "2539d5de-f96e-431e-b31b-a537e52e0ef5",
"name": "repos_hub4j-test-org_temp-createdispatcheventwithclientpayload",
"request": {
"url": "/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload",
"method": "GET",
"headers": {
"Accept": {
"equalTo": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"
}
}
},
"response": {
"status": 200,
"bodyFileName": "repos_hub4j-test-org_temp-createdispatcheventwithclientpayload-2.json",
"headers": {
"Server": "GitHub.com",
"Date": "Thu, 30 Sep 2021 12:34:49 GMT",
"Content-Type": "application/json; charset=utf-8",
"Cache-Control": "private, max-age=60, s-maxage=60",
"Vary": [
"Accept, Authorization, Cookie, X-GitHub-OTP",
"Accept-Encoding, Accept, X-Requested-With"
],
"ETag": "W/\"5ab7e1e833c6acac05ab9315823a4f6b6a662de9d5f2539189c24ce631404077\"",
"Last-Modified": "Thu, 30 Sep 2021 12:34:44 GMT",
"X-OAuth-Scopes": "admin:enterprise, admin:gpg_key, admin:org, admin:org_hook, admin:public_key, admin:repo_hook, delete:packages, delete_repo, gist, notifications, repo, user, workflow, write:discussion, write:packages",
"X-Accepted-OAuth-Scopes": "repo",
"X-GitHub-Media-Type": "unknown, github.v3",
"X-RateLimit-Limit": "5000",
"X-RateLimit-Remaining": "4940",
"X-RateLimit-Reset": "1633006817",
"X-RateLimit-Used": "60",
"X-RateLimit-Resource": "core",
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
"X-Frame-Options": "deny",
"X-Content-Type-Options": "nosniff",
"X-XSS-Protection": "0",
"Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
"Content-Security-Policy": "default-src 'none'",
"X-GitHub-Request-Id": "DEB6:4B95:4C9D23:4E7593:6155AEE8"
}
},
"uuid": "2539d5de-f96e-431e-b31b-a537e52e0ef5",
"persistent": true,
"insertionIndex": 2
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"id": "bd959614-1104-415e-b9a1-8ac9533d82f7",
"name": "repos_hub4j-test-org_temp-createdispatcheventwithclientpayload_dispatches",
"request": {
"url": "/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/dispatches",
"method": "POST",
"headers": {
"Accept": {
"equalTo": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"
}
},
"bodyPatterns": [
{
"equalToJson": "{\"event_type\":\"test\",\"client_payload\":{\"name\":\"joe.doe\",\"list\":[]}}",
"ignoreArrayOrder": true,
"ignoreExtraElements": false
}
]
},
"response": {
"status": 204,
"headers": {
"Server": "GitHub.com",
"Date": "Thu, 30 Sep 2021 12:34:49 GMT",
"X-OAuth-Scopes": "admin:enterprise, admin:gpg_key, admin:org, admin:org_hook, admin:public_key, admin:repo_hook, delete:packages, delete_repo, gist, notifications, repo, user, workflow, write:discussion, write:packages",
"X-Accepted-OAuth-Scopes": "",
"X-GitHub-Media-Type": "unknown, github.v3",
"X-RateLimit-Limit": "5000",
"X-RateLimit-Remaining": "4939",
"X-RateLimit-Reset": "1633006817",
"X-RateLimit-Used": "61",
"X-RateLimit-Resource": "core",
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
"X-Frame-Options": "deny",
"X-Content-Type-Options": "nosniff",
"X-XSS-Protection": "0",
"Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
"Content-Security-Policy": "default-src 'none'",
"Vary": "Accept-Encoding, Accept, X-Requested-With",
"X-GitHub-Request-Id": "DEB7:0B4B:3073ED:320304:6155AEE9"
}
},
"uuid": "bd959614-1104-415e-b9a1-8ac9533d82f7",
"persistent": true,
"insertionIndex": 3
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"id": "43c6892b-f0dc-45a7-97b9-f81a8e3f244e",
"name": "user",
"request": {
"url": "/user",
"method": "GET",
"headers": {
"Accept": {
"equalTo": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"
}
}
},
"response": {
"status": 200,
"bodyFileName": "user-1.json",
"headers": {
"Server": "GitHub.com",
"Date": "Thu, 30 Sep 2021 12:34:43 GMT",
"Content-Type": "application/json; charset=utf-8",
"Cache-Control": "private, max-age=60, s-maxage=60",
"Vary": [
"Accept, Authorization, Cookie, X-GitHub-OTP",
"Accept-Encoding, Accept, X-Requested-With"
],
"ETag": "W/\"b8f235ad3cd7cc5e96a09241a22fa6048c7877f43ada37c29eb075107f7f3b1a\"",
"Last-Modified": "Mon, 06 Sep 2021 20:36:46 GMT",
"X-OAuth-Scopes": "admin:enterprise, admin:gpg_key, admin:org, admin:org_hook, admin:public_key, admin:repo_hook, delete:packages, delete_repo, gist, notifications, repo, user, workflow, write:discussion, write:packages",
"X-Accepted-OAuth-Scopes": "",
"X-GitHub-Media-Type": "unknown, github.v3",
"X-RateLimit-Limit": "5000",
"X-RateLimit-Remaining": "4945",
"X-RateLimit-Reset": "1633006817",
"X-RateLimit-Used": "55",
"X-RateLimit-Resource": "core",
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
"X-Frame-Options": "deny",
"X-Content-Type-Options": "nosniff",
"X-XSS-Protection": "0",
"Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
"Content-Security-Policy": "default-src 'none'",
"X-GitHub-Request-Id": "DEB3:FDF5:134720:14986B:6155AEE2"
}
},
"uuid": "43c6892b-f0dc-45a7-97b9-f81a8e3f244e",
"persistent": true,
"insertionIndex": 1
}
Loading

0 comments on commit 8f3a100

Please sign in to comment.