Skip to content

Commit bee9b5b

Browse files
committed
Merge pull request #43 from github/no-cname
Gracefully fallback for domains without CNAMEs
2 parents a295d26 + 0eba70c commit bee9b5b

File tree

5 files changed

+49
-6
lines changed

5 files changed

+49
-6
lines changed

lib/github-pages-health-check/repository.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class Repository < Checkable
55
attr_reader :name, :owner
66

77
REPO_REGEX = %r{\A[a-z0-9_\-]+/[a-z0-9_\-\.]+\z}i
8-
8+
99
HASH_METHODS = [
1010
:name_with_owner, :built?, :last_built, :build_duration, :build_error
1111
].freeze
@@ -39,7 +39,7 @@ def built?
3939
end
4040

4141
def build_error
42-
last_build.error.message unless built?
42+
last_build.error["message"] unless built?
4343
end
4444
alias_method :reason, :build_error
4545

@@ -52,6 +52,7 @@ def last_built
5252
end
5353

5454
def domain
55+
return if cname.nil?
5556
@domain ||= GitHubPages::HealthCheck::Domain.new(cname)
5657
end
5758

lib/github-pages-health-check/site.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@ def initialize(repository_or_domain, access_token: nil)
1313
end
1414

1515
def check!
16-
domain.check!
17-
repository.check! unless repository.nil?
16+
[domain, repository].each { |check| check.check! unless check.nil? }
1817
true
1918
end
2019

2120
def to_hash
22-
hash = domain.to_hash.dup
21+
hash = (domain || {}).to_hash.dup
2322
hash = hash.merge(repository.to_hash) unless repository.nil?
2423
hash[:valid?] = valid?
2524
hash[:reason] = reason
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"url": "https://api.github.com/repos/github/pages.github.com/pages",
3+
"status": "built",
4+
"cname": null,
5+
"custom_404": false
6+
}

spec/github_pages_health_check_repository_spec.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@
102102

103103
context "without an access token" do
104104
before { subject.instance_variable_set("@access_token", nil) }
105-
105+
106106
it "raises an error" do
107107
expected = GitHubPages::HealthCheck::Errors::MissingAccessTokenError
108108
expect { subject.send(:client) }.to raise_error(expected)
@@ -130,5 +130,18 @@
130130
expect(subject.domain.class).to eql(GitHubPages::HealthCheck::Domain)
131131
expect(subject.domain.host).to eql("pages.github.com")
132132
end
133+
134+
context "without a CNAME" do
135+
before do
136+
subject.instance_variable_set("@access_token", "1234")
137+
fixture = File.read(fixture_path("pages_info_no_cname.json"))
138+
stub_request(:get, "https://api.github.com/repos/github/pages.github.com/pages").
139+
to_return(:status => 200, :body => fixture, :headers => {'Content-Type'=>'application/json'})
140+
end
141+
142+
it "doesn't try to build the domain" do
143+
expect(subject.domain).to be_nil
144+
end
145+
end
133146
end
134147
end

spec/github_pages_health_check_site_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,30 @@
5050
end
5151
end
5252

53+
context "with no cname" do
54+
before do
55+
if init_type == "repo"
56+
fixture = File.read(fixture_path("pages_info_no_cname.json"))
57+
stub_request(:get, "https://api.github.com/repos/github/pages.github.com/pages").
58+
to_return(:status => 200, :body => fixture, :headers => {'Content-Type'=>'application/json'})
59+
60+
fixture = File.read(fixture_path("build_success.json"))
61+
stub_request(:get, "https://api.github.com/repos/github/pages.github.com/pages/builds/latest").
62+
to_return(:status => 200, :body => fixture, :headers => {'Content-Type'=>'application/json'})
63+
end
64+
end
65+
66+
if init_type == "repo"
67+
it "knows it doesn't know the domain" do
68+
expect(subject.domain).to eql(nil)
69+
end
70+
71+
it "doesnt err out when it checks" do
72+
expect(subject.check!).to eql(true)
73+
end
74+
end
75+
end
76+
5377
context "json" do
5478
let(:json) { JSON.parse subject.to_json }
5579

0 commit comments

Comments
 (0)