Skip to content

Commit 864c981

Browse files
authored
github: add 'create repository dispatch' action (#228)
1 parent d82c2b2 commit 864c981

6 files changed

Lines changed: 157 additions & 1 deletion

File tree

tasks/git/src/main/java/com/walmartlabs/concord/plugins/git/GitHubTask.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.fasterxml.jackson.databind.ObjectMapper;
2525
import com.walmartlabs.concord.plugins.git.actions.CreateBranchAction;
2626
import com.walmartlabs.concord.plugins.git.actions.CreatePrAction;
27+
import com.walmartlabs.concord.plugins.git.actions.CreateRepositoryDispatchAction;
2728
import com.walmartlabs.concord.plugins.git.actions.GetRefAction;
2829
import com.walmartlabs.concord.plugins.git.actions.GetTagAction;
2930
import com.walmartlabs.concord.plugins.git.actions.ListCommitsAction;
@@ -178,6 +179,7 @@ public Map<String, Object> execute(Map<String, Object> in, Map<String, Object> d
178179
case LISTCOMMITS -> new ListCommitsAction().execute(txId, apiInfo, dryRunMode, VariablesGithubTaskParams.listCommits(input));
179180
case GETTAG -> new GetTagAction().execute(txId, apiInfo, dryRunMode, VariablesGithubTaskParams.getTag(input));
180181
case GETREF -> new GetRefAction().execute(txId, apiInfo, dryRunMode, VariablesGithubTaskParams.getRef(input));
182+
case CREATEREPOSITORYDISPATCH -> new CreateRepositoryDispatchAction().execute(txId, apiInfo, dryRunMode, VariablesGithubTaskParams.createRepositoryDispatch(input));
181183
};
182184
}
183185

tasks/git/src/main/java/com/walmartlabs/concord/plugins/git/GitHubTaskParams.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,12 @@ record GetTag(
8181
boolean failIfNotFound
8282
) implements GitHubTaskParams {
8383
}
84+
85+
record CreateRepositoryDispatch(
86+
String org,
87+
String repo,
88+
String eventType,
89+
java.util.Map<String, Object> clientPayload
90+
) implements GitHubTaskParams {
91+
}
8492
}

tasks/git/src/main/java/com/walmartlabs/concord/plugins/git/VariablesGithubTaskParams.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ public enum Action {
6262
GETSHORTSHA,
6363
LISTCOMMITS,
6464
GETTAG,
65-
GETREF
65+
GETREF,
66+
CREATEREPOSITORYDISPATCH
6667
}
6768

6869
public static Variables merge(Map<String, Object> taskDefaults, Map<String, Object> input) {
@@ -136,6 +137,15 @@ public static GetRef getRef(Variables variables) {
136137
);
137138
}
138139

