Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for building the base URL from ENV on Enterprise #40

Merged
merged 2 commits into from
Aug 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions lib/jekyll-mentions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ def mention_base(config = {})
mention_config = config["jekyll-mentions"]
case mention_config
when nil, NilClass
GITHUB_DOT_COM
default_mention_base
when String
mention_config.to_s
when Hash
mention_config.fetch("base_url", GITHUB_DOT_COM)
mention_config.fetch("base_url", default_mention_base)
else
raise InvalidJekyllMentionConfig,
"Your jekyll-mentions config has to either be a" \
Expand All @@ -82,6 +82,17 @@ def mentionable?(doc)
(doc.is_a?(Jekyll::Page) || doc.write?) &&
doc.output_ext == ".html" || (doc.permalink && doc.permalink.end_with?("/"))
end

private

def default_mention_base
if !ENV["SSL"].to_s.empty? && !ENV["GITHUB_HOSTNAME"].to_s.empty?
scheme = ENV["SSL"] == "true" ? "https://" : "http://"
"#{scheme}#{ENV["GITHUB_HOSTNAME"].chomp("/")}"
else
GITHUB_DOT_COM
end
end
end
end
end
Expand Down
36 changes: 36 additions & 0 deletions spec/mentions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,40 @@ def para(content)
expect(basic_post.output).to start_with(para(result.sub(default_src, mentions_src)))
end
end

context "with the SSL and GITHUB_HOSTNAME environment variables set" do
let(:ssl) { "true" }
let(:github_hostname) { "github.vm" }
let(:default_mention_base) { "https://github.vm" }

before(:each) do
ENV["SSL"] = ssl
ENV["GITHUB_HOSTNAME"] = github_hostname
end

after(:each) do
ENV.delete("SSL")
ENV.delete("GITHUB_HOSTNAME")
end

it "has a default source based on SSL and GITHUB_HOSTNAME" do
expect(mentions.mention_base).to eql(default_mention_base)
end

it "uses correct mention URLs when SSL and GITHUB_HOSTNAME are set" do
# Re-render the site, so that ENV is used
site.render
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is done because ENV values are set in this context's before(:each) block, but the site is rendered in the parent before(:each) block, meaning the original render doesn't get the ENV values.

expect(basic_post.output).to start_with(para(result.sub(default_src, default_mention_base)))
end

it "falls back to using the default if SSL is empty" do
ENV["SSL"] = ""
expect(mentions.mention_base).to eql(default_src)
end

it "falls back to using the default if GITHUB_HOSTNAME is empty" do
ENV["GITHUB_HOSTNAME"] = ""
expect(mentions.mention_base).to eql(default_src)
end
end
end