Maxim Abramchuk is a Rubyroid Labs Developer, keen on making the world around him better. This concerns his work as well. We have asked Maxim to tell our readers an easy way to make your code style great again. Check it now.

Why code style is important?

OK, having code style is good, but why is it important. There are a few reasons for that:

  1. Convention
  2. Consistency
  3. Awesomeness

code style

As you see, there is more than one answer why you should keep it clean. But it’s hard to find a developer, who would enjoy spending hours on such menial job. Luckily, you don’t have to. And what is the best thing about it, it wouldn’t even cost you a penny. Of course, if you want to take an easy way, you can always pay $12 for HoundCI, but we know a better way to do this.

How to improve code style for free?

Recently CircleCI was made free for private Github repository, so it was worth trying out instead of Jenkins.
code style

One of great things about Ruby on Rails, is that you can always find a bunch of gems for any needs.

I came across ruby gem pronto, which provides the following functionality.
code style
It works really easy from a local computer, but we can get even more from that. My idea was to setup it all from a remote CircleCI, so that it works every time Rails application is built. Bash script couldn’t have been better for that. To do so I needed 2 major things:

  1. Github access token
  2. ID of the pull request being built

Getting the token is pretty easy, CircleCI automatically sets a bunch of environment variables:
code
Now, let’s find out the pull request.

Finding out Pull Request ID

Unfortunately CircleCI doesn’t give the ID so easily. But after some investigation I managed to find a variable CI_PULL_REQUEST, containing a PR url like https://github.com/rails/rails/pull/20554. So, I created a regexp to get the PR number from the URL:

[ruby]$PULL_REQUEST_URL | grep -o -E ‘[0–9]+$’ |

head -1 | sed -e ‘s/^0\+//’[/ruby]

Now we have PULL_REQUEST_ID and GITHUB_ACCESS_TOKEN. Looks like everything is ready, right? But after you try to run Bash script, you will figure out that CircleCI does a shallow clone of the repo, which contains only part of the commits. What you need to do is to override CircleCI checkout process and specify a depth of fetching to 1 million commits.

How to override CircleCI checkout process

Gist with all the necessary code is here.

Add gem pronto to your Gemfile.

Add other gems according to your needs (ruby, scss, coffee, js and etc).

https://github.com/mmozuras/pronto#runners

Here you can find list of other gems, such as pronto-rubocop, pronto-scss, pronto-coffeelint to be able to setup the linters you want.

  1. Add to your .circle.yml following lines:

[ruby]
dependencies:

post:

— bin/cisetup

 

checkout:

post:

— git fetch origin — depth=1000000[/ruby]

  1. Create an executable file called cisetup (or whatever you wrote in .circle.yml) with following lines in it:

[ruby]export GITHUB_ACCESS_TOKEN=<your_github_access_token>

export PULL_REQUEST_URL=${CI_PULL_REQUEST}

export PULL_REQUEST_ID=`echo $PULL_REQUEST_URL |

grep -o -E ‘[0–9]+$’ | head -1 | sed -e ‘s/^0\+//’`

((bin/bundle exec pronto run -f github_pr -c origin/master)) || true[/ruby]

  1. Now you are good to go

 

Enjoy your perfectly styled code with minimum efforts made.

Questions? Comments? Let’s talk about them in the comments section below.

How useful was this post?

Click on a star to rate it!

Average rating 4.9 / 5. Vote count: 17

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?


Author

Daria Stolyar is a Marketing Manager at Rubyroid Labs. You can follow her at Linkedin.

2 Comments

  1. Why does the shallow commit pose a problem? And why are 1,000,000 commits necessary? Wouldn’t that only be. Ended if your PR was 1,000,000 commits diverged from master?

    • Maxim Abramchuck Reply

      After trying to build the first PR it caused an error at the remote machine. After some hours of debugging the pronto gem, debugging my script at the remote server via SSH, I figured out that CircleCI does a shallow clone of my repo, which contains only a part of my commits. So, I have to override the CircleCI checkout process and specify a depth of fetching to 1 million commits. After that everything works like a charm.

Write A Comment