...

Source file src/github.com/google/go-github/v33/github/repos.go

Documentation: github.com/google/go-github/v33/github

     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  	"fmt"
    12  	"strings"
    13  )
    14  
    15  // RepositoriesService handles communication with the repository related
    16  // methods of the GitHub API.
    17  //
    18  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/
    19  type RepositoriesService service
    20  
    21  // Repository represents a GitHub repository.
    22  type Repository struct {
    23  	ID                  *int64           `json:"id,omitempty"`
    24  	NodeID              *string          `json:"node_id,omitempty"`
    25  	Owner               *User            `json:"owner,omitempty"`
    26  	Name                *string          `json:"name,omitempty"`
    27  	FullName            *string          `json:"full_name,omitempty"`
    28  	Description         *string          `json:"description,omitempty"`
    29  	Homepage            *string          `json:"homepage,omitempty"`
    30  	CodeOfConduct       *CodeOfConduct   `json:"code_of_conduct,omitempty"`
    31  	DefaultBranch       *string          `json:"default_branch,omitempty"`
    32  	MasterBranch        *string          `json:"master_branch,omitempty"`
    33  	CreatedAt           *Timestamp       `json:"created_at,omitempty"`
    34  	PushedAt            *Timestamp       `json:"pushed_at,omitempty"`
    35  	UpdatedAt           *Timestamp       `json:"updated_at,omitempty"`
    36  	HTMLURL             *string          `json:"html_url,omitempty"`
    37  	CloneURL            *string          `json:"clone_url,omitempty"`
    38  	GitURL              *string          `json:"git_url,omitempty"`
    39  	MirrorURL           *string          `json:"mirror_url,omitempty"`
    40  	SSHURL              *string          `json:"ssh_url,omitempty"`
    41  	SVNURL              *string          `json:"svn_url,omitempty"`
    42  	Language            *string          `json:"language,omitempty"`
    43  	Fork                *bool            `json:"fork,omitempty"`
    44  	ForksCount          *int             `json:"forks_count,omitempty"`
    45  	NetworkCount        *int             `json:"network_count,omitempty"`
    46  	OpenIssuesCount     *int             `json:"open_issues_count,omitempty"`
    47  	StargazersCount     *int             `json:"stargazers_count,omitempty"`
    48  	SubscribersCount    *int             `json:"subscribers_count,omitempty"`
    49  	WatchersCount       *int             `json:"watchers_count,omitempty"`
    50  	Size                *int             `json:"size,omitempty"`
    51  	AutoInit            *bool            `json:"auto_init,omitempty"`
    52  	Parent              *Repository      `json:"parent,omitempty"`
    53  	Source              *Repository      `json:"source,omitempty"`
    54  	TemplateRepository  *Repository      `json:"template_repository,omitempty"`
    55  	Organization        *Organization    `json:"organization,omitempty"`
    56  	Permissions         *map[string]bool `json:"permissions,omitempty"`
    57  	AllowRebaseMerge    *bool            `json:"allow_rebase_merge,omitempty"`
    58  	AllowSquashMerge    *bool            `json:"allow_squash_merge,omitempty"`
    59  	AllowMergeCommit    *bool            `json:"allow_merge_commit,omitempty"`
    60  	DeleteBranchOnMerge *bool            `json:"delete_branch_on_merge,omitempty"`
    61  	Topics              []string         `json:"topics,omitempty"`
    62  	Archived            *bool            `json:"archived,omitempty"`
    63  	Disabled            *bool            `json:"disabled,omitempty"`
    64  
    65  	// Only provided when using RepositoriesService.Get while in preview
    66  	License *License `json:"license,omitempty"`
    67  
    68  	// Additional mutable fields when creating and editing a repository
    69  	Private           *bool   `json:"private,omitempty"`
    70  	HasIssues         *bool   `json:"has_issues,omitempty"`
    71  	HasWiki           *bool   `json:"has_wiki,omitempty"`
    72  	HasPages          *bool   `json:"has_pages,omitempty"`
    73  	HasProjects       *bool   `json:"has_projects,omitempty"`
    74  	HasDownloads      *bool   `json:"has_downloads,omitempty"`
    75  	IsTemplate        *bool   `json:"is_template,omitempty"`
    76  	LicenseTemplate   *string `json:"license_template,omitempty"`
    77  	GitignoreTemplate *string `json:"gitignore_template,omitempty"`
    78  
    79  	// Creating an organization repository. Required for non-owners.
    80  	TeamID *int64 `json:"team_id,omitempty"`
    81  
    82  	// API URLs
    83  	URL              *string `json:"url,omitempty"`
    84  	ArchiveURL       *string `json:"archive_url,omitempty"`
    85  	AssigneesURL     *string `json:"assignees_url,omitempty"`
    86  	BlobsURL         *string `json:"blobs_url,omitempty"`
    87  	BranchesURL      *string `json:"branches_url,omitempty"`
    88  	CollaboratorsURL *string `json:"collaborators_url,omitempty"`
    89  	CommentsURL      *string `json:"comments_url,omitempty"`
    90  	CommitsURL       *string `json:"commits_url,omitempty"`
    91  	CompareURL       *string `json:"compare_url,omitempty"`
    92  	ContentsURL      *string `json:"contents_url,omitempty"`
    93  	ContributorsURL  *string `json:"contributors_url,omitempty"`
    94  	DeploymentsURL   *string `json:"deployments_url,omitempty"`
    95  	DownloadsURL     *string `json:"downloads_url,omitempty"`
    96  	EventsURL        *string `json:"events_url,omitempty"`
    97  	ForksURL         *string `json:"forks_url,omitempty"`
    98  	GitCommitsURL    *string `json:"git_commits_url,omitempty"`
    99  	GitRefsURL       *string `json:"git_refs_url,omitempty"`
   100  	GitTagsURL       *string `json:"git_tags_url,omitempty"`
   101  	HooksURL         *string `json:"hooks_url,omitempty"`
   102  	IssueCommentURL  *string `json:"issue_comment_url,omitempty"`
   103  	IssueEventsURL   *string `json:"issue_events_url,omitempty"`
   104  	IssuesURL        *string `json:"issues_url,omitempty"`
   105  	KeysURL          *string `json:"keys_url,omitempty"`
   106  	LabelsURL        *string `json:"labels_url,omitempty"`
   107  	LanguagesURL     *string `json:"languages_url,omitempty"`
   108  	MergesURL        *string `json:"merges_url,omitempty"`
   109  	MilestonesURL    *string `json:"milestones_url,omitempty"`
   110  	NotificationsURL *string `json:"notifications_url,omitempty"`
   111  	PullsURL         *string `json:"pulls_url,omitempty"`
   112  	ReleasesURL      *string `json:"releases_url,omitempty"`
   113  	StargazersURL    *string `json:"stargazers_url,omitempty"`
   114  	StatusesURL      *string `json:"statuses_url,omitempty"`
   115  	SubscribersURL   *string `json:"subscribers_url,omitempty"`
   116  	SubscriptionURL  *string `json:"subscription_url,omitempty"`
   117  	TagsURL          *string `json:"tags_url,omitempty"`
   118  	TreesURL         *string `json:"trees_url,omitempty"`
   119  	TeamsURL         *string `json:"teams_url,omitempty"`
   120  
   121  	// TextMatches is only populated from search results that request text matches
   122  	// See: search.go and https://docs.github.com/en/free-pro-team@latest/rest/reference/search/#text-match-metadata
   123  	TextMatches []*TextMatch `json:"text_matches,omitempty"`
   124  
   125  	// Visibility is only used for Create and Edit endpoints. The visibility field
   126  	// overrides the field parameter when both are used.
   127  	// Can be one of public, private or internal.
   128  	Visibility *string `json:"visibility,omitempty"`
   129  }
   130  
   131  func (r Repository) String() string {
   132  	return Stringify(r)
   133  }
   134  
   135  // BranchListOptions specifies the optional parameters to the
   136  // RepositoriesService.ListBranches method.
   137  type BranchListOptions struct {
   138  	// Setting to true returns only protected branches.
   139  	// When set to false, only unprotected branches are returned.
   140  	// Omitting this parameter returns all branches.
   141  	// Default: nil
   142  	Protected *bool `url:"protected,omitempty"`
   143  
   144  	ListOptions
   145  }
   146  
   147  // RepositoryListOptions specifies the optional parameters to the
   148  // RepositoriesService.List method.
   149  type RepositoryListOptions struct {
   150  	// Visibility of repositories to list. Can be one of all, public, or private.
   151  	// Default: all
   152  	Visibility string `url:"visibility,omitempty"`
   153  
   154  	// List repos of given affiliation[s].
   155  	// Comma-separated list of values. Can include:
   156  	// * owner: Repositories that are owned by the authenticated user.
   157  	// * collaborator: Repositories that the user has been added to as a
   158  	//   collaborator.
   159  	// * organization_member: Repositories that the user has access to through
   160  	//   being a member of an organization. This includes every repository on
   161  	//   every team that the user is on.
   162  	// Default: owner,collaborator,organization_member
   163  	Affiliation string `url:"affiliation,omitempty"`
   164  
   165  	// Type of repositories to list.
   166  	// Can be one of all, owner, public, private, member. Default: all
   167  	// Will cause a 422 error if used in the same request as visibility or
   168  	// affiliation.
   169  	Type string `url:"type,omitempty"`
   170  
   171  	// How to sort the repository list. Can be one of created, updated, pushed,
   172  	// full_name. Default: full_name
   173  	Sort string `url:"sort,omitempty"`
   174  
   175  	// Direction in which to sort repositories. Can be one of asc or desc.
   176  	// Default: when using full_name: asc; otherwise desc
   177  	Direction string `url:"direction,omitempty"`
   178  
   179  	ListOptions
   180  }
   181  
   182  // List the repositories for a user. Passing the empty string will list
   183  // repositories for the authenticated user.
   184  //
   185  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#list-repositories-for-the-authenticated-user
   186  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#list-repositories-for-a-user
   187  func (s *RepositoriesService) List(ctx context.Context, user string, opts *RepositoryListOptions) ([]*Repository, *Response, error) {
   188  	var u string
   189  	if user != "" {
   190  		u = fmt.Sprintf("users/%v/repos", user)
   191  	} else {
   192  		u = "user/repos"
   193  	}
   194  	u, err := addOptions(u, opts)
   195  	if err != nil {
   196  		return nil, nil, err
   197  	}
   198  
   199  	req, err := s.client.NewRequest("GET", u, nil)
   200  	if err != nil {
   201  		return nil, nil, err
   202  	}
   203  
   204  	// TODO: remove custom Accept headers when APIs fully launch.
   205  	acceptHeaders := []string{mediaTypeTopicsPreview}
   206  	req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
   207  
   208  	var repos []*Repository
   209  	resp, err := s.client.Do(ctx, req, &repos)
   210  	if err != nil {
   211  		return nil, resp, err
   212  	}
   213  
   214  	return repos, resp, nil
   215  }
   216  
   217  // RepositoryListByOrgOptions specifies the optional parameters to the
   218  // RepositoriesService.ListByOrg method.
   219  type RepositoryListByOrgOptions struct {
   220  	// Type of repositories to list. Possible values are: all, public, private,
   221  	// forks, sources, member. Default is "all".
   222  	Type string `url:"type,omitempty"`
   223  
   224  	// How to sort the repository list. Can be one of created, updated, pushed,
   225  	// full_name. Default is "created".
   226  	Sort string `url:"sort,omitempty"`
   227  
   228  	// Direction in which to sort repositories. Can be one of asc or desc.
   229  	// Default when using full_name: asc; otherwise desc.
   230  	Direction string `url:"direction,omitempty"`
   231  
   232  	ListOptions
   233  }
   234  
   235  // ListByOrg lists the repositories for an organization.
   236  //
   237  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#list-organization-repositories
   238  func (s *RepositoriesService) ListByOrg(ctx context.Context, org string, opts *RepositoryListByOrgOptions) ([]*Repository, *Response, error) {
   239  	u := fmt.Sprintf("orgs/%v/repos", org)
   240  	u, err := addOptions(u, opts)
   241  	if err != nil {
   242  		return nil, nil, err
   243  	}
   244  
   245  	req, err := s.client.NewRequest("GET", u, nil)
   246  	if err != nil {
   247  		return nil, nil, err
   248  	}
   249  
   250  	// TODO: remove custom Accept headers when APIs fully launch.
   251  	acceptHeaders := []string{mediaTypeTopicsPreview}
   252  	req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
   253  
   254  	var repos []*Repository
   255  	resp, err := s.client.Do(ctx, req, &repos)
   256  	if err != nil {
   257  		return nil, resp, err
   258  	}
   259  
   260  	return repos, resp, nil
   261  }
   262  
   263  // RepositoryListAllOptions specifies the optional parameters to the
   264  // RepositoriesService.ListAll method.
   265  type RepositoryListAllOptions struct {
   266  	// ID of the last repository seen
   267  	Since int64 `url:"since,omitempty"`
   268  }
   269  
   270  // ListAll lists all GitHub repositories in the order that they were created.
   271  //
   272  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#list-public-repositories
   273  func (s *RepositoriesService) ListAll(ctx context.Context, opts *RepositoryListAllOptions) ([]*Repository, *Response, error) {
   274  	u, err := addOptions("repositories", opts)
   275  	if err != nil {
   276  		return nil, nil, err
   277  	}
   278  
   279  	req, err := s.client.NewRequest("GET", u, nil)
   280  	if err != nil {
   281  		return nil, nil, err
   282  	}
   283  
   284  	var repos []*Repository
   285  	resp, err := s.client.Do(ctx, req, &repos)
   286  	if err != nil {
   287  		return nil, resp, err
   288  	}
   289  
   290  	return repos, resp, nil
   291  }
   292  
   293  // createRepoRequest is a subset of Repository and is used internally
   294  // by Create to pass only the known fields for the endpoint.
   295  //
   296  // See https://github.com/google/go-github/issues/1014 for more
   297  // information.
   298  type createRepoRequest struct {
   299  	// Name is required when creating a repo.
   300  	Name        *string `json:"name,omitempty"`
   301  	Description *string `json:"description,omitempty"`
   302  	Homepage    *string `json:"homepage,omitempty"`
   303  
   304  	Private     *bool   `json:"private,omitempty"`
   305  	Visibility  *string `json:"visibility,omitempty"`
   306  	HasIssues   *bool   `json:"has_issues,omitempty"`
   307  	HasProjects *bool   `json:"has_projects,omitempty"`
   308  	HasWiki     *bool   `json:"has_wiki,omitempty"`
   309  	IsTemplate  *bool   `json:"is_template,omitempty"`
   310  
   311  	// Creating an organization repository. Required for non-owners.
   312  	TeamID *int64 `json:"team_id,omitempty"`
   313  
   314  	AutoInit            *bool   `json:"auto_init,omitempty"`
   315  	GitignoreTemplate   *string `json:"gitignore_template,omitempty"`
   316  	LicenseTemplate     *string `json:"license_template,omitempty"`
   317  	AllowSquashMerge    *bool   `json:"allow_squash_merge,omitempty"`
   318  	AllowMergeCommit    *bool   `json:"allow_merge_commit,omitempty"`
   319  	AllowRebaseMerge    *bool   `json:"allow_rebase_merge,omitempty"`
   320  	DeleteBranchOnMerge *bool   `json:"delete_branch_on_merge,omitempty"`
   321  }
   322  
   323  // Create a new repository. If an organization is specified, the new
   324  // repository will be created under that org. If the empty string is
   325  // specified, it will be created for the authenticated user.
   326  //
   327  // Note that only a subset of the repo fields are used and repo must
   328  // not be nil.
   329  //
   330  // Also note that this method will return the response without actually
   331  // waiting for GitHub to finish creating the repository and letting the
   332  // changes propagate throughout its servers. You may set up a loop with
   333  // exponential back-off to verify repository's creation.
   334  //
   335  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#create-a-repository-for-the-authenticated-user
   336  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#create-an-organization-repository
   337  func (s *RepositoriesService) Create(ctx context.Context, org string, repo *Repository) (*Repository, *Response, error) {
   338  	var u string
   339  	if org != "" {
   340  		u = fmt.Sprintf("orgs/%v/repos", org)
   341  	} else {
   342  		u = "user/repos"
   343  	}
   344  
   345  	repoReq := &createRepoRequest{
   346  		Name:                repo.Name,
   347  		Description:         repo.Description,
   348  		Homepage:            repo.Homepage,
   349  		Private:             repo.Private,
   350  		Visibility:          repo.Visibility,
   351  		HasIssues:           repo.HasIssues,
   352  		HasProjects:         repo.HasProjects,
   353  		HasWiki:             repo.HasWiki,
   354  		IsTemplate:          repo.IsTemplate,
   355  		TeamID:              repo.TeamID,
   356  		AutoInit:            repo.AutoInit,
   357  		GitignoreTemplate:   repo.GitignoreTemplate,
   358  		LicenseTemplate:     repo.LicenseTemplate,
   359  		AllowSquashMerge:    repo.AllowSquashMerge,
   360  		AllowMergeCommit:    repo.AllowMergeCommit,
   361  		AllowRebaseMerge:    repo.AllowRebaseMerge,
   362  		DeleteBranchOnMerge: repo.DeleteBranchOnMerge,
   363  	}
   364  
   365  	req, err := s.client.NewRequest("POST", u, repoReq)
   366  	if err != nil {
   367  		return nil, nil, err
   368  	}
   369  
   370  	acceptHeaders := []string{mediaTypeRepositoryTemplatePreview, mediaTypeRepositoryVisibilityPreview}
   371  	req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
   372  	r := new(Repository)
   373  	resp, err := s.client.Do(ctx, req, r)
   374  	if err != nil {
   375  		return nil, resp, err
   376  	}
   377  
   378  	return r, resp, nil
   379  }
   380  
   381  // TemplateRepoRequest represents a request to create a repository from a template.
   382  type TemplateRepoRequest struct {
   383  	// Name is required when creating a repo.
   384  	Name        *string `json:"name,omitempty"`
   385  	Owner       *string `json:"owner,omitempty"`
   386  	Description *string `json:"description,omitempty"`
   387  
   388  	Private *bool `json:"private,omitempty"`
   389  }
   390  
   391  // CreateFromTemplate generates a repository from a template.
   392  //
   393  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#create-a-repository-using-a-template
   394  func (s *RepositoriesService) CreateFromTemplate(ctx context.Context, templateOwner, templateRepo string, templateRepoReq *TemplateRepoRequest) (*Repository, *Response, error) {
   395  	u := fmt.Sprintf("repos/%v/%v/generate", templateOwner, templateRepo)
   396  
   397  	req, err := s.client.NewRequest("POST", u, templateRepoReq)
   398  	if err != nil {
   399  		return nil, nil, err
   400  	}
   401  
   402  	req.Header.Set("Accept", mediaTypeRepositoryTemplatePreview)
   403  	r := new(Repository)
   404  	resp, err := s.client.Do(ctx, req, r)
   405  	if err != nil {
   406  		return nil, resp, err
   407  	}
   408  
   409  	return r, resp, nil
   410  }
   411  
   412  // Get fetches a repository.
   413  //
   414  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#get-a-repository
   415  func (s *RepositoriesService) Get(ctx context.Context, owner, repo string) (*Repository, *Response, error) {
   416  	u := fmt.Sprintf("repos/%v/%v", owner, repo)
   417  	req, err := s.client.NewRequest("GET", u, nil)
   418  	if err != nil {
   419  		return nil, nil, err
   420  	}
   421  
   422  	// TODO: remove custom Accept header when the license support fully launches
   423  	// https://docs.github.com/en/free-pro-team@latest/rest/reference/licenses/#get-a-repositorys-license
   424  	acceptHeaders := []string{
   425  		mediaTypeCodesOfConductPreview,
   426  		mediaTypeTopicsPreview,
   427  		mediaTypeRepositoryTemplatePreview,
   428  		mediaTypeRepositoryVisibilityPreview,
   429  	}
   430  	req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
   431  
   432  	repository := new(Repository)
   433  	resp, err := s.client.Do(ctx, req, repository)
   434  	if err != nil {
   435  		return nil, resp, err
   436  	}
   437  
   438  	return repository, resp, nil
   439  }
   440  
   441  // GetCodeOfConduct gets the contents of a repository's code of conduct.
   442  //
   443  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/codes-of-conduct/#get-the-code-of-conduct-for-a-repository
   444  func (s *RepositoriesService) GetCodeOfConduct(ctx context.Context, owner, repo string) (*CodeOfConduct, *Response, error) {
   445  	u := fmt.Sprintf("repos/%v/%v/community/code_of_conduct", owner, repo)
   446  	req, err := s.client.NewRequest("GET", u, nil)
   447  	if err != nil {
   448  		return nil, nil, err
   449  	}
   450  
   451  	// TODO: remove custom Accept header when this API fully launches.
   452  	req.Header.Set("Accept", mediaTypeCodesOfConductPreview)
   453  
   454  	coc := new(CodeOfConduct)
   455  	resp, err := s.client.Do(ctx, req, coc)
   456  	if err != nil {
   457  		return nil, resp, err
   458  	}
   459  
   460  	return coc, resp, nil
   461  }
   462  
   463  // GetByID fetches a repository.
   464  //
   465  // Note: GetByID uses the undocumented GitHub API endpoint /repositories/:id.
   466  func (s *RepositoriesService) GetByID(ctx context.Context, id int64) (*Repository, *Response, error) {
   467  	u := fmt.Sprintf("repositories/%d", id)
   468  	req, err := s.client.NewRequest("GET", u, nil)
   469  	if err != nil {
   470  		return nil, nil, err
   471  	}
   472  
   473  	repository := new(Repository)
   474  	resp, err := s.client.Do(ctx, req, repository)
   475  	if err != nil {
   476  		return nil, resp, err
   477  	}
   478  
   479  	return repository, resp, nil
   480  }
   481  
   482  // Edit updates a repository.
   483  //
   484  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#update-a-repository
   485  func (s *RepositoriesService) Edit(ctx context.Context, owner, repo string, repository *Repository) (*Repository, *Response, error) {
   486  	u := fmt.Sprintf("repos/%v/%v", owner, repo)
   487  	req, err := s.client.NewRequest("PATCH", u, repository)
   488  	if err != nil {
   489  		return nil, nil, err
   490  	}
   491  
   492  	acceptHeaders := []string{mediaTypeRepositoryTemplatePreview, mediaTypeRepositoryVisibilityPreview}
   493  	req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
   494  	r := new(Repository)
   495  	resp, err := s.client.Do(ctx, req, r)
   496  	if err != nil {
   497  		return nil, resp, err
   498  	}
   499  
   500  	return r, resp, nil
   501  }
   502  
   503  // Delete a repository.
   504  //
   505  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#delete-a-repository
   506  func (s *RepositoriesService) Delete(ctx context.Context, owner, repo string) (*Response, error) {
   507  	u := fmt.Sprintf("repos/%v/%v", owner, repo)
   508  	req, err := s.client.NewRequest("DELETE", u, nil)
   509  	if err != nil {
   510  		return nil, err
   511  	}
   512  
   513  	return s.client.Do(ctx, req, nil)
   514  }
   515  
   516  // Contributor represents a repository contributor
   517  type Contributor struct {
   518  	Login             *string `json:"login,omitempty"`
   519  	ID                *int64  `json:"id,omitempty"`
   520  	NodeID            *string `json:"node_id,omitempty"`
   521  	AvatarURL         *string `json:"avatar_url,omitempty"`
   522  	GravatarID        *string `json:"gravatar_id,omitempty"`
   523  	URL               *string `json:"url,omitempty"`
   524  	HTMLURL           *string `json:"html_url,omitempty"`
   525  	FollowersURL      *string `json:"followers_url,omitempty"`
   526  	FollowingURL      *string `json:"following_url,omitempty"`
   527  	GistsURL          *string `json:"gists_url,omitempty"`
   528  	StarredURL        *string `json:"starred_url,omitempty"`
   529  	SubscriptionsURL  *string `json:"subscriptions_url,omitempty"`
   530  	OrganizationsURL  *string `json:"organizations_url,omitempty"`
   531  	ReposURL          *string `json:"repos_url,omitempty"`
   532  	EventsURL         *string `json:"events_url,omitempty"`
   533  	ReceivedEventsURL *string `json:"received_events_url,omitempty"`
   534  	Type              *string `json:"type,omitempty"`
   535  	SiteAdmin         *bool   `json:"site_admin,omitempty"`
   536  	Contributions     *int    `json:"contributions,omitempty"`
   537  }
   538  
   539  // ListContributorsOptions specifies the optional parameters to the
   540  // RepositoriesService.ListContributors method.
   541  type ListContributorsOptions struct {
   542  	// Include anonymous contributors in results or not
   543  	Anon string `url:"anon,omitempty"`
   544  
   545  	ListOptions
   546  }
   547  
   548  // GetVulnerabilityAlerts checks if vulnerability alerts are enabled for a repository.
   549  //
   550  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#check-if-vulnerability-alerts-are-enabled-for-a-repository
   551  func (s *RepositoriesService) GetVulnerabilityAlerts(ctx context.Context, owner, repository string) (bool, *Response, error) {
   552  	u := fmt.Sprintf("repos/%v/%v/vulnerability-alerts", owner, repository)
   553  
   554  	req, err := s.client.NewRequest("GET", u, nil)
   555  	if err != nil {
   556  		return false, nil, err
   557  	}
   558  
   559  	// TODO: remove custom Accept header when this API fully launches
   560  	req.Header.Set("Accept", mediaTypeRequiredVulnerabilityAlertsPreview)
   561  
   562  	resp, err := s.client.Do(ctx, req, nil)
   563  	vulnerabilityAlertsEnabled, err := parseBoolResponse(err)
   564  
   565  	return vulnerabilityAlertsEnabled, resp, err
   566  }
   567  
   568  // EnableVulnerabilityAlerts enables vulnerability alerts and the dependency graph for a repository.
   569  //
   570  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#enable-vulnerability-alerts
   571  func (s *RepositoriesService) EnableVulnerabilityAlerts(ctx context.Context, owner, repository string) (*Response, error) {
   572  	u := fmt.Sprintf("repos/%v/%v/vulnerability-alerts", owner, repository)
   573  
   574  	req, err := s.client.NewRequest("PUT", u, nil)
   575  	if err != nil {
   576  		return nil, err
   577  	}
   578  
   579  	// TODO: remove custom Accept header when this API fully launches
   580  	req.Header.Set("Accept", mediaTypeRequiredVulnerabilityAlertsPreview)
   581  
   582  	return s.client.Do(ctx, req, nil)
   583  }
   584  
   585  // DisableVulnerabilityAlerts disables vulnerability alerts and the dependency graph for a repository.
   586  //
   587  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#disable-vulnerability-alerts
   588  func (s *RepositoriesService) DisableVulnerabilityAlerts(ctx context.Context, owner, repository string) (*Response, error) {
   589  	u := fmt.Sprintf("repos/%v/%v/vulnerability-alerts", owner, repository)
   590  
   591  	req, err := s.client.NewRequest("DELETE", u, nil)
   592  	if err != nil {
   593  		return nil, err
   594  	}
   595  
   596  	// TODO: remove custom Accept header when this API fully launches
   597  	req.Header.Set("Accept", mediaTypeRequiredVulnerabilityAlertsPreview)
   598  
   599  	return s.client.Do(ctx, req, nil)
   600  }
   601  
   602  // EnableAutomatedSecurityFixes enables the automated security fixes for a repository.
   603  //
   604  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#enable-automated-security-fixes
   605  func (s *RepositoriesService) EnableAutomatedSecurityFixes(ctx context.Context, owner, repository string) (*Response, error) {
   606  	u := fmt.Sprintf("repos/%v/%v/automated-security-fixes", owner, repository)
   607  
   608  	req, err := s.client.NewRequest("PUT", u, nil)
   609  	if err != nil {
   610  		return nil, err
   611  	}
   612  
   613  	// TODO: remove custom Accept header when this API fully launches
   614  	req.Header.Set("Accept", mediaTypeRequiredAutomatedSecurityFixesPreview)
   615  
   616  	return s.client.Do(ctx, req, nil)
   617  }
   618  
   619  // DisableAutomatedSecurityFixes disables vulnerability alerts and the dependency graph for a repository.
   620  //
   621  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#disable-automated-security-fixes
   622  func (s *RepositoriesService) DisableAutomatedSecurityFixes(ctx context.Context, owner, repository string) (*Response, error) {
   623  	u := fmt.Sprintf("repos/%v/%v/automated-security-fixes", owner, repository)
   624  
   625  	req, err := s.client.NewRequest("DELETE", u, nil)
   626  	if err != nil {
   627  		return nil, err
   628  	}
   629  
   630  	// TODO: remove custom Accept header when this API fully launches
   631  	req.Header.Set("Accept", mediaTypeRequiredAutomatedSecurityFixesPreview)
   632  
   633  	return s.client.Do(ctx, req, nil)
   634  }
   635  
   636  // ListContributors lists contributors for a repository.
   637  //
   638  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#list-repository-contributors
   639  func (s *RepositoriesService) ListContributors(ctx context.Context, owner string, repository string, opts *ListContributorsOptions) ([]*Contributor, *Response, error) {
   640  	u := fmt.Sprintf("repos/%v/%v/contributors", owner, repository)
   641  	u, err := addOptions(u, opts)
   642  	if err != nil {
   643  		return nil, nil, err
   644  	}
   645  
   646  	req, err := s.client.NewRequest("GET", u, nil)
   647  	if err != nil {
   648  		return nil, nil, err
   649  	}
   650  
   651  	var contributor []*Contributor
   652  	resp, err := s.client.Do(ctx, req, &contributor)
   653  	if err != nil {
   654  		return nil, nil, err
   655  	}
   656  
   657  	return contributor, resp, nil
   658  }
   659  
   660  // ListLanguages lists languages for the specified repository. The returned map
   661  // specifies the languages and the number of bytes of code written in that
   662  // language. For example:
   663  //
   664  //     {
   665  //       "C": 78769,
   666  //       "Python": 7769
   667  //     }
   668  //
   669  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#list-repository-languages
   670  func (s *RepositoriesService) ListLanguages(ctx context.Context, owner string, repo string) (map[string]int, *Response, error) {
   671  	u := fmt.Sprintf("repos/%v/%v/languages", owner, repo)
   672  	req, err := s.client.NewRequest("GET", u, nil)
   673  	if err != nil {
   674  		return nil, nil, err
   675  	}
   676  
   677  	languages := make(map[string]int)
   678  	resp, err := s.client.Do(ctx, req, &languages)
   679  	if err != nil {
   680  		return nil, resp, err
   681  	}
   682  
   683  	return languages, resp, nil
   684  }
   685  
   686  // ListTeams lists the teams for the specified repository.
   687  //
   688  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#list-repository-teams
   689  func (s *RepositoriesService) ListTeams(ctx context.Context, owner string, repo string, opts *ListOptions) ([]*Team, *Response, error) {
   690  	u := fmt.Sprintf("repos/%v/%v/teams", owner, repo)
   691  	u, err := addOptions(u, opts)
   692  	if err != nil {
   693  		return nil, nil, err
   694  	}
   695  
   696  	req, err := s.client.NewRequest("GET", u, nil)
   697  	if err != nil {
   698  		return nil, nil, err
   699  	}
   700  
   701  	var teams []*Team
   702  	resp, err := s.client.Do(ctx, req, &teams)
   703  	if err != nil {
   704  		return nil, resp, err
   705  	}
   706  
   707  	return teams, resp, nil
   708  }
   709  
   710  // RepositoryTag represents a repository tag.
   711  type RepositoryTag struct {
   712  	Name       *string `json:"name,omitempty"`
   713  	Commit     *Commit `json:"commit,omitempty"`
   714  	ZipballURL *string `json:"zipball_url,omitempty"`
   715  	TarballURL *string `json:"tarball_url,omitempty"`
   716  }
   717  
   718  // ListTags lists tags for the specified repository.
   719  //
   720  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#list-repository-tags
   721  func (s *RepositoriesService) ListTags(ctx context.Context, owner string, repo string, opts *ListOptions) ([]*RepositoryTag, *Response, error) {
   722  	u := fmt.Sprintf("repos/%v/%v/tags", owner, repo)
   723  	u, err := addOptions(u, opts)
   724  	if err != nil {
   725  		return nil, nil, err
   726  	}
   727  
   728  	req, err := s.client.NewRequest("GET", u, nil)
   729  	if err != nil {
   730  		return nil, nil, err
   731  	}
   732  
   733  	var tags []*RepositoryTag
   734  	resp, err := s.client.Do(ctx, req, &tags)
   735  	if err != nil {
   736  		return nil, resp, err
   737  	}
   738  
   739  	return tags, resp, nil
   740  }
   741  
   742  // Branch represents a repository branch
   743  type Branch struct {
   744  	Name      *string           `json:"name,omitempty"`
   745  	Commit    *RepositoryCommit `json:"commit,omitempty"`
   746  	Protected *bool             `json:"protected,omitempty"`
   747  }
   748  
   749  // Protection represents a repository branch's protection.
   750  type Protection struct {
   751  	RequiredStatusChecks       *RequiredStatusChecks          `json:"required_status_checks"`
   752  	RequiredPullRequestReviews *PullRequestReviewsEnforcement `json:"required_pull_request_reviews"`
   753  	EnforceAdmins              *AdminEnforcement              `json:"enforce_admins"`
   754  	Restrictions               *BranchRestrictions            `json:"restrictions"`
   755  	RequireLinearHistory       *RequireLinearHistory          `json:"required_linear_history"`
   756  	AllowForcePushes           *AllowForcePushes              `json:"allow_force_pushes"`
   757  	AllowDeletions             *AllowDeletions                `json:"allow_deletions"`
   758  }
   759  
   760  // ProtectionRequest represents a request to create/edit a branch's protection.
   761  type ProtectionRequest struct {
   762  	RequiredStatusChecks       *RequiredStatusChecks                 `json:"required_status_checks"`
   763  	RequiredPullRequestReviews *PullRequestReviewsEnforcementRequest `json:"required_pull_request_reviews"`
   764  	EnforceAdmins              bool                                  `json:"enforce_admins"`
   765  	Restrictions               *BranchRestrictionsRequest            `json:"restrictions"`
   766  	// Enforces a linear commit Git history, which prevents anyone from pushing merge commits to a branch.
   767  	RequireLinearHistory *bool `json:"required_linear_history,omitempty"`
   768  	// Permits force pushes to the protected branch by anyone with write access to the repository.
   769  	AllowForcePushes *bool `json:"allow_force_pushes,omitempty"`
   770  	// Allows deletion of the protected branch by anyone with write access to the repository.
   771  	AllowDeletions *bool `json:"allow_deletions,omitempty"`
   772  }
   773  
   774  // RequiredStatusChecks represents the protection status of a individual branch.
   775  type RequiredStatusChecks struct {
   776  	// Require branches to be up to date before merging. (Required.)
   777  	Strict bool `json:"strict"`
   778  	// The list of status checks to require in order to merge into this
   779  	// branch. (Required; use []string{} instead of nil for empty list.)
   780  	Contexts []string `json:"contexts"`
   781  }
   782  
   783  // RequiredStatusChecksRequest represents a request to edit a protected branch's status checks.
   784  type RequiredStatusChecksRequest struct {
   785  	Strict   *bool    `json:"strict,omitempty"`
   786  	Contexts []string `json:"contexts,omitempty"`
   787  }
   788  
   789  // PullRequestReviewsEnforcement represents the pull request reviews enforcement of a protected branch.
   790  type PullRequestReviewsEnforcement struct {
   791  	// Specifies which users and teams can dismiss pull request reviews.
   792  	DismissalRestrictions *DismissalRestrictions `json:"dismissal_restrictions,omitempty"`
   793  	// Specifies if approved reviews are dismissed automatically, when a new commit is pushed.
   794  	DismissStaleReviews bool `json:"dismiss_stale_reviews"`
   795  	// RequireCodeOwnerReviews specifies if an approved review is required in pull requests including files with a designated code owner.
   796  	RequireCodeOwnerReviews bool `json:"require_code_owner_reviews"`
   797  	// RequiredApprovingReviewCount specifies the number of approvals required before the pull request can be merged.
   798  	// Valid values are 1-6.
   799  	RequiredApprovingReviewCount int `json:"required_approving_review_count"`
   800  }
   801  
   802  // PullRequestReviewsEnforcementRequest represents request to set the pull request review
   803  // enforcement of a protected branch. It is separate from PullRequestReviewsEnforcement above
   804  // because the request structure is different from the response structure.
   805  type PullRequestReviewsEnforcementRequest struct {
   806  	// Specifies which users and teams should be allowed to dismiss pull request reviews.
   807  	// User and team dismissal restrictions are only available for
   808  	// organization-owned repositories. Must be nil for personal repositories.
   809  	DismissalRestrictionsRequest *DismissalRestrictionsRequest `json:"dismissal_restrictions,omitempty"`
   810  	// Specifies if approved reviews can be dismissed automatically, when a new commit is pushed. (Required)
   811  	DismissStaleReviews bool `json:"dismiss_stale_reviews"`
   812  	// RequireCodeOwnerReviews specifies if an approved review is required in pull requests including files with a designated code owner.
   813  	RequireCodeOwnerReviews bool `json:"require_code_owner_reviews"`
   814  	// RequiredApprovingReviewCount specifies the number of approvals required before the pull request can be merged.
   815  	// Valid values are 1-6.
   816  	RequiredApprovingReviewCount int `json:"required_approving_review_count"`
   817  }
   818  
   819  // PullRequestReviewsEnforcementUpdate represents request to patch the pull request review
   820  // enforcement of a protected branch. It is separate from PullRequestReviewsEnforcementRequest above
   821  // because the patch request does not require all fields to be initialized.
   822  type PullRequestReviewsEnforcementUpdate struct {
   823  	// Specifies which users and teams can dismiss pull request reviews. Can be omitted.
   824  	DismissalRestrictionsRequest *DismissalRestrictionsRequest `json:"dismissal_restrictions,omitempty"`
   825  	// Specifies if approved reviews can be dismissed automatically, when a new commit is pushed. Can be omitted.
   826  	DismissStaleReviews *bool `json:"dismiss_stale_reviews,omitempty"`
   827  	// RequireCodeOwnerReviews specifies if an approved review is required in pull requests including files with a designated code owner.
   828  	RequireCodeOwnerReviews bool `json:"require_code_owner_reviews,omitempty"`
   829  	// RequiredApprovingReviewCount specifies the number of approvals required before the pull request can be merged.
   830  	// Valid values are 1 - 6.
   831  	RequiredApprovingReviewCount int `json:"required_approving_review_count"`
   832  }
   833  
   834  // RequireLinearHistory represents the configuration to enfore branches with no merge commit.
   835  type RequireLinearHistory struct {
   836  	Enabled bool `json:"enabled"`
   837  }
   838  
   839  // AllowDeletions represents the configuration to accept deletion of protected branches.
   840  type AllowDeletions struct {
   841  	Enabled bool `json:"enabled"`
   842  }
   843  
   844  // AllowForcePushes represents the configuration to accept forced pushes on protected branches.
   845  type AllowForcePushes struct {
   846  	Enabled bool `json:"enabled"`
   847  }
   848  
   849  // AdminEnforcement represents the configuration to enforce required status checks for repository administrators.
   850  type AdminEnforcement struct {
   851  	URL     *string `json:"url,omitempty"`
   852  	Enabled bool    `json:"enabled"`
   853  }
   854  
   855  // BranchRestrictions represents the restriction that only certain users or
   856  // teams may push to a branch.
   857  type BranchRestrictions struct {
   858  	// The list of user logins with push access.
   859  	Users []*User `json:"users"`
   860  	// The list of team slugs with push access.
   861  	Teams []*Team `json:"teams"`
   862  	// The list of app slugs with push access.
   863  	Apps []*App `json:"apps"`
   864  }
   865  
   866  // BranchRestrictionsRequest represents the request to create/edit the
   867  // restriction that only certain users or teams may push to a branch. It is
   868  // separate from BranchRestrictions above because the request structure is
   869  // different from the response structure.
   870  type BranchRestrictionsRequest struct {
   871  	// The list of user logins with push access. (Required; use []string{} instead of nil for empty list.)
   872  	Users []string `json:"users"`
   873  	// The list of team slugs with push access. (Required; use []string{} instead of nil for empty list.)
   874  	Teams []string `json:"teams"`
   875  	// The list of app slugs with push access.
   876  	Apps []string `json:"apps,omitempty"`
   877  }
   878  
   879  // DismissalRestrictions specifies which users and teams can dismiss pull request reviews.
   880  type DismissalRestrictions struct {
   881  	// The list of users who can dimiss pull request reviews.
   882  	Users []*User `json:"users"`
   883  	// The list of teams which can dismiss pull request reviews.
   884  	Teams []*Team `json:"teams"`
   885  }
   886  
   887  // DismissalRestrictionsRequest represents the request to create/edit the
   888  // restriction to allows only specific users or teams to dimiss pull request reviews. It is
   889  // separate from DismissalRestrictions above because the request structure is
   890  // different from the response structure.
   891  // Note: Both Users and Teams must be nil, or both must be non-nil.
   892  type DismissalRestrictionsRequest struct {
   893  	// The list of user logins who can dismiss pull request reviews. (Required; use nil to disable dismissal_restrictions or &[]string{} otherwise.)
   894  	Users *[]string `json:"users,omitempty"`
   895  	// The list of team slugs which can dismiss pull request reviews. (Required; use nil to disable dismissal_restrictions or &[]string{} otherwise.)
   896  	Teams *[]string `json:"teams,omitempty"`
   897  }
   898  
   899  // SignaturesProtectedBranch represents the protection status of an individual branch.
   900  type SignaturesProtectedBranch struct {
   901  	URL *string `json:"url,omitempty"`
   902  	// Commits pushed to matching branches must have verified signatures.
   903  	Enabled *bool `json:"enabled,omitempty"`
   904  }
   905  
   906  // ListBranches lists branches for the specified repository.
   907  //
   908  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#list-branches
   909  func (s *RepositoriesService) ListBranches(ctx context.Context, owner string, repo string, opts *BranchListOptions) ([]*Branch, *Response, error) {
   910  	u := fmt.Sprintf("repos/%v/%v/branches", owner, repo)
   911  	u, err := addOptions(u, opts)
   912  	if err != nil {
   913  		return nil, nil, err
   914  	}
   915  
   916  	req, err := s.client.NewRequest("GET", u, nil)
   917  	if err != nil {
   918  		return nil, nil, err
   919  	}
   920  
   921  	// TODO: remove custom Accept header when this API fully launches
   922  	req.Header.Set("Accept", mediaTypeRequiredApprovingReviewsPreview)
   923  
   924  	var branches []*Branch
   925  	resp, err := s.client.Do(ctx, req, &branches)
   926  	if err != nil {
   927  		return nil, resp, err
   928  	}
   929  
   930  	return branches, resp, nil
   931  }
   932  
   933  // GetBranch gets the specified branch for a repository.
   934  //
   935  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#get-a-branch
   936  func (s *RepositoriesService) GetBranch(ctx context.Context, owner, repo, branch string) (*Branch, *Response, error) {
   937  	u := fmt.Sprintf("repos/%v/%v/branches/%v", owner, repo, branch)
   938  	req, err := s.client.NewRequest("GET", u, nil)
   939  	if err != nil {
   940  		return nil, nil, err
   941  	}
   942  
   943  	// TODO: remove custom Accept header when this API fully launches
   944  	req.Header.Set("Accept", mediaTypeRequiredApprovingReviewsPreview)
   945  
   946  	b := new(Branch)
   947  	resp, err := s.client.Do(ctx, req, b)
   948  	if err != nil {
   949  		return nil, resp, err
   950  	}
   951  
   952  	return b, resp, nil
   953  }
   954  
   955  // GetBranchProtection gets the protection of a given branch.
   956  //
   957  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#get-branch-protection
   958  func (s *RepositoriesService) GetBranchProtection(ctx context.Context, owner, repo, branch string) (*Protection, *Response, error) {
   959  	u := fmt.Sprintf("repos/%v/%v/branches/%v/protection", owner, repo, branch)
   960  	req, err := s.client.NewRequest("GET", u, nil)
   961  	if err != nil {
   962  		return nil, nil, err
   963  	}
   964  
   965  	// TODO: remove custom Accept header when this API fully launches
   966  	req.Header.Set("Accept", mediaTypeRequiredApprovingReviewsPreview)
   967  
   968  	p := new(Protection)
   969  	resp, err := s.client.Do(ctx, req, p)
   970  	if err != nil {
   971  		return nil, resp, err
   972  	}
   973  
   974  	return p, resp, nil
   975  }
   976  
   977  // GetRequiredStatusChecks gets the required status checks for a given protected branch.
   978  //
   979  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#get-status-checks-protection
   980  func (s *RepositoriesService) GetRequiredStatusChecks(ctx context.Context, owner, repo, branch string) (*RequiredStatusChecks, *Response, error) {
   981  	u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/required_status_checks", owner, repo, branch)
   982  	req, err := s.client.NewRequest("GET", u, nil)
   983  	if err != nil {
   984  		return nil, nil, err
   985  	}
   986  
   987  	// TODO: remove custom Accept header when this API fully launches
   988  	req.Header.Set("Accept", mediaTypeRequiredApprovingReviewsPreview)
   989  
   990  	p := new(RequiredStatusChecks)
   991  	resp, err := s.client.Do(ctx, req, p)
   992  	if err != nil {
   993  		return nil, resp, err
   994  	}
   995  
   996  	return p, resp, nil
   997  }
   998  
   999  // ListRequiredStatusChecksContexts lists the required status checks contexts for a given protected branch.
  1000  //
  1001  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#get-all-status-check-contexts
  1002  func (s *RepositoriesService) ListRequiredStatusChecksContexts(ctx context.Context, owner, repo, branch string) (contexts []string, resp *Response, err error) {
  1003  	u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/required_status_checks/contexts", owner, repo, branch)
  1004  	req, err := s.client.NewRequest("GET", u, nil)
  1005  	if err != nil {
  1006  		return nil, nil, err
  1007  	}
  1008  
  1009  	// TODO: remove custom Accept header when this API fully launches
  1010  	req.Header.Set("Accept", mediaTypeRequiredApprovingReviewsPreview)
  1011  
  1012  	resp, err = s.client.Do(ctx, req, &contexts)
  1013  	if err != nil {
  1014  		return nil, resp, err
  1015  	}
  1016  
  1017  	return contexts, resp, nil
  1018  }
  1019  
  1020  // UpdateBranchProtection updates the protection of a given branch.
  1021  //
  1022  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#update-branch-protection
  1023  func (s *RepositoriesService) UpdateBranchProtection(ctx context.Context, owner, repo, branch string, preq *ProtectionRequest) (*Protection, *Response, error) {
  1024  	u := fmt.Sprintf("repos/%v/%v/branches/%v/protection", owner, repo, branch)
  1025  	req, err := s.client.NewRequest("PUT", u, preq)
  1026  	if err != nil {
  1027  		return nil, nil, err
  1028  	}
  1029  
  1030  	// TODO: remove custom Accept header when this API fully launches
  1031  	req.Header.Set("Accept", mediaTypeRequiredApprovingReviewsPreview)
  1032  
  1033  	p := new(Protection)
  1034  	resp, err := s.client.Do(ctx, req, p)
  1035  	if err != nil {
  1036  		return nil, resp, err
  1037  	}
  1038  
  1039  	return p, resp, nil
  1040  }
  1041  
  1042  // RemoveBranchProtection removes the protection of a given branch.
  1043  //
  1044  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#delete-branch-protection
  1045  func (s *RepositoriesService) RemoveBranchProtection(ctx context.Context, owner, repo, branch string) (*Response, error) {
  1046  	u := fmt.Sprintf("repos/%v/%v/branches/%v/protection", owner, repo, branch)
  1047  	req, err := s.client.NewRequest("DELETE", u, nil)
  1048  	if err != nil {
  1049  		return nil, err
  1050  	}
  1051  
  1052  	// TODO: remove custom Accept header when this API fully launches
  1053  	req.Header.Set("Accept", mediaTypeRequiredApprovingReviewsPreview)
  1054  
  1055  	return s.client.Do(ctx, req, nil)
  1056  }
  1057  
  1058  // GetSignaturesProtectedBranch gets required signatures of protected branch.
  1059  //
  1060  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#get-commit-signature-protection
  1061  func (s *RepositoriesService) GetSignaturesProtectedBranch(ctx context.Context, owner, repo, branch string) (*SignaturesProtectedBranch, *Response, error) {
  1062  	u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/required_signatures", owner, repo, branch)
  1063  	req, err := s.client.NewRequest("GET", u, nil)
  1064  	if err != nil {
  1065  		return nil, nil, err
  1066  	}
  1067  
  1068  	// TODO: remove custom Accept header when this API fully launches
  1069  	req.Header.Set("Accept", mediaTypeSignaturePreview)
  1070  
  1071  	p := new(SignaturesProtectedBranch)
  1072  	resp, err := s.client.Do(ctx, req, p)
  1073  	if err != nil {
  1074  		return nil, resp, err
  1075  	}
  1076  
  1077  	return p, resp, nil
  1078  }
  1079  
  1080  // RequireSignaturesOnProtectedBranch makes signed commits required on a protected branch.
  1081  // It requires admin access and branch protection to be enabled.
  1082  //
  1083  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#create-commit-signature-protection
  1084  func (s *RepositoriesService) RequireSignaturesOnProtectedBranch(ctx context.Context, owner, repo, branch string) (*SignaturesProtectedBranch, *Response, error) {
  1085  	u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/required_signatures", owner, repo, branch)
  1086  	req, err := s.client.NewRequest("POST", u, nil)
  1087  	if err != nil {
  1088  		return nil, nil, err
  1089  	}
  1090  
  1091  	// TODO: remove custom Accept header when this API fully launches
  1092  	req.Header.Set("Accept", mediaTypeSignaturePreview)
  1093  
  1094  	r := new(SignaturesProtectedBranch)
  1095  	resp, err := s.client.Do(ctx, req, r)
  1096  	if err != nil {
  1097  		return nil, resp, err
  1098  	}
  1099  
  1100  	return r, resp, err
  1101  }
  1102  
  1103  // OptionalSignaturesOnProtectedBranch removes required signed commits on a given branch.
  1104  //
  1105  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#delete-commit-signature-protection
  1106  func (s *RepositoriesService) OptionalSignaturesOnProtectedBranch(ctx context.Context, owner, repo, branch string) (*Response, error) {
  1107  	u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/required_signatures", owner, repo, branch)
  1108  	req, err := s.client.NewRequest("DELETE", u, nil)
  1109  	if err != nil {
  1110  		return nil, err
  1111  	}
  1112  
  1113  	// TODO: remove custom Accept header when this API fully launches
  1114  	req.Header.Set("Accept", mediaTypeSignaturePreview)
  1115  
  1116  	return s.client.Do(ctx, req, nil)
  1117  }
  1118  
  1119  // UpdateRequiredStatusChecks updates the required status checks for a given protected branch.
  1120  //
  1121  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#update-status-check-protection
  1122  func (s *RepositoriesService) UpdateRequiredStatusChecks(ctx context.Context, owner, repo, branch string, sreq *RequiredStatusChecksRequest) (*RequiredStatusChecks, *Response, error) {
  1123  	u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/required_status_checks", owner, repo, branch)
  1124  	req, err := s.client.NewRequest("PATCH", u, sreq)
  1125  	if err != nil {
  1126  		return nil, nil, err
  1127  	}
  1128  
  1129  	sc := new(RequiredStatusChecks)
  1130  	resp, err := s.client.Do(ctx, req, sc)
  1131  	if err != nil {
  1132  		return nil, resp, err
  1133  	}
  1134  
  1135  	return sc, resp, nil
  1136  }
  1137  
  1138  // License gets the contents of a repository's license if one is detected.
  1139  //
  1140  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/licenses/#get-the-license-for-a-repository
  1141  func (s *RepositoriesService) License(ctx context.Context, owner, repo string) (*RepositoryLicense, *Response, error) {
  1142  	u := fmt.Sprintf("repos/%v/%v/license", owner, repo)
  1143  	req, err := s.client.NewRequest("GET", u, nil)
  1144  	if err != nil {
  1145  		return nil, nil, err
  1146  	}
  1147  
  1148  	r := &RepositoryLicense{}
  1149  	resp, err := s.client.Do(ctx, req, r)
  1150  	if err != nil {
  1151  		return nil, resp, err
  1152  	}
  1153  
  1154  	return r, resp, nil
  1155  }
  1156  
  1157  // GetPullRequestReviewEnforcement gets pull request review enforcement of a protected branch.
  1158  //
  1159  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#get-pull-request-review-protection
  1160  func (s *RepositoriesService) GetPullRequestReviewEnforcement(ctx context.Context, owner, repo, branch string) (*PullRequestReviewsEnforcement, *Response, error) {
  1161  	u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/required_pull_request_reviews", owner, repo, branch)
  1162  	req, err := s.client.NewRequest("GET", u, nil)
  1163  	if err != nil {
  1164  		return nil, nil, err
  1165  	}
  1166  
  1167  	// TODO: remove custom Accept header when this API fully launches
  1168  	req.Header.Set("Accept", mediaTypeRequiredApprovingReviewsPreview)
  1169  
  1170  	r := new(PullRequestReviewsEnforcement)
  1171  	resp, err := s.client.Do(ctx, req, r)
  1172  	if err != nil {
  1173  		return nil, resp, err
  1174  	}
  1175  
  1176  	return r, resp, nil
  1177  }
  1178  
  1179  // UpdatePullRequestReviewEnforcement patches pull request review enforcement of a protected branch.
  1180  // It requires admin access and branch protection to be enabled.
  1181  //
  1182  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#update-pull-request-review-protection
  1183  func (s *RepositoriesService) UpdatePullRequestReviewEnforcement(ctx context.Context, owner, repo, branch string, patch *PullRequestReviewsEnforcementUpdate) (*PullRequestReviewsEnforcement, *Response, error) {
  1184  	u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/required_pull_request_reviews", owner, repo, branch)
  1185  	req, err := s.client.NewRequest("PATCH", u, patch)
  1186  	if err != nil {
  1187  		return nil, nil, err
  1188  	}
  1189  
  1190  	// TODO: remove custom Accept header when this API fully launches
  1191  	req.Header.Set("Accept", mediaTypeRequiredApprovingReviewsPreview)
  1192  
  1193  	r := new(PullRequestReviewsEnforcement)
  1194  	resp, err := s.client.Do(ctx, req, r)
  1195  	if err != nil {
  1196  		return nil, resp, err
  1197  	}
  1198  
  1199  	return r, resp, err
  1200  }
  1201  
  1202  // DisableDismissalRestrictions disables dismissal restrictions of a protected branch.
  1203  // It requires admin access and branch protection to be enabled.
  1204  //
  1205  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#update-pull-request-review-protection
  1206  func (s *RepositoriesService) DisableDismissalRestrictions(ctx context.Context, owner, repo, branch string) (*PullRequestReviewsEnforcement, *Response, error) {
  1207  	u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/required_pull_request_reviews", owner, repo, branch)
  1208  
  1209  	data := new(struct {
  1210  		DismissalRestrictionsRequest `json:"dismissal_restrictions"`
  1211  	})
  1212  
  1213  	req, err := s.client.NewRequest("PATCH", u, data)
  1214  	if err != nil {
  1215  		return nil, nil, err
  1216  	}
  1217  
  1218  	// TODO: remove custom Accept header when this API fully launches
  1219  	req.Header.Set("Accept", mediaTypeRequiredApprovingReviewsPreview)
  1220  
  1221  	r := new(PullRequestReviewsEnforcement)
  1222  	resp, err := s.client.Do(ctx, req, r)
  1223  	if err != nil {
  1224  		return nil, resp, err
  1225  	}
  1226  
  1227  	return r, resp, err
  1228  }
  1229  
  1230  // RemovePullRequestReviewEnforcement removes pull request enforcement of a protected branch.
  1231  //
  1232  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#delete-pull-request-review-protection
  1233  func (s *RepositoriesService) RemovePullRequestReviewEnforcement(ctx context.Context, owner, repo, branch string) (*Response, error) {
  1234  	u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/required_pull_request_reviews", owner, repo, branch)
  1235  	req, err := s.client.NewRequest("DELETE", u, nil)
  1236  	if err != nil {
  1237  		return nil, err
  1238  	}
  1239  
  1240  	// TODO: remove custom Accept header when this API fully launches
  1241  	req.Header.Set("Accept", mediaTypeRequiredApprovingReviewsPreview)
  1242  
  1243  	return s.client.Do(ctx, req, nil)
  1244  }
  1245  
  1246  // GetAdminEnforcement gets admin enforcement information of a protected branch.
  1247  //
  1248  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#get-admin-branch-protection
  1249  func (s *RepositoriesService) GetAdminEnforcement(ctx context.Context, owner, repo, branch string) (*AdminEnforcement, *Response, error) {
  1250  	u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/enforce_admins", owner, repo, branch)
  1251  	req, err := s.client.NewRequest("GET", u, nil)
  1252  	if err != nil {
  1253  		return nil, nil, err
  1254  	}
  1255  
  1256  	// TODO: remove custom Accept header when this API fully launches
  1257  	req.Header.Set("Accept", mediaTypeRequiredApprovingReviewsPreview)
  1258  
  1259  	r := new(AdminEnforcement)
  1260  	resp, err := s.client.Do(ctx, req, r)
  1261  	if err != nil {
  1262  		return nil, resp, err
  1263  	}
  1264  
  1265  	return r, resp, nil
  1266  }
  1267  
  1268  // AddAdminEnforcement adds admin enforcement to a protected branch.
  1269  // It requires admin access and branch protection to be enabled.
  1270  //
  1271  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#set-admin-branch-protection
  1272  func (s *RepositoriesService) AddAdminEnforcement(ctx context.Context, owner, repo, branch string) (*AdminEnforcement, *Response, error) {
  1273  	u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/enforce_admins", owner, repo, branch)
  1274  	req, err := s.client.NewRequest("POST", u, nil)
  1275  	if err != nil {
  1276  		return nil, nil, err
  1277  	}
  1278  
  1279  	// TODO: remove custom Accept header when this API fully launches
  1280  	req.Header.Set("Accept", mediaTypeRequiredApprovingReviewsPreview)
  1281  
  1282  	r := new(AdminEnforcement)
  1283  	resp, err := s.client.Do(ctx, req, r)
  1284  	if err != nil {
  1285  		return nil, resp, err
  1286  	}
  1287  
  1288  	return r, resp, err
  1289  }
  1290  
  1291  // RemoveAdminEnforcement removes admin enforcement from a protected branch.
  1292  //
  1293  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#delete-admin-branch-protection
  1294  func (s *RepositoriesService) RemoveAdminEnforcement(ctx context.Context, owner, repo, branch string) (*Response, error) {
  1295  	u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/enforce_admins", owner, repo, branch)
  1296  	req, err := s.client.NewRequest("DELETE", u, nil)
  1297  	if err != nil {
  1298  		return nil, err
  1299  	}
  1300  
  1301  	// TODO: remove custom Accept header when this API fully launches
  1302  	req.Header.Set("Accept", mediaTypeRequiredApprovingReviewsPreview)
  1303  
  1304  	return s.client.Do(ctx, req, nil)
  1305  }
  1306  
  1307  // repositoryTopics represents a collection of repository topics.
  1308  type repositoryTopics struct {
  1309  	Names []string `json:"names"`
  1310  }
  1311  
  1312  // ListAllTopics lists topics for a repository.
  1313  //
  1314  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#get-all-repository-topics
  1315  func (s *RepositoriesService) ListAllTopics(ctx context.Context, owner, repo string) ([]string, *Response, error) {
  1316  	u := fmt.Sprintf("repos/%v/%v/topics", owner, repo)
  1317  	req, err := s.client.NewRequest("GET", u, nil)
  1318  	if err != nil {
  1319  		return nil, nil, err
  1320  	}
  1321  
  1322  	// TODO: remove custom Accept header when this API fully launches.
  1323  	req.Header.Set("Accept", mediaTypeTopicsPreview)
  1324  
  1325  	topics := new(repositoryTopics)
  1326  	resp, err := s.client.Do(ctx, req, topics)
  1327  	if err != nil {
  1328  		return nil, resp, err
  1329  	}
  1330  
  1331  	return topics.Names, resp, nil
  1332  }
  1333  
  1334  // ReplaceAllTopics replaces topics for a repository.
  1335  //
  1336  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#replace-all-repository-topics
  1337  func (s *RepositoriesService) ReplaceAllTopics(ctx context.Context, owner, repo string, topics []string) ([]string, *Response, error) {
  1338  	u := fmt.Sprintf("repos/%v/%v/topics", owner, repo)
  1339  	t := &repositoryTopics{
  1340  		Names: topics,
  1341  	}
  1342  	if t.Names == nil {
  1343  		t.Names = []string{}
  1344  	}
  1345  	req, err := s.client.NewRequest("PUT", u, t)
  1346  	if err != nil {
  1347  		return nil, nil, err
  1348  	}
  1349  
  1350  	// TODO: remove custom Accept header when this API fully launches.
  1351  	req.Header.Set("Accept", mediaTypeTopicsPreview)
  1352  
  1353  	t = new(repositoryTopics)
  1354  	resp, err := s.client.Do(ctx, req, t)
  1355  	if err != nil {
  1356  		return nil, resp, err
  1357  	}
  1358  
  1359  	return t.Names, resp, nil
  1360  }
  1361  
  1362  // ListApps lists the GitHub apps that have push access to a given protected branch.
  1363  // It requires the GitHub apps to have `write` access to the `content` permission.
  1364  //
  1365  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#get-apps-with-access-to-the-protected-branch
  1366  func (s *RepositoriesService) ListApps(ctx context.Context, owner, repo, branch string) ([]*App, *Response, error) {
  1367  	u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/restrictions/apps", owner, repo, branch)
  1368  	req, err := s.client.NewRequest("GET", u, nil)
  1369  	if err != nil {
  1370  		return nil, nil, err
  1371  	}
  1372  
  1373  	var apps []*App
  1374  	resp, err := s.client.Do(ctx, req, &apps)
  1375  	if err != nil {
  1376  		return nil, resp, err
  1377  	}
  1378  
  1379  	return apps, resp, nil
  1380  }
  1381  
  1382  // ReplaceAppRestrictions replaces the apps that have push access to a given protected branch.
  1383  // It removes all apps that previously had push access and grants push access to the new list of apps.
  1384  // It requires the GitHub apps to have `write` access to the `content` permission.
  1385  //
  1386  // Note: The list of users, apps, and teams in total is limited to 100 items.
  1387  //
  1388  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#set-app-access-restrictions
  1389  func (s *RepositoriesService) ReplaceAppRestrictions(ctx context.Context, owner, repo, branch string, slug []string) ([]*App, *Response, error) {
  1390  	u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/restrictions/apps", owner, repo, branch)
  1391  	req, err := s.client.NewRequest("PUT", u, slug)
  1392  	if err != nil {
  1393  		return nil, nil, err
  1394  	}
  1395  
  1396  	var apps []*App
  1397  	resp, err := s.client.Do(ctx, req, &apps)
  1398  	if err != nil {
  1399  		return nil, nil, err
  1400  	}
  1401  
  1402  	return apps, resp, nil
  1403  }
  1404  
  1405  // AddAppRestrictions grants the specified apps push access to a given protected branch.
  1406  // It requires the GitHub apps to have `write` access to the `content` permission.
  1407  //
  1408  // Note: The list of users, apps, and teams in total is limited to 100 items.
  1409  //
  1410  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#add-app-access-restrictions
  1411  func (s *RepositoriesService) AddAppRestrictions(ctx context.Context, owner, repo, branch string, slug []string) ([]*App, *Response, error) {
  1412  	u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/restrictions/apps", owner, repo, branch)
  1413  	req, err := s.client.NewRequest("POST", u, slug)
  1414  	if err != nil {
  1415  		return nil, nil, err
  1416  	}
  1417  
  1418  	var apps []*App
  1419  	resp, err := s.client.Do(ctx, req, &apps)
  1420  	if err != nil {
  1421  		return nil, nil, err
  1422  	}
  1423  
  1424  	return apps, resp, nil
  1425  }
  1426  
  1427  // RemoveAppRestrictions removes the ability of an app to push to this branch.
  1428  // It requires the GitHub apps to have `write` access to the `content` permission.
  1429  //
  1430  // Note: The list of users, apps, and teams in total is limited to 100 items.
  1431  //
  1432  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#remove-app-access-restrictions
  1433  func (s *RepositoriesService) RemoveAppRestrictions(ctx context.Context, owner, repo, branch string, slug []string) ([]*App, *Response, error) {
  1434  	u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/restrictions/apps", owner, repo, branch)
  1435  	req, err := s.client.NewRequest("DELETE", u, slug)
  1436  	if err != nil {
  1437  		return nil, nil, err
  1438  	}
  1439  
  1440  	var apps []*App
  1441  	resp, err := s.client.Do(ctx, req, &apps)
  1442  	if err != nil {
  1443  		return nil, nil, err
  1444  	}
  1445  
  1446  	return apps, resp, nil
  1447  }
  1448  
  1449  // TransferRequest represents a request to transfer a repository.
  1450  type TransferRequest struct {
  1451  	NewOwner string  `json:"new_owner"`
  1452  	TeamID   []int64 `json:"team_ids,omitempty"`
  1453  }
  1454  
  1455  // Transfer transfers a repository from one account or organization to another.
  1456  //
  1457  // This method might return an *AcceptedError and a status code of
  1458  // 202. This is because this is the status that GitHub returns to signify that
  1459  // it has now scheduled the transfer of the repository in a background task.
  1460  // A follow up request, after a delay of a second or so, should result
  1461  // in a successful request.
  1462  //
  1463  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#transfer-a-repository
  1464  func (s *RepositoriesService) Transfer(ctx context.Context, owner, repo string, transfer TransferRequest) (*Repository, *Response, error) {
  1465  	u := fmt.Sprintf("repos/%v/%v/transfer", owner, repo)
  1466  
  1467  	req, err := s.client.NewRequest("POST", u, &transfer)
  1468  	if err != nil {
  1469  		return nil, nil, err
  1470  	}
  1471  
  1472  	r := new(Repository)
  1473  	resp, err := s.client.Do(ctx, req, r)
  1474  	if err != nil {
  1475  		return nil, resp, err
  1476  	}
  1477  
  1478  	return r, resp, nil
  1479  }
  1480  
  1481  // DispatchRequestOptions represents a request to trigger a repository_dispatch event.
  1482  type DispatchRequestOptions struct {
  1483  	// EventType is a custom webhook event name. (Required.)
  1484  	EventType string `json:"event_type"`
  1485  	// ClientPayload is a custom JSON payload with extra information about the webhook event.
  1486  	// Defaults to an empty JSON object.
  1487  	ClientPayload *json.RawMessage `json:"client_payload,omitempty"`
  1488  }
  1489  
  1490  // Dispatch triggers a repository_dispatch event in a GitHub Actions workflow.
  1491  //
  1492  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#create-a-repository-dispatch-event
  1493  func (s *RepositoriesService) Dispatch(ctx context.Context, owner, repo string, opts DispatchRequestOptions) (*Repository, *Response, error) {
  1494  	u := fmt.Sprintf("repos/%v/%v/dispatches", owner, repo)
  1495  
  1496  	req, err := s.client.NewRequest("POST", u, &opts)
  1497  	if err != nil {
  1498  		return nil, nil, err
  1499  	}
  1500  
  1501  	r := new(Repository)
  1502  	resp, err := s.client.Do(ctx, req, r)
  1503  	if err != nil {
  1504  		return nil, resp, err
  1505  	}
  1506  
  1507  	return r, resp, nil
  1508  }
  1509  

View as plain text