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