Skip to content

Commit bd31be8

Browse files
Add BuildType to GitHub Repo Pages (#2724)
Fixes: #2723.
1 parent 78c5d00 commit bd31be8

File tree

4 files changed

+145
-11
lines changed

4 files changed

+145
-11
lines changed

github/github-accessors.go

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

github/github-accessors_test.go

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

github/repos_pages.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type Pages struct {
1717
CNAME *string `json:"cname,omitempty"`
1818
Custom404 *bool `json:"custom_404,omitempty"`
1919
HTMLURL *string `json:"html_url,omitempty"`
20+
BuildType *string `json:"build_type,omitempty"`
2021
Source *PagesSource `json:"source,omitempty"`
2122
Public *bool `json:"public,omitempty"`
2223
HTTPSCertificate *PagesHTTPSCertificate `json:"https_certificate,omitempty"`
@@ -58,7 +59,8 @@ type PagesHTTPSCertificate struct {
5859
// createPagesRequest is a subset of Pages and is used internally
5960
// by EnablePages to pass only the known fields for the endpoint.
6061
type createPagesRequest struct {
61-
Source *PagesSource `json:"source,omitempty"`
62+
BuildType *string `json:"build_type,omitempty"`
63+
Source *PagesSource `json:"source,omitempty"`
6264
}
6365

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

7072
pagesReq := &createPagesRequest{
71-
Source: pages.Source,
73+
BuildType: pages.BuildType,
74+
Source: pages.Source,
7275
}
7376

7477
req, err := s.client.NewRequest("POST", u, pagesReq)
@@ -92,6 +95,10 @@ type PagesUpdate struct {
9295
// CNAME represents a custom domain for the repository.
9396
// Leaving CNAME empty will remove the custom domain.
9497
CNAME *string `json:"cname"`
98+
// BuildType is optional and can either be "legacy" or "workflow".
99+
// "workflow" - You are using a github workflow to build your pages.
100+
// "legacy" - You are deploying from a branch.
101+
BuildType *string `json:"build_type,omitempty"`
95102
// Source must include the branch name, and may optionally specify the subdirectory "/docs".
96103
// Possible values for Source.Branch are usually "gh-pages", "main", and "master",
97104
// or any other existing branch name.

github/repos_pages_test.go

Lines changed: 100 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ import (
1717
"github.com/google/go-cmp/cmp"
1818
)
1919

20-
func TestRepositoriesService_EnablePages(t *testing.T) {
20+
func TestRepositoriesService_EnablePagesLegacy(t *testing.T) {
2121
client, mux, _, teardown := setup()
2222
defer teardown()
2323

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

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

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

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

52-
want := &Pages{URL: String("u"), Status: String("s"), CNAME: String("c"), Custom404: Bool(false), HTMLURL: String("h"), Source: &PagesSource{Branch: String("master"), Path: String("/")}}
53+
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("/")}}
5354

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

73-
func TestRepositoriesService_UpdatePages(t *testing.T) {
74+
func TestRepositoriesService_EnablePagesWorkflow(t *testing.T) {
75+
client, mux, _, teardown := setup()
76+
defer teardown()
77+
78+
input := &Pages{
79+
BuildType: String("workflow"),
80+
CNAME: String("www.my-domain.com"), // not passed along.
81+
}
82+
83+
mux.HandleFunc("/repos/o/r/pages", func(w http.ResponseWriter, r *http.Request) {
84+
v := new(createPagesRequest)
85+
json.NewDecoder(r.Body).Decode(v)
86+
87+
testMethod(t, r, "POST")
88+
testHeader(t, r, "Accept", mediaTypeEnablePagesAPIPreview)
89+
want := &createPagesRequest{BuildType: String("workflow")}
90+
if !cmp.Equal(v, want) {
91+
t.Errorf("Request body = %+v, want %+v", v, want)
92+
}
93+
94+
fmt.Fprint(w, `{"url":"u","status":"s","cname":"c","custom_404":false,"html_url":"h","build_type": "workflow"}`)
95+
})
96+
97+
ctx := context.Background()
98+
page, _, err := client.Repositories.EnablePages(ctx, "o", "r", input)
99+
if err != nil {
100+
t.Errorf("Repositories.EnablePages returned error: %v", err)
101+
}
102+
103+
want := &Pages{URL: String("u"), Status: String("s"), CNAME: String("c"), Custom404: Bool(false), HTMLURL: String("h"), BuildType: String("workflow")}
104+
105+
if !cmp.Equal(page, want) {
106+
t.Errorf("Repositories.EnablePages returned %v, want %v", page, want)
107+
}
108+
109+
const methodName = "EnablePages"
110+
testBadOptions(t, methodName, func() (err error) {
111+
_, _, err = client.Repositories.EnablePages(ctx, "\n", "\n", input)
112+
return err
113+
})
114+
115+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
116+
got, resp, err := client.Repositories.EnablePages(ctx, "o", "r", input)
117+
if got != nil {
118+
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
119+
}
120+
return resp, err
121+
})
122+
}
123+
124+
func TestRepositoriesService_UpdatePagesLegacy(t *testing.T) {
74125
client, mux, _, teardown := setup()
75126
defer teardown()
76127

77128
input := &PagesUpdate{
78-
CNAME: String("www.my-domain.com"),
79-
Source: &PagesSource{Branch: String("gh-pages")},
129+
CNAME: String("www.my-domain.com"),
130+
BuildType: String("legacy"),
131+
Source: &PagesSource{Branch: String("gh-pages")},
132+
}
133+
134+
mux.HandleFunc("/repos/o/r/pages", func(w http.ResponseWriter, r *http.Request) {
135+
v := new(PagesUpdate)
136+
json.NewDecoder(r.Body).Decode(v)
137+
138+
testMethod(t, r, "PUT")
139+
want := &PagesUpdate{CNAME: String("www.my-domain.com"), BuildType: String("legacy"), Source: &PagesSource{Branch: String("gh-pages")}}
140+
if !cmp.Equal(v, want) {
141+
t.Errorf("Request body = %+v, want %+v", v, want)
142+
}
143+
144+
fmt.Fprint(w, `{"cname":"www.my-domain.com","build_type":"legacy","source":{"branch":"gh-pages"}}`)
145+
})
146+
147+
ctx := context.Background()
148+
_, err := client.Repositories.UpdatePages(ctx, "o", "r", input)
149+
if err != nil {
150+
t.Errorf("Repositories.UpdatePages returned error: %v", err)
151+
}
152+
153+
const methodName = "UpdatePages"
154+
testBadOptions(t, methodName, func() (err error) {
155+
_, err = client.Repositories.UpdatePages(ctx, "\n", "\n", input)
156+
return err
157+
})
158+
159+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
160+
return client.Repositories.UpdatePages(ctx, "o", "r", input)
161+
})
162+
}
163+
164+
func TestRepositoriesService_UpdatePagesWorkflow(t *testing.T) {
165+
client, mux, _, teardown := setup()
166+
defer teardown()
167+
168+
input := &PagesUpdate{
169+
CNAME: String("www.my-domain.com"),
170+
BuildType: String("workflow"),
80171
}
81172

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

86177
testMethod(t, r, "PUT")
87-
want := &PagesUpdate{CNAME: String("www.my-domain.com"), Source: &PagesSource{Branch: String("gh-pages")}}
178+
want := &PagesUpdate{CNAME: String("www.my-domain.com"), BuildType: String("workflow")}
88179
if !cmp.Equal(v, want) {
89180
t.Errorf("Request body = %+v, want %+v", v, want)
90181
}
91182

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

95186
ctx := context.Background()

0 commit comments

Comments
 (0)