Skip to content

Commit 8f3a100

Browse files
authored
Merge pull request #1248 from mxandeco/add_repository_dispatch_event
Add repository dispatch event
2 parents 9e4b709 + aefe8d3 commit 8f3a100

12 files changed

Lines changed: 669 additions & 0 deletions

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
import java.util.WeakHashMap;
5858

5959
import javax.annotation.Nonnull;
60+
import javax.annotation.Nullable;
6061

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

3215+
/**
3216+
* Create a repository dispatch event, which can be used to start a workflow/action from outside github, as
3217+
* described on https://docs.github.com/en/rest/reference/repos#create-a-repository-dispatch-event
3218+
*
3219+
* @param eventType
3220+
* the eventType
3221+
* @param clientPayload
3222+
* a custom payload , can be nullable
3223+
* @throws IOException
3224+
* the io exception
3225+
*/
3226+
public <T> void dispatch(String eventType, @Nullable T clientPayload) throws IOException {
3227+
root().createRequest()
3228+
.method("POST")
3229+
.withUrlPath(getApiTailUrl("dispatches"))
3230+
.with("event_type", eventType)
3231+
.with("client_payload", clientPayload)
3232+
.send();
3233+
}
3234+
32143235
private <T> T downloadArchive(@Nonnull String type,
32153236
@CheckForNull String ref,
32163237
@Nonnull InputStreamFunction<T> streamFunction) throws IOException {

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.net.URL;
1414
import java.util.ArrayList;
1515
import java.util.Date;
16+
import java.util.HashMap;
1617
import java.util.List;
1718
import java.util.Map;
1819
import java.util.Set;
@@ -987,4 +988,19 @@ public void getCommitsBetweenPaged() throws Exception {
987988
assertThat(mockGitHub.getRequestCount(), equalTo(startingCount + 4));
988989
}
989990

991+
@Test
992+
public void createDispatchEventWithoutClientPayload() throws Exception {
993+
GHRepository repository = getTempRepository();
994+
repository.dispatch("test", null);
995+
}
996+
997+
@Test
998+
public void createDispatchEventWithClientPayload() throws Exception {
999+
GHRepository repository = getTempRepository();
1000+
Map<String, Object> clientPayload = new HashMap<>();
1001+
clientPayload.put("name", "joe.doe");
1002+
clientPayload.put("list", new ArrayList<>());
1003+
repository.dispatch("test", clientPayload);
1004+
}
1005+
9901006
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
{
2+
"id": 412057727,
3+
"node_id": "R_kgDOGI-Afw",
4+
"name": "temp-createDispatchEventWithClientPayload",
5+
"full_name": "hub4j-test-org/temp-createDispatchEventWithClientPayload",
6+
"private": false,
7+
"owner": {
8+
"login": "hub4j-test-org",
9+
"id": 7544739,
10+
"node_id": "MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=",
11+
"avatar_url": "https://avatars.githubusercontent.com/u/7544739?v=4",
12+
"gravatar_id": "",
13+
"url": "https://api.github.com/users/hub4j-test-org",
14+
"html_url": "https://github.com/hub4j-test-org",
15+
"followers_url": "https://api.github.com/users/hub4j-test-org/followers",
16+
"following_url": "https://api.github.com/users/hub4j-test-org/following{/other_user}",
17+
"gists_url": "https://api.github.com/users/hub4j-test-org/gists{/gist_id}",
18+
"starred_url": "https://api.github.com/users/hub4j-test-org/starred{/owner}{/repo}",
19+
"subscriptions_url": "https://api.github.com/users/hub4j-test-org/subscriptions",
20+
"organizations_url": "https://api.github.com/users/hub4j-test-org/orgs",
21+
"repos_url": "https://api.github.com/users/hub4j-test-org/repos",
22+
"events_url": "https://api.github.com/users/hub4j-test-org/events{/privacy}",
23+
"received_events_url": "https://api.github.com/users/hub4j-test-org/received_events",
24+
"type": "Organization",
25+
"site_admin": false
26+
},
27+
"html_url": "https://github.com/hub4j-test-org/temp-createDispatchEventWithClientPayload",
28+
"description": "A test repository for testing the github-api project: temp-createDispatchEventWithClientPayload",
29+
"fork": false,
30+
"url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload",
31+
"forks_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/forks",
32+
"keys_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/keys{/key_id}",
33+
"collaborators_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/collaborators{/collaborator}",
34+
"teams_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/teams",
35+
"hooks_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/hooks",
36+
"issue_events_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/issues/events{/number}",
37+
"events_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/events",
38+
"assignees_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/assignees{/user}",
39+
"branches_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/branches{/branch}",
40+
"tags_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/tags",
41+
"blobs_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/git/blobs{/sha}",
42+
"git_tags_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/git/tags{/sha}",
43+
"git_refs_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/git/refs{/sha}",
44+
"trees_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/git/trees{/sha}",
45+
"statuses_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/statuses/{sha}",
46+
"languages_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/languages",
47+
"stargazers_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/stargazers",
48+
"contributors_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/contributors",
49+
"subscribers_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/subscribers",
50+
"subscription_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/subscription",
51+
"commits_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/commits{/sha}",
52+
"git_commits_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/git/commits{/sha}",
53+
"comments_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/comments{/number}",
54+
"issue_comment_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/issues/comments{/number}",
55+
"contents_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/contents/{+path}",
56+
"compare_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/compare/{base}...{head}",
57+
"merges_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/merges",
58+
"archive_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/{archive_format}{/ref}",
59+
"downloads_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/downloads",
60+
"issues_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/issues{/number}",
61+
"pulls_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/pulls{/number}",
62+
"milestones_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/milestones{/number}",
63+
"notifications_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/notifications{?since,all,participating}",
64+
"labels_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/labels{/name}",
65+
"releases_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/releases{/id}",
66+
"deployments_url": "https://api.github.com/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/deployments",
67+
"created_at": "2021-09-30T12:34:44Z",
68+
"updated_at": "2021-09-30T12:34:44Z",
69+
"pushed_at": "2021-09-30T12:34:45Z",
70+
"git_url": "git://github.com/hub4j-test-org/temp-createDispatchEventWithClientPayload.git",
71+
"ssh_url": "git@github.com:hub4j-test-org/temp-createDispatchEventWithClientPayload.git",
72+
"clone_url": "https://github.com/hub4j-test-org/temp-createDispatchEventWithClientPayload.git",
73+
"svn_url": "https://github.com/hub4j-test-org/temp-createDispatchEventWithClientPayload",
74+
"homepage": "http://github-api.kohsuke.org/",
75+
"size": 0,
76+
"stargazers_count": 0,
77+
"watchers_count": 0,
78+
"language": null,
79+
"has_issues": true,
80+
"has_projects": true,
81+
"has_downloads": true,
82+
"has_wiki": true,
83+
"has_pages": false,
84+
"forks_count": 0,
85+
"mirror_url": null,
86+
"archived": false,
87+
"disabled": false,
88+
"open_issues_count": 0,
89+
"license": null,
90+
"allow_forking": true,
91+
"forks": 0,
92+
"open_issues": 0,
93+
"watchers": 0,
94+
"default_branch": "main",
95+
"permissions": {
96+
"admin": true,
97+
"maintain": true,
98+
"push": true,
99+
"triage": true,
100+
"pull": true
101+
},
102+
"temp_clone_token": "",
103+
"allow_squash_merge": true,
104+
"allow_merge_commit": true,
105+
"allow_rebase_merge": true,
106+
"allow_auto_merge": false,
107+
"delete_branch_on_merge": false,
108+
"organization": {
109+
"login": "hub4j-test-org",
110+
"id": 7544739,
111+
"node_id": "MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=",
112+
"avatar_url": "https://avatars.githubusercontent.com/u/7544739?v=4",
113+
"gravatar_id": "",
114+
"url": "https://api.github.com/users/hub4j-test-org",
115+
"html_url": "https://github.com/hub4j-test-org",
116+
"followers_url": "https://api.github.com/users/hub4j-test-org/followers",
117+
"following_url": "https://api.github.com/users/hub4j-test-org/following{/other_user}",
118+
"gists_url": "https://api.github.com/users/hub4j-test-org/gists{/gist_id}",
119+
"starred_url": "https://api.github.com/users/hub4j-test-org/starred{/owner}{/repo}",
120+
"subscriptions_url": "https://api.github.com/users/hub4j-test-org/subscriptions",
121+
"organizations_url": "https://api.github.com/users/hub4j-test-org/orgs",
122+
"repos_url": "https://api.github.com/users/hub4j-test-org/repos",
123+
"events_url": "https://api.github.com/users/hub4j-test-org/events{/privacy}",
124+
"received_events_url": "https://api.github.com/users/hub4j-test-org/received_events",
125+
"type": "Organization",
126+
"site_admin": false
127+
},
128+
"network_count": 0,
129+
"subscribers_count": 15
130+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"login": "mxandeco",
3+
"id": 42962662,
4+
"node_id": "MDQ6VXNlcjQyOTYyNjYy",
5+
"avatar_url": "https://avatars.githubusercontent.com/u/42962662?v=4",
6+
"gravatar_id": "",
7+
"url": "https://api.github.com/users/mxandeco",
8+
"html_url": "https://github.com/mxandeco",
9+
"followers_url": "https://api.github.com/users/mxandeco/followers",
10+
"following_url": "https://api.github.com/users/mxandeco/following{/other_user}",
11+
"gists_url": "https://api.github.com/users/mxandeco/gists{/gist_id}",
12+
"starred_url": "https://api.github.com/users/mxandeco/starred{/owner}{/repo}",
13+
"subscriptions_url": "https://api.github.com/users/mxandeco/subscriptions",
14+
"organizations_url": "https://api.github.com/users/mxandeco/orgs",
15+
"repos_url": "https://api.github.com/users/mxandeco/repos",
16+
"events_url": "https://api.github.com/users/mxandeco/events{/privacy}",
17+
"received_events_url": "https://api.github.com/users/mxandeco/received_events",
18+
"type": "User",
19+
"site_admin": false,
20+
"name": "Max Xandeco",
21+
"company": null,
22+
"blog": "",
23+
"location": null,
24+
"email": null,
25+
"hireable": null,
26+
"bio": null,
27+
"twitter_username": null,
28+
"public_repos": 18,
29+
"public_gists": 1,
30+
"followers": 0,
31+
"following": 0,
32+
"created_at": "2018-09-04T09:13:09Z",
33+
"updated_at": "2021-09-06T20:36:46Z",
34+
"private_gists": 0,
35+
"total_private_repos": 8,
36+
"owned_private_repos": 4,
37+
"disk_usage": 90,
38+
"collaborators": 1,
39+
"two_factor_authentication": true,
40+
"plan": {
41+
"name": "free",
42+
"space": 976562499,
43+
"collaborators": 0,
44+
"private_repos": 10000
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"id": "2539d5de-f96e-431e-b31b-a537e52e0ef5",
3+
"name": "repos_hub4j-test-org_temp-createdispatcheventwithclientpayload",
4+
"request": {
5+
"url": "/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload",
6+
"method": "GET",
7+
"headers": {
8+
"Accept": {
9+
"equalTo": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"
10+
}
11+
}
12+
},
13+
"response": {
14+
"status": 200,
15+
"bodyFileName": "repos_hub4j-test-org_temp-createdispatcheventwithclientpayload-2.json",
16+
"headers": {
17+
"Server": "GitHub.com",
18+
"Date": "Thu, 30 Sep 2021 12:34:49 GMT",
19+
"Content-Type": "application/json; charset=utf-8",
20+
"Cache-Control": "private, max-age=60, s-maxage=60",
21+
"Vary": [
22+
"Accept, Authorization, Cookie, X-GitHub-OTP",
23+
"Accept-Encoding, Accept, X-Requested-With"
24+
],
25+
"ETag": "W/\"5ab7e1e833c6acac05ab9315823a4f6b6a662de9d5f2539189c24ce631404077\"",
26+
"Last-Modified": "Thu, 30 Sep 2021 12:34:44 GMT",
27+
"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",
28+
"X-Accepted-OAuth-Scopes": "repo",
29+
"X-GitHub-Media-Type": "unknown, github.v3",
30+
"X-RateLimit-Limit": "5000",
31+
"X-RateLimit-Remaining": "4940",
32+
"X-RateLimit-Reset": "1633006817",
33+
"X-RateLimit-Used": "60",
34+
"X-RateLimit-Resource": "core",
35+
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
36+
"X-Frame-Options": "deny",
37+
"X-Content-Type-Options": "nosniff",
38+
"X-XSS-Protection": "0",
39+
"Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
40+
"Content-Security-Policy": "default-src 'none'",
41+
"X-GitHub-Request-Id": "DEB6:4B95:4C9D23:4E7593:6155AEE8"
42+
}
43+
},
44+
"uuid": "2539d5de-f96e-431e-b31b-a537e52e0ef5",
45+
"persistent": true,
46+
"insertionIndex": 2
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"id": "bd959614-1104-415e-b9a1-8ac9533d82f7",
3+
"name": "repos_hub4j-test-org_temp-createdispatcheventwithclientpayload_dispatches",
4+
"request": {
5+
"url": "/repos/hub4j-test-org/temp-createDispatchEventWithClientPayload/dispatches",
6+
"method": "POST",
7+
"headers": {
8+
"Accept": {
9+
"equalTo": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"
10+
}
11+
},
12+
"bodyPatterns": [
13+
{
14+
"equalToJson": "{\"event_type\":\"test\",\"client_payload\":{\"name\":\"joe.doe\",\"list\":[]}}",
15+
"ignoreArrayOrder": true,
16+
"ignoreExtraElements": false
17+
}
18+
]
19+
},
20+
"response": {
21+
"status": 204,
22+
"headers": {
23+
"Server": "GitHub.com",
24+
"Date": "Thu, 30 Sep 2021 12:34:49 GMT",
25+
"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",
26+
"X-Accepted-OAuth-Scopes": "",
27+
"X-GitHub-Media-Type": "unknown, github.v3",
28+
"X-RateLimit-Limit": "5000",
29+
"X-RateLimit-Remaining": "4939",
30+
"X-RateLimit-Reset": "1633006817",
31+
"X-RateLimit-Used": "61",
32+
"X-RateLimit-Resource": "core",
33+
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
34+
"X-Frame-Options": "deny",
35+
"X-Content-Type-Options": "nosniff",
36+
"X-XSS-Protection": "0",
37+
"Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
38+
"Content-Security-Policy": "default-src 'none'",
39+
"Vary": "Accept-Encoding, Accept, X-Requested-With",
40+
"X-GitHub-Request-Id": "DEB7:0B4B:3073ED:320304:6155AEE9"
41+
}
42+
},
43+
"uuid": "bd959614-1104-415e-b9a1-8ac9533d82f7",
44+
"persistent": true,
45+
"insertionIndex": 3
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"id": "43c6892b-f0dc-45a7-97b9-f81a8e3f244e",
3+
"name": "user",
4+
"request": {
5+
"url": "/user",
6+
"method": "GET",
7+
"headers": {
8+
"Accept": {
9+
"equalTo": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"
10+
}
11+
}
12+
},
13+
"response": {
14+
"status": 200,
15+
"bodyFileName": "user-1.json",
16+
"headers": {
17+
"Server": "GitHub.com",
18+
"Date": "Thu, 30 Sep 2021 12:34:43 GMT",
19+
"Content-Type": "application/json; charset=utf-8",
20+
"Cache-Control": "private, max-age=60, s-maxage=60",
21+
"Vary": [
22+
"Accept, Authorization, Cookie, X-GitHub-OTP",
23+
"Accept-Encoding, Accept, X-Requested-With"
24+
],
25+
"ETag": "W/\"b8f235ad3cd7cc5e96a09241a22fa6048c7877f43ada37c29eb075107f7f3b1a\"",
26+
"Last-Modified": "Mon, 06 Sep 2021 20:36:46 GMT",
27+
"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",
28+
"X-Accepted-OAuth-Scopes": "",
29+
"X-GitHub-Media-Type": "unknown, github.v3",
30+
"X-RateLimit-Limit": "5000",
31+
"X-RateLimit-Remaining": "4945",
32+
"X-RateLimit-Reset": "1633006817",
33+
"X-RateLimit-Used": "55",
34+
"X-RateLimit-Resource": "core",
35+
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
36+
"X-Frame-Options": "deny",
37+
"X-Content-Type-Options": "nosniff",
38+
"X-XSS-Protection": "0",
39+
"Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
40+
"Content-Security-Policy": "default-src 'none'",
41+
"X-GitHub-Request-Id": "DEB3:FDF5:134720:14986B:6155AEE2"
42+
}
43+
},
44+
"uuid": "43c6892b-f0dc-45a7-97b9-f81a8e3f244e",
45+
"persistent": true,
46+
"insertionIndex": 1
47+
}

0 commit comments

Comments
 (0)