Skip to content

First usable version #2

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

Merged
merged 3 commits into from
Nov 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
codeclimate_diff (0.1.3)
codeclimate_diff (0.1.4)
colorize
json
optparse
Expand Down
106 changes: 84 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ NOTE: similar code will only work correctly if you run a diff on all the files i
## Installation

1. Install the codeclimate cli:
```
```bash
brew tap codeclimate/formulae
brew install codeclimate
```

2. Add a `.codeclimate.yml` config file eg:
```
```yml
---
version: "2"
plugins:
Expand Down Expand Up @@ -46,10 +46,54 @@ NOTE: similar code will only work correctly if you run a diff on all the files i
- "**/__tests__/"
- "**/__mocks__/"
- "/.gitlab/"
- coverage/
- coverage/ . # simple cov
```

3. Add a `.reek.yml` config file eg:

See https://github.com/troessner/reek#working-with-rails
```yml
detectors:
IrresponsibleModule:
enabled: false

LongParameterList:
max_params: 4 # defaults to 3. You want this number realistic but stretchy so we can move it down

TooManyStatements:
max_statements: 10 # defaults to 5. You want this number realistic but stretchy so we can move it down

directories:
"app/controllers":
IrresponsibleModule:
enabled: false
NestedIterators:
max_allowed_nesting: 2
UnusedPrivateMethod:
enabled: false
InstanceVariableAssumption:
enabled: false
"app/helpers":
IrresponsibleModule:
enabled: false
UtilityFunction:
enabled: false
FeatureEnvy:
enabled: false
"app/mailers":
InstanceVariableAssumption:
enabled: false
"app/models":
InstanceVariableAssumption:
enabled: false
```

4. Add a `.codecimate_diff.yml` configuration file
```
main_branch_name: main # defaults to main
```

3. Install the gem
5. Install the gem

Add this line to your application's Gemfile:

Expand All @@ -61,17 +105,14 @@ NOTE: similar code will only work correctly if you run a diff on all the files i

```bash
$ bundle install

# OR just install it locally
$ gem install codeclimate_diff
```

Then generate the executable:

$ bundle binstubs codeclimate_diff


4. Run the baseline and commit the result to the repo
6. Run the baseline and commit the result to the repo

```
./bin/codeclimate_diff --baseline
Expand All @@ -83,9 +124,9 @@ NOTE: similar code will only work correctly if you run a diff on all the files i

2. Do some work

3. Check if you've added any issues (about 10 secs per code file changed on your branch)
3. Check if you've added any issues (about 10 secs per code file changed on your branch):

```
```bash
# runs on all code files changed in your branch
./bin/codeclimate_diff

Expand All @@ -99,22 +140,43 @@ NOTE: similar code will only work correctly if you run a diff on all the files i
# only shows the new and fixed issues
./bin/codeclimate_diff --new-only
```
4. Now you have time to fix the issues yay!

## Configuration
4. Now you have time to fix the issues, horray!


Example:
## Setting it up to download the latest baseline from your CI Pipeline (Gitlab only)

Gitlab has a codeclimate template you can add to your pipeline that runs on main builds and then runs on your branch and outputs a difference (see https://docs.gitlab.com/ee/ci/testing/code_quality.html).

With a few tweaks to your CI configuration, we can pull down the main build baseline from the job so we don't have to do it locally.

1. In your Gitlab CI Configuration where you include the `Code-Quality.gitlab-ci.yml` template:

```yml
include:
- template: Code-Quality.gitlab-ci.yml

# add this bit:
code_quality:
artifacts:
paths: [gl-code-quality-report.json] . # without this, the artifact can't be downloaded
```

2. Add your project settings to the `.codecimate_diff.yml` configuration file:
```yml
main_branch_name: main

# settings to pull down the baseline from the pipeline in Gitlab before checking your branch
gitlab:
download_baseline_from_pipeline: true # If false or excluded, you will need to generate the baseline manually
project_id: '<project id>'
host: https://gitlab.digitalnz.org/
baseline_filename: 'gl-code-quality-report.json'
```

.codeclimate_diff.yml
3. Create a personal access token with `read_api` access and save it in the `CODECLIMATE_DIFF_GITLAB_PERSONAL_ACCESS_TOKEN` env variable

```rb
gitlab:
main_branch_name: main
download_baseline_for_pipeline: true
project_id: '..'
host: https://gitlab.digitalnz.org/
personal_access_token: <LOAD FROM ENV VARIABLE>
```
Now when you run it on the changed files in your branch, it will refresh the baseline first!

## Development

Expand Down
6 changes: 3 additions & 3 deletions lib/codeclimate_diff/downloader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
module CodeclimateDiff
class Downloader
def self.refresh_baseline_if_configured
should_download = CodeclimateDiff.configuration["gitlab"]["download_baseline_for_pipeline"]
return unless should_download
return unless CodeclimateDiff.configuration["gitlab"]
return unless CodeclimateDiff.configuration["gitlab"]["download_baseline_from_pipeline"]

puts "Downloading baseline file from gitlab"
branch_name = CodeclimateDiff.configuration["main_branch_name"]
branch_name = CodeclimateDiff.configuration["main_branch_name"] || "main"
project_id = CodeclimateDiff.configuration["gitlab"]["project_id"]
host = CodeclimateDiff.configuration["gitlab"]["host"]
baseline_filename = CodeclimateDiff.configuration["gitlab"]["baseline_filename"]
Expand Down
2 changes: 1 addition & 1 deletion lib/codeclimate_diff/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module CodeclimateDiff
class Runner
def self.calculate_changed_filenames(pattern)
extra_grep_filter = pattern ? " | grep '#{pattern}'" : ""
branch_name = CodeclimateDiff.configuration["main_branch_name"]
branch_name = CodeclimateDiff.configuration["main_branch_name"] || "main"
files_changed_str = `git diff --name-only #{branch_name} | grep --invert-match spec/ | grep --extended-regexp '.js$|.rb$'#{extra_grep_filter}`
puts "Files changed on branch: #{files_changed_str}"

Expand Down
2 changes: 1 addition & 1 deletion lib/codeclimate_diff/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module CodeclimateDiff
VERSION = "0.1.3"
VERSION = "0.1.4"
end