Skip to content

Commit 912367f

Browse files
committed
adding GithubCreateForkAction and GithubCreateForkActionTest
adding two new constants (ORGANIZATION and DEFAULT_BRANCH_ONLY) inside GithubConstants
1 parent 0108ea3 commit 912367f

File tree

5 files changed

+156
-16776
lines changed

5 files changed

+156
-16776
lines changed

server/libs/modules/components/github/src/main/java/com/bytechef/component/github/GithubComponentHandler.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.bytechef.component.github.action.GithubAddAssigneesToIssueAction;
2828
import com.bytechef.component.github.action.GithubAddLabelsToIssueAction;
2929
import com.bytechef.component.github.action.GithubCreateCommentOnIssueAction;
30+
import com.bytechef.component.github.action.GithubCreateForkAction;
3031
import com.bytechef.component.github.action.GithubCreateIssueAction;
3132
import com.bytechef.component.github.action.GithubGetIssueAction;
3233
import com.bytechef.component.github.action.GithubListIssuesAction;
@@ -53,6 +54,7 @@ public class GithubComponentHandler implements ComponentHandler {
5354
GithubAddAssigneesToIssueAction.ACTION_DEFINITION,
5455
GithubAddLabelsToIssueAction.ACTION_DEFINITION,
5556
GithubCreateCommentOnIssueAction.ACTION_DEFINITION,
57+
GithubCreateForkAction.ACTION_DEFINITION,
5658
GithubCreateIssueAction.ACTION_DEFINITION,
5759
GitHubCreatePullRequestAction.ACTION_DEFINITION,
5860
GithubGetIssueAction.ACTION_DEFINITION,
@@ -64,6 +66,7 @@ public class GithubComponentHandler implements ComponentHandler {
6466
tool(GithubAddAssigneesToIssueAction.ACTION_DEFINITION),
6567
tool(GithubAddLabelsToIssueAction.ACTION_DEFINITION),
6668
tool(GithubCreateCommentOnIssueAction.ACTION_DEFINITION),
69+
tool(GithubCreateForkAction.ACTION_DEFINITION),
6770
tool(GithubCreateIssueAction.ACTION_DEFINITION),
6871
tool(GitHubCreatePullRequestAction.ACTION_DEFINITION),
6972
tool(GithubGetIssueAction.ACTION_DEFINITION),
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright 2025 ByteChef
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.bytechef.component.github.action;
18+
19+
import static com.bytechef.component.definition.ComponentDsl.ModifiableActionDefinition;
20+
import static com.bytechef.component.definition.ComponentDsl.action;
21+
import static com.bytechef.component.definition.ComponentDsl.bool;
22+
import static com.bytechef.component.definition.ComponentDsl.object;
23+
import static com.bytechef.component.definition.ComponentDsl.outputSchema;
24+
import static com.bytechef.component.definition.ComponentDsl.string;
25+
import static com.bytechef.component.definition.Context.Http.responseType;
26+
import static com.bytechef.component.github.constant.GithubConstants.DEFAULT_BRANCH_ONLY;
27+
import static com.bytechef.component.github.constant.GithubConstants.NAME;
28+
import static com.bytechef.component.github.constant.GithubConstants.ORGANIZATION;
29+
import static com.bytechef.component.github.constant.GithubConstants.OWNER;
30+
import static com.bytechef.component.github.constant.GithubConstants.OWNER_PROPERTY;
31+
import static com.bytechef.component.github.constant.GithubConstants.REPOSITORY;
32+
33+
import com.bytechef.component.definition.Context;
34+
import com.bytechef.component.definition.Context.Http;
35+
import com.bytechef.component.definition.Parameters;
36+
import com.bytechef.component.definition.TypeReference;
37+
import java.util.Map;
38+
39+
/**
40+
* @author Ivona Pavela
41+
*/
42+
public class GithubCreateForkAction {
43+
44+
public static final ModifiableActionDefinition ACTION_DEFINITION = action("createFork")
45+
.title("Create Fork")
46+
.description("Create a fork of Github repository.")
47+
.properties(
48+
OWNER_PROPERTY,
49+
string(REPOSITORY)
50+
.label("Repository")
51+
.description("Repository that will be forked.")
52+
.required(true),
53+
string(NAME)
54+
.label("Name")
55+
.description("A new name for the fork.")
56+
.maxLength(100)
57+
.required(false),
58+
string(ORGANIZATION)
59+
.label("Organization")
60+
.description("The organization name if forking into an organization.")
61+
.required(false),
62+
bool(DEFAULT_BRANCH_ONLY)
63+
.label("Default branch only")
64+
.description("When forking from an existing repository, fork with only the default branch.")
65+
.defaultValue(true))
66+
.output(
67+
outputSchema(
68+
object()
69+
.properties(
70+
string("url")
71+
.description("URL of the created fork."))))
72+
.perform(GithubCreateForkAction::perform);
73+
74+
public static Map<String, Object> perform(
75+
Parameters inputParameters, Parameters connectionParameters, Context context) {
76+
77+
return context
78+
.http(http -> http.post(
79+
"/repos/" + inputParameters.getRequiredString(OWNER) + "/"
80+
+ inputParameters.getRequiredString(REPOSITORY) + "/forks"))
81+
.body(Http.Body.of(
82+
NAME, inputParameters.getString(NAME),
83+
ORGANIZATION, inputParameters.getString(ORGANIZATION),
84+
DEFAULT_BRANCH_ONLY, inputParameters.getBoolean(DEFAULT_BRANCH_ONLY)))
85+
.configuration(responseType(Http.ResponseType.JSON))
86+
.execute()
87+
.getBody(new TypeReference<>() {});
88+
}
89+
}

server/libs/modules/components/github/src/main/java/com/bytechef/component/github/constant/GithubConstants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ public class GithubConstants {
4747
public static final String HEAD_REPO = "head_repo";
4848
public static final String BASE = "base";
4949
public static final String DRAFT = "draft";
50+
public static final String ORGANIZATION = "organization";
51+
public static final String DEFAULT_BRANCH_ONLY = "defaultBranchOnly";
5052

5153
public static final ModifiableObjectProperty ISSUE_OUTPUT_PROPERTY = object()
5254
.properties(
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2025 ByteChef
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.bytechef.component.github.action;
18+
19+
import com.bytechef.component.definition.Context;
20+
import com.bytechef.component.definition.Parameters;
21+
import com.bytechef.component.definition.TypeReference;
22+
import com.bytechef.component.test.definition.MockParametersFactory;
23+
import org.junit.jupiter.api.Test;
24+
import java.util.Map;
25+
26+
import static com.bytechef.component.github.constant.GithubConstants.DEFAULT_BRANCH_ONLY;
27+
import static com.bytechef.component.github.constant.GithubConstants.NAME;
28+
import static com.bytechef.component.github.constant.GithubConstants.ORGANIZATION;
29+
import static com.bytechef.component.github.constant.GithubConstants.OWNER;
30+
import static com.bytechef.component.github.constant.GithubConstants.REPOSITORY;
31+
import static org.junit.jupiter.api.Assertions.assertEquals;
32+
import static org.mockito.ArgumentMatchers.any;
33+
import static org.mockito.Mockito.when;
34+
35+
/**
36+
* @author Ivona Pavela
37+
*/
38+
class GithubCreateForkActionTest extends AbstractGithubActionTest {
39+
40+
private final Parameters mockedParameters = MockParametersFactory.create(
41+
Map.of(OWNER, "testOwner", REPOSITORY, "testRepo", NAME, "name",
42+
ORGANIZATION, "testOrganization", DEFAULT_BRANCH_ONLY, true));
43+
44+
@Test
45+
void testPerform() throws Exception {
46+
when(mockedHttp.post(stringArgumentCaptor.capture()))
47+
.thenReturn(mockedExecutor);
48+
when(mockedResponse.getBody(any(TypeReference.class)))
49+
.thenReturn(Map.of());
50+
51+
Object result = executePerformFunction(GithubCreateForkAction.ACTION_DEFINITION, mockedParameters);
52+
53+
assertEquals(Map.of(), result);
54+
assertEquals("/repos/testOwner/testRepo/forks", stringArgumentCaptor.getValue());
55+
56+
Context.Http.Body body = bodyArgumentCaptor.getValue();
57+
58+
assertEquals(Map.of(NAME, "name", ORGANIZATION, "testOrganization",
59+
DEFAULT_BRANCH_ONLY, true), body.getContent());
60+
}
61+
62+
}

0 commit comments

Comments
 (0)