-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a spec that ensures includes are used where possible, using Octokit to post code review comments to files that should be using an include, but doesn't.
- Loading branch information
Showing
5 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative './support/githubber' | ||
|
||
describe 'includes used' do | ||
def make_relative(path) | ||
root_dir = File.expand_path(File.join(__dir__, '..')) + File::SEPARATOR | ||
path.sub(root_dir, '') | ||
end | ||
|
||
def create_pull_request_review(message, filename, line_number) | ||
@githubber.create_pull_request_review(message, filename, line_number) | ||
true | ||
rescue StandardError => e | ||
puts 'Failed to create pull request review. Falling back to regular spec failure.' | ||
puts e | ||
false | ||
end | ||
|
||
def line_should_use_include(line) | ||
return nil if line.include? '<!--noinclude-->' | ||
|
||
match = line.match(/ `([^\|]+)`/) | ||
|
||
return nil unless match | ||
|
||
field_name = match[1] | ||
include_file = @fields[field_name] | ||
|
||
return nil unless include_file | ||
return nil if line.include? "{% include #{include_file}" | ||
|
||
[include_file, field_name] | ||
end | ||
|
||
before(:all) do | ||
def map_file(file_name) | ||
field_name = File.basename(file_name, '.md').gsub(/-[a-z]/) { |match| match[1].upcase } | ||
file_name = file_name.split('_includes/').last | ||
[ field_name, file_name ] | ||
end | ||
|
||
root_dir = File.expand_path(File.join(__dir__, '..')) | ||
include_dir = File.join(root_dir, '_includes') | ||
@fields = Dir.glob(File.join(include_dir, 'fields', '*.md')).map { |f| map_file(f) }.to_h | ||
@events = Dir.glob(File.join(include_dir, 'events', '*.md')).map { |f| map_file(f) }.to_h | ||
@files = Dir.glob(File.join(root_dir, '**', '*.md')).reject { |f| f.include?('_includes/fields') || f.include?('_includes/events') } | ||
@githubber = SwedbankPay::Githubber.new | ||
end | ||
|
||
context 'with fields' do | ||
subject { @fields } | ||
it { is_expected.not_to be_empty } | ||
end | ||
|
||
context 'with events' do | ||
subject { @events } | ||
it { is_expected.not_to be_empty } | ||
end | ||
|
||
context 'with GITHUB_CONTEXT' do | ||
subject { ENV['GITHUB_CONTEXT'] } | ||
it { is_expected.not_to be_empty } | ||
end | ||
|
||
context 'with files' do | ||
subject { @files } | ||
it { is_expected.not_to be_empty } | ||
|
||
it 'should use includes where available' do | ||
@files.each do |file| | ||
File.readlines(file).each_with_index do |line, line_number| | ||
(include_file, field_name) = line_should_use_include(line) | ||
next unless include_file | ||
|
||
relative_filename = make_relative(file) | ||
msg = "#{relative_filename}:#{line_number} should use the include '#{include_file}' for the field `#{field_name}`." | ||
|
||
unless create_pull_request_review(msg, relative_filename, line_number) | ||
expect(line).to include("{% include #{include_file}"), msg | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'octokit' | ||
|
||
module SwedbankPay | ||
class Githubber | ||
def initialize | ||
github_token = ENV['GITHUB_TOKEN'] | ||
github_context_env = ENV['GITHUB_CONTEXT'] | ||
|
||
raise 'GITHUB_TOKEN is not set' if github_token.nil? || github_token.empty? | ||
raise 'GITHUB_CONTEXT is not set' if github_context_env.nil? || github_context_env.empty? | ||
|
||
@octokit = Octokit::Client.new(access_token: github_token) | ||
@github_context = JSON.parse(github_context_env) | ||
end | ||
|
||
def create_pull_request_review(message, filename, line_number) | ||
repo = @github_context['repository'] | ||
pr_number = @github_context['event']['number'] | ||
|
||
raise 'Could not find repository' if repo.nil? || repo.empty? | ||
raise 'Could not find pull request number' if pr_number.nil? || pr_number.zero? | ||
|
||
comments = [{ path: filename, position: line_number, body: message }] | ||
options = { event: 'REQUEST_CHANGES', comments: comments } | ||
|
||
puts "Creating pull request review for #{filename}:#{line_number} in #{repo}##{pr_number}." | ||
|
||
# TODO: For some reason, the API call below fails with the following error: | ||
# POST https://api.github.com/repos/SwedbankPay/developer.swedbankpay.com/pulls/1711/reviews: | ||
# 422 - Unprocessable Entity | ||
# Error summary: | ||
# Pull request review thread position is invalid and Pull request review | ||
# thread diff hunk can't be blank | ||
# // See: https://docs.github.com/rest/reference/pulls#create-a-review-for-a-pull-request | ||
@octokit.create_pull_request_review(repo, pr_number, options) | ||
end | ||
end | ||
end |