Skip to content
16 changes: 16 additions & 0 deletions github/github-accessors.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions github/repos_pages.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
58 changes: 58 additions & 0 deletions github/repos_pages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
package github

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"reflect"
"testing"
Expand Down Expand Up @@ -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()
Expand Down