...

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

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

View as plain text