Skip to content

Commit de398bd

Browse files
committed
Add health checks support for Github Pages
1 parent 4fec23d commit de398bd

File tree

2 files changed

+189
-0
lines changed

2 files changed

+189
-0
lines changed

github/repos_pages.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,44 @@ type PagesBuild struct {
4747
UpdatedAt *Timestamp `json:"updated_at,omitempty"`
4848
}
4949

50+
// PagesDomain represents a domain associated with a Github Pages site.
51+
type PagesDomain struct {
52+
Host *string `json:"host,omitempty"`
53+
URI *string `json:"uri,omitempty"`
54+
Nameservers *string `json:"nameservers,omitempty"`
55+
DNSResolves *bool `json:"dns_resolves,omitempty"`
56+
IsProxied *bool `json:"is_proxied,omitempty"`
57+
IsCloudflareIP *bool `json:"is_cloudflare_ip,omitempty"`
58+
IsFastlyIP *bool `json:"is_fastly_ip,omitempty"`
59+
IsOldIPAddress *bool `json:"is_old_ip_address,omitempty"`
60+
IsARecord *bool `json:"is_a_record,omitempty"`
61+
HasCNAMERecordPresent *bool `json:"has_cname_record,omitempty"`
62+
HasMXRecordsPresent *bool `json:"has_mx_records_present,omitempty"`
63+
IsValidDomain *bool `json:"is_valid_domain,omitempty"`
64+
IsApexDomain *bool `json:"is_apex_domain,omitempty"`
65+
ShouldBeARecord *bool `json:"should_be_a_record,omitempty"`
66+
IsCNAMEToGithubUserDomain *bool `json:"is_cname_to_github_user_domain,omitempty"`
67+
IsCNAMEToPagesDotGithubDotCom *bool `json:"is_cname_to_pages_dot_github_dot_com,omitempty"`
68+
IsCNAMEToFastly *bool `json:"is_cname_to_fastly,omitempty"`
69+
IsPointedToGithubPagesIP *bool `json:"is_pointed_to_github_pages_ip,omitempty"`
70+
IsNonGithubPagesIPPresent *bool `json:"is_non_github_pages_ip_present,omitempty"`
71+
IsPagesDomain *bool `json:"is_pages_domain,omitempty"`
72+
IsServedByPages *bool `json:"is_served_by_pages,omitempty"`
73+
IsValid *bool `json:"is_valid,omitempty"`
74+
Reason *string `json:"reason,omitempty"`
75+
RespondsToHTTPS *bool `json:"responds_to_https,omitempty"`
76+
EnforcesHTTPS *bool `json:"enforces_https,omitempty"`
77+
HTTPSError *string `json:"https_error,omitempty"`
78+
IsHTTPSEligible *bool `json:"is_https_eligible,omitempty"`
79+
CAAError *string `json:"caa_error,omitempty"`
80+
}
81+
82+
// PagesHealthCheckResponse represents the response given for the health check of a Github Pages site.
83+
type PagesHealthCheckResponse struct {
84+
Domain *PagesDomain `json:"domain,omitempty"`
85+
AltDomain *PagesDomain `json:"alt_domain,omitempty"`
86+
}
87+
5088
// PagesHTTPSCertificate represents the HTTPS Certificate information for a GitHub Pages site.
5189
type PagesHTTPSCertificate struct {
5290
State *string `json:"state,omitempty"`
@@ -247,3 +285,22 @@ func (s *RepositoriesService) RequestPageBuild(ctx context.Context, owner, repo
247285

248286
return build, resp, nil
249287
}
288+
289+
// GetPagesHealthCheck gets a DNS health check for the CNAME record configured for a repository's GitHub Pages.
290+
//
291+
// GitHub API docs: https://docs.github.com/en/rest/pages#get-a-dns-health-check-for-github-pages
292+
func (s *RepositoriesService) GetPageHealthCheck(ctx context.Context, owner, repo string) (*PagesHealthCheckResponse, *Response, error) {
293+
u := fmt.Sprintf("repos/%v/%v/pages/health", owner, repo)
294+
req, err := s.client.NewRequest("GET", u, nil)
295+
if err != nil {
296+
return nil, nil, err
297+
}
298+
299+
healthCheckResponse := new(PagesHealthCheckResponse)
300+
resp, err := s.client.Do(ctx, req, healthCheckResponse)
301+
if err != nil {
302+
return nil, resp, err
303+
}
304+
305+
return healthCheckResponse, resp, nil
306+
}

github/repos_pages_test.go

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,54 @@ func TestRepositoriesService_RequestPageBuild(t *testing.T) {
449449
})
450450
}
451451

