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

Fix the CI build: Rubocop fixes #92

Merged
merged 12 commits into from
Aug 8, 2022
15 changes: 5 additions & 10 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,12 @@ jobs:
strategy:
fail-fast: false
matrix:
ruby: ['2.5', '2.6', '2.7', '3.0']
ruby: ['2.5', '2.6', '2.7', '3.0', '3.1']
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v2
- name: Set up Ruby
uses: ruby/setup-ruby@v1
- uses: actions/checkout@v2
- uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
- name: Install dependencies
run: bundle install
- name: Run tests
run: bundle exec rake
bundler-cache: true
- run: bundle exec rake --trace
5 changes: 3 additions & 2 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@
AllCops:
Exclude:
- 'spike/*.rb'
- 'vendor/**/*'
NewCops: enable
TargetRubyVersion: 2.5

Gemspec/RequiredRubyVersion:
Enabled: false

Metrics/BlockLength:
Layout/LineLength:
Exclude:
- 'spec/**/*'
- 'test/**/*'

Metrics/LineLength:
Metrics/BlockLength:
Exclude:
- 'spec/**/*'
- 'test/**/*'
Expand Down
1 change: 0 additions & 1 deletion .tool-versions

This file was deleted.

3 changes: 1 addition & 2 deletions lib/unwrappr/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ class CLI < Clamp::Command
exit(0)
end

subcommand 'all', 'run bundle update, push to GitHub, '\
'create a pr and annotate changes' do
subcommand 'all', 'run bundle update, push to GitHub, create a pr and annotate changes' do
option ['-R', '--recursive'],
:flag,
'Recurse into subdirectories',
Expand Down
15 changes: 1 addition & 14 deletions lib/unwrappr/github/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,7 @@ def annotate_pull_request(pr_number, lock_files)
end

def git_client
@git_client ||= Octokit::Client.new(access_token: github_token)
end

def github_token
@github_token ||= ENV.fetch('GITHUB_TOKEN')
rescue KeyError
raise %(
Missing environment variable GITHUB_TOKEN.
See https://github.com/settings/tokens to set up personal access tokens.
Add to the environment:

export GITHUB_TOKEN=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

)
@git_client ||= Octokit::Client.new(access_token: Octokit.access_token_from_environment)
end
end
end
Expand Down
15 changes: 14 additions & 1 deletion lib/unwrappr/octokit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@
# Wrapper around octokit
module Octokit
def self.client
@client ||= Client.new(access_token: ENV['GITHUB_TOKEN'])
@client ||= Client.new(access_token: access_token_from_environment)
end

def self.access_token_from_environment
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice extraction!

ENV.fetch('GITHUB_TOKEN') do
raise <<~MESSAGE
Missing environment variable GITHUB_TOKEN.
See https://github.com/settings/tokens to set up personal access tokens.
Add to the environment:

export GITHUB_TOKEN=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

MESSAGE
end
end
end
4 changes: 2 additions & 2 deletions lib/unwrappr/researchers/github_repo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class GithubRepo
}ix.freeze

def research(_gem_change, gem_change_info)
repo = match_repo(gem_change_info, :source_code_uri) ||
match_repo(gem_change_info, :homepage_uri)
repo = match_repo(gem_change_info, 'source_code_uri') ||
match_repo(gem_change_info, 'homepage_uri')
gem_change_info.merge(github_repo: repo)
end

Expand Down
5 changes: 2 additions & 3 deletions lib/unwrappr/ruby_gems.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def gem_info(name, version)
def parse(response, name)
case response.status
when 200
JSON.parse(response.body, object_class: OpenStruct)
JSON.parse(response.body)
when 404
nil
else
Expand All @@ -27,8 +27,7 @@ def parse(response, name)
end

def error_message(response:, name:)
"Rubygems response for #{name}: "\
"HTTP #{response.status}: #{response.body}"
"Rubygems response for #{name}: HTTP #{response.status}: #{response.body}"
end
end
end
Expand Down
10 changes: 4 additions & 6 deletions lib/unwrappr/writers/project_links.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@ def write
private

def change_log
link_or_strikethrough('change-log',
ruby_gems_info&.changelog_uri)
link_or_strikethrough('change-log', ruby_gems_info('changelog_uri'))
end

def source_code
link_or_strikethrough('source-code',
ruby_gems_info&.source_code_uri)
link_or_strikethrough('source-code', ruby_gems_info('source_code_uri'))
end

GEM_DIFF_URL_TEMPLATE = 'https://my.diffend.io/gems/%s/%s/%s'
Expand All @@ -46,8 +44,8 @@ def gem_diff
link_or_strikethrough('gem-diff', gem_diff_url)
end

def ruby_gems_info
@gem_change_info[:ruby_gems]
def ruby_gems_info(*args)
@gem_change_info.dig(:ruby_gems, *args)
end

def link_or_strikethrough(text, url)
Expand Down
7 changes: 1 addition & 6 deletions lib/unwrappr/writers/security_vulnerabilities.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,7 @@ def cve_url(id)
end

def cvss_v2(advisory)
# rubocop:disable Style/GuardClause
if advisory.cvss_v2
"CVSS V2: [#{advisory.cvss_v2} #{advisory.criticality}]"\
"(#{cvss_v2_url(advisory.cve_id)})"
end
# rubocop:enable Style/GuardClause
"CVSS V2: [#{advisory.cvss_v2} #{advisory.criticality}](#{cvss_v2_url(advisory.cve_id)})" if advisory.cvss_v2
end

