github.com/google/go-github/v71@v71.0.0/github/repos_pages.go (about)

     1  // Copyright 2014 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  	"fmt"
    11  )
    12  
    13  // Pages represents a GitHub Pages site configuration.
    14  type Pages struct {
    15  	URL              *string                `json:"url,omitempty"`
    16  	Status           *string                `json:"status,omitempty"`
    17  	CNAME            *string                `json:"cname,omitempty"`
    18  	Custom404        *bool                  `json:"custom_404,omitempty"`
    19  	HTMLURL          *string                `json:"html_url,omitempty"`
    20  	BuildType        *string                `json:"build_type,omitempty"`
    21  	Source           *PagesSource           `json:"source,omitempty"`
    22  	Public           *bool                  `json:"public,omitempty"`
    23  	HTTPSCertificate *PagesHTTPSCertificate `json:"https_certificate,omitempty"`
    24  	HTTPSEnforced    *bool                  `json:"https_enforced,omitempty"`
    25  }
    26  
    27  // PagesSource represents a GitHub page's source.
    28  type PagesSource struct {
    29  	Branch *string `json:"branch,omitempty"`
    30  	Path   *string `json:"path,omitempty"`
    31  }
    32  
    33  // PagesError represents a build error for a GitHub Pages site.
    34  type PagesError struct {
    35  	Message *string `json:"message,omitempty"`
    36  }
    37  
    38  // PagesBuild represents the build information for a GitHub Pages site.
    39  type PagesBuild struct {
    40  	URL       *string     `json:"url,omitempty"`
    41  	Status    *string     `json:"status,omitempty"`
    42  	Error     *PagesError `json:"error,omitempty"`
    43  	Pusher    *User       `json:"pusher,omitempty"`
    44  	Commit    *string     `json:"commit,omitempty"`
    45  	Duration  *int        `json:"duration,omitempty"`
    46  	CreatedAt *Timestamp  `json:"created_at,omitempty"`
    47  	UpdatedAt *Timestamp  `json:"updated_at,omitempty"`
    48  }
    49  
    50  // PagesDomain represents a domain associated with a GitHub Pages site.
    51  type PagesDomain struct {
    52  	Host                          *string `json:"host,omitempty"`
    53  	URI                           *string `json:"uri,omitempty"`
    54  	Nameservers                   *string `json:"nameservers,omitempty"`
    55  	DNSResolves                   *bool   `json:"dns_resolves,omitempty"`
    56  	IsProxied                     *bool   `json:"is_proxied,omitempty"`
    57  	IsCloudflareIP                *bool   `json:"is_cloudflare_ip,omitempty"`
    58  	IsFastlyIP                    *bool   `json:"is_fastly_ip,omitempty"`
    59  	IsOldIPAddress                *bool   `json:"is_old_ip_address,omitempty"`
    60  	IsARecord                     *bool   `json:"is_a_record,omitempty"`
    61  	HasCNAMERecord                *bool   `json:"has_cname_record,omitempty"`
    62  	HasMXRecordsPresent           *bool   `json:"has_mx_records_present,omitempty"`
    63  	IsValidDomain                 *bool   `json:"is_valid_domain,omitempty"`
    64  	IsApexDomain                  *bool   `json:"is_apex_domain,omitempty"`
    65  	ShouldBeARecord               *bool   `json:"should_be_a_record,omitempty"`
    66  	IsCNAMEToGithubUserDomain     *bool   `json:"is_cname_to_github_user_domain,omitempty"`
    67  	IsCNAMEToPagesDotGithubDotCom *bool   `json:"is_cname_to_pages_dot_github_dot_com,omitempty"`
    68  	IsCNAMEToFastly               *bool   `json:"is_cname_to_fastly,omitempty"`
    69  	IsPointedToGithubPagesIP      *bool   `json:"is_pointed_to_github_pages_ip,omitempty"`
    70  	IsNonGithubPagesIPPresent     *bool   `json:"is_non_github_pages_ip_present,omitempty"`
    71  	IsPagesDomain                 *bool   `json:"is_pages_domain,omitempty"`
    72  	IsServedByPages               *bool   `json:"is_served_by_pages,omitempty"`
    73  	IsValid                       *bool   `json:"is_valid,omitempty"`
    74  	Reason                        *string `json:"reason,omitempty"`
    75  	RespondsToHTTPS               *bool   `json:"responds_to_https,omitempty"`
    76  	EnforcesHTTPS                 *bool   `json:"enforces_https,omitempty"`
    77  	HTTPSError                    *string `json:"https_error,omitempty"`
    78  	IsHTTPSEligible               *bool   `json:"is_https_eligible,omitempty"`
    79  	CAAError                      *string `json:"caa_error,omitempty"`
    80  }
    81  
    82  // PagesHealthCheckResponse represents the response given for the health check of a GitHub Pages site.
    83  type PagesHealthCheckResponse struct {
    84  	Domain    *PagesDomain `json:"domain,omitempty"`
    85  	AltDomain *PagesDomain `json:"alt_domain,omitempty"`
    86  }
    87  
    88  // PagesHTTPSCertificate represents the HTTPS Certificate information for a GitHub Pages site.
    89  type PagesHTTPSCertificate struct {
    90  	State       *string  `json:"state,omitempty"`
    91  	Description *string  `json:"description,omitempty"`
    92  	Domains     []string `json:"domains,omitempty"`
    93  	// GitHub's API doesn't return a standard Timestamp, rather it returns a YYYY-MM-DD string.
    94  	ExpiresAt *string `json:"expires_at,omitempty"`
    95  }
    96  
    97  // createPagesRequest is a subset of Pages and is used internally
    98  // by EnablePages to pass only the known fields for the endpoint.
    99  type createPagesRequest struct {
   100  	BuildType *string      `json:"build_type,omitempty"`
   101  	Source    *PagesSource `json:"source,omitempty"`
   102  }
   103  
   104  // EnablePages enables GitHub Pages for the named repo.
   105  //
   106  // GitHub API docs: https://docs.github.com/rest/pages/pages#create-a-github-pages-site
   107  //
   108  //meta:operation POST /repos/{owner}/{repo}/pages
   109  func (s *RepositoriesService) EnablePages(ctx context.Context, owner, repo string, pages *Pages) (*Pages, *Response, error) {
   110  	u := fmt.Sprintf("repos/%v/%v/pages", owner, repo)
   111  
   112  	pagesReq := &createPagesRequest{
   113  		BuildType: pages.BuildType,
   114  		Source:    pages.Source,
   115  	}
   116  
   117  	req, err := s.client.NewRequest("POST", u, pagesReq)
   118  	if err != nil {
   119  		return nil, nil, err
   120  	}
   121  
   122  	req.Header.Set("Accept", mediaTypeEnablePagesAPIPreview)
   123  
   124  	enable := new(Pages)
   125  	resp, err := s.client.Do(ctx, req, enable)
   126  	if err != nil {
   127  		return nil, resp, err
   128  	}
   129  
   130  	return enable, resp, nil
   131  }
   132  
   133  // PagesUpdate sets up parameters needed to update a GitHub Pages site.
   134  type PagesUpdate struct {
   135  	// CNAME represents a custom domain for the repository.
   136  	// Leaving CNAME empty will remove the custom domain.
   137  	CNAME *string `json:"cname"`
   138  	// BuildType is optional and can either be "legacy" or "workflow".
   139  	// "workflow" - You are using a github workflow to build your pages.
   140  	// "legacy"   - You are deploying from a branch.
   141  	BuildType *string `json:"build_type,omitempty"`
   142  	// Source must include the branch name, and may optionally specify the subdirectory "/docs".
   143  	// Possible values for Source.Branch are usually "gh-pages", "main", and "master",
   144  	// or any other existing branch name.
   145  	// Possible values for Source.Path are: "/", and "/docs".
   146  	Source *PagesSource `json:"source,omitempty"`
   147  	// Public configures access controls for the site.
   148  	// If "true", the site will be accessible to anyone on the internet. If "false",
   149  	// the site will be accessible to anyone with read access to the repository that
   150  	// published the site.
   151  	Public *bool `json:"public,omitempty"`
   152  	// HTTPSEnforced specifies whether HTTPS should be enforced for the repository.
   153  	HTTPSEnforced *bool `json:"https_enforced,omitempty"`
   154  }
   155  
   156  // UpdatePages updates GitHub Pages for the named repo.
   157  //
   158  // GitHub API docs: https://docs.github.com/rest/pages/pages#update-information-about-a-github-pages-site
   159  //
   160  //meta:operation PUT /repos/{owner}/{repo}/pages
   161  func (s *RepositoriesService) UpdatePages(ctx context.Context, owner, repo string, opts *PagesUpdate) (*Response, error) {
   162  	u := fmt.Sprintf("repos/%v/%v/pages", owner, repo)
   163  
   164  	req, err := s.client.NewRequest("PUT", u, opts)
   165  	if err != nil {
   166  		return nil, err
   167  	}
   168  
   169  	resp, err := s.client.Do(ctx, req, nil)
   170  	if err != nil {
   171  		return resp, err
   172  	}
   173  	return resp, nil
   174  }
   175  
   176  // PagesUpdateWithoutCNAME defines parameters for updating a GitHub Pages site on GitHub Enterprise Servers.
   177  // Sending a request with a CNAME (any value, empty string, or null) results in a 400 error: "Custom domains are not available for GitHub Pages".
   178  type PagesUpdateWithoutCNAME struct {
   179  	BuildType     *string      `json:"build_type,omitempty"`
   180  	Source        *PagesSource `json:"source,omitempty"`
   181  	Public        *bool        `json:"public,omitempty"`
   182  	HTTPSEnforced *bool        `json:"https_enforced,omitempty"`
   183  }
   184  
   185  // UpdatePagesGHES updates GitHub Pages for the named repo in GitHub Enterprise Servers.
   186  //
   187  // GitHub API docs: https://docs.github.com/rest/pages/pages#update-information-about-a-github-pages-site
   188  //
   189  //meta:operation PUT /repos/{owner}/{repo}/pages
   190  func (s *RepositoriesService) UpdatePagesGHES(ctx context.Context, owner, repo string, opts *PagesUpdateWithoutCNAME) (*Response, error) {
   191  	u := fmt.Sprintf("repos/%v/%v/pages", owner, repo)
   192  
   193  	req, err := s.client.NewRequest("PUT", u, opts)
   194  
   195  	if err != nil {
   196  		return nil, err
   197  	}
   198  
   199  	resp, err := s.client.Do(ctx, req, nil)
   200  	if err != nil {
   201  		return resp, err
   202  	}
   203  	return resp, nil
   204  }
   205  
   206  // DisablePages disables GitHub Pages for the named repo.
   207  //
   208  // GitHub API docs: https://docs.github.com/rest/pages/pages#delete-a-github-pages-site
   209  //
   210  //meta:operation DELETE /repos/{owner}/{repo}/pages
   211  func (s *RepositoriesService) DisablePages(ctx context.Context, owner, repo string) (*Response, error) {
   212  	u := fmt.Sprintf("repos/%v/%v/pages", owner, repo)
   213  	req, err := s.client.NewRequest("DELETE", u, nil)
   214  	if err != nil {
   215  		return nil, err
   216  	}
   217  
   218  	// TODO: remove custom Accept header when this API fully launches.
   219  	req.Header.Set("Accept", mediaTypeEnablePagesAPIPreview)
   220  
   221  	return s.client.Do(ctx, req, nil)
   222  }
   223  
   224  // GetPagesInfo fetches information about a GitHub Pages site.
   225  //
   226  // GitHub API docs: https://docs.github.com/rest/pages/pages#get-a-github-pages-site
   227  //
   228  //meta:operation GET /repos/{owner}/{repo}/pages
   229  func (s *RepositoriesService) GetPagesInfo(ctx context.Context, owner, repo string) (*Pages, *Response, error) {
   230  	u := fmt.Sprintf("repos/%v/%v/pages", owner, repo)
   231  	req, err := s.client.NewRequest("GET", u, nil)
   232  	if err != nil {
   233  		return nil, nil, err
   234  	}
   235  
   236  	site := new(Pages)
   237  	resp, err := s.client.Do(ctx, req, site)
   238  	if err != nil {
   239  		return nil, resp, err
   240  	}
   241  
   242  	return site, resp, nil
   243  }
   244  
   245  // ListPagesBuilds lists the builds for a GitHub Pages site.
   246  //
   247  // GitHub API docs: https://docs.github.com/rest/pages/pages#list-github-pages-builds
   248  //
   249  //meta:operation GET /repos/{owner}/{repo}/pages/builds
   250  func (s *RepositoriesService) ListPagesBuilds(ctx context.Context, owner, repo string, opts *ListOptions) ([]*PagesBuild, *Response, error) {
   251  	u := fmt.Sprintf("repos/%v/%v/pages/builds", owner, repo)
   252  	u, err := addOptions(u, opts)
   253  	if err != nil {
   254  		return nil, nil, err
   255  	}
   256  
   257  	req, err := s.client.NewRequest("GET", u, nil)
   258  	if err != nil {
   259  		return nil, nil, err
   260  	}
   261  
   262  	var pages []*PagesBuild
   263  	resp, err := s.client.Do(ctx, req, &pages)
   264  	if err != nil {
   265  		return nil, resp, err
   266  	}
   267  
   268  	return pages, resp, nil
   269  }
   270  
   271  // GetLatestPagesBuild fetches the latest build information for a GitHub pages site.
   272  //
   273  // GitHub API docs: https://docs.github.com/rest/pages/pages#get-latest-pages-build
   274  //
   275  //meta:operation GET /repos/{owner}/{repo}/pages/builds/latest
   276  func (s *RepositoriesService) GetLatestPagesBuild(ctx context.Context, owner, repo string) (*PagesBuild, *Response, error) {
   277  	u := fmt.Sprintf("repos/%v/%v/pages/builds/latest", owner, repo)
   278  	req, err := s.client.NewRequest("GET", u, nil)
   279  	if err != nil {
   280  		return nil, nil, err
   281  	}
   282  
   283  	build := new(PagesBuild)
   284  	resp, err := s.client.Do(ctx, req, build)
   285  	if err != nil {
   286  		return nil, resp, err
   287  	}
   288  
   289  	return build, resp, nil
   290  }
   291  
   292  // GetPageBuild fetches the specific build information for a GitHub pages site.
   293  //
   294  // GitHub API docs: https://docs.github.com/rest/pages/pages#get-github-pages-build
   295  //
   296  //meta:operation GET /repos/{owner}/{repo}/pages/builds/{build_id}
   297  func (s *RepositoriesService) GetPageBuild(ctx context.Context, owner, repo string, id int64) (*PagesBuild, *Response, error) {
   298  	u := fmt.Sprintf("repos/%v/%v/pages/builds/%v", owner, repo, id)
   299  	req, err := s.client.NewRequest("GET", u, nil)
   300  	if err != nil {
   301  		return nil, nil, err
   302  	}
   303  
   304  	build := new(PagesBuild)
   305  	resp, err := s.client.Do(ctx, req, build)
   306  	if err != nil {
   307  		return nil, resp, err
   308  	}
   309  
   310  	return build, resp, nil
   311  }
   312  
   313  // RequestPageBuild requests a build of a GitHub Pages site without needing to push new commit.
   314  //
   315  // GitHub API docs: https://docs.github.com/rest/pages/pages#request-a-github-pages-build
   316  //
   317  //meta:operation POST /repos/{owner}/{repo}/pages/builds
   318  func (s *RepositoriesService) RequestPageBuild(ctx context.Context, owner, repo string) (*PagesBuild, *Response, error) {
   319  	u := fmt.Sprintf("repos/%v/%v/pages/builds", owner, repo)
   320  	req, err := s.client.NewRequest("POST", u, nil)
   321  	if err != nil {
   322  		return nil, nil, err
   323  	}
   324  
   325  	build := new(PagesBuild)
   326  	resp, err := s.client.Do(ctx, req, build)
   327  	if err != nil {
   328  		return nil, resp, err
   329  	}
   330  
   331  	return build, resp, nil
   332  }
   333  
   334  // GetPageHealthCheck gets a DNS health check for the CNAME record configured for a repository's GitHub Pages.
   335  //
   336  // GitHub API docs: https://docs.github.com/rest/pages/pages#get-a-dns-health-check-for-github-pages
   337  //
   338  //meta:operation GET /repos/{owner}/{repo}/pages/health
   339  func (s *RepositoriesService) GetPageHealthCheck(ctx context.Context, owner, repo string) (*PagesHealthCheckResponse, *Response, error) {
   340  	u := fmt.Sprintf("repos/%v/%v/pages/health", owner, repo)
   341  	req, err := s.client.NewRequest("GET", u, nil)
   342  	if err != nil {
   343  		return nil, nil, err
   344  	}
   345  
   346  	healthCheckResponse := new(PagesHealthCheckResponse)
   347  	resp, err := s.client.Do(ctx, req, healthCheckResponse)
   348  	if err != nil {
   349  		return nil, resp, err
   350  	}
   351  
   352  	return healthCheckResponse, resp, nil
   353  }