From 13c9341cdacd34444442e093c2a6c5668278b396 Mon Sep 17 00:00:00 2001 From: Brandon Stubbs Date: Tue, 28 Mar 2023 17:57:27 -0400 Subject: [PATCH] Add BuildType to Github Repo Pages resolves: #2723 --- github/github-accessors.go | 16 +++++ github/github-accessors_test.go | 20 ++++++ github/repos_pages.go | 11 +++- github/repos_pages_test.go | 109 +++++++++++++++++++++++++++++--- 4 files changed, 145 insertions(+), 11 deletions(-) diff --git a/github/github-accessors.go b/github/github-accessors.go index 9c435048aec..89e11c507b2 100644 --- a/github/github-accessors.go +++ b/github/github-accessors.go @@ -11774,6 +11774,14 @@ func (p *PageBuildEvent) GetSender() *User { return p.Sender } +// GetBuildType returns the BuildType field if it's non-nil, zero value otherwise. +func (p *Pages) GetBuildType() string { + if p == nil || p.BuildType == nil { + return "" + } + return *p.BuildType +} + // GetCNAME returns the CNAME field if it's non-nil, zero value otherwise. func (p *Pages) GetCNAME() string { if p == nil || p.CNAME == nil { @@ -11966,6 +11974,14 @@ func (p *PageStats) GetTotalPages() int { return *p.TotalPages } +// GetBuildType returns the BuildType field if it's non-nil, zero value otherwise. +func (p *PagesUpdate) GetBuildType() string { + if p == nil || p.BuildType == nil { + return "" + } + return *p.BuildType +} + // GetCNAME returns the CNAME field if it's non-nil, zero value otherwise. func (p *PagesUpdate) GetCNAME() string { if p == nil || p.CNAME == nil { diff --git a/github/github-accessors_test.go b/github/github-accessors_test.go index 5091cc46c90..2c7c1233eff 100644 --- a/github/github-accessors_test.go +++ b/github/github-accessors_test.go @@ -13806,6 +13806,16 @@ func TestPageBuildEvent_GetSender(tt *testing.T) { p.GetSender() } +func TestPages_GetBuildType(tt *testing.T) { + var zeroValue string + p := &Pages{BuildType: &zeroValue} + p.GetBuildType() + p = &Pages{} + p.GetBuildType() + p = nil + p.GetBuildType() +} + func TestPages_GetCNAME(tt *testing.T) { var zeroValue string p := &Pages{CNAME: &zeroValue} @@ -14034,6 +14044,16 @@ func TestPageStats_GetTotalPages(tt *testing.T) { p.GetTotalPages() } +func TestPagesUpdate_GetBuildType(tt *testing.T) { + var zeroValue string + p := &PagesUpdate{BuildType: &zeroValue} + p.GetBuildType() + p = &PagesUpdate{} + p.GetBuildType() + p = nil + p.GetBuildType() +} + func TestPagesUpdate_GetCNAME(tt *testing.T) { var zeroValue string p := &PagesUpdate{CNAME: &zeroValue} diff --git a/github/repos_pages.go b/github/repos_pages.go index 737cec0f22a..845de8a0dba 100644 --- a/github/repos_pages.go +++ b/github/repos_pages.go @@ -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"` @@ -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. @@ -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) @@ -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. diff --git a/github/repos_pages_test.go b/github/repos_pages_test.go index 75d6ccb2bf4..6245aac256f 100644 --- a/github/repos_pages_test.go +++ b/github/repos_pages_test.go @@ -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("/"), @@ -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() @@ -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) @@ -70,13 +71,103 @@ 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) { @@ -84,12 +175,12 @@ func TestRepositoriesService_UpdatePages(t *testing.T) { 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()