def cvss_v2_url(id)
Expand Down
2 changes: 1 addition & 1 deletion lib/unwrappr/writers/title.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class << self
def write(gem_change, gem_change_info)
embellished_gem_name = maybe_link(
gem_change.name,
gem_change_info[:ruby_gems]&.homepage_uri
gem_change_info.dig(:ruby_gems, 'homepage_uri')
)
"### #{embellished_gem_name}\n"
end
Expand Down
6 changes: 0 additions & 6 deletions spec/lib/unwrappr/github/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,6 @@
end

context 'without a token' do
before do
expect(ENV).to receive(:fetch)
.with('GITHUB_TOKEN')
.and_raise(KeyError, 'key not found GITHUB_TOKEN')
end

it 'provides useful feedback' do
expect { make_pull_request! }.to raise_error(RuntimeError, /^Missing environment variable/)
end
Expand Down
9 changes: 4 additions & 5 deletions spec/lib/unwrappr/lock_file_annotator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ module Unwrappr
describe '#annotate' do
subject(:annotate) { annotator.annotate }

context 'given a Gemfile.lock that changes: '\
'rspec-support 3.7.0 -> 3.7.1' do
context 'given a Gemfile.lock that changes: rspec-support 3.7.0 -> 3.7.1' do
let(:lock_file_diff_source) { instance_double(Github::PrSource) }
let(:annotation_sink) { instance_spy(Github::PrSink) }
let(:base_lock_file) { <<~BASE_FILE }
Expand Down Expand Up @@ -97,9 +96,9 @@ module Unwrappr
before do
allow(::Unwrappr::RubyGems).to receive(:gem_info)
.with('rspec-support', GemVersion.new('3.7.1'))
.and_return(spy(homepage_uri: 'home-uri',
source_code_uri: 'source-uri',
changelog_uri: 'changelog-uri'))
.and_return({ 'homepage_uri' => 'home-uri',
'source_code_uri' => 'source-uri',
'changelog_uri' => 'changelog-uri' })
allow(lock_file_diff_source).to receive(:each_file)
.and_yield(LockFileDiff.new(filename: 'Gemfile.lock',
base_file: base_lock_file,
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/unwrappr/lock_file_comparator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module Unwrappr

it 'calls the comparator with indexed specs versions' do
expect(SpecVersionComparator).to receive(:perform)
.with({ name1: 'version1' }, name2: 'version2')
.with({ name1: 'version1' }, { name2: 'version2' })

perform
end
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/unwrappr/researchers/github_repo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ module Researchers
let(:gem_change_info) do
{
ruby_gems: {
source_code_uri: source_code_uri,
homepage_uri: homepage_uri
'source_code_uri' => source_code_uri,
'homepage_uri' => homepage_uri
}
}
end
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/unwrappr/ruby_gems_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module Unwrappr
let(:response_body) { '{"key": "value" }' }

it 'returns provided details' do
expect(subject.key).to eql('value')
expect(subject['key']).to eql('value')
end
end

Expand Down
18 changes: 12 additions & 6 deletions spec/lib/unwrappr/writers/project_links_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ module Writers
context 'given gem change info with urls' do
let(:gem_change_info) do
{
ruby_gems: spy(source_code_uri: 'source-uri',
changelog_uri: 'changelog-uri')
ruby_gems: {
'source_code_uri' => 'source-uri',
'changelog_uri' => 'changelog-uri'
}
}
end

Expand All @@ -34,8 +36,10 @@ module Writers
context 'given gem change info with urls for an added gem' do
let(:gem_change_info) do
{
ruby_gems: spy(source_code_uri: 'source-uri',
changelog_uri: 'changelog-uri')
ruby_gems: {
'source_code_uri' => 'source-uri',
'changelog_uri' => 'changelog-uri'
}
}
end
let(:base_version) { nil }
Expand All @@ -48,8 +52,10 @@ module Writers
context 'given gem change info with urls for a removed gem' do
let(:gem_change_info) do
{
ruby_gems: spy(source_code_uri: 'source-uri',
changelog_uri: 'changelog-uri')
ruby_gems: {
'source_code_uri' => 'source-uri',
'changelog_uri' => 'changelog-uri'
}
}
end
let(:head_version) { nil }
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/unwrappr/writers/title_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

context 'given a gem homepage URI' do
let(:gem_change_info) { { ruby_gems: ruby_gems } }
let(:ruby_gems) { spy(homepage_uri: 'home-uri') }
let(:ruby_gems) { { 'homepage_uri' => 'home-uri' } }

it { should eq "### [test-gem](home-uri)\n" }
end
Expand Down
1 change: 1 addition & 0 deletions unwrappr.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Gem::Specification.new do |spec| # rubocop:disable Metrics/BlockLength:
'changelog_uri' => "#{GITHUB_URL}/blob/HEAD/CHANGELOG.md",
'documentation_uri' => "#{GITHUB_URL}/blob/HEAD/README.md",
'homepage_uri' => HOMEPAGE_URL,
'rubygems_mfa_required' => 'true',
'source_code_uri' => GITHUB_URL
}
end