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

Copy c5-conventions/rubocop.yml into the new app #148

Conversation

bunnymatic
Copy link
Contributor

@bunnymatic bunnymatic commented Jan 23, 2020

Problem

Our connection to the rubocop.yml file in https://github.com/carbonfive/c5-conventions seems fragile.

Updates to that repo will affect all consumers out in the world (who we don't know) and may cause issues with their app. We've already seen it affect 1 client project in house.

Solution

By default, pull the rubocop.yml from c5-conventions at the time the
app is constructed and pin it. This will disconnect the constructed app
from the possibly dynamic rubocop file.

Additionally, we add some commentary to the top of the file to indcate
the SHA which was the original pull and a note to suggest upgrading at
the developers' discretion.

Changes

  • by default, fetch the rubocop file from github and write it to the
    newly created target app
  • add rspec
  • add a test for this new functionality (the pull of the rubocop from GitHub)
  • make the default rake task run rubocop and rspec

Fixes #146

@bunnymatic bunnymatic force-pushed the features/include-c5-conventions-rubocop-file-instead-of-live-link branch 3 times, most recently from 6bbda03 to ae0f2f4 Compare January 23, 2020 06:59
raise "#{command} failed with status #{$CHILD_STATUS.exitstatus}."
end

output
end
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 was a combo of running rubocop -a and adding the return results to the function which i wanted to grab the SHA from the c5-conventions repo.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

as it turns out, this didn't actually work. rolled it back to just return the output which i needed for the git ls-remote.

If rubocop corrects this again, we may need to either suppress the rule or figure out why it's not working.

Copy link
Contributor

@mattbrictson mattbrictson left a comment

Choose a reason for hiding this comment

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

Looks good! After talking with @christiannelson I think we need to have the guard clause to ensure this is done only for the C5 raygun-rails template. Otherwise I have a couple Ruby-isms to suggest; see comments below.

''
].join("\n")
rubocop_contents = URI.open(rubocop_file)
write_file("#{@app_dir}/.rubocop.yml", prefix + rubocop_contents.string)
Copy link
Contributor

Choose a reason for hiding this comment

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

I like using Ruby heredoc for multiline strings like the prefix:

Suggested change
write_file("#{@app_dir}/.rubocop.yml", prefix + rubocop_contents.string)
write_file("#{@app_dir}/.rubocop.yml", <<~PREFIX + rubocop_contents.string)
# Sourced from #{C5_CONVENTIONS_REPO} @ #{sha}"
#
# If you make changes to this file, consider opening a PR to backport them to the c5-conventions repo:
# https://github.com/#{C5_CONVENTIONS_REPO}/blob/master/rubocop/rubocop.yml
PREFIX

Copy link
Contributor

Choose a reason for hiding this comment

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

Or taking it a step further:

IO.write("#{@app_dir}/.rubocop.yml", <<~RUBOCOP_YML)
  # Sourced from #{C5_CONVENTIONS_REPO} @ #{sha}"
  #
  # If you make changes to this file, consider opening a PR to backport them to the c5-conventions repo:
  # https://github.com/#{C5_CONVENTIONS_REPO}/blob/master/rubocop/rubocop.yml
  
  #{rubocop_contents.string}
RUBOCOP_YML

Comment on lines 238 to 242
def write_file(filename, contents)
fp = File.open(filename, 'w')
fp.write(contents)
fp.close
end
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is equivalent to Ruby's:

IO.write(filename, contents)

https://ruby-doc.org/core-2.7.0/IO.html#method-c-write

@@ -105,6 +107,8 @@ def copy_prototype

FileUtils.mv dirs_to_move, app_dir
FileUtils.remove_dir extraneous_dir

fetch_rubocop_file
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we guard this so we only do it for C5 repos? That way if raygun is used with a third-party template we are not potentially clobbering their rubocop config.

Suggested change
fetch_rubocop_file
fetch_rubocop_file if @prototype_repo == CARBONFIVE_REPO

Sort of like we do here:

raygun/lib/raygun/raygun.rb

Lines 173 to 179 in 03c07eb

def print_next_steps
if @prototype_repo == CARBONFIVE_REPO
print_next_steps_carbon_five
else
print_next_steps_for_custom_repo
end
end

Copy link
Contributor Author

@bunnymatic bunnymatic left a comment

Choose a reason for hiding this comment

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

  • Addressed PR comments
  • Added rspec and set the default rake task to spec
  • Added a test for the fetch_rubocop_repo function

raise "#{command} failed with status #{$CHILD_STATUS.exitstatus}."
end

output
end
Copy link
Contributor Author