452+
func TestRepositoriesService_GetPageHealthCheck(t *testing.T) {
453+
client, mux, _, teardown := setup()
454+
defer teardown()
455+
456+
mux.HandleFunc("/repos/o/r/pages/health", func(w http.ResponseWriter, r *http.Request) {
457+
testMethod(t, r, "GET")
458+
fmt.Fprint(w, `{"domain":{"host":"example.com","uri":"http://example.com/","nameservers":"default","dns_resolves":true},"alt_domain":{"host":"www.example.com","uri":"http://www.example.com/","nameservers":"default","dns_resolves":true}}`)
459+
})
460+
461+
ctx := context.Background()
462+
healthCheckResponse, _, err := client.Repositories.GetPageHealthCheck(ctx, "o", "r")
463+
if err != nil {
464+
t.Errorf("Repositories.GetPageHealthCheck returned error: %v", err)
465+
}
466+
467+
want := &PagesHealthCheckResponse{
468+
Domain: &PagesDomain{
469+
Host: String("example.com"),
470+
URI: String("http://example.com/"),
471+
Nameservers: String("default"),
472+
DNSResolves: Bool(true),
473+
},
474+
AltDomain: &PagesDomain{
475+
Host: String("www.example.com"),
476+
URI: String("http://www.example.com/"),
477+
Nameservers: String("default"),
478+
DNSResolves: Bool(true),
479+
},
480+
}
481+
if !cmp.Equal(healthCheckResponse, want) {
482+
t.Errorf("Repositories.GetPageHealthCheck returned %+v, want %+v", healthCheckResponse, want)
483+
}
484+
485+
const methodName = "GetPageHealthCheck"
486+
testBadOptions(t, methodName, func() (err error) {
487+
_, _, err = client.Repositories.GetPageHealthCheck(ctx, "\n", "\n")
488+
return err
489+
})
490+
491+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
492+
got, resp, err := client.Repositories.GetPageHealthCheck(ctx, "o", "r")
493+
if got != nil {
494+
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
495+
}
496+
return resp, err
497+
})
498+
}
499+
452500
func TestPagesSource_Marshal(t *testing.T) {
453501
testJSONMarshal(t, &PagesSource{}, "{}")
454502

@@ -559,6 +607,90 @@ func TestPagesBuild_Marshal(t *testing.T) {
559607
testJSONMarshal(t, u, want)
560608
}
561609

610+
func TestPagesHealthCheckResponse_Marshal(t *testing.T) {
611+
testJSONMarshal(t, &PagesHealthCheckResponse{}, "{}")
612+
613+
u := &PagesHealthCheckResponse{
614+
Domain: &PagesDomain{
615+
Host: String("example.com"),
616+
URI: String("http://example.com/"),
617+
Nameservers: String("default"),
618+
DNSResolves: Bool(true),
619+
IsProxied: Bool(false),
620+
IsCloudflareIP: Bool(false),
621+
IsFastlyIP: Bool(false),
622+
IsOldIPAddress: Bool(false),
623+
IsARecord: Bool(true),
624+
HasCNAMERecordPresent: Bool(false),
625+
HasMXRecordsPresent: Bool(false),
626+
IsValidDomain: Bool(true),
627+
IsApexDomain: Bool(true),
628+
ShouldBeARecord: Bool(true),
629+
IsCNAMEToGithubUserDomain: Bool(false),
630+
IsCNAMEToPagesDotGithubDotCom: Bool(false),
631+
IsCNAMEToFastly: Bool(false),
632+
IsPointedToGithubPagesIP: Bool(true),
633+
IsNonGithubPagesIPPresent: Bool(false),
634+
IsPagesDomain: Bool(false),
635+
IsServedByPages: Bool(true),
636+
IsValid: Bool(true),
637+
Reason: String("some reason"),
638+
RespondsToHTTPS: Bool(true),
639+
EnforcesHTTPS: Bool(true),
640+
HTTPSError: String("some error"),
641+
IsHTTPSEligible: Bool(true),
642+
CAAError: String("some error"),
643+
},
644+
AltDomain: &PagesDomain{
645+
Host: String("www.example.com"),
646+
URI: String("http://www.example.com/"),
647+
Nameservers: String("default"),
648+
DNSResolves: Bool(true),
649+
},
650+
}
651+
652+
want := `{
653+
"domain": {
654+
"host": "example.com",
655+
"uri": "http://example.com/",
656+
"nameservers": "default",
657+
"dns_resolves": true,
658+
"is_proxied": false,
659+
"is_cloudflare_ip": false,
660+
"is_fastly_ip": false,
661+
"is_old_ip_address": false,
662+
"is_a_record": true,
663+
"has_cname_record": false,
664+
"has_mx_records_present": false,
665+
"is_valid_domain": true,
666+
"is_apex_domain": true,
667+
"should_be_a_record": true,
668+
"is_cname_to_github_user_domain": false,
669+
"is_cname_to_pages_dot_github_dot_com": false,
670+
"is_cname_to_fastly": false,
671+
"is_pointed_to_github_pages_ip": true,
672+
"is_non_github_pages_ip_present": false,
673+
"is_pages_domain": false,
674+
"is_served_by_pages": true,
675+
"is_valid": true,
676+
"reason": "some reason",
677+
"responds_to_https": true,
678+
"enforces_https": true,
679+
"https_error": "some error",
680+
"is_https_eligible": true,
681+
"caa_error": "some error"
682+
},
683+
"alt_domain": {
684+
"host": "www.example.com",
685+
"uri": "http://www.example.com/",
686+
"nameservers": "default",
687+
"dns_resolves": true
688+
}
689+
}`
690+
691+
testJSONMarshal(t, u, want)
692+
}
693+
562694
func TestCreatePagesRequest_Marshal(t *testing.T) {
563695
testJSONMarshal(t, &createPagesRequest{}, "{}")
564696

0 commit comments

Comments
 (0)