|
| 1 | +/* |
| 2 | +Copyright 2020 The Flux CD contributors. |
| 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 | + http://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 gitprovider |
| 18 | + |
| 19 | +import "context" |
| 20 | + |
| 21 | +// Client is an interface that allows talking to a Git provider |
| 22 | +type Client interface { |
| 23 | + // The Client allows accessing all known resources |
| 24 | + ResourceClient |
| 25 | + |
| 26 | + // SupportedDomain returns the domain endpoint for this client, e.g. "github.com", "gitlab.com" or |
| 27 | + // "my-custom-git-server.com:6443". This allows a higher-level user to know what Client to use for |
| 28 | + // what endpoints. |
| 29 | + // This field is set at client creation time, and can't be changed |
| 30 | + SupportedDomain() string |
| 31 | + |
| 32 | + // ProviderID returns the provider ID (e.g. "github", "gitlab") for this client |
| 33 | + // This field is set at client creation time, and can't be changed |
| 34 | + ProviderID() ProviderID |
| 35 | + |
| 36 | + // Raw returns the Go client used under the hood for accessing the Git provider |
| 37 | + Raw() interface{} |
| 38 | +} |
| 39 | + |
| 40 | +// ResourceClient allows access to resource-specific clients |
| 41 | +type ResourceClient interface { |
| 42 | + // Organization gets the OrganizationClient for the specific top-level organization, or user account. |
| 43 | + // ErrNotTopLevelOrganization will be returned if the organization is not top-level |
| 44 | + Organization(o OrganizationRef) OrganizationClient |
| 45 | + |
| 46 | + // Organizations returns the OrganizationsClient handling sets of organizations |
| 47 | + Organizations() OrganizationsClient |
| 48 | + |
| 49 | + // Repository gets the RepositoryClient for the specified RepositoryRef |
| 50 | + Repository(r RepositoryRef) RepositoryClient |
| 51 | + |
| 52 | + // Repositories returns the RepositoriesClient handling sets of organizations |
| 53 | + Repositories() RepositoriesClient |
| 54 | +} |
| 55 | + |
| 56 | +// OrganizationsClient operates on organizations the user has access to |
| 57 | +type OrganizationsClient interface { |
| 58 | + // Get a specific organization the user has access to |
| 59 | + // This might also refer to a sub-organization |
| 60 | + // ErrNotFound is returned if the resource does not exist |
| 61 | + Get(ctx context.Context, o OrganizationRef) (*Organization, error) |
| 62 | + |
| 63 | + // List all top-level organizations the specific user has access to |
| 64 | + // List should return all available organizations, using multiple paginated requests if needed |
| 65 | + List(ctx context.Context) ([]Organization, error) |
| 66 | + |
| 67 | + // Children returns the immediate child-organizations for the specific IdentityRef o. |
| 68 | + // The IdentityRef may point to any sub-organization that exists |
| 69 | + // This is not supported in GitHub |
| 70 | + // Children should return all available organizations, using multiple paginated requests if needed |
| 71 | + Children(ctx context.Context, o OrganizationRef) ([]Organization, error) |
| 72 | + |
| 73 | + // Possibly add Create/Update/Delete methods later |
| 74 | +} |
| 75 | + |
| 76 | +// OrganizationClient operates on a given/specific organization |
| 77 | +type OrganizationClient interface { |
| 78 | + // Teams gives access to the TeamsClient for this specific organization |
| 79 | + Teams() OrganizationTeamsClient |
| 80 | +} |
| 81 | + |
| 82 | +// OrganizationTeamsClient handles teams organization-wide |
| 83 | +type OrganizationTeamsClient interface { |
| 84 | + // Get a team within the specific organization |
| 85 | + // teamName may include slashes, to point to e.g. "sub-teams" i.e. subgroups in Gitlab |
| 86 | + // teamName must not be an empty string |
| 87 | + // ErrNotFound is returned if the resource does not exist |
| 88 | + Get(ctx context.Context, teamName string) (*Team, error) |
| 89 | + |
| 90 | + // List all teams (recursively, in terms of subgroups) within the specific organization |
| 91 | + // List should return all available organizations, using multiple paginated requests if needed |
| 92 | + List(ctx context.Context) ([]Team, error) |
| 93 | + |
| 94 | + // Possibly add Create/Update/Delete methods later |
| 95 | +} |
| 96 | + |
| 97 | +// RepositoriesClient operates on repositories the user has access to |
| 98 | +type RepositoriesClient interface { |
| 99 | + // Get returns the repository at the given path |
| 100 | + // ErrNotFound is returned if the resource does not exist |
| 101 | + Get(ctx context.Context, r RepositoryRef) (*Repository, error) |
| 102 | + |
| 103 | + // List all repositories in the given organization or user account |
| 104 | + // List should return all available repositories, using multiple paginated requests if needed |
| 105 | + List(ctx context.Context, o IdentityRef) ([]Repository, error) |
| 106 | + |
| 107 | + // Create creates a repository at the given organization path, with the given URL-encoded name and options |
| 108 | + // ErrAlreadyExists will be returned if the resource already exists |
| 109 | + Create(ctx context.Context, r *Repository, opts ...RepositoryCreateOption) (*Repository, error) |
| 110 | + |
| 111 | + // Update will update the desired state of the repository. Only set fields will be respected. |
| 112 | + // ErrNotFound is returned if the resource does not exist |
| 113 | + Update(ctx context.Context, r *Repository) (*Repository, error) |
| 114 | + |
| 115 | + // Reconcile makes sure r is the actual state in the backing Git provider. If r doesn't exist |
| 116 | + // under the hood, it is created. If r is already the actual state, this is a no-op. If r isn't |
| 117 | + // the actual state, the resource will be updated. |
| 118 | + Reconcile(ctx context.Context, r *Repository, opts ...RepositoryReconcileOption) (*Repository, error) |
| 119 | +} |
| 120 | + |
| 121 | +// RepositoryClient operates on a given/specific repository |
| 122 | +type RepositoryClient interface { |
| 123 | + // TeamAccess returns a client for operating on the teams that have access to this specific repository |
| 124 | + TeamAccess() RepositoryTeamAccessClient |
| 125 | + |
| 126 | + // Credentials gives access to manipulating credentials for accessing this specific repository |
| 127 | + Credentials() RepositoryCredentialsClient |
| 128 | +} |
| 129 | + |
| 130 | +// RepositoryTeamAccessClient operates on the teams list for a specific repository |
| 131 | +type RepositoryTeamAccessClient interface { |
| 132 | + // Create adds a given team to the repo's team access control list |
| 133 | + // ErrAlreadyExists will be returned if the resource already exists |
| 134 | + // The embedded RepositoryInfo of ta does not need to be populated, but if it is, |
| 135 | + // it must equal to the RepositoryRef given to the RepositoryClient. |
| 136 | + Create(ctx context.Context, ta *TeamAccess) error |
| 137 | + |
| 138 | + // Lists the team access control list for this repo |
| 139 | + List(ctx context.Context) ([]TeamAccess, error) |
| 140 | + |
| 141 | + // Reconcile makes sure ta is the actual state in the backing Git provider. If ta doesn't exist |
| 142 | + // under the hood, it is created. If ta is already the actual state, this is a no-op. If ta isn't |
| 143 | + // the actual state, the resource will be deleted and recreated. |
| 144 | + // The embedded RepositoryInfo of ta does not need to be populated, but if it is, |
| 145 | + // it must equal to the RepositoryRef given to the RepositoryClient. |
| 146 | + Reconcile(ctx context.Context, ta *TeamAccess) (*TeamAccess, error) |
| 147 | + |
| 148 | + // Delete removes the given team from the repo's team access control list |
| 149 | + // ErrNotFound is returned if the resource does not exist |
| 150 | + Delete(ctx context.Context, ta *TeamAccess) error |
| 151 | +} |
| 152 | + |
| 153 | +// RepositoryCredentialsClient operates on the access credential list for a specific repository |
| 154 | +type RepositoryCredentialsClient interface { |
| 155 | + // Create a credential with the given human-readable name, the given bytes and optional options |
| 156 | + // ErrAlreadyExists will be returned if the resource already exists |
| 157 | + Create(ctx context.Context, c RepositoryCredential) (RepositoryCredential, error) |
| 158 | + |
| 159 | + // Lists all credentials for the given credential type |
| 160 | + List(ctx context.Context, t RepositoryCredentialType) ([]RepositoryCredential, error) |
| 161 | + |
| 162 | + // Reconcile makes sure c is the actual state in the backing Git provider. If c doesn't exist |
| 163 | + // under the hood, it is created. If c is already the actual state, this is a no-op. If c isn't |
| 164 | + // the actual state, the resource will deleted and recreated. |
| 165 | + Reconcile(ctx context.Context, c RepositoryCredential) (RepositoryCredential, error) |
| 166 | + |
| 167 | + // Deletes a credential from the repo. name corresponds to GetName() of the credential |
| 168 | + // ErrNotFound is returned if the resource does not exist |
| 169 | + Delete(ctx context.Context, c RepositoryCredential) error |
| 170 | +} |
0 commit comments