Choose a reason for hiding this comment

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

as it turns out, this didn't actually work. rolled it back to just return the output which i needed for the git ls-remote.

If rubocop corrects this again, we may need to either suppress the rule or figure out why it's not working.

Copy link
Contributor

@mattbrictson mattbrictson left a comment

Choose a reason for hiding this comment

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

Looks good! One minor suggestion below. I am going to test this out locally later today.

Rakefile Outdated
Comment on lines 9 to 11
rescue LoadError
# no rspec available
end
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we can remove this rescue because we have the rspec dependency listed in the gemspec. If a developer forgets to use bundle exec that is a legitimate mistake and they should see an exception.

Copy link
Contributor

@mattbrictson mattbrictson left a comment

Choose a reason for hiding this comment

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

I tested this locally and ran into two issues.

The first is minor: the generated .rubocop.yml has this header that is misleading and not very useful.

# Configuration hierarchy:
#
# 1. Rubocop defaults
# 2. Carbon Five defaults (this file)
# 3. Project overrides
#
# See http://rubocop.readthedocs.io/en/latest/configuration/#inheriting-configuration-from-a-remote-url for details.
#

It would be nice to strip this out. Or maybe update c5-conventions to just get rid of it altogether? In any case, it's not a blocker for this PR.

The bigger issue is that the generated project results in a broken RuboCop config:

Running RuboCop...
Error: unrecognized cop Rails/ApplicationRecord found in .rubocop.yml, unrecognized cop Rails/HasAndBelongsToMany found in .rubocop.yml, unrecognized cop Rails/RakeEnvironment found in .rubocop.yml
RuboCop failed!

This because the c5-conventions file specifies Rails/* cops but does not require rubocop-rails! I think we need to fix this upstream before we can merge this PR. What do you think?

@bunnymatic
Copy link
Contributor Author

I already fixed that. I think we just need to coordinate the merge with this carbonfive/c5-conventions#10.

@bunnymatic bunnymatic force-pushed the features/include-c5-conventions-rubocop-file-instead-of-live-link branch 2 times, most recently from 03f8c3d to cc37fde Compare January 25, 2020 17:04
@bunnymatic
Copy link
Contributor Author

As for the thing about this comment in the rubocop file

# Configuration hierarchy:
#
# 1. Rubocop defaults
# 2. Carbon Five default
...

I agree with the fact that we should not include it. I read it a couple times and wondered if it even belongs where it is. If apps are actually pulling this in remotely, it is unlikely that anyone is looking at it so I wonder about the value of those comments. So I'm inclined to update comments in the C5 conventions repo so they make more sense and then they wouldn't have to be trimmed during this copy. And they might provide some valuable information.

Copy link
Contributor

@mattbrictson mattbrictson left a comment

Choose a reason for hiding this comment

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

Thanks! This all makes sense. I assume you will merge carbonfive/c5-conventions#10 first?

@mattbrictson
Copy link
Contributor

As for the thing about this comment in the rubocop file
...
I'm inclined to update comments in the C5 conventions repo so they make more sense and then they wouldn't have to be trimmed during this copy. And they might provide some valuable information.

Logged a ticket for this: carbonfive/c5-conventions#12

@bunnymatic bunnymatic force-pushed the features/include-c5-conventions-rubocop-file-instead-of-live-link branch from cc37fde to 39b54e9 Compare January 29, 2020 19:06
--------

Our connection to the rubocop.yml file in https://github.com/carbonfive/c5-conventions seems fragile.

Updates to that repo will affect all consumers out in the world (who we don't know) and may cause issues with their app. We've already seen it affect 1 client project in house.

Solution
---------

By default, pull the `rubocop.yml` from `c5-conventions` at the time the
app is constructed and pin it.  This will disconnect the constructed app
from the possibly dynamic rubocop file.

Additionally, we add some commentary to the top of the file to indcate
the SHA which was the original pull and a note to suggest upgrading at
the developers' discretion.

Changes
-------

* by default, fetch the rubocop file from github and write it to the
newly created target app
* add rspec
* add a test for this new functionality (the pull of the rubocop from GitHub)

Fixes #146
@bunnymatic bunnymatic force-pushed the features/include-c5-conventions-rubocop-file-instead-of-live-link branch from 5deb520 to 860eb23 Compare January 29, 2020 19:09
@bunnymatic bunnymatic merged commit dbff3fd into master Jan 29, 2020
@bunnymatic bunnymatic deleted the features/include-c5-conventions-rubocop-file-instead-of-live-link branch January 29, 2020 19:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fragile connection to rubocop.yml with carbonfive/c5-conventions
2 participants