...

Source file src/github.com/google/go-github/v45/github/repos_pages.go

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

     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/rest/pages#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  	// Public configures access controls for the site.
    99  	// If "true", the site will be accessible to anyone on the internet. If "false",
   100  	// the site will be accessible to anyone with read access to the repository that
   101  	// published the site.
   102  	Public *bool `json:"public,omitempty"`
   103  	// HTTPSEnforced specifies whether HTTPS should be enforced for the repository.
   104  	HTTPSEnforced *bool `json:"https_enforced,omitempty"`
   105  }
   106  
   107  // UpdatePages updates GitHub Pages for the named repo.
   108  //
   109  // GitHub API docs: https://docs.github.com/en/rest/pages#update-information-about-a-github-pages-site
   110  func (s *RepositoriesService) UpdatePages(ctx context.Context, owner, repo string, opts *PagesUpdate) (*Response, error) {
   111  	u := fmt.Sprintf("repos/%v/%v/pages", owner, repo)
   112  
   113  	req, err := s.client.NewRequest("PUT", u, opts)
   114  	if err != nil {
   115  		return nil, err
   116  	}
   117  
   118  	resp, err := s.client.Do(ctx, req, nil)
   119  	if err != nil {
   120  		return resp, err
   121  	}
   122  
   123  	return resp, nil
   124  }
   125  
   126  // DisablePages disables GitHub Pages for the named repo.
   127  //
   128  // GitHub API docs: https://docs.github.com/en/rest/pages#delete-a-github-pages-site
   129  func (s *RepositoriesService) DisablePages(ctx context.Context, owner, repo string) (*Response, error) {
   130  	u := fmt.Sprintf("repos/%v/%v/pages", owner, repo)
   131  	req, err := s.client.NewRequest("DELETE", u, nil)
   132  	if err != nil {
   133  		return nil, err
   134  	}
   135  
   136  	// TODO: remove custom Accept header when this API fully launches.
   137  	req.Header.Set("Accept", mediaTypeEnablePagesAPIPreview)
   138  
   139  	return s.client.Do(ctx, req, nil)
   140  }
   141  
   142  // GetPagesInfo fetches information about a GitHub Pages site.
   143  //
   144  // GitHub API docs: https://docs.github.com/en/rest/pages#get-a-github-pages-site
   145  func (s *RepositoriesService) GetPagesInfo(ctx context.Context, owner, repo string) (*Pages, *Response, error) {
   146  	u := fmt.Sprintf("repos/%v/%v/pages", owner, repo)
   147  	req, err := s.client.NewRequest("GET", u, nil)
   148  	if err != nil {
   149  		return nil, nil, err
   150  	}
   151  
   152  	site := new(Pages)
   153  	resp, err := s.client.Do(ctx, req, site)
   154  	if err != nil {
   155  		return nil, resp, err
   156  	}
   157  
   158  	return site, resp, nil
   159  }
   160  
   161  // ListPagesBuilds lists the builds for a GitHub Pages site.
   162  //
   163  // GitHub API docs: https://docs.github.com/en/rest/pages#list-github-pages-builds
   164  func (s *RepositoriesService) ListPagesBuilds(ctx context.Context, owner, repo string, opts *ListOptions) ([]*PagesBuild, *Response, error) {
   165  	u := fmt.Sprintf("repos/%v/%v/pages/builds", owner, repo)
   166  	u, err := addOptions(u, opts)
   167  	if err != nil {
   168  		return nil, nil, err
   169  	}
   170  
   171  	req, err := s.client.NewRequest("GET", u, nil)
   172  	if err != nil {
   173  		return nil, nil, err
   174  	}
   175  
   176  	var pages []*PagesBuild
   177  	resp, err := s.client.Do(ctx, req, &pages)
   178  	if err != nil {
   179  		return nil, resp, err
   180  	}
   181  
   182  	return pages, resp, nil
   183  }
   184  
   185  // GetLatestPagesBuild fetches the latest build information for a GitHub pages site.
   186  //
   187  // GitHub API docs: https://docs.github.com/en/rest/pages#get-latest-pages-build
   188  func (s *RepositoriesService) GetLatestPagesBuild(ctx context.Context, owner, repo string) (*PagesBuild, *Response, error) {
   189  	u := fmt.Sprintf("repos/%v/%v/pages/builds/latest", owner, repo)
   190  	req, err := s.client.NewRequest("GET", u, nil)
   191  	if err != nil {
   192  		return nil, nil, err
   193  	}
   194  
   195  	build := new(PagesBuild)
   196  	resp, err := s.client.Do(ctx, req, build)
   197  	if err != nil {
   198  		return nil, resp, err
   199  	}
   200  
   201  	return build, resp, nil
   202  }
   203  
   204  // GetPageBuild fetches the specific build information for a GitHub pages site.
   205  //
   206  // GitHub API docs: https://docs.github.com/en/rest/pages#get-github-pages-build
   207  func (s *RepositoriesService) GetPageBuild(ctx context.Context, owner, repo string, id int64) (*PagesBuild, *Response, error) {
   208  	u := fmt.Sprintf("repos/%v/%v/pages/builds/%v", owner, repo, id)
   209  	req, err := s.client.NewRequest("GET", u, nil)
   210  	if err != nil {
   211  		return nil, nil, err
   212  	}
   213  
   214  	build := new(PagesBuild)
   215  	resp, err := s.client.Do(ctx, req, build)
   216  	if err != nil {
   217  		return nil, resp, err
   218  	}
   219  
   220  	return build, resp, nil
   221  }
   222  
   223  // RequestPageBuild requests a build of a GitHub Pages site without needing to push new commit.
   224  //
   225  // GitHub API docs: https://docs.github.com/en/rest/pages#request-a-github-pages-build
   226  func (s *RepositoriesService) RequestPageBuild(ctx context.Context, owner, repo string) (*PagesBuild, *Response, error) {
   227  	u := fmt.Sprintf("repos/%v/%v/pages/builds", owner, repo)
   228  	req, err := s.client.NewRequest("POST", u, nil)
   229  	if err != nil {
   230  		return nil, nil, err
   231  	}
   232  
   233  	build := new(PagesBuild)
   234  	resp, err := s.client.Do(ctx, req, build)
   235  	if err != nil {
   236  		return nil, resp, err
   237  	}
   238  
   239  	return build, resp, nil
   240  }
   241  

View as plain text