|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * Copyright 2023 Google LLC |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +/* |
| 19 | + * GENERATED CODE WARNING |
| 20 | + * This file was automatically generated - do not edit! |
| 21 | + */ |
| 22 | + |
| 23 | +require_once __DIR__ . '/../../../vendor/autoload.php'; |
| 24 | + |
| 25 | +// [START cloudbuild_v2_generated_RepositoryManager_BatchCreateRepositories_sync] |
| 26 | +use Google\ApiCore\ApiException; |
| 27 | +use Google\ApiCore\OperationResponse; |
| 28 | +use Google\Cloud\Build\V2\BatchCreateRepositoriesResponse; |
| 29 | +use Google\Cloud\Build\V2\CreateRepositoryRequest; |
| 30 | +use Google\Cloud\Build\V2\Repository; |
| 31 | +use Google\Cloud\Build\V2\RepositoryManagerClient; |
| 32 | +use Google\Rpc\Status; |
| 33 | + |
| 34 | +/** |
| 35 | + * Creates multiple repositories inside a connection. |
| 36 | + * |
| 37 | + * @param string $formattedParent The connection to contain all the repositories being created. |
| 38 | + * Format: projects/*/locations/*/connections/* |
| 39 | + * The parent field in the CreateRepositoryRequest messages |
| 40 | + * must either be empty or match this field. Please see |
| 41 | + * {@see RepositoryManagerClient::connectionName()} for help formatting this field. |
| 42 | + * @param string $formattedRequestsParent The connection to contain the repository. If the request is part |
| 43 | + * of a BatchCreateRepositoriesRequest, this field should be empty or match |
| 44 | + * the parent specified there. Please see |
| 45 | + * {@see RepositoryManagerClient::connectionName()} for help formatting this field. |
| 46 | + * @param string $requestsRepositoryRemoteUri Git Clone HTTPS URI. |
| 47 | + * @param string $requestsRepositoryId The ID to use for the repository, which will become the final |
| 48 | + * component of the repository's resource name. This ID should be unique in |
| 49 | + * the connection. Allows alphanumeric characters and any of |
| 50 | + * -._~%!$&'()*+,;=@. |
| 51 | + */ |
| 52 | +function batch_create_repositories_sample( |
| 53 | + string $formattedParent, |
| 54 | + string $formattedRequestsParent, |
| 55 | + string $requestsRepositoryRemoteUri, |
| 56 | + string $requestsRepositoryId |
| 57 | +): void { |
| 58 | + // Create a client. |
| 59 | + $repositoryManagerClient = new RepositoryManagerClient(); |
| 60 | + |
| 61 | + // Prepare any non-scalar elements to be passed along with the request. |
| 62 | + $requestsRepository = (new Repository()) |
| 63 | + ->setRemoteUri($requestsRepositoryRemoteUri); |
| 64 | + $createRepositoryRequest = (new CreateRepositoryRequest()) |
| 65 | + ->setParent($formattedRequestsParent) |
| 66 | + ->setRepository($requestsRepository) |
| 67 | + ->setRepositoryId($requestsRepositoryId); |
| 68 | + $requests = [$createRepositoryRequest,]; |
| 69 | + |
| 70 | + // Call the API and handle any network failures. |
| 71 | + try { |
| 72 | + /** @var OperationResponse $response */ |
| 73 | + $response = $repositoryManagerClient->batchCreateRepositories($formattedParent, $requests); |
| 74 | + $response->pollUntilComplete(); |
| 75 | + |
| 76 | + if ($response->operationSucceeded()) { |
| 77 | + /** @var BatchCreateRepositoriesResponse $result */ |
| 78 | + $result = $response->getResult(); |
| 79 | + printf('Operation successful with response data: %s' . PHP_EOL, $result->serializeToJsonString()); |
| 80 | + } else { |
| 81 | + /** @var Status $error */ |
| 82 | + $error = $response->getError(); |
| 83 | + printf('Operation failed with error data: %s' . PHP_EOL, $error->serializeToJsonString()); |
| 84 | + } |
| 85 | + } catch (ApiException $ex) { |
| 86 | + printf('Call failed with message: %s' . PHP_EOL, $ex->getMessage()); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +/** |
| 91 | + * Helper to execute the sample. |
| 92 | + * |
| 93 | + * This sample has been automatically generated and should be regarded as a code |
| 94 | + * template only. It will require modifications to work: |
| 95 | + * - It may require correct/in-range values for request initialization. |
| 96 | + * - It may require specifying regional endpoints when creating the service client, |
| 97 | + * please see the apiEndpoint client configuration option for more details. |
| 98 | + */ |
| 99 | +function callSample(): void |
| 100 | +{ |
| 101 | + $formattedParent = RepositoryManagerClient::connectionName( |
| 102 | + '[PROJECT]', |
| 103 | + '[LOCATION]', |
| 104 | + '[CONNECTION]' |
| 105 | + ); |
| 106 | + $formattedRequestsParent = RepositoryManagerClient::connectionName( |
| 107 | + '[PROJECT]', |
| 108 | + '[LOCATION]', |
| 109 | + '[CONNECTION]' |
| 110 | + ); |
| 111 | + $requestsRepositoryRemoteUri = '[REMOTE_URI]'; |
| 112 | + $requestsRepositoryId = '[REPOSITORY_ID]'; |
| 113 | + |
| 114 | + batch_create_repositories_sample( |
| 115 | + $formattedParent, |
| 116 | + $formattedRequestsParent, |
| 117 | + $requestsRepositoryRemoteUri, |
| 118 | + $requestsRepositoryId |
| 119 | + ); |
| 120 | +} |
| 121 | +// [END cloudbuild_v2_generated_RepositoryManager_BatchCreateRepositories_sync] |
0 commit comments