github.com/google/go-github/v60@v60.0.0/github/repos.go (about) 1 // Copyright 2013 The go-github AUTHORS. All rights reserved. 2 // 3 // Use of this source code is governed by a BSD-style 4 // license that can be found in the LICENSE file. 5 6 package github 7 8 import ( 9 "context" 10 "encoding/json" 11 "errors" 12 "fmt" 13 "net/http" 14 "net/url" 15 "strings" 16 ) 17 18 const githubBranchNotProtected string = "Branch not protected" 19 20 var ErrBranchNotProtected = errors.New("branch is not protected") 21 22 // RepositoriesService handles communication with the repository related 23 // methods of the GitHub API. 24 // 25 // GitHub API docs: https://docs.github.com/rest/repos/ 26 type RepositoriesService service 27 28 // Repository represents a GitHub repository. 29 type Repository struct { 30 ID *int64 `json:"id,omitempty"` 31 NodeID *string `json:"node_id,omitempty"` 32 Owner *User `json:"owner,omitempty"` 33 Name *string `json:"name,omitempty"` 34 FullName *string `json:"full_name,omitempty"` 35 Description *string `json:"description,omitempty"` 36 Homepage *string `json:"homepage,omitempty"` 37 CodeOfConduct *CodeOfConduct `json:"code_of_conduct,omitempty"` 38 DefaultBranch *string `json:"default_branch,omitempty"` 39 MasterBranch *string `json:"master_branch,omitempty"` 40 CreatedAt *Timestamp `json:"created_at,omitempty"` 41 PushedAt *Timestamp `json:"pushed_at,omitempty"` 42 UpdatedAt *Timestamp `json:"updated_at,omitempty"` 43 HTMLURL *string `json:"html_url,omitempty"` 44 CloneURL *string `json:"clone_url,omitempty"` 45 GitURL *string `json:"git_url,omitempty"` 46 MirrorURL *string `json:"mirror_url,omitempty"` 47 SSHURL *string `json:"ssh_url,omitempty"` 48 SVNURL *string `json:"svn_url,omitempty"` 49 Language *string `json:"language,omitempty"` 50 Fork *bool `json:"fork,omitempty"` 51 ForksCount *int `json:"forks_count,omitempty"` 52 NetworkCount *int `json:"network_count,omitempty"` 53 OpenIssuesCount *int `json:"open_issues_count,omitempty"` 54 OpenIssues *int `json:"open_issues,omitempty"` // Deprecated: Replaced by OpenIssuesCount. For backward compatibility OpenIssues is still populated. 55 StargazersCount *int `json:"stargazers_count,omitempty"` 56 SubscribersCount *int `json:"subscribers_count,omitempty"` 57 WatchersCount *int `json:"watchers_count,omitempty"` // Deprecated: Replaced by StargazersCount. For backward compatibility WatchersCount is still populated. 58 Watchers *int `json:"watchers,omitempty"` // Deprecated: Replaced by StargazersCount. For backward compatibility Watchers is still populated. 59 Size *int `json:"size,omitempty"` 60 AutoInit *bool `json:"auto_init,omitempty"` 61 Parent *Repository `json:"parent,omitempty"` 62 Source *Repository `json:"source,omitempty"` 63 TemplateRepository *Repository `json:"template_repository,omitempty"` 64 Organization *Organization `json:"organization,omitempty"` 65 Permissions map[string]bool `json:"permissions,omitempty"` 66 AllowRebaseMerge *bool `json:"allow_rebase_merge,omitempty"` 67 AllowUpdateBranch *bool `json:"allow_update_branch,omitempty"` 68 AllowSquashMerge *bool `json:"allow_squash_merge,omitempty"` 69 AllowMergeCommit *bool `json:"allow_merge_commit,omitempty"` 70 AllowAutoMerge *bool `json:"allow_auto_merge,omitempty"` 71 AllowForking *bool `json:"allow_forking,omitempty"` 72 WebCommitSignoffRequired *bool `json:"web_commit_signoff_required,omitempty"` 73 DeleteBranchOnMerge *bool `json:"delete_branch_on_merge,omitempty"` 74 UseSquashPRTitleAsDefault *bool `json:"use_squash_pr_title_as_default,omitempty"` 75 SquashMergeCommitTitle *string `json:"squash_merge_commit_title,omitempty"` // Can be one of: "PR_TITLE", "COMMIT_OR_PR_TITLE" 76 SquashMergeCommitMessage *string `json:"squash_merge_commit_message,omitempty"` // Can be one of: "PR_BODY", "COMMIT_MESSAGES", "BLANK" 77 MergeCommitTitle *string `json:"merge_commit_title,omitempty"` // Can be one of: "PR_TITLE", "MERGE_MESSAGE" 78 MergeCommitMessage *string `json:"merge_commit_message,omitempty"` // Can be one of: "PR_BODY", "PR_TITLE", "BLANK" 79 Topics []string `json:"topics,omitempty"` 80 CustomProperties map[string]string `json:"custom_properties,omitempty"` 81 Archived *bool `json:"archived,omitempty"` 82 Disabled *bool `json:"disabled,omitempty"` 83 84 // Only provided when using RepositoriesService.Get while in preview 85 License *License `json:"license,omitempty"` 86 87 // Additional mutable fields when creating and editing a repository 88 Private *bool `json:"private,omitempty"` 89 HasIssues *bool `json:"has_issues,omitempty"` 90 HasWiki *bool `json:"has_wiki,omitempty"` 91 HasPages *bool `json:"has_pages,omitempty"` 92 HasProjects *bool `json:"has_projects,omitempty"` 93 HasDownloads *bool `json:"has_downloads,omitempty"` 94 HasDiscussions *bool `json:"has_discussions,omitempty"` 95 IsTemplate *bool `json:"is_template,omitempty"` 96 LicenseTemplate *string `json:"license_template,omitempty"` 97 GitignoreTemplate *string `json:"gitignore_template,omitempty"` 98 99 // Options for configuring Advanced Security and Secret Scanning 100 SecurityAndAnalysis *SecurityAndAnalysis `json:"security_and_analysis,omitempty"` 101 102 // Creating an organization repository. Required for non-owners. 103 TeamID *int64 `json:"team_id,omitempty"` 104 105 // API URLs 106 URL *string `json:"url,omitempty"` 107 ArchiveURL *string `json:"archive_url,omitempty"` 108 AssigneesURL *string `json:"assignees_url,omitempty"` 109 BlobsURL *string `json:"blobs_url,omitempty"` 110 BranchesURL *string `json:"branches_url,omitempty"` 111 CollaboratorsURL *string `json:"collaborators_url,omitempty"` 112 CommentsURL *string `json:"comments_url,omitempty"` 113 CommitsURL *string `json:"commits_url,omitempty"` 114 CompareURL *string `json:"compare_url,omitempty"` 115 ContentsURL *string `json:"contents_url,omitempty"` 116 ContributorsURL *string `json:"contributors_url,omitempty"` 117 DeploymentsURL *string `json:"deployments_url,omitempty"` 118 DownloadsURL *string `json:"downloads_url,omitempty"` 119 EventsURL *string `json:"events_url,omitempty"` 120 ForksURL *string `json:"forks_url,omitempty"` 121 GitCommitsURL *string `json:"git_commits_url,omitempty"` 122 GitRefsURL *string `json:"git_refs_url,omitempty"` 123 GitTagsURL *string `json:"git_tags_url,omitempty"` 124 HooksURL *string `json:"hooks_url,omitempty"` 125 IssueCommentURL *string `json:"issue_comment_url,omitempty"` 126 IssueEventsURL *string `json:"issue_events_url,omitempty"` 127 IssuesURL *string `json:"issues_url,omitempty"` 128 KeysURL *string `json:"keys_url,omitempty"` 129 LabelsURL *string `json:"labels_url,omitempty"` 130 LanguagesURL *string `json:"languages_url,omitempty"` 131 MergesURL *string `json:"merges_url,omitempty"` 132 MilestonesURL *string `json:"milestones_url,omitempty"` 133 NotificationsURL *string `json:"notifications_url,omitempty"` 134 PullsURL *string `json:"pulls_url,omitempty"` 135 ReleasesURL *string `json:"releases_url,omitempty"` 136 StargazersURL *string `json:"stargazers_url,omitempty"` 137 StatusesURL *string `json:"statuses_url,omitempty"` 138 SubscribersURL *string `json:"subscribers_url,omitempty"` 139 SubscriptionURL *string `json:"subscription_url,omitempty"` 140 TagsURL *string `json:"tags_url,omitempty"` 141 TreesURL *string `json:"trees_url,omitempty"` 142 TeamsURL *string `json:"teams_url,omitempty"` 143 144 // TextMatches is only populated from search results that request text matches 145 // See: search.go and https://docs.github.com/rest/search/#text-match-metadata 146 TextMatches []*TextMatch `json:"text_matches,omitempty"` 147 148 // Visibility is only used for Create and Edit endpoints. The visibility field 149 // overrides the field parameter when both are used. 150 // Can be one of public, private or internal. 151 Visibility *string `json:"visibility,omitempty"` 152 153 // RoleName is only returned by the API 'check team permissions for a repository'. 154 // See: teams.go (IsTeamRepoByID) https://docs.github.com/rest/teams/teams#check-team-permissions-for-a-repository 155 RoleName *string `json:"role_name,omitempty"` 156 } 157 158 func (r Repository) String() string { 159 return Stringify(r) 160 } 161 162 // BranchListOptions specifies the optional parameters to the 163 // RepositoriesService.ListBranches method. 164 type BranchListOptions struct { 165 // Setting to true returns only protected branches. 166 // When set to false, only unprotected branches are returned. 167 // Omitting this parameter returns all branches. 168 // Default: nil 169 Protected *bool `url:"protected,omitempty"` 170 171 ListOptions 172 } 173 174 // RepositoryListOptions specifies the optional parameters to the 175 // RepositoriesService.List method. 176 type RepositoryListOptions struct { 177 // See RepositoryListByAuthenticatedUserOptions.Visibility 178 Visibility string `url:"visibility,omitempty"` 179 180 // See RepositoryListByAuthenticatedUserOptions.Affiliation 181 Affiliation string `url:"affiliation,omitempty"` 182 183 // See RepositoryListByUserOptions.Type or RepositoryListByAuthenticatedUserOptions.Type 184 Type string `url:"type,omitempty"` 185 186 // See RepositoryListByUserOptions.Sort or RepositoryListByAuthenticatedUserOptions.Sort 187 Sort string `url:"sort,omitempty"` 188 189 // See RepositoryListByUserOptions.Direction or RepositoryListByAuthenticatedUserOptions.Direction 190 Direction string `url:"direction,omitempty"` 191 192 ListOptions 193 } 194 195 // SecurityAndAnalysis specifies the optional advanced security features 196 // that are enabled on a given repository. 197 type SecurityAndAnalysis struct { 198 AdvancedSecurity *AdvancedSecurity `json:"advanced_security,omitempty"` 199 SecretScanning *SecretScanning `json:"secret_scanning,omitempty"` 200 SecretScanningPushProtection *SecretScanningPushProtection `json:"secret_scanning_push_protection,omitempty"` 201 DependabotSecurityUpdates *DependabotSecurityUpdates `json:"dependabot_security_updates,omitempty"` 202 SecretScanningValidityChecks *SecretScanningValidityChecks `json:"secret_scanning_validity_checks,omitempty"` 203 } 204 205 func (s SecurityAndAnalysis) String() string { 206 return Stringify(s) 207 } 208 209 // AdvancedSecurity specifies the state of advanced security on a repository. 210 // 211 // GitHub API docs: https://docs.github.com/github/getting-started-with-github/learning-about-github/about-github-advanced-security 212 type AdvancedSecurity struct { 213 Status *string `json:"status,omitempty"` 214 } 215 216 func (a AdvancedSecurity) String() string { 217 return Stringify(a) 218 } 219 220 // SecretScanning specifies the state of secret scanning on a repository. 221 // 222 // GitHub API docs: https://docs.github.com/code-security/secret-security/about-secret-scanning 223 type SecretScanning struct { 224 Status *string `json:"status,omitempty"` 225 } 226 227 func (s SecretScanning) String() string { 228 return Stringify(s) 229 } 230 231 // SecretScanningPushProtection specifies the state of secret scanning push protection on a repository. 232 // 233 // GitHub API docs: https://docs.github.com/code-security/secret-scanning/about-secret-scanning#about-secret-scanning-for-partner-patterns 234 type SecretScanningPushProtection struct { 235 Status *string `json:"status,omitempty"` 236 } 237 238 func (s SecretScanningPushProtection) String() string { 239 return Stringify(s) 240 } 241 242 // DependabotSecurityUpdates specifies the state of Dependabot security updates on a repository. 243 // 244 // GitHub API docs: https://docs.github.com/code-security/dependabot/dependabot-security-updates/about-dependabot-security-updates 245 type DependabotSecurityUpdates struct { 246 Status *string `json:"status,omitempty"` 247 } 248 249 func (d DependabotSecurityUpdates) String() string { 250 return Stringify(d) 251 } 252 253 // SecretScanningValidityChecks represents the state of secret scanning validity checks on a repository. 254 // 255 // GitHub API docs: https://docs.github.com/en/enterprise-cloud@latest/repositories/managing-your-repositorys-settings-and-features/enabling-features-for-your-repository/managing-security-and-analysis-settings-for-your-repository#allowing-validity-checks-for-partner-patterns-in-a-repository 256 type SecretScanningValidityChecks struct { 257 Status *string `json:"status,omitempty"` 258 } 259 260 // List calls either RepositoriesService.ListByUser or RepositoriesService.ListByAuthenticatedUser 261 // depending on whether user is empty. 262 // 263 // Deprecated: Use RepositoriesService.ListByUser or RepositoriesService.ListByAuthenticatedUser instead. 264 // 265 // GitHub API docs: https://docs.github.com/rest/repos/repos#list-repositories-for-a-user 266 // GitHub API docs: https://docs.github.com/rest/repos/repos#list-repositories-for-the-authenticated-user 267 // 268 //meta:operation GET /user/repos 269 //meta:operation GET /users/{username}/repos 270 func (s *RepositoriesService) List(ctx context.Context, user string, opts *RepositoryListOptions) ([]*Repository, *Response, error) { 271 if opts == nil { 272 opts = &RepositoryListOptions{} 273 } 274 if user != "" { 275 return s.ListByUser(ctx, user, &RepositoryListByUserOptions{ 276 Type: opts.Type, 277 Sort: opts.Sort, 278 Direction: opts.Direction, 279 ListOptions: opts.ListOptions, 280 }) 281 } 282 return s.ListByAuthenticatedUser(ctx, &RepositoryListByAuthenticatedUserOptions{ 283 Visibility: opts.Visibility, 284 Affiliation: opts.Affiliation, 285 Type: opts.Type, 286 Sort: opts.Sort, 287 Direction: opts.Direction, 288 ListOptions: opts.ListOptions, 289 }) 290 } 291 292 // RepositoryListByUserOptions specifies the optional parameters to the 293 // RepositoriesService.ListByUser method. 294 type RepositoryListByUserOptions struct { 295 // Limit results to repositories of the specified type. 296 // Default: owner 297 // Can be one of: all, owner, member 298 Type string `url:"type,omitempty"` 299 300 // The property to sort the results by. 301 // Default: full_name 302 // Can be one of: created, updated, pushed, full_name 303 Sort string `url:"sort,omitempty"` 304 305 // The order to sort by. 306 // Default: asc when using full_name, otherwise desc. 307 // Can be one of: asc, desc 308 Direction string `url:"direction,omitempty"` 309 310 ListOptions 311 } 312 313 // ListByUser lists public repositories for the specified user. 314 // 315 // GitHub API docs: https://docs.github.com/rest/repos/repos#list-repositories-for-a-user 316 // 317 //meta:operation GET /users/{username}/repos 318 func (s *RepositoriesService) ListByUser(ctx context.Context, user string, opts *RepositoryListByUserOptions) ([]*Repository, *Response, error) { 319 u := fmt.Sprintf("users/%v/repos", user) 320 u, err := addOptions(u, opts) 321 if err != nil { 322 return nil, nil, err 323 } 324 325 req, err := s.client.NewRequest("GET", u, nil) 326 if err != nil { 327 return nil, nil, err 328 } 329 330 var repos []*Repository 331 resp, err := s.client.Do(ctx, req, &repos) 332 if err != nil { 333 return nil, resp, err 334 } 335 336 return repos, resp, nil 337 } 338 339 // RepositoryListByAuthenticatedUserOptions specifies the optional parameters to the 340 // RepositoriesService.ListByAuthenticatedUser method. 341 type RepositoryListByAuthenticatedUserOptions struct { 342 // Limit results to repositories with the specified visibility. 343 // Default: all 344 // Can be one of: all, public, private 345 Visibility string `url:"visibility,omitempty"` 346 347 // List repos of given affiliation[s]. 348 // Comma-separated list of values. Can include: 349 // * owner: Repositories that are owned by the authenticated user. 350 // * collaborator: Repositories that the user has been added to as a 351 // collaborator. 352 // * organization_member: Repositories that the user has access to through 353 // being a member of an organization. This includes every repository on 354 // every team that the user is on. 355 // Default: owner,collaborator,organization_member 356 Affiliation string `url:"affiliation,omitempty"` 357 358 // Limit results to repositories of the specified type. Will cause a 422 error if 359 // used in the same request as visibility or affiliation. 360 // Default: all 361 // Can be one of: all, owner, public, private, member 362 Type string `url:"type,omitempty"` 363 364 // The property to sort the results by. 365 // Default: full_name 366 // Can be one of: created, updated, pushed, full_name 367 Sort string `url:"sort,omitempty"` 368 369 // Direction in which to sort repositories. Can be one of asc or desc. 370 // Default: when using full_name: asc; otherwise desc 371 Direction string `url:"direction,omitempty"` 372 373 ListOptions 374 } 375 376 // ListByAuthenticatedUser lists repositories for the authenticated user. 377 // 378 // GitHub API docs: https://docs.github.com/rest/repos/repos#list-repositories-for-the-authenticated-user 379 // 380 //meta:operation GET /user/repos 381 func (s *RepositoriesService) ListByAuthenticatedUser(ctx context.Context, opts *RepositoryListByAuthenticatedUserOptions) ([]*Repository, *Response, error) { 382 u := "user/repos" 383 u, err := addOptions(u, opts) 384 if err != nil { 385 return nil, nil, err 386 } 387 388 req, err := s.client.NewRequest("GET", u, nil) 389 if err != nil { 390 return nil, nil, err 391 } 392 393 var repos []*Repository 394 resp, err := s.client.Do(ctx, req, &repos) 395 if err != nil { 396 return nil, resp, err 397 } 398 399 return repos, resp, nil 400 } 401 402 // RepositoryListByOrgOptions specifies the optional parameters to the 403 // RepositoriesService.ListByOrg method. 404 type RepositoryListByOrgOptions struct { 405 // Type of repositories to list. Possible values are: all, public, private, 406 // forks, sources, member. Default is "all". 407 Type string `url:"type,omitempty"` 408 409 // How to sort the repository list. Can be one of created, updated, pushed, 410 // full_name. Default is "created". 411 Sort string `url:"sort,omitempty"` 412 413 // Direction in which to sort repositories. Can be one of asc or desc. 414 // Default when using full_name: asc; otherwise desc. 415 Direction string `url:"direction,omitempty"` 416 417 ListOptions 418 } 419 420 // ListByOrg lists the repositories for an organization. 421 // 422 // GitHub API docs: https://docs.github.com/rest/repos/repos#list-organization-repositories 423 // 424 //meta:operation GET /orgs/{org}/repos 425 func (s *RepositoriesService) ListByOrg(ctx context.Context, org string, opts *RepositoryListByOrgOptions) ([]*Repository, *Response, error) { 426 u := fmt.Sprintf("orgs/%v/repos", org) 427 u, err := addOptions(u, opts) 428 if err != nil { 429 return nil, nil, err 430 } 431 432 req, err := s.client.NewRequest("GET", u, nil) 433 if err != nil { 434 return nil, nil, err 435 } 436 437 // TODO: remove custom Accept headers when APIs fully launch. 438 acceptHeaders := []string{mediaTypeTopicsPreview, mediaTypeRepositoryVisibilityPreview} 439 req.Header.Set("Accept", strings.Join(acceptHeaders, ", ")) 440 441 var repos []*Repository 442 resp, err := s.client.Do(ctx, req, &repos) 443 if err != nil { 444 return nil, resp, err 445 } 446 447 return repos, resp, nil 448 } 449 450 // RepositoryListAllOptions specifies the optional parameters to the 451 // RepositoriesService.ListAll method. 452 type RepositoryListAllOptions struct { 453 // ID of the last repository seen 454 Since int64 `url:"since,omitempty"` 455 } 456 457 // ListAll lists all GitHub repositories in the order that they were created. 458 // 459 // GitHub API docs: https://docs.github.com/rest/repos/repos#list-public-repositories 460 // 461 //meta:operation GET /repositories 462 func (s *RepositoriesService) ListAll(ctx context.Context, opts *RepositoryListAllOptions) ([]*Repository, *Response, error) { 463 u, err := addOptions("repositories", opts) 464 if err != nil { 465 return nil, nil, err 466 } 467 468 req, err := s.client.NewRequest("GET", u, nil) 469 if err != nil { 470 return nil, nil, err 471 } 472 473 var repos []*Repository 474 resp, err := s.client.Do(ctx, req, &repos) 475 if err != nil { 476 return nil, resp, err 477 } 478 479 return repos, resp, nil 480 } 481 482 // createRepoRequest is a subset of Repository and is used internally 483 // by Create to pass only the known fields for the endpoint. 484 // 485 // See https://github.com/google/go-github/issues/1014 for more 486 // information. 487 type createRepoRequest struct { 488 // Name is required when creating a repo. 489 Name *string `json:"name,omitempty"` 490 Description *string `json:"description,omitempty"` 491 Homepage *string `json:"homepage,omitempty"` 492 493 Private *bool `json:"private,omitempty"` 494 Visibility *string `json:"visibility,omitempty"` 495 HasIssues *bool `json:"has_issues,omitempty"` 496 HasProjects *bool `json:"has_projects,omitempty"` 497 HasWiki *bool `json:"has_wiki,omitempty"` 498 HasDiscussions *bool `json:"has_discussions,omitempty"` 499 IsTemplate *bool `json:"is_template,omitempty"` 500 501 // Creating an organization repository. Required for non-owners. 502 TeamID *int64 `json:"team_id,omitempty"` 503 504 AutoInit *bool `json:"auto_init,omitempty"` 505 GitignoreTemplate *string `json:"gitignore_template,omitempty"` 506 LicenseTemplate *string `json:"license_template,omitempty"` 507 AllowSquashMerge *bool `json:"allow_squash_merge,omitempty"` 508 AllowMergeCommit *bool `json:"allow_merge_commit,omitempty"` 509 AllowRebaseMerge *bool `json:"allow_rebase_merge,omitempty"` 510 AllowUpdateBranch *bool `json:"allow_update_branch,omitempty"` 511 AllowAutoMerge *bool `json:"allow_auto_merge,omitempty"` 512 AllowForking *bool `json:"allow_forking,omitempty"` 513 DeleteBranchOnMerge *bool `json:"delete_branch_on_merge,omitempty"` 514 UseSquashPRTitleAsDefault *bool `json:"use_squash_pr_title_as_default,omitempty"` 515 SquashMergeCommitTitle *string `json:"squash_merge_commit_title,omitempty"` 516 SquashMergeCommitMessage *string `json:"squash_merge_commit_message,omitempty"` 517 MergeCommitTitle *string `json:"merge_commit_title,omitempty"` 518 MergeCommitMessage *string `json:"merge_commit_message,omitempty"` 519 } 520 521 // Create a new repository. If an organization is specified, the new 522 // repository will be created under that org. If the empty string is 523 // specified, it will be created for the authenticated user. 524 // 525 // Note that only a subset of the repo fields are used and repo must 526 // not be nil. 527 // 528 // Also note that this method will return the response without actually 529 // waiting for GitHub to finish creating the repository and letting the 530 // changes propagate throughout its servers. You may set up a loop with 531 // exponential back-off to verify repository's creation. 532 // 533 // GitHub API docs: https://docs.github.com/rest/repos/repos#create-a-repository-for-the-authenticated-user 534 // GitHub API docs: https://docs.github.com/rest/repos/repos#create-an-organization-repository 535 // 536 //meta:operation POST /orgs/{org}/repos 537 //meta:operation POST /user/repos 538 func (s *RepositoriesService) Create(ctx context.Context, org string, repo *Repository) (*Repository, *Response, error) { 539 var u string 540 if org != "" { 541 u = fmt.Sprintf("orgs/%v/repos", org) 542 } else { 543 u = "user/repos" 544 } 545 546 repoReq := &createRepoRequest{ 547 Name: repo.Name, 548 Description: repo.Description, 549 Homepage: repo.Homepage, 550 Private: repo.Private, 551 Visibility: repo.Visibility, 552 HasIssues: repo.HasIssues, 553 HasProjects: repo.HasProjects, 554 HasWiki: repo.HasWiki, 555 HasDiscussions: repo.HasDiscussions, 556 IsTemplate: repo.IsTemplate, 557 TeamID: repo.TeamID, 558 AutoInit: repo.AutoInit, 559 GitignoreTemplate: repo.GitignoreTemplate, 560 LicenseTemplate: repo.LicenseTemplate, 561 AllowSquashMerge: repo.AllowSquashMerge, 562 AllowMergeCommit: repo.AllowMergeCommit, 563 AllowRebaseMerge: repo.AllowRebaseMerge, 564 AllowUpdateBranch: repo.AllowUpdateBranch, 565 AllowAutoMerge: repo.AllowAutoMerge, 566 AllowForking: repo.AllowForking, 567 DeleteBranchOnMerge: repo.DeleteBranchOnMerge, 568 UseSquashPRTitleAsDefault: repo.UseSquashPRTitleAsDefault, 569 SquashMergeCommitTitle: repo.SquashMergeCommitTitle, 570 SquashMergeCommitMessage: repo.SquashMergeCommitMessage, 571 MergeCommitTitle: repo.MergeCommitTitle, 572 MergeCommitMessage: repo.MergeCommitMessage, 573 } 574 575 req, err := s.client.NewRequest("POST", u, repoReq) 576 if err != nil { 577 return nil, nil, err 578 } 579 580 acceptHeaders := []string{mediaTypeRepositoryTemplatePreview, mediaTypeRepositoryVisibilityPreview} 581 req.Header.Set("Accept", strings.Join(acceptHeaders, ", ")) 582 r := new(Repository) 583 resp, err := s.client.Do(ctx, req, r) 584 if err != nil { 585 return nil, resp, err 586 } 587 588 return r, resp, nil 589 } 590 591 // TemplateRepoRequest represents a request to create a repository from a template. 592 type TemplateRepoRequest struct { 593 // Name is required when creating a repo. 594 Name *string `json:"name,omitempty"` 595 Owner *string `json:"owner,omitempty"` 596 Description *string `json:"description,omitempty"` 597 598 IncludeAllBranches *bool `json:"include_all_branches,omitempty"` 599 Private *bool `json:"private,omitempty"` 600 } 601 602 // CreateFromTemplate generates a repository from a template. 603 // 604 // GitHub API docs: https://docs.github.com/rest/repos/repos#create-a-repository-using-a-template 605 // 606 //meta:operation POST /repos/{template_owner}/{template_repo}/generate 607 func (s *RepositoriesService) CreateFromTemplate(ctx context.Context, templateOwner, templateRepo string, templateRepoReq *TemplateRepoRequest) (*Repository, *Response, error) { 608 u := fmt.Sprintf("repos/%v/%v/generate", templateOwner, templateRepo) 609 610 req, err := s.client.NewRequest("POST", u, templateRepoReq) 611 if err != nil { 612 return nil, nil, err 613 } 614 615 req.Header.Set("Accept", mediaTypeRepositoryTemplatePreview) 616 r := new(Repository) 617 resp, err := s.client.Do(ctx, req, r) 618 if err != nil { 619 return nil, resp, err 620 } 621 622 return r, resp, nil 623 } 624 625 // Get fetches a repository. 626 // 627 // GitHub API docs: https://docs.github.com/rest/repos/repos#get-a-repository 628 // 629 //meta:operation GET /repos/{owner}/{repo} 630 func (s *RepositoriesService) Get(ctx context.Context, owner, repo string) (*Repository, *Response, error) { 631 u := fmt.Sprintf("repos/%v/%v", owner, repo) 632 req, err := s.client.NewRequest("GET", u, nil) 633 if err != nil { 634 return nil, nil, err 635 } 636 637 // TODO: remove custom Accept header when the license support fully launches 638 // https://docs.github.com/rest/licenses/#get-a-repositorys-license 639 acceptHeaders := []string{ 640 mediaTypeCodesOfConductPreview, 641 mediaTypeTopicsPreview, 642 mediaTypeRepositoryTemplatePreview, 643 mediaTypeRepositoryVisibilityPreview, 644 } 645 req.Header.Set("Accept", strings.Join(acceptHeaders, ", ")) 646 647 repository := new(Repository) 648 resp, err := s.client.Do(ctx, req, repository) 649 if err != nil { 650 return nil, resp, err 651 } 652 653 return repository, resp, nil 654 } 655 656 // GetCodeOfConduct gets the contents of a repository's code of conduct. 657 // Note that https://docs.github.com/rest/codes-of-conduct#about-the-codes-of-conduct-api 658 // says to use the GET /repos/{owner}/{repo} endpoint. 659 // 660 // GitHub API docs: https://docs.github.com/rest/repos/repos#get-a-repository 661 // 662 //meta:operation GET /repos/{owner}/{repo} 663 func (s *RepositoriesService) GetCodeOfConduct(ctx context.Context, owner, repo string) (*CodeOfConduct, *Response, error) { 664 u := fmt.Sprintf("repos/%v/%v", owner, repo) 665 req, err := s.client.NewRequest("GET", u, nil) 666 if err != nil { 667 return nil, nil, err 668 } 669 670 // TODO: remove custom Accept header when this API fully launches. 671 req.Header.Set("Accept", mediaTypeCodesOfConductPreview) 672 673 r := new(Repository) 674 resp, err := s.client.Do(ctx, req, r) 675 if err != nil { 676 return nil, resp, err 677 } 678 679 return r.GetCodeOfConduct(), resp, nil 680 } 681 682 // GetByID fetches a repository. 683 // 684 // Note: GetByID uses the undocumented GitHub API endpoint "GET /repositories/{repository_id}". 685 // 686 //meta:operation GET /repositories/{repository_id} 687 func (s *RepositoriesService) GetByID(ctx context.Context, id int64) (*Repository, *Response, error) { 688 u := fmt.Sprintf("repositories/%d", id) 689 req, err := s.client.NewRequest("GET", u, nil) 690 if err != nil { 691 return nil, nil, err 692 } 693 694 repository := new(Repository) 695 resp, err := s.client.Do(ctx, req, repository) 696 if err != nil { 697 return nil, resp, err 698 } 699 700 return repository, resp, nil 701 } 702 703 // Edit updates a repository. 704 // 705 // GitHub API docs: https://docs.github.com/rest/repos/repos#update-a-repository 706 // 707 //meta:operation PATCH /repos/{owner}/{repo} 708 func (s *RepositoriesService) Edit(ctx context.Context, owner, repo string, repository *Repository) (*Repository, *Response, error) { 709 u := fmt.Sprintf("repos/%v/%v", owner, repo) 710 req, err := s.client.NewRequest("PATCH", u, repository) 711 if err != nil { 712 return nil, nil, err 713 } 714 715 acceptHeaders := []string{mediaTypeRepositoryTemplatePreview, mediaTypeRepositoryVisibilityPreview} 716 req.Header.Set("Accept", strings.Join(acceptHeaders, ", ")) 717 r := new(Repository) 718 resp, err := s.client.Do(ctx, req, r) 719 if err != nil { 720 return nil, resp, err 721 } 722 723 return r, resp, nil 724 } 725 726 // Delete a repository. 727 // 728 // GitHub API docs: https://docs.github.com/rest/repos/repos#delete-a-repository 729 // 730 //meta:operation DELETE /repos/{owner}/{repo} 731 func (s *RepositoriesService) Delete(ctx context.Context, owner, repo string) (*Response, error) { 732 u := fmt.Sprintf("repos/%v/%v", owner, repo) 733 req, err := s.client.NewRequest("DELETE", u, nil) 734 if err != nil { 735 return nil, err 736 } 737 738 return s.client.Do(ctx, req, nil) 739 } 740 741 // Contributor represents a repository contributor 742 type Contributor struct { 743 Login *string `json:"login,omitempty"` 744 ID *int64 `json:"id,omitempty"` 745 NodeID *string `json:"node_id,omitempty"` 746 AvatarURL *string `json:"avatar_url,omitempty"` 747 GravatarID *string `json:"gravatar_id,omitempty"` 748 URL *string `json:"url,omitempty"` 749 HTMLURL *string `json:"html_url,omitempty"` 750 FollowersURL *string `json:"followers_url,omitempty"` 751 FollowingURL *string `json:"following_url,omitempty"` 752 GistsURL *string `json:"gists_url,omitempty"` 753 StarredURL *string `json:"starred_url,omitempty"` 754 SubscriptionsURL *string `json:"subscriptions_url,omitempty"` 755 OrganizationsURL *string `json:"organizations_url,omitempty"` 756 ReposURL *string `json:"repos_url,omitempty"` 757 EventsURL *string `json:"events_url,omitempty"` 758 ReceivedEventsURL *string `json:"received_events_url,omitempty"` 759 Type *string `json:"type,omitempty"` 760 SiteAdmin *bool `json:"site_admin,omitempty"` 761 Contributions *int `json:"contributions,omitempty"` 762 Name *string `json:"name,omitempty"` 763 Email *string `json:"email,omitempty"` 764 } 765 766 // ListContributorsOptions specifies the optional parameters to the 767 // RepositoriesService.ListContributors method. 768 type ListContributorsOptions struct { 769 // Include anonymous contributors in results or not 770 Anon string `url:"anon,omitempty"` 771 772 ListOptions 773 } 774 775 // GetVulnerabilityAlerts checks if vulnerability alerts are enabled for a repository. 776 // 777 // GitHub API docs: https://docs.github.com/rest/repos/repos#check-if-vulnerability-alerts-are-enabled-for-a-repository 778 // 779 //meta:operation GET /repos/{owner}/{repo}/vulnerability-alerts 780 func (s *RepositoriesService) GetVulnerabilityAlerts(ctx context.Context, owner, repository string) (bool, *Response, error) { 781 u := fmt.Sprintf("repos/%v/%v/vulnerability-alerts", owner, repository) 782 783 req, err := s.client.NewRequest("GET", u, nil) 784 if err != nil { 785 return false, nil, err 786 } 787 788 // TODO: remove custom Accept header when this API fully launches 789 req.Header.Set("Accept", mediaTypeRequiredVulnerabilityAlertsPreview) 790 791 resp, err := s.client.Do(ctx, req, nil) 792 vulnerabilityAlertsEnabled, err := parseBoolResponse(err) 793 return vulnerabilityAlertsEnabled, resp, err 794 } 795 796 // EnableVulnerabilityAlerts enables vulnerability alerts and the dependency graph for a repository. 797 // 798 // GitHub API docs: https://docs.github.com/rest/repos/repos#enable-vulnerability-alerts 799 // 800 //meta:operation PUT /repos/{owner}/{repo}/vulnerability-alerts 801 func (s *RepositoriesService) EnableVulnerabilityAlerts(ctx context.Context, owner, repository string) (*Response, error) { 802 u := fmt.Sprintf("repos/%v/%v/vulnerability-alerts", owner, repository) 803 804 req, err := s.client.NewRequest("PUT", u, nil) 805 if err != nil { 806 return nil, err 807 } 808 809 // TODO: remove custom Accept header when this API fully launches 810 req.Header.Set("Accept", mediaTypeRequiredVulnerabilityAlertsPreview) 811 812 return s.client.Do(ctx, req, nil) 813 } 814 815 // DisableVulnerabilityAlerts disables vulnerability alerts and the dependency graph for a repository. 816 // 817 // GitHub API docs: https://docs.github.com/rest/repos/repos#disable-vulnerability-alerts 818 // 819 //meta:operation DELETE /repos/{owner}/{repo}/vulnerability-alerts 820 func (s *RepositoriesService) DisableVulnerabilityAlerts(ctx context.Context, owner, repository string) (*Response, error) { 821 u := fmt.Sprintf("repos/%v/%v/vulnerability-alerts", owner, repository) 822 823 req, err := s.client.NewRequest("DELETE", u, nil) 824 if err != nil { 825 return nil, err 826 } 827 828 // TODO: remove custom Accept header when this API fully launches 829 req.Header.Set("Accept", mediaTypeRequiredVulnerabilityAlertsPreview) 830 831 return s.client.Do(ctx, req, nil) 832 } 833 834 // GetAutomatedSecurityFixes checks if the automated security fixes for a repository are enabled. 835 // 836 // GitHub API docs: https://docs.github.com/rest/repos/repos#check-if-automated-security-fixes-are-enabled-for-a-repository 837 // 838 //meta:operation GET /repos/{owner}/{repo}/automated-security-fixes 839 func (s *RepositoriesService) GetAutomatedSecurityFixes(ctx context.Context, owner, repository string) (*AutomatedSecurityFixes, *Response, error) { 840 u := fmt.Sprintf("repos/%v/%v/automated-security-fixes", owner, repository) 841 842 req, err := s.client.NewRequest("GET", u, nil) 843 if err != nil { 844 return nil, nil, err 845 } 846 847 p := new(AutomatedSecurityFixes) 848 resp, err := s.client.Do(ctx, req, p) 849 if err != nil { 850 return nil, resp, err 851 } 852 return p, resp, nil 853 } 854 855 // EnableAutomatedSecurityFixes enables the automated security fixes for a repository. 856 // 857 // GitHub API docs: https://docs.github.com/rest/repos/repos#enable-automated-security-fixes 858 // 859 //meta:operation PUT /repos/{owner}/{repo}/automated-security-fixes 860 func (s *RepositoriesService) EnableAutomatedSecurityFixes(ctx context.Context, owner, repository string) (*Response, error) { 861 u := fmt.Sprintf("repos/%v/%v/automated-security-fixes", owner, repository) 862 863 req, err := s.client.NewRequest("PUT", u, nil) 864 if err != nil { 865 return nil, err 866 } 867 868 return s.client.Do(ctx, req, nil) 869 } 870 871 // DisableAutomatedSecurityFixes disables vulnerability alerts and the dependency graph for a repository. 872 // 873 // GitHub API docs: https://docs.github.com/rest/repos/repos#disable-automated-security-fixes 874 // 875 //meta:operation DELETE /repos/{owner}/{repo}/automated-security-fixes 876 func (s *RepositoriesService) DisableAutomatedSecurityFixes(ctx context.Context, owner, repository string) (*Response, error) { 877 u := fmt.Sprintf("repos/%v/%v/automated-security-fixes", owner, repository) 878 879 req, err := s.client.NewRequest("DELETE", u, nil) 880 if err != nil { 881 return nil, err 882 } 883 884 return s.client.Do(ctx, req, nil) 885 } 886 887 // ListContributors lists contributors for a repository. 888 // 889 // GitHub API docs: https://docs.github.com/rest/repos/repos#list-repository-contributors 890 // 891 //meta:operation GET /repos/{owner}/{repo}/contributors 892 func (s *RepositoriesService) ListContributors(ctx context.Context, owner string, repository string, opts *ListContributorsOptions) ([]*Contributor, *Response, error) { 893 u := fmt.Sprintf("repos/%v/%v/contributors", owner, repository) 894 u, err := addOptions(u, opts) 895 if err != nil { 896 return nil, nil, err 897 } 898 899 req, err := s.client.NewRequest("GET", u, nil) 900 if err != nil { 901 return nil, nil, err 902 } 903 904 var contributor []*Contributor 905 resp, err := s.client.Do(ctx, req, &contributor) 906 if err != nil { 907 return nil, resp, err 908 } 909 910 return contributor, resp, nil 911 } 912 913 // ListLanguages lists languages for the specified repository. The returned map 914 // specifies the languages and the number of bytes of code written in that 915 // language. For example: 916 // 917 // { 918 // "C": 78769, 919 // "Python": 7769 920 // } 921 // 922 // GitHub API docs: https://docs.github.com/rest/repos/repos#list-repository-languages 923 // 924 //meta:operation GET /repos/{owner}/{repo}/languages 925 func (s *RepositoriesService) ListLanguages(ctx context.Context, owner string, repo string) (map[string]int, *Response, error) { 926 u := fmt.Sprintf("repos/%v/%v/languages", owner, repo) 927 req, err := s.client.NewRequest("GET", u, nil) 928 if err != nil { 929 return nil, nil, err 930 } 931 932 languages := make(map[string]int) 933 resp, err := s.client.Do(ctx, req, &languages) 934 if err != nil { 935 return nil, resp, err 936 } 937 938 return languages, resp, nil 939 } 940 941 // ListTeams lists the teams for the specified repository. 942 // 943 // GitHub API docs: https://docs.github.com/rest/repos/repos#list-repository-teams 944 // 945 //meta:operation GET /repos/{owner}/{repo}/teams 946 func (s *RepositoriesService) ListTeams(ctx context.Context, owner string, repo string, opts *ListOptions) ([]*Team, *Response, error) { 947 u := fmt.Sprintf("repos/%v/%v/teams", owner, repo) 948 u, err := addOptions(u, opts) 949 if err != nil { 950 return nil, nil, err 951 } 952 953 req, err := s.client.NewRequest("GET", u, nil) 954 if err != nil { 955 return nil, nil, err 956 } 957 958 var teams []*Team 959 resp, err := s.client.Do(ctx, req, &teams) 960 if err != nil { 961 return nil, resp, err 962 } 963 964 return teams, resp, nil 965 } 966 967 // RepositoryTag represents a repository tag. 968 type RepositoryTag struct { 969 Name *string `json:"name,omitempty"` 970 Commit *Commit `json:"commit,omitempty"` 971 ZipballURL *string `json:"zipball_url,omitempty"` 972 TarballURL *string `json:"tarball_url,omitempty"` 973 } 974 975 // ListTags lists tags for the specified repository. 976 // 977 // GitHub API docs: https://docs.github.com/rest/repos/repos#list-repository-tags 978 // 979 //meta:operation GET /repos/{owner}/{repo}/tags 980 func (s *RepositoriesService) ListTags(ctx context.Context, owner string, repo string, opts *ListOptions) ([]*RepositoryTag, *Response, error) { 981 u := fmt.Sprintf("repos/%v/%v/tags", owner, repo) 982 u, err := addOptions(u, opts) 983 if err != nil { 984 return nil, nil, err 985 } 986 987 req, err := s.client.NewRequest("GET", u, nil) 988 if err != nil { 989 return nil, nil, err 990 } 991 992 var tags []*RepositoryTag 993 resp, err := s.client.Do(ctx, req, &tags) 994 if err != nil { 995 return nil, resp, err 996 } 997 998 return tags, resp, nil 999 } 1000 1001 // Branch represents a repository branch 1002 type Branch struct { 1003 Name *string `json:"name,omitempty"` 1004 Commit *RepositoryCommit `json:"commit,omitempty"` 1005 Protected *bool `json:"protected,omitempty"` 1006 } 1007 1008 // Protection represents a repository branch's protection. 1009 type Protection struct { 1010 RequiredStatusChecks *RequiredStatusChecks `json:"required_status_checks"` 1011 RequiredPullRequestReviews *PullRequestReviewsEnforcement `json:"required_pull_request_reviews"` 1012 EnforceAdmins *AdminEnforcement `json:"enforce_admins"` 1013 Restrictions *BranchRestrictions `json:"restrictions"` 1014 RequireLinearHistory *RequireLinearHistory `json:"required_linear_history"` 1015 AllowForcePushes *AllowForcePushes `json:"allow_force_pushes"` 1016 AllowDeletions *AllowDeletions `json:"allow_deletions"` 1017 RequiredConversationResolution *RequiredConversationResolution `json:"required_conversation_resolution"` 1018 BlockCreations *BlockCreations `json:"block_creations,omitempty"` 1019 LockBranch *LockBranch `json:"lock_branch,omitempty"` 1020 AllowForkSyncing *AllowForkSyncing `json:"allow_fork_syncing,omitempty"` 1021 RequiredSignatures *SignaturesProtectedBranch `json:"required_signatures,omitempty"` 1022 URL *string `json:"url,omitempty"` 1023 } 1024 1025 // BlockCreations represents whether users can push changes that create branches. If this is true, this 1026 // setting blocks pushes that create new branches, unless the push is initiated by a user, team, or app 1027 // which has the ability to push. 1028 type BlockCreations struct { 1029 Enabled *bool `json:"enabled,omitempty"` 1030 } 1031 1032 // LockBranch represents if the branch is marked as read-only. If this is true, users will not be able to push to the branch. 1033 type LockBranch struct { 1034 Enabled *bool `json:"enabled,omitempty"` 1035 } 1036 1037 // AllowForkSyncing represents whether users can pull changes from upstream when the branch is locked. 1038 type AllowForkSyncing struct { 1039 Enabled *bool `json:"enabled,omitempty"` 1040 } 1041 1042 // BranchProtectionRule represents the rule applied to a repositories branch. 1043 type BranchProtectionRule struct { 1044 ID *int64 `json:"id,omitempty"` 1045 RepositoryID *int64 `json:"repository_id,omitempty"` 1046 Name *string `json:"name,omitempty"` 1047 CreatedAt *Timestamp `json:"created_at,omitempty"` 1048 UpdatedAt *Timestamp `json:"updated_at,omitempty"` 1049 PullRequestReviewsEnforcementLevel *string `json:"pull_request_reviews_enforcement_level,omitempty"` 1050 RequiredApprovingReviewCount *int `json:"required_approving_review_count,omitempty"` 1051 DismissStaleReviewsOnPush *bool `json:"dismiss_stale_reviews_on_push,omitempty"` 1052 AuthorizedDismissalActorsOnly *bool `json:"authorized_dismissal_actors_only,omitempty"` 1053 IgnoreApprovalsFromContributors *bool `json:"ignore_approvals_from_contributors,omitempty"` 1054 RequireCodeOwnerReview *bool `json:"require_code_owner_review,omitempty"` 1055 RequiredStatusChecks []string `json:"required_status_checks,omitempty"` 1056 RequiredStatusChecksEnforcementLevel *string `json:"required_status_checks_enforcement_level,omitempty"` 1057 StrictRequiredStatusChecksPolicy *bool `json:"strict_required_status_checks_policy,omitempty"` 1058 SignatureRequirementEnforcementLevel *string `json:"signature_requirement_enforcement_level,omitempty"` 1059 LinearHistoryRequirementEnforcementLevel *string `json:"linear_history_requirement_enforcement_level,omitempty"` 1060 AdminEnforced *bool `json:"admin_enforced,omitempty"` 1061 AllowForcePushesEnforcementLevel *string `json:"allow_force_pushes_enforcement_level,omitempty"` 1062 AllowDeletionsEnforcementLevel *string `json:"allow_deletions_enforcement_level,omitempty"` 1063 MergeQueueEnforcementLevel *string `json:"merge_queue_enforcement_level,omitempty"` 1064 RequiredDeploymentsEnforcementLevel *string `json:"required_deployments_enforcement_level,omitempty"` 1065 RequiredConversationResolutionLevel *string `json:"required_conversation_resolution_level,omitempty"` 1066 AuthorizedActorsOnly *bool `json:"authorized_actors_only,omitempty"` 1067 AuthorizedActorNames []string `json:"authorized_actor_names,omitempty"` 1068 } 1069 1070 // ProtectionChanges represents the changes to the rule if the BranchProtection was edited. 1071 type ProtectionChanges struct { 1072 AdminEnforced *AdminEnforcedChanges `json:"admin_enforced,omitempty"` 1073 AllowDeletionsEnforcementLevel *AllowDeletionsEnforcementLevelChanges `json:"allow_deletions_enforcement_level,omitempty"` 1074 AuthorizedActorNames *AuthorizedActorNames `json:"authorized_actor_names,omitempty"` 1075 AuthorizedActorsOnly *AuthorizedActorsOnly `json:"authorized_actors_only,omitempty"` 1076 AuthorizedDismissalActorsOnly *AuthorizedDismissalActorsOnlyChanges `json:"authorized_dismissal_actors_only,omitempty"` 1077 CreateProtected *CreateProtectedChanges `json:"create_protected,omitempty"` 1078 DismissStaleReviewsOnPush *DismissStaleReviewsOnPushChanges `json:"dismiss_stale_reviews_on_push,omitempty"` 1079 LinearHistoryRequirementEnforcementLevel *LinearHistoryRequirementEnforcementLevelChanges `json:"linear_history_requirement_enforcement_level,omitempty"` 1080 PullRequestReviewsEnforcementLevel *PullRequestReviewsEnforcementLevelChanges `json:"pull_request_reviews_enforcement_level,omitempty"` 1081 RequireCodeOwnerReview *RequireCodeOwnerReviewChanges `json:"require_code_owner_review,omitempty"` 1082 RequiredConversationResolutionLevel *RequiredConversationResolutionLevelChanges `json:"required_conversation_resolution_level,omitempty"` 1083 RequiredDeploymentsEnforcementLevel *RequiredDeploymentsEnforcementLevelChanges `json:"required_deployments_enforcement_level,omitempty"` 1084 RequiredStatusChecks *RequiredStatusChecksChanges `json:"required_status_checks,omitempty"` 1085 RequiredStatusChecksEnforcementLevel *RequiredStatusChecksEnforcementLevelChanges `json:"required_status_checks_enforcement_level,omitempty"` 1086 SignatureRequirementEnforcementLevel *SignatureRequirementEnforcementLevelChanges `json:"signature_requirement_enforcement_level,omitempty"` 1087 } 1088 1089 // AdminEnforcedChanges represents the changes made to the AdminEnforced policy. 1090 type AdminEnforcedChanges struct { 1091 From *bool `json:"from,omitempty"` 1092 } 1093 1094 // AllowDeletionsEnforcementLevelChanges represents the changes made to the AllowDeletionsEnforcementLevel policy. 1095 type AllowDeletionsEnforcementLevelChanges struct { 1096 From *string `json:"from,omitempty"` 1097 } 1098 1099 // AuthorizedActorNames represents who are authorized to edit the branch protection rules. 1100 type AuthorizedActorNames struct { 1101 From []string `json:"from,omitempty"` 1102 } 1103 1104 // AuthorizedActorsOnly represents if the branch rule can be edited by authorized actors only. 1105 type AuthorizedActorsOnly struct { 1106 From *bool `json:"from,omitempty"` 1107 } 1108 1109 // AuthorizedDismissalActorsOnlyChanges represents the changes made to the AuthorizedDismissalActorsOnly policy. 1110 type AuthorizedDismissalActorsOnlyChanges struct { 1111 From *bool `json:"from,omitempty"` 1112 } 1113 1114 // CreateProtectedChanges represents the changes made to the CreateProtected policy. 1115 type CreateProtectedChanges struct { 1116 From *bool `json:"from,omitempty"` 1117 } 1118 1119 // DismissStaleReviewsOnPushChanges represents the changes made to the DismissStaleReviewsOnPushChanges policy. 1120 type DismissStaleReviewsOnPushChanges struct { 1121 From *bool `json:"from,omitempty"` 1122 } 1123 1124 // LinearHistoryRequirementEnforcementLevelChanges represents the changes made to the LinearHistoryRequirementEnforcementLevel policy. 1125 type LinearHistoryRequirementEnforcementLevelChanges struct { 1126 From *string `json:"from,omitempty"` 1127 } 1128 1129 // PullRequestReviewsEnforcementLevelChanges represents the changes made to the PullRequestReviewsEnforcementLevel policy. 1130 type PullRequestReviewsEnforcementLevelChanges struct { 1131 From *string `json:"from,omitempty"` 1132 } 1133 1134 // RequireCodeOwnerReviewChanges represents the changes made to the RequireCodeOwnerReview policy. 1135 type RequireCodeOwnerReviewChanges struct { 1136 From *bool `json:"from,omitempty"` 1137 } 1138 1139 // RequiredConversationResolutionLevelChanges represents the changes made to the RequiredConversationResolutionLevel policy. 1140 type RequiredConversationResolutionLevelChanges struct { 1141 From *string `json:"from,omitempty"` 1142 } 1143 1144 // RequiredDeploymentsEnforcementLevelChanges represents the changes made to the RequiredDeploymentsEnforcementLevel policy. 1145 type RequiredDeploymentsEnforcementLevelChanges struct { 1146 From *string `json:"from,omitempty"` 1147 } 1148 1149 // RequiredStatusChecksChanges represents the changes made to the RequiredStatusChecks policy. 1150 type RequiredStatusChecksChanges struct { 1151 From []string `json:"from,omitempty"` 1152 } 1153 1154 // RequiredStatusChecksEnforcementLevelChanges represents the changes made to the RequiredStatusChecksEnforcementLevel policy. 1155 type RequiredStatusChecksEnforcementLevelChanges struct { 1156 From *string `json:"from,omitempty"` 1157 } 1158 1159 // SignatureRequirementEnforcementLevelChanges represents the changes made to the SignatureRequirementEnforcementLevel policy. 1160 type SignatureRequirementEnforcementLevelChanges struct { 1161 From *string `json:"from,omitempty"` 1162 } 1163 1164 // ProtectionRequest represents a request to create/edit a branch's protection. 1165 type ProtectionRequest struct { 1166 RequiredStatusChecks *RequiredStatusChecks `json:"required_status_checks"` 1167 RequiredPullRequestReviews *PullRequestReviewsEnforcementRequest `json:"required_pull_request_reviews"` 1168 EnforceAdmins bool `json:"enforce_admins"` 1169 Restrictions *BranchRestrictionsRequest `json:"restrictions"` 1170 // Enforces a linear commit Git history, which prevents anyone from pushing merge commits to a branch. 1171 RequireLinearHistory *bool `json:"required_linear_history,omitempty"` 1172 // Permits force pushes to the protected branch by anyone with write access to the repository. 1173 AllowForcePushes *bool `json:"allow_force_pushes,omitempty"` 1174 // Allows deletion of the protected branch by anyone with write access to the repository. 1175 AllowDeletions *bool `json:"allow_deletions,omitempty"` 1176 // RequiredConversationResolution, if set to true, requires all comments 1177 // on the pull request to be resolved before it can be merged to a protected branch. 1178 RequiredConversationResolution *bool `json:"required_conversation_resolution,omitempty"` 1179 // BlockCreations, if set to true, will cause the restrictions setting to also block pushes 1180 // which create new branches, unless initiated by a user, team, app with the ability to push. 1181 BlockCreations *bool `json:"block_creations,omitempty"` 1182 // LockBranch, if set to true, will prevent users from pushing to the branch. 1183 LockBranch *bool `json:"lock_branch,omitempty"` 1184 // AllowForkSyncing, if set to true, will allow users to pull changes from upstream 1185 // when the branch is locked. 1186 AllowForkSyncing *bool `json:"allow_fork_syncing,omitempty"` 1187 } 1188 1189 // RequiredStatusChecks represents the protection status of a individual branch. 1190 type RequiredStatusChecks struct { 1191 // Require branches to be up to date before merging. (Required.) 1192 Strict bool `json:"strict"` 1193 // The list of status checks to require in order to merge into this 1194 // branch. An empty slice is valid. (Deprecated. Note: only one of 1195 // Contexts/Checks can be populated, but at least one must be populated). 1196 Contexts *[]string `json:"contexts,omitempty"` 1197 // The list of status checks to require in order to merge into this 1198 // branch. An empty slice is valid. 1199 Checks *[]*RequiredStatusCheck `json:"checks,omitempty"` 1200 ContextsURL *string `json:"contexts_url,omitempty"` 1201 URL *string `json:"url,omitempty"` 1202 } 1203 1204 // RequiredStatusChecksRequest represents a request to edit a protected branch's status checks. 1205 type RequiredStatusChecksRequest struct { 1206 Strict *bool `json:"strict,omitempty"` 1207 // Deprecated. Note: if both Contexts and Checks are populated, 1208 // the GitHub API will only use Checks. 1209 Contexts []string `json:"contexts,omitempty"` 1210 Checks []*RequiredStatusCheck `json:"checks,omitempty"` 1211 } 1212 1213 // RequiredStatusCheck represents a status check of a protected branch. 1214 type RequiredStatusCheck struct { 1215 // The name of the required check. 1216 Context string `json:"context"` 1217 // The ID of the GitHub App that must provide this check. 1218 // Omit this field to automatically select the GitHub App 1219 // that has recently provided this check, 1220 // or any app if it was not set by a GitHub App. 1221 // Pass -1 to explicitly allow any app to set the status. 1222 AppID *int64 `json:"app_id,omitempty"` 1223 } 1224 1225 // PullRequestReviewsEnforcement represents the pull request reviews enforcement of a protected branch. 1226 type PullRequestReviewsEnforcement struct { 1227 // Allow specific users, teams, or apps to bypass pull request requirements. 1228 BypassPullRequestAllowances *BypassPullRequestAllowances `json:"bypass_pull_request_allowances,omitempty"` 1229 // Specifies which users, teams and apps can dismiss pull request reviews. 1230 DismissalRestrictions *DismissalRestrictions `json:"dismissal_restrictions,omitempty"` 1231 // Specifies if approved reviews are dismissed automatically, when a new commit is pushed. 1232 DismissStaleReviews bool `json:"dismiss_stale_reviews"` 1233 // RequireCodeOwnerReviews specifies if an approved review is required in pull requests including files with a designated code owner. 1234 RequireCodeOwnerReviews bool `json:"require_code_owner_reviews"` 1235 // RequiredApprovingReviewCount specifies the number of approvals required before the pull request can be merged. 1236 // Valid values are 1-6. 1237 RequiredApprovingReviewCount int `json:"required_approving_review_count"` 1238 // RequireLastPushApproval specifies whether the last pusher to a pull request branch can approve it. 1239 RequireLastPushApproval bool `json:"require_last_push_approval"` 1240 } 1241 1242 // PullRequestReviewsEnforcementRequest represents request to set the pull request review 1243 // enforcement of a protected branch. It is separate from PullRequestReviewsEnforcement above 1244 // because the request structure is different from the response structure. 1245 type PullRequestReviewsEnforcementRequest struct { 1246 // Allow specific users, teams, or apps to bypass pull request requirements. 1247 BypassPullRequestAllowancesRequest *BypassPullRequestAllowancesRequest `json:"bypass_pull_request_allowances,omitempty"` 1248 // Specifies which users, teams and apps should be allowed to dismiss pull request reviews. 1249 // User, team and app dismissal restrictions are only available for 1250 // organization-owned repositories. Must be nil for personal repositories. 1251 DismissalRestrictionsRequest *DismissalRestrictionsRequest `json:"dismissal_restrictions,omitempty"` 1252 // Specifies if approved reviews can be dismissed automatically, when a new commit is pushed. (Required) 1253 DismissStaleReviews bool `json:"dismiss_stale_reviews"` 1254 // RequireCodeOwnerReviews specifies if an approved review is required in pull requests including files with a designated code owner. 1255 RequireCodeOwnerReviews bool `json:"require_code_owner_reviews"` 1256 // RequiredApprovingReviewCount specifies the number of approvals required before the pull request can be merged. 1257 // Valid values are 1-6. 1258 RequiredApprovingReviewCount int `json:"required_approving_review_count"` 1259 // RequireLastPushApproval specifies whether the last pusher to a pull request branch can approve it. 1260 RequireLastPushApproval *bool `json:"require_last_push_approval,omitempty"` 1261 } 1262 1263 // PullRequestReviewsEnforcementUpdate represents request to patch the pull request review 1264 // enforcement of a protected branch. It is separate from PullRequestReviewsEnforcementRequest above 1265 // because the patch request does not require all fields to be initialized. 1266 type PullRequestReviewsEnforcementUpdate struct { 1267 // Allow specific users, teams, or apps to bypass pull request requirements. 1268 BypassPullRequestAllowancesRequest *BypassPullRequestAllowancesRequest `json:"bypass_pull_request_allowances,omitempty"` 1269 // Specifies which users, teams and apps can dismiss pull request reviews. Can be omitted. 1270 DismissalRestrictionsRequest *DismissalRestrictionsRequest `json:"dismissal_restrictions,omitempty"` 1271 // Specifies if approved reviews can be dismissed automatically, when a new commit is pushed. Can be omitted. 1272 DismissStaleReviews *bool `json:"dismiss_stale_reviews,omitempty"` 1273 // RequireCodeOwnerReviews specifies if merging pull requests is blocked until code owners have reviewed. 1274 RequireCodeOwnerReviews *bool `json:"require_code_owner_reviews,omitempty"` 1275 // RequiredApprovingReviewCount specifies the number of approvals required before the pull request can be merged. 1276 // Valid values are 1 - 6 or 0 to not require reviewers. 1277 RequiredApprovingReviewCount int `json:"required_approving_review_count"` 1278 // RequireLastPushApproval specifies whether the last pusher to a pull request branch can approve it. 1279 RequireLastPushApproval *bool `json:"require_last_push_approval,omitempty"` 1280 } 1281 1282 // RequireLinearHistory represents the configuration to enforce branches with no merge commit. 1283 type RequireLinearHistory struct { 1284 Enabled bool `json:"enabled"` 1285 } 1286 1287 // AllowDeletions represents the configuration to accept deletion of protected branches. 1288 type AllowDeletions struct { 1289 Enabled bool `json:"enabled"` 1290 } 1291 1292 // AllowForcePushes represents the configuration to accept forced pushes on protected branches. 1293 type AllowForcePushes struct { 1294 Enabled bool `json:"enabled"` 1295 } 1296 1297 // RequiredConversationResolution requires all comments on the pull request to be resolved before it can be 1298 // merged to a protected branch when enabled. 1299 type RequiredConversationResolution struct { 1300 Enabled bool `json:"enabled"` 1301 } 1302 1303 // AdminEnforcement represents the configuration to enforce required status checks for repository administrators. 1304 type AdminEnforcement struct { 1305 URL *string `json:"url,omitempty"` 1306 Enabled bool `json:"enabled"` 1307 } 1308 1309 // BranchRestrictions represents the restriction that only certain users or 1310 // teams may push to a branch. 1311 type BranchRestrictions struct { 1312 // The list of user logins with push access. 1313 Users []*User `json:"users"` 1314 // The list of team slugs with push access. 1315 Teams []*Team `json:"teams"` 1316 // The list of app slugs with push access. 1317 Apps []*App `json:"apps"` 1318 } 1319 1320 // BranchRestrictionsRequest represents the request to create/edit the 1321 // restriction that only certain users or teams may push to a branch. It is 1322 // separate from BranchRestrictions above because the request structure is 1323 // different from the response structure. 1324 type BranchRestrictionsRequest struct { 1325 // The list of user logins with push access. (Required; use []string{} instead of nil for empty list.) 1326 Users []string `json:"users"` 1327 // The list of team slugs with push access. (Required; use []string{} instead of nil for empty list.) 1328 Teams []string `json:"teams"` 1329 // The list of app slugs with push access. 1330 Apps []string `json:"apps"` 1331 } 1332 1333 // BypassPullRequestAllowances represents the people, teams, or apps who are allowed to bypass required pull requests. 1334 type BypassPullRequestAllowances struct { 1335 // The list of users allowed to bypass pull request requirements. 1336 Users []*User `json:"users"` 1337 // The list of teams allowed to bypass pull request requirements. 1338 Teams []*Team `json:"teams"` 1339 // The list of apps allowed to bypass pull request requirements. 1340 Apps []*App `json:"apps"` 1341 } 1342 1343 // BypassPullRequestAllowancesRequest represents the people, teams, or apps who are 1344 // allowed to bypass required pull requests. 1345 // It is separate from BypassPullRequestAllowances above because the request structure is 1346 // different from the response structure. 1347 type BypassPullRequestAllowancesRequest struct { 1348 // The list of user logins allowed to bypass pull request requirements. 1349 Users []string `json:"users"` 1350 // The list of team slugs allowed to bypass pull request requirements. 1351 Teams []string `json:"teams"` 1352 // The list of app slugs allowed to bypass pull request requirements. 1353 Apps []string `json:"apps"` 1354 } 1355 1356 // DismissalRestrictions specifies which users and teams can dismiss pull request reviews. 1357 type DismissalRestrictions struct { 1358 // The list of users who can dimiss pull request reviews. 1359 Users []*User `json:"users"` 1360 // The list of teams which can dismiss pull request reviews. 1361 Teams []*Team `json:"teams"` 1362 // The list of apps which can dismiss pull request reviews. 1363 Apps []*App `json:"apps"` 1364 } 1365 1366 // DismissalRestrictionsRequest represents the request to create/edit the 1367 // restriction to allows only specific users, teams or apps to dimiss pull request reviews. It is 1368 // separate from DismissalRestrictions above because the request structure is 1369 // different from the response structure. 1370 // Note: Both Users and Teams must be nil, or both must be non-nil. 1371 type DismissalRestrictionsRequest struct { 1372 // The list of user logins who can dismiss pull request reviews. (Required; use nil to disable dismissal_restrictions or &[]string{} otherwise.) 1373 Users *[]string `json:"users,omitempty"` 1374 // The list of team slugs which can dismiss pull request reviews. (Required; use nil to disable dismissal_restrictions or &[]string{} otherwise.) 1375 Teams *[]string `json:"teams,omitempty"` 1376 // The list of app slugs which can dismiss pull request reviews. (Required; use nil to disable dismissal_restrictions or &[]string{} otherwise.) 1377 Apps *[]string `json:"apps,omitempty"` 1378 } 1379 1380 // SignaturesProtectedBranch represents the protection status of an individual branch. 1381 type SignaturesProtectedBranch struct { 1382 URL *string `json:"url,omitempty"` 1383 // Commits pushed to matching branches must have verified signatures. 1384 Enabled *bool `json:"enabled,omitempty"` 1385 } 1386 1387 // AutomatedSecurityFixes represents their status. 1388 type AutomatedSecurityFixes struct { 1389 Enabled *bool `json:"enabled"` 1390 Paused *bool `json:"paused"` 1391 } 1392 1393 // ListBranches lists branches for the specified repository. 1394 // 1395 // GitHub API docs: https://docs.github.com/rest/branches/branches#list-branches 1396 // 1397 //meta:operation GET /repos/{owner}/{repo}/branches 1398 func (s *RepositoriesService) ListBranches(ctx context.Context, owner string, repo string, opts *BranchListOptions) ([]*Branch, *Response, error) { 1399 u := fmt.Sprintf("repos/%v/%v/branches", owner, repo) 1400 u, err := addOptions(u, opts) 1401 if err != nil { 1402 return nil, nil, err 1403 } 1404 1405 req, err := s.client.NewRequest("GET", u, nil) 1406 if err != nil { 1407 return nil, nil, err 1408 } 1409 1410 var branches []*Branch 1411 resp, err := s.client.Do(ctx, req, &branches) 1412 if err != nil { 1413 return nil, resp, err 1414 } 1415 1416 return branches, resp, nil 1417 } 1418 1419 // GetBranch gets the specified branch for a repository. 1420 // 1421 // Note: the branch name is URL path escaped for you. See: https://pkg.go.dev/net/url#PathEscape . 1422 // 1423 // GitHub API docs: https://docs.github.com/rest/branches/branches#get-a-branch 1424 // 1425 //meta:operation GET /repos/{owner}/{repo}/branches/{branch} 1426 func (s *RepositoriesService) GetBranch(ctx context.Context, owner, repo, branch string, maxRedirects int) (*Branch, *Response, error) { 1427 u := fmt.Sprintf("repos/%v/%v/branches/%v", owner, repo, url.PathEscape(branch)) 1428 1429 resp, err := s.client.roundTripWithOptionalFollowRedirect(ctx, u, maxRedirects) 1430 if err != nil { 1431 return nil, nil, err 1432 } 1433 defer resp.Body.Close() 1434 1435 if resp.StatusCode != http.StatusOK { 1436 return nil, newResponse(resp), fmt.Errorf("unexpected status code: %s", resp.Status) 1437 } 1438 1439 b := new(Branch) 1440 err = json.NewDecoder(resp.Body).Decode(b) 1441 return b, newResponse(resp), err 1442 } 1443 1444 // renameBranchRequest represents a request to rename a branch. 1445 type renameBranchRequest struct { 1446 NewName string `json:"new_name"` 1447 } 1448 1449 // RenameBranch renames a branch in a repository. 1450 // 1451 // To rename a non-default branch: Users must have push access. GitHub Apps must have the `contents:write` repository permission. 1452 // To rename the default branch: Users must have admin or owner permissions. GitHub Apps must have the `administration:write` repository permission. 1453 // 1454 // Note: the branch name is URL path escaped for you. See: https://pkg.go.dev/net/url#PathEscape . 1455 // 1456 // GitHub API docs: https://docs.github.com/rest/branches/branches#rename-a-branch 1457 // 1458 //meta:operation POST /repos/{owner}/{repo}/branches/{branch}/rename 1459 func (s *RepositoriesService) RenameBranch(ctx context.Context, owner, repo, branch, newName string) (*Branch, *Response, error) { 1460 u := fmt.Sprintf("repos/%v/%v/branches/%v/rename", owner, repo, url.PathEscape(branch)) 1461 r := &renameBranchRequest{NewName: newName} 1462 req, err := s.client.NewRequest("POST", u, r) 1463 if err != nil { 1464 return nil, nil, err 1465 } 1466 1467 b := new(Branch) 1468 resp, err := s.client.Do(ctx, req, b) 1469 if err != nil { 1470 return nil, resp, err 1471 } 1472 1473 return b, resp, nil 1474 } 1475 1476 // GetBranchProtection gets the protection of a given branch. 1477 // 1478 // Note: the branch name is URL path escaped for you. See: https://pkg.go.dev/net/url#PathEscape . 1479 // 1480 // GitHub API docs: https://docs.github.com/rest/branches/branch-protection#get-branch-protection 1481 // 1482 //meta:operation GET /repos/{owner}/{repo}/branches/{branch}/protection 1483 func (s *RepositoriesService) GetBranchProtection(ctx context.Context, owner, repo, branch string) (*Protection, *Response, error) { 1484 u := fmt.Sprintf("repos/%v/%v/branches/%v/protection", owner, repo, url.PathEscape(branch)) 1485 req, err := s.client.NewRequest("GET", u, nil) 1486 if err != nil { 1487 return nil, nil, err 1488 } 1489 1490 // TODO: remove custom Accept header when this API fully launches 1491 req.Header.Set("Accept", mediaTypeRequiredApprovingReviewsPreview) 1492 1493 p := new(Protection) 1494 resp, err := s.client.Do(ctx, req, p) 1495 if err != nil { 1496 if isBranchNotProtected(err) { 1497 err = ErrBranchNotProtected 1498 } 1499 return nil, resp, err 1500 } 1501 1502 return p, resp, nil 1503 } 1504 1505 // GetRequiredStatusChecks gets the required status checks for a given protected branch. 1506 // 1507 // Note: the branch name is URL path escaped for you. See: https://pkg.go.dev/net/url#PathEscape . 1508 // 1509 // GitHub API docs: https://docs.github.com/rest/branches/branch-protection#get-status-checks-protection 1510 // 1511 //meta:operation GET /repos/{owner}/{repo}/branches/{branch}/protection/required_status_checks 1512 func (s *RepositoriesService) GetRequiredStatusChecks(ctx context.Context, owner, repo, branch string) (*RequiredStatusChecks, *Response, error) { 1513 u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/required_status_checks", owner, repo, url.PathEscape(branch)) 1514 req, err := s.client.NewRequest("GET", u, nil) 1515 if err != nil { 1516 return nil, nil, err 1517 } 1518 1519 p := new(RequiredStatusChecks) 1520 resp, err := s.client.Do(ctx, req, p) 1521 if err != nil { 1522 if isBranchNotProtected(err) { 1523 err = ErrBranchNotProtected 1524 } 1525 return nil, resp, err 1526 } 1527 1528 return p, resp, nil 1529 } 1530 1531 // ListRequiredStatusChecksContexts lists the required status checks contexts for a given protected branch. 1532 // 1533 // Note: the branch name is URL path escaped for you. See: https://pkg.go.dev/net/url#PathEscape . 1534 // 1535 // GitHub API docs: https://docs.github.com/rest/branches/branch-protection#get-all-status-check-contexts 1536 // 1537 //meta:operation GET /repos/{owner}/{repo}/branches/{branch}/protection/required_status_checks/contexts 1538 func (s *RepositoriesService) ListRequiredStatusChecksContexts(ctx context.Context, owner, repo, branch string) (contexts []string, resp *Response, err error) { 1539 u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/required_status_checks/contexts", owner, repo, url.PathEscape(branch)) 1540 req, err := s.client.NewRequest("GET", u, nil) 1541 if err != nil { 1542 return nil, nil, err 1543 } 1544 1545 resp, err = s.client.Do(ctx, req, &contexts) 1546 if err != nil { 1547 if isBranchNotProtected(err) { 1548 err = ErrBranchNotProtected 1549 } 1550 return nil, resp, err 1551 } 1552 1553 return contexts, resp, nil 1554 } 1555 1556 // UpdateBranchProtection updates the protection of a given branch. 1557 // 1558 // Note: the branch name is URL path escaped for you. See: https://pkg.go.dev/net/url#PathEscape . 1559 // 1560 // GitHub API docs: https://docs.github.com/rest/branches/branch-protection#update-branch-protection 1561 // 1562 //meta:operation PUT /repos/{owner}/{repo}/branches/{branch}/protection 1563 func (s *RepositoriesService) UpdateBranchProtection(ctx context.Context, owner, repo, branch string, preq *ProtectionRequest) (*Protection, *Response, error) { 1564 u := fmt.Sprintf("repos/%v/%v/branches/%v/protection", owner, repo, url.PathEscape(branch)) 1565 req, err := s.client.NewRequest("PUT", u, preq) 1566 if err != nil { 1567 return nil, nil, err 1568 } 1569 1570 // TODO: remove custom Accept header when this API fully launches 1571 req.Header.Set("Accept", mediaTypeRequiredApprovingReviewsPreview) 1572 1573 p := new(Protection) 1574 resp, err := s.client.Do(ctx, req, p) 1575 if err != nil { 1576 return nil, resp, err 1577 } 1578 1579 return p, resp, nil 1580 } 1581 1582 // RemoveBranchProtection removes the protection of a given branch. 1583 // 1584 // Note: the branch name is URL path escaped for you. See: https://pkg.go.dev/net/url#PathEscape . 1585 // 1586 // GitHub API docs: https://docs.github.com/rest/branches/branch-protection#delete-branch-protection 1587 // 1588 //meta:operation DELETE /repos/{owner}/{repo}/branches/{branch}/protection 1589 func (s *RepositoriesService) RemoveBranchProtection(ctx context.Context, owner, repo, branch string) (*Response, error) { 1590 u := fmt.Sprintf("repos/%v/%v/branches/%v/protection", owner, repo, url.PathEscape(branch)) 1591 req, err := s.client.NewRequest("DELETE", u, nil) 1592 if err != nil { 1593 return nil, err 1594 } 1595 1596 return s.client.Do(ctx, req, nil) 1597 } 1598 1599 // GetSignaturesProtectedBranch gets required signatures of protected branch. 1600 // 1601 // Note: the branch name is URL path escaped for you. See: https://pkg.go.dev/net/url#PathEscape . 1602 // 1603 // GitHub API docs: https://docs.github.com/rest/branches/branch-protection#get-commit-signature-protection 1604 // 1605 //meta:operation GET /repos/{owner}/{repo}/branches/{branch}/protection/required_signatures 1606 func (s *RepositoriesService) GetSignaturesProtectedBranch(ctx context.Context, owner, repo, branch string) (*SignaturesProtectedBranch, *Response, error) { 1607 u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/required_signatures", owner, repo, url.PathEscape(branch)) 1608 req, err := s.client.NewRequest("GET", u, nil) 1609 if err != nil { 1610 return nil, nil, err 1611 } 1612 1613 // TODO: remove custom Accept header when this API fully launches 1614 req.Header.Set("Accept", mediaTypeSignaturePreview) 1615 1616 p := new(SignaturesProtectedBranch) 1617 resp, err := s.client.Do(ctx, req, p) 1618 if err != nil { 1619 return nil, resp, err 1620 } 1621 1622 return p, resp, nil 1623 } 1624 1625 // RequireSignaturesOnProtectedBranch makes signed commits required on a protected branch. 1626 // It requires admin access and branch protection to be enabled. 1627 // 1628 // Note: the branch name is URL path escaped for you. See: https://pkg.go.dev/net/url#PathEscape . 1629 // 1630 // GitHub API docs: https://docs.github.com/rest/branches/branch-protection#create-commit-signature-protection 1631 // 1632 //meta:operation POST /repos/{owner}/{repo}/branches/{branch}/protection/required_signatures 1633 func (s *RepositoriesService) RequireSignaturesOnProtectedBranch(ctx context.Context, owner, repo, branch string) (*SignaturesProtectedBranch, *Response, error) { 1634 u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/required_signatures", owner, repo, url.PathEscape(branch)) 1635 req, err := s.client.NewRequest("POST", u, nil) 1636 if err != nil { 1637 return nil, nil, err 1638 } 1639 1640 // TODO: remove custom Accept header when this API fully launches 1641 req.Header.Set("Accept", mediaTypeSignaturePreview) 1642 1643 r := new(SignaturesProtectedBranch) 1644 resp, err := s.client.Do(ctx, req, r) 1645 if err != nil { 1646 return nil, resp, err 1647 } 1648 1649 return r, resp, nil 1650 } 1651 1652 // OptionalSignaturesOnProtectedBranch removes required signed commits on a given branch. 1653 // 1654 // Note: the branch name is URL path escaped for you. See: https://pkg.go.dev/net/url#PathEscape . 1655 // 1656 // GitHub API docs: https://docs.github.com/rest/branches/branch-protection#delete-commit-signature-protection 1657 // 1658 //meta:operation DELETE /repos/{owner}/{repo}/branches/{branch}/protection/required_signatures 1659 func (s *RepositoriesService) OptionalSignaturesOnProtectedBranch(ctx context.Context, owner, repo, branch string) (*Response, error) { 1660 u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/required_signatures", owner, repo, url.PathEscape(branch)) 1661 req, err := s.client.NewRequest("DELETE", u, nil) 1662 if err != nil { 1663 return nil, err 1664 } 1665 1666 // TODO: remove custom Accept header when this API fully launches 1667 req.Header.Set("Accept", mediaTypeSignaturePreview) 1668 1669 return s.client.Do(ctx, req, nil) 1670 } 1671 1672 // UpdateRequiredStatusChecks updates the required status checks for a given protected branch. 1673 // 1674 // Note: the branch name is URL path escaped for you. See: https://pkg.go.dev/net/url#PathEscape . 1675 // 1676 // GitHub API docs: https://docs.github.com/rest/branches/branch-protection#update-status-check-protection 1677 // 1678 //meta:operation PATCH /repos/{owner}/{repo}/branches/{branch}/protection/required_status_checks 1679 func (s *RepositoriesService) UpdateRequiredStatusChecks(ctx context.Context, owner, repo, branch string, sreq *RequiredStatusChecksRequest) (*RequiredStatusChecks, *Response, error) { 1680 u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/required_status_checks", owner, repo, url.PathEscape(branch)) 1681 req, err := s.client.NewRequest("PATCH", u, sreq) 1682 if err != nil { 1683 return nil, nil, err 1684 } 1685 1686 sc := new(RequiredStatusChecks) 1687 resp, err := s.client.Do(ctx, req, sc) 1688 if err != nil { 1689 return nil, resp, err 1690 } 1691 1692 return sc, resp, nil 1693 } 1694 1695 // RemoveRequiredStatusChecks removes the required status checks for a given protected branch. 1696 // 1697 // Note: the branch name is URL path escaped for you. See: https://pkg.go.dev/net/url#PathEscape . 1698 // 1699 // GitHub API docs: https://docs.github.com/rest/branches/branch-protection#remove-status-check-protection 1700 // 1701 //meta:operation DELETE /repos/{owner}/{repo}/branches/{branch}/protection/required_status_checks 1702 func (s *RepositoriesService) RemoveRequiredStatusChecks(ctx context.Context, owner, repo, branch string) (*Response, error) { 1703 u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/required_status_checks", owner, repo, url.PathEscape(branch)) 1704 req, err := s.client.NewRequest("DELETE", u, nil) 1705 if err != nil { 1706 return nil, err 1707 } 1708 1709 return s.client.Do(ctx, req, nil) 1710 } 1711 1712 // License gets the contents of a repository's license if one is detected. 1713 // 1714 // GitHub API docs: https://docs.github.com/rest/licenses/licenses#get-the-license-for-a-repository 1715 // 1716 //meta:operation GET /repos/{owner}/{repo}/license 1717 func (s *RepositoriesService) License(ctx context.Context, owner, repo string) (*RepositoryLicense, *Response, error) { 1718 u := fmt.Sprintf("repos/%v/%v/license", owner, repo) 1719 req, err := s.client.NewRequest("GET", u, nil) 1720 if err != nil { 1721 return nil, nil, err 1722 } 1723 1724 r := &RepositoryLicense{} 1725 resp, err := s.client.Do(ctx, req, r) 1726 if err != nil { 1727 return nil, resp, err 1728 } 1729 1730 return r, resp, nil 1731 } 1732 1733 // GetPullRequestReviewEnforcement gets pull request review enforcement of a protected branch. 1734 // 1735 // Note: the branch name is URL path escaped for you. See: https://pkg.go.dev/net/url#PathEscape . 1736 // 1737 // GitHub API docs: https://docs.github.com/rest/branches/branch-protection#get-pull-request-review-protection 1738 // 1739 //meta:operation GET /repos/{owner}/{repo}/branches/{branch}/protection/required_pull_request_reviews 1740 func (s *RepositoriesService) GetPullRequestReviewEnforcement(ctx context.Context, owner, repo, branch string) (*PullRequestReviewsEnforcement, *Response, error) { 1741 u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/required_pull_request_reviews", owner, repo, url.PathEscape(branch)) 1742 req, err := s.client.NewRequest("GET", u, nil) 1743 if err != nil { 1744 return nil, nil, err 1745 } 1746 1747 // TODO: remove custom Accept header when this API fully launches 1748 req.Header.Set("Accept", mediaTypeRequiredApprovingReviewsPreview) 1749 1750 r := new(PullRequestReviewsEnforcement) 1751 resp, err := s.client.Do(ctx, req, r) 1752 if err != nil { 1753 return nil, resp, err 1754 } 1755 1756 return r, resp, nil 1757 } 1758 1759 // UpdatePullRequestReviewEnforcement patches pull request review enforcement of a protected branch. 1760 // It requires admin access and branch protection to be enabled. 1761 // 1762 // Note: the branch name is URL path escaped for you. See: https://pkg.go.dev/net/url#PathEscape . 1763 // 1764 // GitHub API docs: https://docs.github.com/rest/branches/branch-protection#update-pull-request-review-protection 1765 // 1766 //meta:operation PATCH /repos/{owner}/{repo}/branches/{branch}/protection/required_pull_request_reviews 1767 func (s *RepositoriesService) UpdatePullRequestReviewEnforcement(ctx context.Context, owner, repo, branch string, patch *PullRequestReviewsEnforcementUpdate) (*PullRequestReviewsEnforcement, *Response, error) { 1768 u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/required_pull_request_reviews", owner, repo, url.PathEscape(branch)) 1769 req, err := s.client.NewRequest("PATCH", u, patch) 1770 if err != nil { 1771 return nil, nil, err 1772 } 1773 1774 // TODO: remove custom Accept header when this API fully launches 1775 req.Header.Set("Accept", mediaTypeRequiredApprovingReviewsPreview) 1776 1777 r := new(PullRequestReviewsEnforcement) 1778 resp, err := s.client.Do(ctx, req, r) 1779 if err != nil { 1780 return nil, resp, err 1781 } 1782 1783 return r, resp, nil 1784 } 1785 1786 // DisableDismissalRestrictions disables dismissal restrictions of a protected branch. 1787 // It requires admin access and branch protection to be enabled. 1788 // 1789 // Note: the branch name is URL path escaped for you. See: https://pkg.go.dev/net/url#PathEscape . 1790 // 1791 // GitHub API docs: https://docs.github.com/rest/branches/branch-protection#update-pull-request-review-protection 1792 // 1793 //meta:operation PATCH /repos/{owner}/{repo}/branches/{branch}/protection/required_pull_request_reviews 1794 func (s *RepositoriesService) DisableDismissalRestrictions(ctx context.Context, owner, repo, branch string) (*PullRequestReviewsEnforcement, *Response, error) { 1795 u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/required_pull_request_reviews", owner, repo, url.PathEscape(branch)) 1796 1797 data := new(struct { 1798 DismissalRestrictionsRequest `json:"dismissal_restrictions"` 1799 }) 1800 1801 req, err := s.client.NewRequest("PATCH", u, data) 1802 if err != nil { 1803 return nil, nil, err 1804 } 1805 1806 // TODO: remove custom Accept header when this API fully launches 1807 req.Header.Set("Accept", mediaTypeRequiredApprovingReviewsPreview) 1808 1809 r := new(PullRequestReviewsEnforcement) 1810 resp, err := s.client.Do(ctx, req, r) 1811 if err != nil { 1812 return nil, resp, err 1813 } 1814 1815 return r, resp, nil 1816 } 1817 1818 // RemovePullRequestReviewEnforcement removes pull request enforcement of a protected branch. 1819 // 1820 // Note: the branch name is URL path escaped for you. See: https://pkg.go.dev/net/url#PathEscape . 1821 // 1822 // GitHub API docs: https://docs.github.com/rest/branches/branch-protection#delete-pull-request-review-protection 1823 // 1824 //meta:operation DELETE /repos/{owner}/{repo}/branches/{branch}/protection/required_pull_request_reviews 1825 func (s *RepositoriesService) RemovePullRequestReviewEnforcement(ctx context.Context, owner, repo, branch string) (*Response, error) { 1826 u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/required_pull_request_reviews", owner, repo, url.PathEscape(branch)) 1827 req, err := s.client.NewRequest("DELETE", u, nil) 1828 if err != nil { 1829 return nil, err 1830 } 1831 1832 return s.client.Do(ctx, req, nil) 1833 } 1834 1835 // GetAdminEnforcement gets admin enforcement information of a protected branch. 1836 // 1837 // Note: the branch name is URL path escaped for you. See: https://pkg.go.dev/net/url#PathEscape . 1838 // 1839 // GitHub API docs: https://docs.github.com/rest/branches/branch-protection#get-admin-branch-protection 1840 // 1841 //meta:operation GET /repos/{owner}/{repo}/branches/{branch}/protection/enforce_admins 1842 func (s *RepositoriesService) GetAdminEnforcement(ctx context.Context, owner, repo, branch string) (*AdminEnforcement, *Response, error) { 1843 u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/enforce_admins", owner, repo, url.PathEscape(branch)) 1844 req, err := s.client.NewRequest("GET", u, nil) 1845 if err != nil { 1846 return nil, nil, err 1847 } 1848 1849 r := new(AdminEnforcement) 1850 resp, err := s.client.Do(ctx, req, r) 1851 if err != nil { 1852 return nil, resp, err 1853 } 1854 1855 return r, resp, nil 1856 } 1857 1858 // AddAdminEnforcement adds admin enforcement to a protected branch. 1859 // It requires admin access and branch protection to be enabled. 1860 // 1861 // Note: the branch name is URL path escaped for you. See: https://pkg.go.dev/net/url#PathEscape . 1862 // 1863 // GitHub API docs: https://docs.github.com/rest/branches/branch-protection#set-admin-branch-protection 1864 // 1865 //meta:operation POST /repos/{owner}/{repo}/branches/{branch}/protection/enforce_admins 1866 func (s *RepositoriesService) AddAdminEnforcement(ctx context.Context, owner, repo, branch string) (*AdminEnforcement, *Response, error) { 1867 u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/enforce_admins", owner, repo, url.PathEscape(branch)) 1868 req, err := s.client.NewRequest("POST", u, nil) 1869 if err != nil { 1870 return nil, nil, err 1871 } 1872 1873 r := new(AdminEnforcement) 1874 resp, err := s.client.Do(ctx, req, r) 1875 if err != nil { 1876 return nil, resp, err 1877 } 1878 1879 return r, resp, nil 1880 } 1881 1882 // RemoveAdminEnforcement removes admin enforcement from a protected branch. 1883 // 1884 // Note: the branch name is URL path escaped for you. See: https://pkg.go.dev/net/url#PathEscape . 1885 // 1886 // GitHub API docs: https://docs.github.com/rest/branches/branch-protection#delete-admin-branch-protection 1887 // 1888 //meta:operation DELETE /repos/{owner}/{repo}/branches/{branch}/protection/enforce_admins 1889 func (s *RepositoriesService) RemoveAdminEnforcement(ctx context.Context, owner, repo, branch string) (*Response, error) { 1890 u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/enforce_admins", owner, repo, url.PathEscape(branch)) 1891 req, err := s.client.NewRequest("DELETE", u, nil) 1892 if err != nil { 1893 return nil, err 1894 } 1895 1896 return s.client.Do(ctx, req, nil) 1897 } 1898 1899 // repositoryTopics represents a collection of repository topics. 1900 type repositoryTopics struct { 1901 Names []string `json:"names"` 1902 } 1903 1904 // ListAllTopics lists topics for a repository. 1905 // 1906 // GitHub API docs: https://docs.github.com/rest/repos/repos#get-all-repository-topics 1907 // 1908 //meta:operation GET /repos/{owner}/{repo}/topics 1909 func (s *RepositoriesService) ListAllTopics(ctx context.Context, owner, repo string) ([]string, *Response, error) { 1910 u := fmt.Sprintf("repos/%v/%v/topics", owner, repo) 1911 req, err := s.client.NewRequest("GET", u, nil) 1912 if err != nil { 1913 return nil, nil, err 1914 } 1915 1916 // TODO: remove custom Accept header when this API fully launches. 1917 req.Header.Set("Accept", mediaTypeTopicsPreview) 1918 1919 topics := new(repositoryTopics) 1920 resp, err := s.client.Do(ctx, req, topics) 1921 if err != nil { 1922 return nil, resp, err 1923 } 1924 1925 return topics.Names, resp, nil 1926 } 1927 1928 // ReplaceAllTopics replaces all repository topics. 1929 // 1930 // GitHub API docs: https://docs.github.com/rest/repos/repos#replace-all-repository-topics 1931 // 1932 //meta:operation PUT /repos/{owner}/{repo}/topics 1933 func (s *RepositoriesService) ReplaceAllTopics(ctx context.Context, owner, repo string, topics []string) ([]string, *Response, error) { 1934 u := fmt.Sprintf("repos/%v/%v/topics", owner, repo) 1935 t := &repositoryTopics{ 1936 Names: topics, 1937 } 1938 if t.Names == nil { 1939 t.Names = []string{} 1940 } 1941 req, err := s.client.NewRequest("PUT", u, t) 1942 if err != nil { 1943 return nil, nil, err 1944 } 1945 1946 // TODO: remove custom Accept header when this API fully launches. 1947 req.Header.Set("Accept", mediaTypeTopicsPreview) 1948 1949 t = new(repositoryTopics) 1950 resp, err := s.client.Do(ctx, req, t) 1951 if err != nil { 1952 return nil, resp, err 1953 } 1954 1955 return t.Names, resp, nil 1956 } 1957 1958 // ListApps lists the GitHub apps that have push access to a given protected branch. 1959 // It requires the GitHub apps to have `write` access to the `content` permission. 1960 // 1961 // Note: the branch name is URL path escaped for you. See: https://pkg.go.dev/net/url#PathEscape . 1962 // 1963 // Deprecated: Please use ListAppRestrictions instead. 1964 // 1965 // GitHub API docs: https://docs.github.com/rest/branches/branch-protection#get-apps-with-access-to-the-protected-branch 1966 // 1967 //meta:operation GET /repos/{owner}/{repo}/branches/{branch}/protection/restrictions/apps 1968 func (s *RepositoriesService) ListApps(ctx context.Context, owner, repo, branch string) ([]*App, *Response, error) { 1969 u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/restrictions/apps", owner, repo, url.PathEscape(branch)) 1970 req, err := s.client.NewRequest("GET", u, nil) 1971 if err != nil { 1972 return nil, nil, err 1973 } 1974 1975 var apps []*App 1976 resp, err := s.client.Do(ctx, req, &apps) 1977 if err != nil { 1978 return nil, resp, err 1979 } 1980 1981 return apps, resp, nil 1982 } 1983 1984 // ListAppRestrictions lists the GitHub apps that have push access to a given protected branch. 1985 // It requires the GitHub apps to have `write` access to the `content` permission. 1986 // 1987 // Note: This is a wrapper around ListApps so a naming convention with ListUserRestrictions and ListTeamRestrictions is preserved. 1988 // 1989 // GitHub API docs: https://docs.github.com/rest/branches/branch-protection#get-apps-with-access-to-the-protected-branch 1990 // 1991 //meta:operation GET /repos/{owner}/{repo}/branches/{branch}/protection/restrictions/apps 1992 func (s *RepositoriesService) ListAppRestrictions(ctx context.Context, owner, repo, branch string) ([]*App, *Response, error) { 1993 return s.ListApps(ctx, owner, repo, branch) 1994 } 1995 1996 // ReplaceAppRestrictions replaces the apps that have push access to a given protected branch. 1997 // It removes all apps that previously had push access and grants push access to the new list of apps. 1998 // It requires the GitHub apps to have `write` access to the `content` permission. 1999 // 2000 // Note: The list of users, apps, and teams in total is limited to 100 items. 2001 // 2002 // Note: the branch name is URL path escaped for you. See: https://pkg.go.dev/net/url#PathEscape . 2003 // 2004 // GitHub API docs: https://docs.github.com/rest/branches/branch-protection#set-app-access-restrictions 2005 // 2006 //meta:operation PUT /repos/{owner}/{repo}/branches/{branch}/protection/restrictions/apps 2007 func (s *RepositoriesService) ReplaceAppRestrictions(ctx context.Context, owner, repo, branch string, apps []string) ([]*App, *Response, error) { 2008 u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/restrictions/apps", owner, repo, url.PathEscape(branch)) 2009 req, err := s.client.NewRequest("PUT", u, apps) 2010 if err != nil { 2011 return nil, nil, err 2012 } 2013 2014 var newApps []*App 2015 resp, err := s.client.Do(ctx, req, &newApps) 2016 if err != nil { 2017 return nil, resp, err 2018 } 2019 2020 return newApps, resp, nil 2021 } 2022 2023 // AddAppRestrictions grants the specified apps push access to a given protected branch. 2024 // It requires the GitHub apps to have `write` access to the `content` permission. 2025 // 2026 // Note: The list of users, apps, and teams in total is limited to 100 items. 2027 // 2028 // Note: the branch name is URL path escaped for you. See: https://pkg.go.dev/net/url#PathEscape . 2029 // 2030 // GitHub API docs: https://docs.github.com/rest/branches/branch-protection#add-app-access-restrictions 2031 // 2032 //meta:operation POST /repos/{owner}/{repo}/branches/{branch}/protection/restrictions/apps 2033 func (s *RepositoriesService) AddAppRestrictions(ctx context.Context, owner, repo, branch string, apps []string) ([]*App, *Response, error) { 2034 u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/restrictions/apps", owner, repo, url.PathEscape(branch)) 2035 req, err := s.client.NewRequest("POST", u, apps) 2036 if err != nil { 2037 return nil, nil, err 2038 } 2039 2040 var newApps []*App 2041 resp, err := s.client.Do(ctx, req, &newApps) 2042 if err != nil { 2043 return nil, resp, err 2044 } 2045 2046 return newApps, resp, nil 2047 } 2048 2049 // RemoveAppRestrictions removes the restrictions of an app from pushing to this branch. 2050 // It requires the GitHub apps to have `write` access to the `content` permission. 2051 // 2052 // Note: The list of users, apps, and teams in total is limited to 100 items. 2053 // 2054 // Note: the branch name is URL path escaped for you. See: https://pkg.go.dev/net/url#PathEscape . 2055 // 2056 // GitHub API docs: https://docs.github.com/rest/branches/branch-protection#remove-app-access-restrictions 2057 // 2058 //meta:operation DELETE /repos/{owner}/{repo}/branches/{branch}/protection/restrictions/apps 2059 func (s *RepositoriesService) RemoveAppRestrictions(ctx context.Context, owner, repo, branch string, apps []string) ([]*App, *Response, error) { 2060 u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/restrictions/apps", owner, repo, url.PathEscape(branch)) 2061 req, err := s.client.NewRequest("DELETE", u, apps) 2062 if err != nil { 2063 return nil, nil, err 2064 } 2065 2066 var newApps []*App 2067 resp, err := s.client.Do(ctx, req, &newApps) 2068 if err != nil { 2069 return nil, resp, err 2070 } 2071 2072 return newApps, resp, nil 2073 } 2074 2075 // ListTeamRestrictions lists the GitHub teams that have push access to a given protected branch. 2076 // It requires the GitHub teams to have `write` access to the `content` permission. 2077 // 2078 // Note: the branch name is URL path escaped for you. See: https://pkg.go.dev/net/url#PathEscape . 2079 // 2080 // GitHub API docs: https://docs.github.com/rest/branches/branch-protection#get-teams-with-access-to-the-protected-branch 2081 // 2082 //meta:operation GET /repos/{owner}/{repo}/branches/{branch}/protection/restrictions/teams 2083 func (s *RepositoriesService) ListTeamRestrictions(ctx context.Context, owner, repo, branch string) ([]*Team, *Response, error) { 2084 u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/restrictions/teams", owner, repo, url.PathEscape(branch)) 2085 req, err := s.client.NewRequest("GET", u, nil) 2086 if err != nil { 2087 return nil, nil, err 2088 } 2089 2090 var teams []*Team 2091 resp, err := s.client.Do(ctx, req, &teams) 2092 if err != nil { 2093 return nil, resp, err 2094 } 2095 2096 return teams, resp, nil 2097 } 2098 2099 // ReplaceTeamRestrictions replaces the team that have push access to a given protected branch. 2100 // This removes all teams that previously had push access and grants push access to the new list of teams. 2101 // It requires the GitHub teams to have `write` access to the `content` permission. 2102 // 2103 // Note: The list of users, apps, and teams in total is limited to 100 items. 2104 // 2105 // Note: the branch name is URL path escaped for you. See: https://pkg.go.dev/net/url#PathEscape . 2106 // 2107 // GitHub API docs: https://docs.github.com/rest/branches/branch-protection#set-team-access-restrictions 2108 // 2109 //meta:operation PUT /repos/{owner}/{repo}/branches/{branch}/protection/restrictions/teams 2110 func (s *RepositoriesService) ReplaceTeamRestrictions(ctx context.Context, owner, repo, branch string, teams []string) ([]*Team, *Response, error) { 2111 u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/restrictions/teams", owner, repo, url.PathEscape(branch)) 2112 req, err := s.client.NewRequest("PUT", u, teams) 2113 if err != nil { 2114 return nil, nil, err 2115 } 2116 2117 var newTeams []*Team 2118 resp, err := s.client.Do(ctx, req, &newTeams) 2119 if err != nil { 2120 return nil, resp, err 2121 } 2122 2123 return newTeams, resp, nil 2124 } 2125 2126 // AddTeamRestrictions grants the specified teams push access to a given protected branch. 2127 // It requires the GitHub teams to have `write` access to the `content` permission. 2128 // 2129 // Note: The list of users, apps, and teams in total is limited to 100 items. 2130 // 2131 // Note: the branch name is URL path escaped for you. See: https://pkg.go.dev/net/url#PathEscape . 2132 // 2133 // GitHub API docs: https://docs.github.com/rest/branches/branch-protection#add-team-access-restrictions 2134 // 2135 //meta:operation POST /repos/{owner}/{repo}/branches/{branch}/protection/restrictions/teams 2136 func (s *RepositoriesService) AddTeamRestrictions(ctx context.Context, owner, repo, branch string, teams []string) ([]*Team, *Response, error) { 2137 u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/restrictions/teams", owner, repo, url.PathEscape(branch)) 2138 req, err := s.client.NewRequest("POST", u, teams) 2139 if err != nil { 2140 return nil, nil, err 2141 } 2142 2143 var newTeams []*Team 2144 resp, err := s.client.Do(ctx, req, &newTeams) 2145 if err != nil { 2146 return nil, resp, err 2147 } 2148 2149 return newTeams, resp, nil 2150 } 2151 2152 // RemoveTeamRestrictions removes the restrictions of a team from pushing to this branch. 2153 // It requires the GitHub teams to have `write` access to the `content` permission. 2154 // 2155 // Note: The list of users, apps, and teams in total is limited to 100 items. 2156 // 2157 // Note: the branch name is URL path escaped for you. See: https://pkg.go.dev/net/url#PathEscape . 2158 // 2159 // GitHub API docs: https://docs.github.com/rest/branches/branch-protection#remove-team-access-restrictions 2160 // 2161 //meta:operation DELETE /repos/{owner}/{repo}/branches/{branch}/protection/restrictions/teams 2162 func (s *RepositoriesService) RemoveTeamRestrictions(ctx context.Context, owner, repo, branch string, teams []string) ([]*Team, *Response, error) { 2163 u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/restrictions/teams", owner, repo, url.PathEscape(branch)) 2164 req, err := s.client.NewRequest("DELETE", u, teams) 2165 if err != nil { 2166 return nil, nil, err 2167 } 2168 2169 var newTeams []*Team 2170 resp, err := s.client.Do(ctx, req, &newTeams) 2171 if err != nil { 2172 return nil, resp, err 2173 } 2174 2175 return newTeams, resp, nil 2176 } 2177 2178 // ListUserRestrictions lists the GitHub users that have push access to a given protected branch. 2179 // It requires the GitHub users to have `write` access to the `content` permission. 2180 // 2181 // Note: the branch name is URL path escaped for you. See: https://pkg.go.dev/net/url#PathEscape . 2182 // 2183 // GitHub API docs: https://docs.github.com/rest/branches/branch-protection#get-users-with-access-to-the-protected-branch 2184 // 2185 //meta:operation GET /repos/{owner}/{repo}/branches/{branch}/protection/restrictions/users 2186 func (s *RepositoriesService) ListUserRestrictions(ctx context.Context, owner, repo, branch string) ([]*User, *Response, error) { 2187 u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/restrictions/users", owner, repo, url.PathEscape(branch)) 2188 req, err := s.client.NewRequest("GET", u, nil) 2189 if err != nil { 2190 return nil, nil, err 2191 } 2192 2193 var users []*User 2194 resp, err := s.client.Do(ctx, req, &users) 2195 if err != nil { 2196 return nil, resp, err 2197 } 2198 2199 return users, resp, nil 2200 } 2201 2202 // ReplaceUserRestrictions replaces the user that have push access to a given protected branch. 2203 // It removes all users that previously had push access and grants push access to the new list of users. 2204 // It requires the GitHub users to have `write` access to the `content` permission. 2205 // 2206 // Note: The list of users, apps, and teams in total is limited to 100 items. 2207 // 2208 // Note: the branch name is URL path escaped for you. See: https://pkg.go.dev/net/url#PathEscape . 2209 // 2210 // GitHub API docs: https://docs.github.com/rest/branches/branch-protection#set-user-access-restrictions 2211 // 2212 //meta:operation PUT /repos/{owner}/{repo}/branches/{branch}/protection/restrictions/users 2213 func (s *RepositoriesService) ReplaceUserRestrictions(ctx context.Context, owner, repo, branch string, users []string) ([]*User, *Response, error) { 2214 u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/restrictions/users", owner, repo, url.PathEscape(branch)) 2215 req, err := s.client.NewRequest("PUT", u, users) 2216 if err != nil { 2217 return nil, nil, err 2218 } 2219 2220 var newUsers []*User 2221 resp, err := s.client.Do(ctx, req, &newUsers) 2222 if err != nil { 2223 return nil, resp, err 2224 } 2225 2226 return newUsers, resp, nil 2227 } 2228 2229 // AddUserRestrictions grants the specified users push access to a given protected branch. 2230 // It requires the GitHub users to have `write` access to the `content` permission. 2231 // 2232 // Note: The list of users, apps, and teams in total is limited to 100 items. 2233 // 2234 // Note: the branch name is URL path escaped for you. See: https://pkg.go.dev/net/url#PathEscape . 2235 // 2236 // GitHub API docs: https://docs.github.com/rest/branches/branch-protection#add-user-access-restrictions 2237 // 2238 //meta:operation POST /repos/{owner}/{repo}/branches/{branch}/protection/restrictions/users 2239 func (s *RepositoriesService) AddUserRestrictions(ctx context.Context, owner, repo, branch string, users []string) ([]*User, *Response, error) { 2240 u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/restrictions/users", owner, repo, url.PathEscape(branch)) 2241 req, err := s.client.NewRequest("POST", u, users) 2242 if err != nil { 2243 return nil, nil, err 2244 } 2245 2246 var newUsers []*User 2247 resp, err := s.client.Do(ctx, req, &newUsers) 2248 if err != nil { 2249 return nil, resp, err 2250 } 2251 2252 return newUsers, resp, nil 2253 } 2254 2255 // RemoveUserRestrictions removes the restrictions of a user from pushing to this branch. 2256 // It requires the GitHub users to have `write` access to the `content` permission. 2257 // 2258 // Note: The list of users, apps, and teams in total is limited to 100 items. 2259 // 2260 // Note: the branch name is URL path escaped for you. See: https://pkg.go.dev/net/url#PathEscape . 2261 // 2262 // GitHub API docs: https://docs.github.com/rest/branches/branch-protection#remove-user-access-restrictions 2263 // 2264 //meta:operation DELETE /repos/{owner}/{repo}/branches/{branch}/protection/restrictions/users 2265 func (s *RepositoriesService) RemoveUserRestrictions(ctx context.Context, owner, repo, branch string, users []string) ([]*User, *Response, error) { 2266 u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/restrictions/users", owner, repo, url.PathEscape(branch)) 2267 req, err := s.client.NewRequest("DELETE", u, users) 2268 if err != nil { 2269 return nil, nil, err 2270 } 2271 2272 var newUsers []*User 2273 resp, err := s.client.Do(ctx, req, &newUsers) 2274 if err != nil { 2275 return nil, resp, err 2276 } 2277 2278 return newUsers, resp, nil 2279 } 2280 2281 // TransferRequest represents a request to transfer a repository. 2282 type TransferRequest struct { 2283 NewOwner string `json:"new_owner"` 2284 NewName *string `json:"new_name,omitempty"` 2285 TeamID []int64 `json:"team_ids,omitempty"` 2286 } 2287 2288 // Transfer transfers a repository from one account or organization to another. 2289 // 2290 // This method might return an *AcceptedError and a status code of 2291 // 202. This is because this is the status that GitHub returns to signify that 2292 // it has now scheduled the transfer of the repository in a background task. 2293 // A follow up request, after a delay of a second or so, should result 2294 // in a successful request. 2295 // 2296 // GitHub API docs: https://docs.github.com/rest/repos/repos#transfer-a-repository 2297 // 2298 //meta:operation POST /repos/{owner}/{repo}/transfer 2299 func (s *RepositoriesService) Transfer(ctx context.Context, owner, repo string, transfer TransferRequest) (*Repository, *Response, error) { 2300 u := fmt.Sprintf("repos/%v/%v/transfer", owner, repo) 2301 2302 req, err := s.client.NewRequest("POST", u, &transfer) 2303 if err != nil { 2304 return nil, nil, err 2305 } 2306 2307 r := new(Repository) 2308 resp, err := s.client.Do(ctx, req, r) 2309 if err != nil { 2310 return nil, resp, err 2311 } 2312 2313 return r, resp, nil 2314 } 2315 2316 // DispatchRequestOptions represents a request to trigger a repository_dispatch event. 2317 type DispatchRequestOptions struct { 2318 // EventType is a custom webhook event name. (Required.) 2319 EventType string `json:"event_type"` 2320 // ClientPayload is a custom JSON payload with extra information about the webhook event. 2321 // Defaults to an empty JSON object. 2322 ClientPayload *json.RawMessage `json:"client_payload,omitempty"` 2323 } 2324 2325 // Dispatch triggers a repository_dispatch event in a GitHub Actions workflow. 2326 // 2327 // GitHub API docs: https://docs.github.com/rest/repos/repos#create-a-repository-dispatch-event 2328 // 2329 //meta:operation POST /repos/{owner}/{repo}/dispatches 2330 func (s *RepositoriesService) Dispatch(ctx context.Context, owner, repo string, opts DispatchRequestOptions) (*Repository, *Response, error) { 2331 u := fmt.Sprintf("repos/%v/%v/dispatches", owner, repo) 2332 2333 req, err := s.client.NewRequest("POST", u, &opts) 2334 if err != nil { 2335 return nil, nil, err 2336 } 2337 2338 r := new(Repository) 2339 resp, err := s.client.Do(ctx, req, r) 2340 if err != nil { 2341 return nil, resp, err 2342 } 2343 2344 return r, resp, nil 2345 } 2346 2347 // isBranchNotProtected determines whether a branch is not protected 2348 // based on the error message returned by GitHub API. 2349 func isBranchNotProtected(err error) bool { 2350 errorResponse, ok := err.(*ErrorResponse) 2351 return ok && errorResponse.Message == githubBranchNotProtected 2352 } 2353 2354 // EnablePrivateReporting enables private reporting of vulnerabilities for a 2355 // repository. 2356 // 2357 // GitHub API docs: https://docs.github.com/rest/repos/repos#enable-private-vulnerability-reporting-for-a-repository 2358 // 2359 //meta:operation PUT /repos/{owner}/{repo}/private-vulnerability-reporting 2360 func (s *RepositoriesService) EnablePrivateReporting(ctx context.Context, owner, repo string) (*Response, error) { 2361 u := fmt.Sprintf("repos/%v/%v/private-vulnerability-reporting", owner, repo) 2362 2363 req, err := s.client.NewRequest("PUT", u, nil) 2364 if err != nil { 2365 return nil, err 2366 } 2367 2368 resp, err := s.client.Do(ctx, req, nil) 2369 if err != nil { 2370 return resp, err 2371 } 2372 2373 return resp, nil 2374 } 2375 2376 // DisablePrivateReporting disables private reporting of vulnerabilities for a 2377 // repository. 2378 // 2379 // GitHub API docs: https://docs.github.com/rest/repos/repos#disable-private-vulnerability-reporting-for-a-repository 2380 // 2381 //meta:operation DELETE /repos/{owner}/{repo}/private-vulnerability-reporting 2382 func (s *RepositoriesService) DisablePrivateReporting(ctx context.Context, owner, repo string) (*Response, error) { 2383 u := fmt.Sprintf("repos/%v/%v/private-vulnerability-reporting", owner, repo) 2384 2385 req, err := s.client.NewRequest("DELETE", u, nil) 2386 if err != nil { 2387 return nil, err 2388 } 2389 2390 resp, err := s.client.Do(ctx, req, nil) 2391 if err != nil { 2392 return resp, err 2393 } 2394 2395 return resp, nil 2396 }