github.com/fluxcd/go-git-providers@v0.19.3/gitprovider/client.go (about) 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 // HasTokenPermission returns a boolean indicating whether the supplied token has the requested 37 // permission. Permissions should be coarse-grained and applicable to *all* providers. 38 HasTokenPermission(ctx context.Context, permission TokenPermission) (bool, error) 39 40 // Raw returns the Go client used under the hood to access the Git provider. 41 Raw() interface{} 42 } 43 44 // ResourceClient allows access to resource-specific sub-clients. 45 type ResourceClient interface { 46 // Organizations returns the OrganizationsClient handling sets of organizations. 47 Organizations() OrganizationsClient 48 49 // OrgRepositories returns the OrgRepositoriesClient handling sets of repositories in an organization. 50 OrgRepositories() OrgRepositoriesClient 51 52 // UserRepositories returns the UserRepositoriesClient handling sets of repositories for a user. 53 UserRepositories() UserRepositoriesClient 54 } 55 56 // 57 // Clients accessed through the top-level client, returning resource objects. 58 // 59 60 // OrganizationsClient operates on organizations the user has access to. 61 type OrganizationsClient interface { 62 // Get a specific organization the user has access to. 63 // This might also refer to a sub-organization. 64 // 65 // ErrNotFound is returned if the resource does not exist. 66 Get(ctx context.Context, o OrganizationRef) (Organization, error) 67 68 // List all top-level organizations the specific user has access to. 69 // 70 // List returns all available organizations, using multiple paginated requests if needed. 71 List(ctx context.Context) ([]Organization, error) 72 73 // Children returns the immediate child-organizations for the specific OrganizationRef o. 74 // The OrganizationRef may point to any existing sub-organization. 75 // 76 // This is not supported in GitHub. 77 // 78 // Children returns all available organizations, using multiple paginated requests if needed. 79 Children(ctx context.Context, o OrganizationRef) ([]Organization, error) 80 81 // Possibly add Create/Update/Delete methods later 82 } 83 84 // OrgRepositoriesClient operates on repositories for organizations. 85 type OrgRepositoriesClient interface { 86 // Get returns the repository for the given reference. 87 // 88 // ErrNotFound is returned if the resource does not exist. 89 Get(ctx context.Context, r OrgRepositoryRef) (OrgRepository, error) 90 91 // List all repositories in the given organization. 92 // 93 // List returns all available repositories, using multiple paginated requests if needed. 94 List(ctx context.Context, o OrganizationRef) ([]OrgRepository, error) 95 96 // Create creates a repository for the given organization, with the data and options. 97 // 98 // ErrAlreadyExists will be returned if the resource already exists. 99 Create(ctx context.Context, r OrgRepositoryRef, req RepositoryInfo, opts ...RepositoryCreateOption) (OrgRepository, error) 100 101 // Reconcile makes sure the given desired state (req) becomes the actual state in the backing Git provider. 102 // 103 // If req doesn't exist under the hood, it is created (actionTaken == true). 104 // If req doesn't equal the actual state, the resource will be updated (actionTaken == true). 105 // If req is already the actual state, this is a no-op (actionTaken == false). 106 Reconcile(ctx context.Context, r OrgRepositoryRef, req RepositoryInfo, opts ...RepositoryReconcileOption) (resp OrgRepository, actionTaken bool, err error) 107 } 108 109 // UserRepositoriesClient operates on repositories for users. 110 type UserRepositoriesClient interface { 111 // Get returns the repository at the given path. 112 // 113 // ErrNotFound is returned if the resource does not exist. 114 Get(ctx context.Context, r UserRepositoryRef) (UserRepository, error) 115 116 // List all repositories for the given user. 117 // 118 // List returns all available repositories, using multiple paginated requests if needed. 119 List(ctx context.Context, o UserRef) ([]UserRepository, error) 120 121 // Create creates a repository for the given user, with the data and options 122 // 123 // ErrAlreadyExists will be returned if the resource already exists. 124 Create(ctx context.Context, r UserRepositoryRef, req RepositoryInfo, opts ...RepositoryCreateOption) (UserRepository, error) 125 126 // GetUserLogin returns the current authenticated user. 127 GetUserLogin(ctx context.Context) (IdentityRef, error) 128 129 // Reconcile makes sure the given desired state (req) becomes the actual state in the backing Git provider. 130 // 131 // If req doesn't exist under the hood, it is created (actionTaken == true). 132 // If req doesn't equal the actual state, the resource will be updated (actionTaken == true). 133 // If req is already the actual state, this is a no-op (actionTaken == false). 134 Reconcile(ctx context.Context, r UserRepositoryRef, req RepositoryInfo, opts ...RepositoryReconcileOption) (resp UserRepository, actionTaken bool, err error) 135 } 136 137 // 138 // Clients accessed through resource objects. 139 // 140 141 // TeamsClient allows reading teams for a specific organization. 142 // This client can be accessed through Organization.Teams(). 143 type TeamsClient interface { 144 // Get a team within the specific organization. 145 // 146 // name may include slashes, but must not be an empty string. 147 // Teams are sub-groups in GitLab. 148 // 149 // ErrNotFound is returned if the resource does not exist. 150 Get(ctx context.Context, name string) (Team, error) 151 152 // List all teams (recursively, in terms of subgroups) within the specific organization. 153 // 154 // List returns all available organizations, using multiple paginated requests if needed. 155 List(ctx context.Context) ([]Team, error) 156 157 // Possibly add Create/Update/Delete methods later 158 } 159 160 // TeamAccessClient operates on the teams list for a specific repository. 161 // This client can be accessed through Repository.TeamAccess(). 162 type TeamAccessClient interface { 163 // Get a team's permission level of this given repository. 164 // 165 // ErrNotFound is returned if the resource does not exist. 166 Get(ctx context.Context, name string) (TeamAccess, error) 167 168 // List the team access control list for this repository. 169 // 170 // List returns all available team access lists, using multiple paginated requests if needed. 171 List(ctx context.Context) ([]TeamAccess, error) 172 173 // Create adds a given team to the repository's team access control list. 174 // 175 // ErrAlreadyExists will be returned if the resource already exists. 176 Create(ctx context.Context, req TeamAccessInfo) (TeamAccess, error) 177 178 // Reconcile makes sure the given desired state (req) becomes the actual state in the backing Git provider. 179 // 180 // If req doesn't exist under the hood, it is created (actionTaken == true). 181 // If req doesn't equal the actual state, the resource will be updated (actionTaken == true). 182 // If req is already the actual state, this is a no-op (actionTaken == false). 183 Reconcile(ctx context.Context, req TeamAccessInfo) (resp TeamAccess, actionTaken bool, err error) 184 } 185 186 // DeployKeyClient operates on the access credential list for a specific repository. 187 // This client can be accessed through Repository.DeployKeys(). 188 type DeployKeyClient interface { 189 // Get a DeployKey by its name. 190 // 191 // ErrNotFound is returned if the resource does not exist. 192 Get(ctx context.Context, name string) (DeployKey, error) 193 194 // List all deploy keys for the given repository. 195 // 196 // List returns all available deploy keys for the given type, 197 // using multiple paginated requests if needed. 198 List(ctx context.Context) ([]DeployKey, error) 199 200 // Create a deploy key with the given specifications. 201 // 202 // ErrAlreadyExists will be returned if the resource already exists. 203 Create(ctx context.Context, req DeployKeyInfo) (DeployKey, error) 204 205 // Reconcile makes sure the given desired state (req) becomes the actual state in the backing Git provider. 206 // 207 // If req doesn't exist under the hood, it is created (actionTaken == true). 208 // If req doesn't equal the actual state, the resource will be updated (actionTaken == true). 209 // If req is already the actual state, this is a no-op (actionTaken == false). 210 Reconcile(ctx context.Context, req DeployKeyInfo) (resp DeployKey, actionTaken bool, err error) 211 } 212 213 // DeployTokenClient operates on the deploy token list of a specific repository. 214 // This client can be accessed through Repository.DeployTokens(). 215 type DeployTokenClient interface { 216 // Get a DeployToken by its name. 217 // 218 // ErrNotFound is returned if the resource does not exist. 219 Get(ctx context.Context, name string) (DeployToken, error) 220 221 // List all deploy tokens for the given repository. 222 // 223 // List returns all available deploy tokens for the given type, 224 // using multiple paginated requests if needed. 225 List(ctx context.Context) ([]DeployToken, error) 226 227 // Create a deploy token with the given specifications. 228 // 229 // ErrAlreadyExists will be returned if the resource already exists. 230 Create(ctx context.Context, req DeployTokenInfo) (DeployToken, error) 231 232 // Reconcile makes sure the given desired state (req) becomes the actual state in the backing Git provider. 233 // 234 // If req doesn't exist under the hood, it is created (actionTaken == true). 235 // If req doesn't equal the actual state, the resource will be updated (actionTaken == true). 236 // If req is already the actual state, this is a no-op (actionTaken == false). 237 Reconcile(ctx context.Context, req DeployTokenInfo) (resp DeployToken, actionTaken bool, err error) 238 } 239 240 // CommitClient operates on the commits list for a specific repository. 241 // This client can be accessed through Repository.Commits(). 242 type CommitClient interface { 243 244 // ListPage lists repository commits of the given page and page size. 245 ListPage(ctx context.Context, branch string, perPage int, page int) ([]Commit, error) 246 // Create creates a commit with the given specifications. 247 Create(ctx context.Context, branch string, message string, files []CommitFile) (Commit, error) 248 } 249 250 // BranchClient operates on the branches for a specific repository. 251 // This client can be accessed through Repository.Branches(). 252 type BranchClient interface { 253 // Create creates a branch with the given specifications. 254 Create(ctx context.Context, branch, sha string) error 255 } 256 257 // PullRequestClient operates on the pull requests for a specific repository. 258 // This client can be accessed through Repository.PullRequests(). 259 type PullRequestClient interface { 260 // List lists all pull requests in the repository 261 List(ctx context.Context) ([]PullRequest, error) 262 // Create creates a pull request with the given specifications. 263 Create(ctx context.Context, title, branch, baseBranch, description string) (PullRequest, error) 264 // Edit allows for changing an existing pull request using the given options. Please refer to "EditOptions" for details on which data can be 265 // edited. 266 Edit(ctx context.Context, number int, opts EditOptions) (PullRequest, error) 267 // Get retrieves an existing pull request by number 268 Get(ctx context.Context, number int) (PullRequest, error) 269 // Merge merges a pull request with via either the "Squash" or "Merge" method 270 Merge(ctx context.Context, number int, mergeMethod MergeMethod, message string) error 271 } 272 273 // EditOptions is provided to a PullRequestClient's "Edit" method for updating an existing pull request. 274 type EditOptions struct { 275 // Title is set to a non-nil value to request a pull request's title to be changed. 276 Title *string 277 } 278 279 // FileClient operates on the branches for a specific repository. 280 // This client can be accessed through Repository.Branches(). 281 type FileClient interface { 282 // GetFiles fetch files content from specific path and branch 283 Get(ctx context.Context, path, branch string, optFns ...FilesGetOption) ([]*CommitFile, error) 284 } 285 286 // TreeClient operates on the trees for a Git repository which describe the hierarchy between files in the repository 287 // This client can be accessed through Repository.Trees() 288 type TreeClient interface { 289 // Get retrieves tree information and items 290 Get(ctx context.Context, sha string, recursive bool) (*TreeInfo, error) 291 // List retrieves list of tree files (files/blob) from given tree sha/id or path+branch 292 List(ctx context.Context, sha string, path string, recursive bool) ([]*TreeEntry, error) 293 }