├── test
├── test_site
│ ├── .gitignore
│ ├── _config.yml
│ ├── about.md
│ ├── index.html
│ └── _posts
│ │ └── 2014-07-14-welcome-to-jekyll.markdown
└── test_netlify.rb
├── lib
├── jekyll-netlify.rb
└── jekyll
│ ├── netlify
│ ├── version.rb
│ ├── environment.rb
│ └── generator.rb
│ └── netlify.rb
├── Gemfile
├── Rakefile
├── .github
├── dependabot.yml
└── workflows
│ ├── semgrep.yml
│ ├── ruby.yml
│ └── auto-pr.yml
├── .rubocop.yml
├── LICENSE
├── jekyll-netlify.gemspec
├── .gitignore
└── README.md
/test/test_site/.gitignore:
--------------------------------------------------------------------------------
1 | _site
--------------------------------------------------------------------------------
/lib/jekyll-netlify.rb:
--------------------------------------------------------------------------------
1 | require 'jekyll/netlify'
2 |
--------------------------------------------------------------------------------
/Gemfile:
--------------------------------------------------------------------------------
1 | source 'https://rubygems.org'
2 |
3 | gemspec
4 |
--------------------------------------------------------------------------------
/lib/jekyll/netlify/version.rb:
--------------------------------------------------------------------------------
1 | # frozen_string_literal: true
2 |
3 | module Jekyll
4 | module Netlify
5 | VERSION = '0.2.0'.freeze
6 | end
7 | end
8 |
--------------------------------------------------------------------------------
/lib/jekyll/netlify.rb:
--------------------------------------------------------------------------------
1 | require 'jekyll/netlify/version'
2 | require 'jekyll/netlify/generator'
3 |
4 | module Jekyll
5 | # Netlify metadata
6 | module Netlify
7 | end
8 | end
9 |
--------------------------------------------------------------------------------
/test/test_site/_config.yml:
--------------------------------------------------------------------------------
1 | # Site settings
2 | title: Jekyll Test Site for Jekyll::Netlify
3 | description: "This is a test jekyll site with git contents to properly test the Jekyll::Netlify generator"
4 | baseurl: ""
5 | url: "http://fake.com"
6 |
--------------------------------------------------------------------------------
/Rakefile:
--------------------------------------------------------------------------------
1 | require 'bundler/gem_tasks'
2 | require 'rake/testtask'
3 |
4 | task :default => [:test]
5 |
6 | Rake::TestTask.new(:test) do |t|
7 | t.libs << 'test'
8 | t.test_files = FileList['test/test_*.rb']
9 | t.verbose = true
10 | end
11 |
--------------------------------------------------------------------------------
/.github/dependabot.yml:
--------------------------------------------------------------------------------
1 | # Please see the documentation for all configuration options:
2 | # https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
3 |
4 | version: 2
5 | updates:
6 | - package-ecosystem: "bundler"
7 | directory: "/"
8 | schedule:
9 | interval: "weekly"
10 |
--------------------------------------------------------------------------------
/test/test_site/about.md:
--------------------------------------------------------------------------------
1 | ---
2 | layout: page
3 | title: About
4 | permalink: /about/
5 | ---
6 |
7 | This is a test Jekyll site for Jekyll::Netlify. You can view the plugin at [Github](https://github.com/jayvdb/jekyll-netlify).
8 |
9 | You are welcome to submit pull requests or suggest ideas!
10 |
11 | Site: [site.url]({{ site.url }})
12 |
--------------------------------------------------------------------------------
/.github/workflows/semgrep.yml:
--------------------------------------------------------------------------------
1 | on:
2 | pull_request: {}
3 | push:
4 | branches:
5 | - main
6 | - master
7 | name: Semgrep
8 | jobs:
9 | semgrep:
10 | name: Scan
11 | runs-on: ubuntu-latest
12 | steps:
13 | - uses: actions/checkout@v2
14 | - uses: returntocorp/semgrep-action@v1
15 | with:
16 | publishToken: ${{ secrets.SEMGREP_APP_TOKEN }}
17 |
--------------------------------------------------------------------------------
/.github/workflows/ruby.yml:
--------------------------------------------------------------------------------
1 | name: Ruby
2 |
3 | on:
4 | push:
5 | branches: ['main']
6 | pull_request:
7 | branches: ['main']
8 |
9 | jobs:
10 | test:
11 |
12 | runs-on: ubuntu-latest
13 |
14 | steps:
15 | - uses: actions/checkout@v2
16 | - name: Set up Ruby
17 | uses: ruby/setup-ruby@v1
18 | with:
19 | ruby-version: 2.6
20 | - name: Install dependencies
21 | run: bundle install
22 | - name: Run tests
23 | run: bundle exec rake
24 |
--------------------------------------------------------------------------------
/test/test_site/index.html:
--------------------------------------------------------------------------------
1 | ---
2 | layout: default
3 | ---
4 |
5 |
6 |
7 |
Posts
8 |
9 |
10 | {% for post in site.posts %}
11 | -
12 | {{ post.date | date: "%b %-d, %Y" }}
13 | {{ post.title }}
14 |
15 | {% endfor %}
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/.github/workflows/auto-pr.yml:
--------------------------------------------------------------------------------
1 | name: Add comment to successful Dependabot PRs
2 |
3 | on:
4 | workflow_run:
5 | workflows: ["Ruby"]
6 | types:
7 | - completed
8 |
9 | jobs:
10 | add-dependabot-comment:
11 | runs-on: ubuntu-latest
12 | if: ${{ github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success' && github.actor == 'dependabot[bot]' }}
13 | steps:
14 | - name: Add merge comment for dependabot PRs
15 | uses: jo-sm/automerge-dependabot@v1
16 | with:
17 | run-id: ${{ github.event.workflow_run.id }}
18 | token: ${{ secrets.GHA_TOKEN }}
19 |
--------------------------------------------------------------------------------
/.rubocop.yml:
--------------------------------------------------------------------------------
1 | ---
2 | AllCops:
3 | TargetRubyVersion: 2.1
4 | Metrics/ClassLength:
5 | Exclude:
6 | - test/**/*.rb
7 | Naming/FileName:
8 | Exclude:
9 | - lib/jekyll-netlify.rb
10 | Metrics/MethodLength:
11 | Max: 30
12 | Metrics/AbcSize:
13 | Max: 30
14 | Metrics/BlockLength:
15 | Exclude:
16 | - test/**/*.rb
17 | Style/TrailingCommaInLiteral:
18 | Enabled: false
19 | Style/TrailingCommaInArguments:
20 | Enabled: false
21 | Style/ClassAndModuleChildren:
22 | Enabled: false
23 | Style/MultilineTernaryOperator:
24 | Enabled: false
25 | Style/ConditionalAssignment:
26 | Enabled: false
27 | Style/GuardClause:
28 | Enabled: false
29 |
--------------------------------------------------------------------------------
/test/test_site/_posts/2014-07-14-welcome-to-jekyll.markdown:
--------------------------------------------------------------------------------
1 | ---
2 | layout: post
3 | title: "Welcome to Jekyll!"
4 | date: 2014-07-14 02:00:29
5 | categories: jekyll update
6 | ---
7 |
8 | You'll find this post in your `_posts` directory - edit this post and re-build (or run with the `-w` switch) to see your changes!
9 | To add new posts, simply add a file in the `_posts` directory that follows the convention: YYYY-MM-DD-name-of-post.ext.
10 |
11 | Jekyll also offers powerful support for code snippets:
12 |
13 | {% highlight ruby %}
14 | def print_hi(name)
15 | puts "Hi, #{name}"
16 | end
17 | print_hi('Tom')
18 | #=> prints 'Hi, Tom' to STDOUT.
19 | {% endhighlight %}
20 |
21 | Check out the [Jekyll docs][jekyll] for more info on how to get the most out of Jekyll. File all bugs/feature requests at [Jekyll's GitHub repo][jekyll-gh].
22 |
23 | Appending to a post.
24 |
25 | ---
26 |
27 | I hope Jekyll::GitMetadata will be a useful plugin!
28 |
29 | [jekyll-gh]: https://github.com/jekyll/jekyll
30 | [jekyll]: http://jekyllrb.com
31 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2017 John Vandenberg
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/lib/jekyll/netlify/environment.rb:
--------------------------------------------------------------------------------
1 | module Jekyll
2 | module Netlify
3 | # :no_doc:
4 | class Environment
5 | attr_reader :jekyll_env
6 |
7 | def initialize
8 | @netlify_context = ENV['CONTEXT']
9 | if netlify?
10 | @jekyll_env = 'production'
11 | else
12 | @jekyll_env = Jekyll.env
13 | end
14 | end
15 |
16 | def prefixed_env
17 | if suffix_context?
18 | @jekyll_env
19 | else
20 | [@jekyll_env, @netlify_context].join('-')
21 | end
22 | end
23 |
24 | def netlify?
25 | ENV['DEPLOY_URL'] && (!ENV['JEKYLL_ENV'] || ENV['JEKYLL_ENV'].empty?)
26 | end
27 |
28 | private
29 |
30 | def production_context?
31 | @netlify_context == 'production'
32 | end
33 |
34 | def netlify_context_blank?
35 | (!@netlify_context || @netlify_context.empty?)
36 | end
37 |
38 | def suffix_context?
39 | @jekyll_env && ((@jekyll_env == @netlify_context) ||
40 | netlify_context_blank? ||
41 | production_context?)
42 | end
43 | end
44 | end
45 | end
46 |
--------------------------------------------------------------------------------
/jekyll-netlify.gemspec:
--------------------------------------------------------------------------------
1 | lib = File.expand_path('../lib', __FILE__)
2 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3 | require 'jekyll/netlify/version'
4 |
5 | Gem::Specification.new do |spec|
6 | spec.name = 'jekyll-netlify'
7 | spec.version = Jekyll::Netlify::VERSION
8 | spec.authors = ['John Vandenberg']
9 | spec.email = ['jayvdb@gmail.com']
10 | spec.summary = 'Netlify metadata for Jekyll.'
11 | spec.description = 'Access Netlify environment values in Jekyll templates'
12 | spec.homepage = 'https://github.com/jayvdb/jekyll-netlify'
13 | spec.license = 'MIT'
14 |
15 | spec.files = `git ls-files -z`.split("\x0")
16 | spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17 | spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18 | spec.require_paths = ['lib']
19 |
20 | spec.add_dependency 'jekyll', '>= 3', '< 5'
21 |
22 | spec.add_development_dependency 'bundler', '>= 2.2.10'
23 | spec.add_development_dependency 'mocha'
24 | spec.add_development_dependency 'rake'
25 | spec.add_development_dependency 'rubocop', '~> 1.18'
26 | spec.add_development_dependency 'shoulda'
27 | spec.add_development_dependency 'kramdown-parser-gfm'
28 | end
29 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.gem
2 | *.rbc
3 | /.config
4 | /coverage/
5 | /InstalledFiles
6 | /pkg/
7 | /spec/reports/
8 | /spec/examples.txt
9 | /test/tmp/
10 | /test/version_tmp/
11 | /tmp/
12 |
13 | # Used by dotenv library to load environment variables.
14 | # .env
15 |
16 | ## Specific to RubyMotion:
17 | .dat*
18 | .repl_history
19 | build/
20 | *.bridgesupport
21 | build-iPhoneOS/
22 | build-iPhoneSimulator/
23 |
24 | ## Specific to RubyMotion (use of CocoaPods):
25 | #
26 | # We recommend against adding the Pods directory to your .gitignore. However
27 | # you should judge for yourself, the pros and cons are mentioned at:
28 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
29 | #
30 | # vendor/Pods/
31 |
32 | ## Documentation cache and generated files:
33 | /.yardoc/
34 | /_yardoc/
35 | /doc/
36 | /rdoc/
37 |
38 | ## Environment normalization:
39 | /.bundle/
40 | /vendor/bundle
41 | /lib/bundler/man/
42 |
43 | # for a library or gem, you might want to ignore these files since the code is
44 | # intended to run in multiple environments; otherwise, check them in:
45 | # Gemfile.lock
46 | # .ruby-version
47 | # .ruby-gemset
48 |
49 | # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
50 | .rvmrc
51 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | [](https://travis-ci.org/jayvdb/jekyll-netlify)
2 | [](https://badge.fury.io/rb/jekyll-netlify)
3 |
4 | # Jekyll::Netlify
5 |
6 | Expose Netlify deploy information to Jekyll templates
7 | and set `site.environment=production`.
8 |
9 | ```
10 | {{site.netlify.branch}} # => Will return the branch name
11 | {{site.netlify.pull_request.url}} # => Will return http://github.com/foo/bar/pulls/23
12 | ```
13 |
14 | ## Installation
15 |
16 | Add to your `Gemfile`:
17 |
18 | ```
19 | group :jekyll_plugins do
20 | gem "jekyll-netlify"
21 | end
22 | ```
23 |
24 | For older versions of Jekyll, add to your `_config.yml`:
25 |
26 | ```yml
27 | plugins:
28 | - jekyll-netlify
29 | ```
30 |
31 | ## Usage
32 |
33 | This plugin adds hash of `site.netlify` containing
34 | [Build Environment information](https://www.netlify.com/docs/continuous-deployment/#build-environment-variables):
35 |
36 | - repository_url (`git@...`)
37 | - branch
38 | - pull_request: (false; true for BitBucket; or Hash for GitHub and GitLab)
39 | - id
40 | - url
41 | - head
42 | - commit
43 | - context
44 | - deploy_url
45 | - url
46 | - deploy_prime_url
47 | - webhook
48 | - title
49 | - url
50 | - body
51 |
--------------------------------------------------------------------------------
/lib/jekyll/netlify/generator.rb:
--------------------------------------------------------------------------------
1 | require_relative 'environment'
2 | module Jekyll
3 | module Netlify
4 | # Netlify plugin generator
5 | class Generator < Jekyll::Generator
6 | safe true
7 |
8 | def generate(site)
9 | env = Environment.new
10 | if netlify?
11 | ENV['JEKYLL_ENV'] = env.jekyll_env
12 | site.config['environment'] = env.jekyll_env
13 | site.config['netlify'] = load_netlify_env
14 | site.config['netlify']['environment'] = env.prefixed_env
15 | if production?
16 | site.config['url'] = site.config['netlify']['url']
17 | else
18 | site.config['url'] = site.config['netlify']['deploy_url']
19 | end
20 | else
21 | site.config['netlify'] = false
22 | end
23 | end
24 |
25 | def netlify?
26 | return false unless ENV.key?('DEPLOY_URL')
27 | deploy_url = ENV['DEPLOY_URL']
28 | return false unless deploy_url.include? 'netlify'
29 |
30 | true
31 | end
32 |
33 | def production?
34 | ENV['CONTEXT'].eql?('production') ? true : false
35 | end
36 |
37 | def pull_request?
38 | ENV['PULL_REQUEST'].eql?('true') ? true : false
39 | end
40 |
41 | def pull_request_id(env = ENV)
42 | branch = env['BRANCH']
43 | return false unless branch =~ /^(pull|merge-requests)/
44 |
45 | parts = branch.split(%r{\/})
46 | parts[1].to_i
47 | end
48 |
49 | def pull_request_url(env = ENV)
50 | repository_url = env['REPOSITORY_URL']
51 | if repository_url.start_with? 'git@'
52 | repository_url = repository_url.tr(':', '/')
53 | repository_url = repository_url.gsub('git@', 'https://')
54 | end
55 | if repository_url.include? 'gitlab'
56 | return repository_url + '/merge_requests/' + pull_request_id.to_s
57 | elsif repository_url.include? 'github'
58 | return repository_url + '/pulls/' + pull_request_id.to_s
59 | end
60 | end
61 |
62 | def webhook?
63 | ENV.key?('WEBHOOK_URL')
64 | end
65 |
66 | def load_netlify_env(env = ENV)
67 | data = {
68 | 'repository_url' => env['REPOSITORY_URL'],
69 | 'branch' => env['BRANCH'],
70 | 'head' => env['HEAD'],
71 | 'commit' => env['COMMIT_REF'],
72 | 'context' => env['CONTEXT'],
73 | 'deploy_url' => env['DEPLOY_URL'],
74 | 'url' => ENV['URL'],
75 | 'deploy_prime_url' => env['DEPLOY_PRIME_URL'],
76 | }
77 | if pull_request?
78 | id = pull_request_id
79 | if id
80 | data['pull_request'] = {
81 | 'id' => id,
82 | 'url' => pull_request_url,
83 | }
84 | else
85 | data['pull_request'] = true
86 | end
87 | else
88 | data['pull_request'] = false
89 | end
90 | data['webhook'] = !webhook? ? false : {
91 | 'title' => env['WEBHOOK_TITLE'],
92 | 'body' => env['WEBHOOK_BODY'],
93 | 'url' => env['WEBHOOK_URL'],
94 | }
95 | data
96 | end
97 | end
98 | end
99 | end
100 |
--------------------------------------------------------------------------------
/test/test_netlify.rb:
--------------------------------------------------------------------------------
1 | require 'minitest/autorun'
2 | require 'minitest/unit'
3 | require 'shoulda'
4 | require 'mocha/minitest'
5 | require 'jekyll'
6 | require 'jekyll-netlify'
7 |
8 | def jekyll_test_site
9 | File.join(File.dirname(__FILE__), 'test_site')
10 | end
11 |
12 | def get_about_page(site)
13 | site.pages[0]
14 | end
15 |
16 | class Jekyll::NetlifyTest < Minitest::Test
17 | context 'normal build' do
18 | setup do
19 | Jekyll.instance_variable_set(
20 | :@logger, Jekyll::LogAdapter.new(Jekyll::Stevenson.new, :error)
21 | )
22 |
23 | ENV.clear
24 |
25 | config = Jekyll.configuration(
26 | source: jekyll_test_site,
27 | destination: File.join(jekyll_test_site, '_site'),
28 | plugins: nil,
29 | )
30 | @site = Jekyll::Site.new(config)
31 | @site.read
32 | @site.generate
33 | @site.render
34 | @about_page = get_about_page(@site)
35 | end
36 |
37 | context 'netlify info' do
38 | setup do
39 | @netlify = @site.config['netlify']
40 | end
41 |
42 | should 'be false' do
43 | assert_equal false, @netlify
44 | end
45 | end
46 |
47 | context 'site.url' do
48 | should 'be from _config.yml' do
49 | assert_equal 'http://fake.com', @site.config['url']
50 | assert_operator @about_page.output, :include?, 'http://fake.com'
51 | end
52 | end
53 | end
54 |
55 | context 'netlify production context' do
56 | setup do
57 | Jekyll.instance_variable_set(
58 | :@logger, Jekyll::LogAdapter.new(Jekyll::Stevenson.new, :error)
59 | )
60 |
61 | ENV.clear
62 |
63 | ENV['URL'] = 'https://example.com'
64 | ENV['BRANCH'] = 'master'
65 | ENV['CONTEXT'] = 'production'
66 | ENV['PULL_REQUEST'] = 'false'
67 | ENV['DEPLOY_URL'] = 'https://578ab634d5d5cf960d620--open-api.netlify.com'
68 | ENV['DEPLOY_PRIME_URL'] = 'https://beta--open-api.netlify.com'
69 |
70 | config = Jekyll.configuration(
71 | source: jekyll_test_site,
72 | destination: File.join(jekyll_test_site, '_site'),
73 | )
74 | @site = Jekyll::Site.new(config)
75 | @site.read
76 | @site.generate
77 | @site.render
78 | @about_page = get_about_page(@site)
79 | end
80 |
81 | context 'info' do
82 | setup do
83 | @netlify = @site.config['netlify']
84 | end
85 |
86 | should 'be a Hash' do
87 | assert_instance_of Hash, @netlify
88 | end
89 |
90 | should 'include URLs' do
91 | assert_operator @netlify, :has_key?, 'url'
92 | assert_equal 'https://example.com', @netlify['url']
93 | end
94 |
95 | should 'be production' do
96 | assert_equal 'production', @site.config['environment']
97 | assert_equal 'production', @netlify['environment']
98 | end
99 |
100 | should 'not be a pull request' do
101 | assert_operator @netlify, :has_key?, 'pull_request'
102 | assert_equal false, @netlify['pull_request']
103 | end
104 |
105 | should 'not include webhook data' do
106 | assert_operator @netlify, :has_key?, 'webhook'
107 | assert_equal false, @netlify['webhook']
108 | end
109 | end
110 |
111 | context 'site.url' do
112 | should 'be netlify production url' do
113 | assert_equal 'https://example.com', @site.config['url']
114 | end
115 | should 'be expanded in about.md' do
116 | assert_operator @about_page.output, :include?, 'https://example.com'
117 | end
118 | end
119 | end
120 |
121 | context 'netlify deploy-preview context' do
122 | setup do
123 | Jekyll.instance_variable_set(
124 | :@logger, Jekyll::LogAdapter.new(Jekyll::Stevenson.new, :error)
125 | )
126 |
127 | ENV.clear
128 | ENV['URL'] = 'https://example.com'
129 | ENV['CONTEXT'] = 'deploy-preview'
130 | ENV['JEKYLL_ENV'] = 'staging'
131 |
132 | ENV['PULL_REQUEST'] = 'false'
133 | ENV['DEPLOY_URL'] = 'https://578ab634d5d5cf960d620--open-api.netlify.com'
134 | ENV['DEPLOY_PRIME_URL'] = 'https://beta--open-api.netlify.com'
135 |
136 | config = Jekyll.configuration(
137 | source: jekyll_test_site,
138 | destination: File.join(jekyll_test_site, '_site'),
139 | )
140 | @site = Jekyll::Site.new(config)
141 | @site.read
142 | @site.generate
143 | @site.render
144 | @about_page = get_about_page(@site)
145 | end
146 |
147 | context 'info' do
148 | setup do
149 | @netlify = @site.config['netlify']
150 | end
151 | should 'be staging-deploy-preview' do
152 | assert_equal 'staging', @site.config['environment']
153 | assert_equal 'staging-deploy-preview', @netlify['environment']
154 | end
155 | end
156 |
157 | context 'site.url' do
158 | should 'be netlify deploy-preview url' do
159 | assert_equal ENV['DEPLOY_URL'], @site.config['url']
160 | end
161 | should 'be expanded in about.md' do
162 | assert_operator @about_page.output, :include?, ENV['DEPLOY_URL']
163 | end
164 | end
165 | end
166 |
167 | context 'netlify unrecognised pull request' do
168 | setup do
169 | Jekyll.instance_variable_set(
170 | :@logger, Jekyll::LogAdapter.new(Jekyll::Stevenson.new, :error)
171 | )
172 |
173 | ENV.clear
174 |
175 | ENV['URL'] = 'https://example.com'
176 | ENV['BRANCH'] = 'foo/bar'
177 | ENV['CONTEXT'] = 'deploy-preview'
178 | ENV['PULL_REQUEST'] = 'true'
179 | ENV['DEPLOY_URL'] = 'https://578ab634d5d5cf960d620--open-api.netlify.com'
180 | ENV['DEPLOY_PRIME_URL'] = 'https://beta--open-api.netlify.com'
181 |
182 | config = Jekyll.configuration(
183 | source: jekyll_test_site,
184 | destination: File.join(jekyll_test_site, '_site'),
185 | )
186 | @site = Jekyll::Site.new(config)
187 | @site.read
188 | @site.generate
189 | end
190 |
191 | context 'info' do
192 | setup do
193 | @netlify = @site.config['netlify']
194 | end
195 |
196 | should 'be a Hash' do
197 | assert_instance_of Hash, @netlify
198 | end
199 |
200 | should 'include URLs' do
201 | assert_operator @netlify, :has_key?, 'url'
202 | assert_equal 'https://example.com', @netlify['url']
203 | end
204 |
205 | should 'be production' do
206 | assert_equal 'production', @site.config['environment']
207 | assert_equal 'production-deploy-preview', @netlify['environment']
208 | end
209 |
210 | should 'be a pull request' do
211 | assert_operator @netlify, :has_key?, 'pull_request'
212 | assert_equal true, @netlify['pull_request']
213 | end
214 |
215 | should 'not include webhook data' do
216 | assert_operator @netlify, :has_key?, 'webhook'
217 | assert_equal false, @netlify['webhook']
218 | end
219 | end
220 | end
221 |
222 | context 'netlify github pull request' do
223 | setup do
224 | Jekyll.instance_variable_set(
225 | :@logger, Jekyll::LogAdapter.new(Jekyll::Stevenson.new, :error)
226 | )
227 |
228 | ENV.clear
229 |
230 | ENV['REPOSITORY_URL'] = 'https://github.com/foo/bar'
231 | ENV['URL'] = 'https://example.com'
232 | ENV['BRANCH'] = 'pull/23/head'
233 | ENV['CONTEXT'] = 'deploy-preview'
234 | ENV['PULL_REQUEST'] = 'true'
235 | ENV['DEPLOY_URL'] = 'https://578ab634d5d5cf960d620--open-api.netlify.com'
236 | ENV['DEPLOY_PRIME_URL'] = 'https://beta--open-api.netlify.com'
237 |
238 | config = Jekyll.configuration(
239 | source: jekyll_test_site,
240 | destination: File.join(jekyll_test_site, '_site'),
241 | )
242 | @site = Jekyll::Site.new(config)
243 | @site.read
244 | @site.generate
245 | end
246 |
247 | context 'info' do
248 | setup do
249 | @netlify = @site.config['netlify']
250 | end
251 |
252 | should 'be a pull request' do
253 | assert_operator @netlify, :has_key?, 'pull_request'
254 | assert_instance_of Hash, @netlify['pull_request']
255 | end
256 |
257 | should 'have a pull request id' do
258 | assert_equal 23, @netlify['pull_request']['id']
259 | end
260 |
261 | should 'have a pull request url' do
262 | assert_instance_of String, @netlify['pull_request']['url']
263 | assert_equal 'https://github.com/foo/bar/pulls/23',
264 | @netlify['pull_request']['url']
265 | end
266 | end
267 | end
268 |
269 | context 'netlify gitlab pull request' do
270 | setup do
271 | Jekyll.instance_variable_set(
272 | :@logger, Jekyll::LogAdapter.new(Jekyll::Stevenson.new, :error)
273 | )
274 |
275 | ENV.clear
276 |
277 | ENV['REPOSITORY_URL'] = 'git@gitlab.com:foo/bar'
278 | ENV['URL'] = 'https://example.com'
279 | ENV['BRANCH'] = 'merge-requests/23/head'
280 | ENV['CONTEXT'] = 'deploy-preview'
281 | ENV['PULL_REQUEST'] = 'true'
282 | ENV['DEPLOY_URL'] = 'https://578ab634d5d5cf960d620--open-api.netlify.com'
283 | ENV['DEPLOY_PRIME_URL'] = 'https://beta--open-api.netlify.com'
284 |
285 | config = Jekyll.configuration(
286 | source: jekyll_test_site,
287 | destination: File.join(jekyll_test_site, '_site'),
288 | )
289 | @site = Jekyll::Site.new(config)
290 | @site.read
291 | @site.generate
292 | end
293 |
294 | context 'info' do
295 | setup do
296 | @netlify = @site.config['netlify']
297 | end
298 |
299 | should 'be a pull request' do
300 | assert_operator @netlify, :has_key?, 'pull_request'
301 | assert_instance_of Hash, @netlify['pull_request']
302 | end
303 |
304 | should 'have a pull request id' do
305 | assert_equal 23, @netlify['pull_request']['id']
306 | end
307 |
308 | should 'have a pull request url' do
309 | assert_instance_of String, @netlify['pull_request']['url']
310 | assert_equal 'https://gitlab.com/foo/bar/merge_requests/23',
311 | @netlify['pull_request']['url']
312 | end
313 | end
314 | end
315 |
316 | context 'netlify webhook deploy' do
317 | setup do
318 | Jekyll.instance_variable_set(
319 | :@logger, Jekyll::LogAdapter.new(Jekyll::Stevenson.new, :error)
320 | )
321 |
322 | ENV.clear
323 |
324 | ENV['URL'] = 'https://example.com'
325 | ENV['DEPLOY_URL'] = 'https://578ab634d5d5cf960d620--open-api.netlify.com'
326 | ENV['DEPLOY_PRIME_URL'] = 'https://beta--open-api.netlify.com'
327 | ENV['PULL_REQUEST'] = 'false'
328 | ENV['WEBHOOK_TITLE'] = 'Title'
329 | ENV['WEBHOOK_URL'] = 'http://webtask.io/foo'
330 | ENV['WEBHOOK_BODY'] = '{}'
331 |
332 | config = Jekyll.configuration(
333 | source: jekyll_test_site,
334 | destination: File.join(jekyll_test_site, '_site'),
335 | )
336 | @site = Jekyll::Site.new(config)
337 | @site.read
338 | @site.generate
339 | end
340 |
341 | context 'info' do
342 | setup do
343 | @netlify = @site.config['netlify']
344 | end
345 |
346 | should 'be a Hash' do
347 | assert_operator @netlify, :has_key?, 'webhook'
348 | assert_instance_of Hash, @netlify['webhook']
349 | end
350 |
351 | should 'include webhook data' do
352 | assert_equal 'Title', @netlify['webhook']['title']
353 | assert_equal '{}', @netlify['webhook']['body']
354 | assert_equal 'http://webtask.io/foo', @netlify['webhook']['url']
355 | end
356 | end
357 | end
358 | end
359 |
--------------------------------------------------------------------------------