github.com/google/go-github/v42@v42.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  	Source           *PagesSource           `json:"source,omitempty"`
    21  	Public           *bool                  `json:"public,omitempty"`
    22  	HTTPSCertificate *PagesHTTPSCertificate `json:"https_certificate,omitempty"`
    23  	HTTPSEnforced    *bool                  `json:"https_enforced,omitempty"`
    24  }
    25  
    26  // PagesSource represents a GitHub page's source.
    27  type PagesSource struct {
    28  	Branch *string `json:"branch,omitempty"`
    29  	Path   *string `json:"path,omitempty"`
    30  }
    31  
    32  // PagesError represents a build error for a GitHub Pages site.
    33  type PagesError struct {
    34  	Message *string `json:"message,omitempty"`
    35  }
    36  
    37  // PagesBuild represents the build information for a GitHub Pages site.
    38  type PagesBuild struct {
    39  	URL       *string     `json:"url,omitempty"`
    40  	Status    *string     `json:"status,omitempty"`
    41  	Error     *PagesError `json:"error,omitempty"`
    42  	Pusher    *User       `json:"pusher,omitempty"`
    43  	Commit    *string     `json:"commit,omitempty"`
    44  	Duration  *int        `json:"duration,omitempty"`
    45  	CreatedAt *Timestamp  `json:"created_at,omitempty"`
    46  	UpdatedAt *Timestamp  `json:"updated_at,omitempty"`
    47  }
    48  
    49  // PagesHTTPSCertificate represents the HTTPS Certificate information for a GitHub Pages site.
    50  type PagesHTTPSCertificate struct {
    51  	State       *string  `json:"state,omitempty"`
    52  	Description *string  `json:"description,omitempty"`
    53  	Domains     []string `json:"domains,omitempty"`
    54  	// GitHub's API doesn't return a standard Timestamp, rather it returns a YYYY-MM-DD string.
    55  	ExpiresAt *string `json:"expires_at,omitempty"`
    56  }
    57  
    58  // createPagesRequest is a subset of Pages and is used internally
    59  // by EnablePages to pass only the known fields for the endpoint.
    60  type createPagesRequest struct {
    61  	Source *PagesSource `json:"source,omitempty"`
    62  }
    63  
    64  // EnablePages enables GitHub Pages for the named repo.
    65  //
    66  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#create-a-github-pages-site
    67  func (s *RepositoriesService) EnablePages(ctx context.Context, owner, repo string, pages *Pages) (*Pages, *Response, error) {
    68  	u := fmt.Sprintf("repos/%v/%v/pages", owner, repo)
    69  
    70  	pagesReq := &createPagesRequest{
    71  		Source: pages.Source,
    72  	}
    73  
    74  	req, err := s.client.NewRequest("POST", u, pagesReq)
    75  	if err != nil {
    76  		return nil, nil, err
    77  	}
    78  
    79  	req.Header.Set("Accept", mediaTypeEnablePagesAPIPreview)
    80  
    81  	enable := new(Pages)
    82  	resp, err := s.client.Do(ctx, req, enable)
    83  	if err != nil {
    84  		return nil, resp, err
    85  	}
    86  
    87  	return enable, resp, nil
    88  }
    89  
    90  // PagesUpdate sets up parameters needed to update a GitHub Pages site.
    91  type PagesUpdate struct {
    92  	// CNAME represents a custom domain for the repository.
    93  	// Leaving CNAME empty will remove the custom domain.
    94  	CNAME *string `json:"cname"`
    95  	// Source must include the branch name, and may optionally specify the subdirectory "/docs".
    96  	// Possible values are: "gh-pages", "master", and "master /docs".
    97  	Source *string `json:"source,omitempty"`
    98  }
    99  
   100  // UpdatePages updates GitHub Pages for the named repo.
   101  //
   102  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#update-information-about-a-github-pages-site
   103  func (s *RepositoriesService) UpdatePages(ctx context.Context, owner, repo string, opts *PagesUpdate) (*Response, error) {
   104  	u := fmt.Sprintf("repos/%v/%v/pages", owner, repo)
   105  
   106  	req, err := s.client.NewRequest("PUT", u, opts)
   107  	if err != nil {
   108  		return nil, err
   109  	}
   110  
   111  	resp, err := s.client.Do(ctx, req, nil)
   112  	if err != nil {
   113  		return resp, err
   114  	}
   115  
   116  	return resp, nil
   117  }
   118  
   119  // DisablePages disables GitHub Pages for the named repo.
   120  //
   121  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#delete-a-github-pages-site
   122  func (s *RepositoriesService) DisablePages(ctx context.Context, owner, repo string) (*Response, error) {
   123  	u := fmt.Sprintf("repos/%v/%v/pages", owner, repo)
   124  	req, err := s.client.NewRequest("DELETE", u, nil)
   125  	if err != nil {
   126  		return nil, err
   127  	}
   128  
   129  	// TODO: remove custom Accept header when this API fully launches.
   130  	req.Header.Set("Accept", mediaTypeEnablePagesAPIPreview)
   131  
   132  	return s.client.Do(ctx, req, nil)
   133  }
   134  
   135  // GetPagesInfo fetches information about a GitHub Pages site.
   136  //
   137  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#get-a-github-pages-site
   138  func (s *RepositoriesService) GetPagesInfo(ctx context.Context, owner, repo string) (*Pages, *Response, error) {
   139  	u := fmt.Sprintf("repos/%v/%v/pages", owner, repo)
   140  	req, err := s.client.NewRequest("GET", u, nil)
   141  	if err != nil {
   142  		return nil, nil, err
   143  	}
   144  
   145  	site := new(Pages)
   146  	resp, err := s.client.Do(ctx, req, site)
   147  	if err != nil {
   148  		return nil, resp, err
   149  	}
   150  
   151  	return site, resp, nil
   152  }
   153  
   154  // ListPagesBuilds lists the builds for a GitHub Pages site.
   155  //
   156  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#list-github-pages-builds
   157  func (s *RepositoriesService) ListPagesBuilds(ctx context.Context, owner, repo string, opts *ListOptions) ([]*PagesBuild, *Response, error) {
   158  	u := fmt.Sprintf("repos/%v/%v/pages/builds", owner, repo)
   159  	u, err := addOptions(u, opts)
   160  	if err != nil {
   161  		return nil, nil, err
   162  	}
   163  
   164  	req, err := s.client.NewRequest("GET", u, nil)
   165  	if err != nil {
   166  		return nil, nil, err
   167  	}
   168  
   169  	var pages []*PagesBuild
   170  	resp, err := s.client.Do(ctx, req, &pages)
   171  	if err != nil {
   172  		return nil, resp, err
   173  	}
   174  
   175  	return pages, resp, nil
   176  }
   177  
   178  // GetLatestPagesBuild fetches the latest build information for a GitHub pages site.
   179  //
   180  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#get-latest-pages-build
   181  func (s *RepositoriesService) GetLatestPagesBuild(ctx context.Context, owner, repo string) (*PagesBuild, *Response, error) {
   182  	u := fmt.Sprintf("repos/%v/%v/pages/builds/latest", owner, repo)
   183  	req, err := s.client.NewRequest("GET", u, nil)
   184  	if err != nil {
   185  		return nil, nil, err
   186  	}
   187  
   188  	build := new(PagesBuild)
   189  	resp, err := s.client.Do(ctx, req, build)
   190  	if err != nil {
   191  		return nil, resp, err
   192  	}
   193  
   194  	return build, resp, nil
   195  }
   196  
   197  // GetPageBuild fetches the specific build information for a GitHub pages site.
   198  //
   199  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#get-github-pages-build
   200  func (s *RepositoriesService) GetPageBuild(ctx context.Context, owner, repo string, id int64) (*PagesBuild, *Response, error) {
   201  	u := fmt.Sprintf("repos/%v/%v/pages/builds/%v", owner, repo, id)
   202  	req, err := s.client.NewRequest("GET", u, nil)
   203  	if err != nil {
   204  		return nil, nil, err
   205  	}
   206  
   207  	build := new(PagesBuild)
   208  	resp, err := s.client.Do(ctx, req, build)
   209  	if err != nil {
   210  		return nil, resp, err
   211  	}
   212  
   213  	return build, resp, nil
   214  }
   215  
   216  // RequestPageBuild requests a build of a GitHub Pages site without needing to push new commit.
   217  //
   218  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#request-a-github-pages-build
   219  func (s *RepositoriesService) RequestPageBuild(ctx context.Context, owner, repo string) (*PagesBuild, *Response, error) {
   220  	u := fmt.Sprintf("repos/%v/%v/pages/builds", owner, repo)
   221  	req, err := s.client.NewRequest("POST", u, nil)
   222  	if err != nil {
   223  		return nil, nil, err
   224  	}
   225  
   226  	build := new(PagesBuild)
   227  	resp, err := s.client.Do(ctx, req, build)
   228  	if err != nil {
   229  		return nil, resp, err
   230  	}
   231  
   232  	return build, resp, nil
   233  }