|
| 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 | +} |
0 commit comments