diff --git a/github/github-accessors.go b/github/github-accessors.go index 8598de67f6e..69547c4f09a 100644 --- a/github/github-accessors.go +++ b/github/github-accessors.go @@ -7220,6 +7220,22 @@ func (p *PageStats) GetTotalPages() int { return *p.TotalPages } +// GetCNAME returns the CNAME field if it's non-nil, zero value otherwise. +func (p *PagesUpdate) GetCNAME() string { + if p == nil || p.CNAME == nil { + return "" + } + return *p.CNAME +} + +// GetSource returns the Source field if it's non-nil, zero value otherwise. +func (p *PagesUpdate) GetSource() string { + if p == nil || p.Source == nil { + return "" + } + return *p.Source +} + // GetHook returns the Hook field. func (p *PingEvent) GetHook() *Hook { if p == nil { diff --git a/github/repos_pages.go b/github/repos_pages.go index ff8d2d55b9c..58826527d9d 100644 --- a/github/repos_pages.go +++ b/github/repos_pages.go @@ -75,6 +75,35 @@ func (s *RepositoriesService) EnablePages(ctx context.Context, owner, repo strin return enable, resp, nil } +// PagesUpdate sets up parameters needed to update a GitHub Pages site. +type PagesUpdate struct { + // CNAME represents a custom domain for the repository. + // Leaving CNAME empty will remove the custom domain. + CNAME *string `json:"cname"` + // Source must include the branch name, and may optionally specify the subdirectory "/docs". + // Possible values are: "gh-pages", "master", and "master /docs". + Source *string `json:"source,omitempty"` +} + +// UpdatePages updates GitHub Pages for the named repo. +// +// GitHub API docs: https://developer.github.com/v3/repos/pages/#update-information-about-a-pages-site +func (s *RepositoriesService) UpdatePages(ctx context.Context, owner, repo string, opts *PagesUpdate) (*Response, error) { + u := fmt.Sprintf("repos/%v/%v/pages", owner, repo) + + req, err := s.client.NewRequest("PUT", u, opts) + if err != nil { + return nil, err + } + + resp, err := s.client.Do(ctx, req, nil) + if err != nil { + return resp, err + } + + return resp, nil +} + // DisablePages disables GitHub Pages for the named repo. // // GitHub API docs: https://developer.github.com/v3/repos/pages/#disable-a-pages-site diff --git a/github/repos_pages_test.go b/github/repos_pages_test.go index 8fe38eb3e24..b2aa272939a 100644 --- a/github/repos_pages_test.go +++ b/github/repos_pages_test.go @@ -6,9 +6,11 @@ package github import ( + "bytes" "context" "encoding/json" "fmt" + "io/ioutil" "net/http" "reflect" "testing" @@ -52,6 +54,62 @@ func TestRepositoriesService_EnablePages(t *testing.T) { } } +func TestRepositoriesService_UpdatePages(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + input := &PagesUpdate{ + CNAME: String("www.my-domain.com"), + Source: String("gh-pages"), + } + + mux.HandleFunc("/repos/o/r/pages", func(w http.ResponseWriter, r *http.Request) { + v := new(PagesUpdate) + json.NewDecoder(r.Body).Decode(v) + + testMethod(t, r, "PUT") + want := &PagesUpdate{CNAME: String("www.my-domain.com"), Source: String("gh-pages")} + if !reflect.DeepEqual(v, want) { + t.Errorf("Request body = %+v, want %+v", v, want) + } + + fmt.Fprint(w, `{"cname":"www.my-domain.com","source":"gh-pages"}`) + }) + + _, err := client.Repositories.UpdatePages(context.Background(), "o", "r", input) + if err != nil { + t.Errorf("Repositories.UpdatePages returned error: %v", err) + } +} + +func TestRepositoriesService_UpdatePages_NullCNAME(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + input := &PagesUpdate{ + Source: String("gh-pages"), + } + + mux.HandleFunc("/repos/o/r/pages", func(w http.ResponseWriter, r *http.Request) { + got, err := ioutil.ReadAll(r.Body) + if err != nil { + t.Fatalf("unable to read body: %v", err) + } + + want := []byte(`{"cname":null,"source":"gh-pages"}` + "\n") + if !bytes.Equal(got, want) { + t.Errorf("Request body = %+v, want %+v", got, want) + } + + fmt.Fprint(w, `{"cname":null,"source":"gh-pages"}`) + }) + + _, err := client.Repositories.UpdatePages(context.Background(), "o", "r", input) + if err != nil { + t.Errorf("Repositories.UpdatePages returned error: %v", err) + } +} + func TestRepositoriesService_DisablePages(t *testing.T) { client, mux, _, teardown := setup() defer teardown()