Skip to content

Commit

Permalink
Add BuildType to Github Repo Pages
Browse files Browse the repository at this point in the history
resolves: #2723
  • Loading branch information
brandonstubbs committed Mar 28, 2023
1 parent a934ccc commit 13c9341
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 11 deletions.
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.

20 changes: 20 additions & 0 deletions github/github-accessors_test.go

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

11 changes: 9 additions & 2 deletions github/repos_pages.go
Expand Up @@ -17,6 +17,7 @@ type Pages struct {
CNAME *string `json:"cname,omitempty"`
Custom404 *bool `json:"custom_404,omitempty"`
HTMLURL *string `json:"html_url,omitempty"`
BuildType *string `json:"build_type,omitempty"`
Source *PagesSource `json:"source,omitempty"`
Public *bool `json:"public,omitempty"`
HTTPSCertificate *PagesHTTPSCertificate `json:"https_certificate,omitempty"`
Expand Down Expand Up @@ -58,7 +59,8 @@ type PagesHTTPSCertificate struct {
// createPagesRequest is a subset of Pages and is used internally
// by EnablePages to pass only the known fields for the endpoint.
type createPagesRequest struct {
Source *PagesSource `json:"source,omitempty"`
BuildType *string `json:"build_type,omitempty"`
Source *PagesSource `json:"source,omitempty"`
}

// EnablePages enables GitHub Pages for the named repo.
Expand All @@ -68,7 +70,8 @@ func (s *RepositoriesService) EnablePages(ctx context.Context, owner, repo strin
u := fmt.Sprintf("repos/%v/%v/pages", owner, repo)

pagesReq := &createPagesRequest{
Source: pages.Source,
BuildType: pages.BuildType,
Source: pages.Source,
}

req, err := s.client.NewRequest("POST", u, pagesReq)
Expand All @@ -92,6 +95,10 @@ type PagesUpdate struct {
// CNAME represents a custom domain for the repository.
// Leaving CNAME empty will remove the custom domain.
CNAME *string `json:"cname"`
// BuildType is optional and can either be "legacy" or "workflow".
// "workflow" - You are using a github workflow to build your pages.
// "legacy" - You are deploying from a branch.
BuildType *string `json:"build_type,omitempty"`
// Source must include the branch name, and may optionally specify the subdirectory "/docs".
// Possible values for Source.Branch are usually "gh-pages", "main", and "master",
// or any other existing branch name.
Expand Down
109 changes: 100 additions & 9 deletions github/repos_pages_test.go
Expand Up @@ -17,11 +17,12 @@ import (
"github.com/google/go-cmp/cmp"
)

func TestRepositoriesService_EnablePages(t *testing.T) {
func TestRepositoriesService_EnablePagesLegacy(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

input := &Pages{
BuildType: String("legacy"),
Source: &PagesSource{
Branch: String("master"),
Path: String("/"),
Expand All @@ -35,12 +36,12 @@ func TestRepositoriesService_EnablePages(t *testing.T) {

testMethod(t, r, "POST")
testHeader(t, r, "Accept", mediaTypeEnablePagesAPIPreview)
want := &createPagesRequest{Source: &PagesSource{Branch: String("master"), Path: String("/")}}
want := &createPagesRequest{BuildType: String("legacy"), Source: &PagesSource{Branch: String("master"), Path: String("/")}}
if !cmp.Equal(v, want) {
t.Errorf("Request body = %+v, want %+v", v, want)
}

fmt.Fprint(w, `{"url":"u","status":"s","cname":"c","custom_404":false,"html_url":"h", "source": {"branch":"master", "path":"/"}}`)
fmt.Fprint(w, `{"url":"u","status":"s","cname":"c","custom_404":false,"html_url":"h","build_type": "legacy","source": {"branch":"master", "path":"/"}}`)
})

ctx := context.Background()
Expand All @@ -49,7 +50,7 @@ func TestRepositoriesService_EnablePages(t *testing.T) {
t.Errorf("Repositories.EnablePages returned error: %v", err)
}

want := &Pages{URL: String("u"), Status: String("s"), CNAME: String("c"), Custom404: Bool(false), HTMLURL: String("h"), Source: &PagesSource{Branch: String("master"), Path: String("/")}}
want := &Pages{URL: String("u"), Status: String("s"), CNAME: String("c"), Custom404: Bool(false), HTMLURL: String("h"), BuildType: String("legacy"), Source: &PagesSource{Branch: String("master"), Path: String("/")}}

if !cmp.Equal(page, want) {
t.Errorf("Repositories.EnablePages returned %v, want %v", page, want)
Expand All @@ -70,26 +71,116 @@ func TestRepositoriesService_EnablePages(t *testing.T) {
})
}

func TestRepositoriesService_UpdatePages(t *testing.T) {
func TestRepositoriesService_EnablePagesWorkflow(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

input := &Pages{
BuildType: String("workflow"),
CNAME: String("www.my-domain.com"), // not passed along.
}

mux.HandleFunc("/repos/o/r/pages", func(w http.ResponseWriter, r *http.Request) {
v := new(createPagesRequest)
json.NewDecoder(r.Body).Decode(v)

testMethod(t, r, "POST")
testHeader(t, r, "Accept", mediaTypeEnablePagesAPIPreview)
want := &createPagesRequest{BuildType: String("workflow")}
if !cmp.Equal(v, want) {
t.Errorf("Request body = %+v, want %+v", v, want)
}

fmt.Fprint(w, `{"url":"u","status":"s","cname":"c","custom_404":false,"html_url":"h","build_type": "workflow"}`)
})

ctx := context.Background()
page, _, err := client.Repositories.EnablePages(ctx, "o", "r", input)
if err != nil {
t.Errorf("Repositories.EnablePages returned error: %v", err)
}

want := &Pages{URL: String("u"), Status: String("s"), CNAME: String("c"), Custom404: Bool(false), HTMLURL: String("h"), BuildType: String("workflow")}

if !cmp.Equal(page, want) {
t.Errorf("Repositories.EnablePages returned %v, want %v", page, want)
}

const methodName = "EnablePages"
testBadOptions(t, methodName, func() (err error) {
_, _, err = client.Repositories.EnablePages(ctx, "\n", "\n", input)
return err
})

testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
got, resp, err := client.Repositories.EnablePages(ctx, "o", "r", input)
if got != nil {
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
}
return resp, err
})
}

func TestRepositoriesService_UpdatePagesLegacy(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

input := &PagesUpdate{
CNAME: String("www.my-domain.com"),
Source: &PagesSource{Branch: String("gh-pages")},
CNAME: String("www.my-domain.com"),
BuildType: String("legacy"),
Source: &PagesSource{Branch: 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"), BuildType: String("legacy"), Source: &PagesSource{Branch: String("gh-pages")}}
if !cmp.Equal(v, want) {
t.Errorf("Request body = %+v, want %+v", v, want)
}

fmt.Fprint(w, `{"cname":"www.my-domain.com","build_type":"legacy","source":{"branch":"gh-pages"}}`)
})

ctx := context.Background()
_, err := client.Repositories.UpdatePages(ctx, "o", "r", input)
if err != nil {
t.Errorf("Repositories.UpdatePages returned error: %v", err)
}

const methodName = "UpdatePages"
testBadOptions(t, methodName, func() (err error) {
_, err = client.Repositories.UpdatePages(ctx, "\n", "\n", input)
return err
})

testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
return client.Repositories.UpdatePages(ctx, "o", "r", input)
})
}

func TestRepositoriesService_UpdatePagesWorkflow(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

input := &PagesUpdate{
CNAME: String("www.my-domain.com"),
BuildType: String("workflow"),
}

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: &PagesSource{Branch: String("gh-pages")}}
want := &PagesUpdate{CNAME: String("www.my-domain.com"), BuildType: String("workflow")}
if !cmp.Equal(v, want) {
t.Errorf("Request body = %+v, want %+v", v, want)
}

fmt.Fprint(w, `{"cname":"www.my-domain.com","source":{"branch":"gh-pages"}}`)
fmt.Fprint(w, `{"cname":"www.my-domain.com","build_type":"workflow"}`)
})

ctx := context.Background()
Expand Down

0 comments on commit 13c9341

Please sign in to comment.