140+
public static CreateRepositoryDispatch createRepositoryDispatch(Variables variables) {
141+
return new CreateRepositoryDispatch(
142+
assertOrg(variables),
143+
assertRepo(variables),
144+
variables.assertString("eventType"),
145+
variables.getMap("clientPayload", Map.of())
146+
);
147+
}
148+
139149
private static String assertOrg(Variables variables) {
140150
return variables.assertString("org");
141151
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.walmartlabs.concord.plugins.git.actions;
2+
3+
/*-
4+
* *****
5+
* Concord
6+
* -----
7+
* Copyright (C) 2017 - 2025 Walmart Inc., Concord Authors
8+
* -----
9+
* Licensed under the Apache License, Version 2.0 (the "License");
10+
* you may not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS,
17+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* See the License for the specific language governing permissions and
19+
* limitations under the License.
20+
* =====
21+
*/
22+
23+
import com.walmartlabs.concord.plugins.git.GitHubTaskAction;
24+
import com.walmartlabs.concord.plugins.git.GitHubTaskParams;
25+
import com.walmartlabs.concord.plugins.git.client.GitHubClient;
26+
import com.walmartlabs.concord.plugins.git.model.GitHubApiInfo;
27+
import org.slf4j.Logger;
28+
import org.slf4j.LoggerFactory;
29+
30+
import java.util.Collections;
31+
import java.util.Map;
32+
import java.util.UUID;
33+
34+
// https://docs.github.com/en/rest/repos/repos?apiVersion=2022-11-28#create-a-repository-dispatch-event
35+
public class CreateRepositoryDispatchAction extends GitHubTaskAction<GitHubTaskParams.CreateRepositoryDispatch> {
36+
37+
private final static Logger log = LoggerFactory.getLogger(CreateRepositoryDispatchAction.class);
38+
39+
@Override
40+
public Map<String, Object> execute(UUID txId, GitHubApiInfo apiInfo, boolean dryRunMode, GitHubTaskParams.CreateRepositoryDispatch input) {
41+
if (dryRunMode) {
42+
log.info("Dry-run mode enabled: Skipping repository dispatch event creation");
43+
return Collections.emptyMap();
44+
}
45+
46+
var client = new GitHubClient(txId, apiInfo);
47+
try {
48+
var body = Map.of(
49+
"event_type", input.eventType(),
50+
"client_payload", input.clientPayload()
51+
);
52+
53+
client.voidResult("POST", "/repos/" + input.org() + "/" + input.repo() + "/dispatches", body);
54+
55+
log.info("Repository dispatch event created in {}/{} with event type '{}'",
56+
input.org(), input.repo(), input.eventType());
57+
58+
return Map.of();
59+
} catch (Exception e) {
60+
throw new RuntimeException("Failed to create repository dispatch event: " + e.getMessage());
61+
}
62+
}
63+
}

tasks/git/src/main/java/com/walmartlabs/concord/plugins/git/client/GitHubClient.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ public List<Map<String, Object>> singleArrayResult(String method, String path, O
8080
return parseResponseAs(response, LIST_OF_OBJECTS_TYPE);
8181
}
8282

83+
public void voidResult(String method, String path, Object body) throws IOException, InterruptedException, URISyntaxException {
84+
sendRequest(method, path, Map.of(), body);
85+
}
86+
8387
public void forEachPage(String path, Map<String, String> params, int pageSize, PageHandler handler)
8488
throws IOException, InterruptedException, URISyntaxException {
8589

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.walmartlabs.concord.plugins.git.actions;
2+
3+
/*-
4+
* *****
5+
* Concord
6+
* -----
7+
* Copyright (C) 2017 - 2025 Walmart Inc., Concord Authors
8+
* -----
9+
* Licensed under the Apache License, Version 2.0 (the "License");
10+
* you may not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS,
17+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* See the License for the specific language governing permissions and
19+
* limitations under the License.
20+
* =====
21+
*/
22+
23+
import com.walmartlabs.concord.plugins.git.GitHubTaskParams;
24+
import com.walmartlabs.concord.plugins.git.model.GitHubApiInfo;
25+
import org.junit.jupiter.api.Test;
26+
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
27+
28+
import java.util.Map;
29+
import java.util.Objects;
30+
import java.util.UUID;
31+
32+
import static org.junit.jupiter.api.Assertions.assertNotNull;
33+
import static org.junit.jupiter.api.Assertions.assertTrue;
34+
35+
@EnabledIfEnvironmentVariable(named = "GH_TEST_TOKEN", matches = ".+")
36+
public class CreateRepositoryDispatchActionTest {
37+
38+
@Test
39+
public void testOkAction() {
40+
var apiInfo = GitHubApiInfo.builder()
41+
.baseUrl("https://github.com")
42+
.accessTokenProvider(() -> Objects.requireNonNull(System.getenv("GH_TEST_TOKEN")))
43+
.build();
44+
45+
var input = new GitHubTaskParams.CreateRepositoryDispatch("walmartlabs", "concord-plugins",
46+
"test-event", Map.of("key", "value"));
47+
48+
var action = new CreateRepositoryDispatchAction();
49+
var result = action.execute(UUID.randomUUID(), apiInfo, false, input);
50+
assertNotNull(result);
51+
assertTrue(result.isEmpty());
52+
}
53+
54+
@Test
55+
public void testDryRun() {
56+
var apiInfo = GitHubApiInfo.builder()
57+
.baseUrl("https://github.com")
58+
.accessTokenProvider(() -> "fake-token")
59+
.build();
60+
61+
var input = new GitHubTaskParams.CreateRepositoryDispatch("walmartlabs", "concord-plugins",
62+
"test-event", Map.of("key", "value"));
63+
64+
var action = new CreateRepositoryDispatchAction();
65+
var result = action.execute(UUID.randomUUID(), apiInfo, true, input);
66+
assertNotNull(result);
67+
assertTrue(result.isEmpty());
68+
}
69+
}

0 commit comments

Comments
 (0)