Skip to content

Commit bb9ad98

Browse files
feat: the main branch doesn't need to be set to work now, and updated docs for getting it to pull down the baseline from the pipeline
1 parent ca0bc98 commit bb9ad98

File tree

3 files changed

+46
-21
lines changed

3 files changed

+46
-21
lines changed

README.md

+44-19
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ NOTE: similar code will only work correctly if you run a diff on all the files i
1010
## Installation
1111

1212
1. Install the codeclimate cli:
13-
```
13+
```bash
1414
brew tap codeclimate/formulae
1515
brew install codeclimate
1616
```
1717

1818
2. Add a `.codeclimate.yml` config file eg:
19-
```
19+
```yml
2020
---
2121
version: "2"
2222
plugins:
@@ -52,7 +52,7 @@ NOTE: similar code will only work correctly if you run a diff on all the files i
5252
3. Add a `.reek.yml` config file eg:
5353

5454
See https://github.com/troessner/reek#working-with-rails
55-
```
55+
```yml
5656
detectors:
5757
IrresponsibleModule:
5858
enabled: false
@@ -90,18 +90,10 @@ NOTE: similar code will only work correctly if you run a diff on all the files i
9090

9191
4. Add a `.codecimate_diff.yml` configuration file
9292
```
93-
main_branch_name: main
94-
95-
# settings to pull down the baseline from the pipeline in Gitlab before checking your branch
96-
gitlab:
97-
download_baseline_from_pipeline: true # If false or excluded, you will need to generate the baseline manually
98-
project_id: '85'
99-
host: https://gitlab.digitalnz.org/
100-
baseline_filename: 'gl-code-quality-report.json'
93+
main_branch_name: main # defaults to main
10194
```
10295

103-
104-
4. Install the gem
96+
5. Install the gem
10597

10698
Add this line to your application's Gemfile:
10799
@@ -113,17 +105,14 @@ NOTE: similar code will only work correctly if you run a diff on all the files i
113105
114106
```bash
115107
$ bundle install
116-
117-
# OR just install it locally
118-
$ gem install codeclimate_diff
119108
```
120109
121110
Then generate the executable:
122111
123112
$ bundle binstubs codeclimate_diff
124113
125114
126-
4. Run the baseline and commit the result to the repo
115+
6. Run the baseline and commit the result to the repo
127116
128117
```
129118
./bin/codeclimate_diff --baseline
@@ -137,7 +126,7 @@ NOTE: similar code will only work correctly if you run a diff on all the files i
137126
138127
3. Check if you've added any issues (about 10 secs per code file changed on your branch):
139128

140-
```
129+
```bash
141130
# runs on all code files changed in your branch
142131
./bin/codeclimate_diff
143132
@@ -151,7 +140,43 @@ NOTE: similar code will only work correctly if you run a diff on all the files i
151140
# only shows the new and fixed issues
152141
./bin/codeclimate_diff --new-only
153142
```
154-
4. Now you have time to fix the issues yay!
143+
144+
4. Now you have time to fix the issues, horray!
145+
146+
147+
## Setting it up to download the latest baseline from your CI Pipeline (Gitlab only)
148+
149+
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).
150+
151+
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.
152+
153+
1. In your Gitlab CI Configuration where you include the `Code-Quality.gitlab-ci.yml` template:
154+
155+
```yml
156+
include:
157+
- template: Code-Quality.gitlab-ci.yml
158+
159+
# add this bit:
160+
code_quality:
161+
artifacts:
162+
paths: [gl-code-quality-report.json] . # without this, the artifact can't be downloaded
163+
```
164+
165+
2. Add your project settings to the `.codecimate_diff.yml` configuration file:
166+
```yml
167+
main_branch_name: main
168+
169+
# settings to pull down the baseline from the pipeline in Gitlab before checking your branch
170+
gitlab:
171+
download_baseline_from_pipeline: true # If false or excluded, you will need to generate the baseline manually
172+
project_id: '<project id>'
173+
host: https://gitlab.digitalnz.org/
174+
baseline_filename: 'gl-code-quality-report.json'
175+
```
176+
177+
3. Create a personal access token with `read_api` access and save it in the `CODECLIMATE_DIFF_GITLAB_PERSONAL_ACCESS_TOKEN` env variable
178+
179+
Now when you run it on the changed files in your branch, it will refresh the baseline first!
155180

156181
## Development
157182

lib/codeclimate_diff/downloader.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def self.refresh_baseline_if_configured
99
return unless CodeclimateDiff.configuration["gitlab"]["download_baseline_from_pipeline"]
1010

1111
puts "Downloading baseline file from gitlab"
12-
branch_name = CodeclimateDiff.configuration["main_branch_name"]
12+
branch_name = CodeclimateDiff.configuration["main_branch_name"] || "main"
1313
project_id = CodeclimateDiff.configuration["gitlab"]["project_id"]
1414
host = CodeclimateDiff.configuration["gitlab"]["host"]
1515
baseline_filename = CodeclimateDiff.configuration["gitlab"]["baseline_filename"]

lib/codeclimate_diff/runner.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module CodeclimateDiff
1111
class Runner
1212
def self.calculate_changed_filenames(pattern)
1313
extra_grep_filter = pattern ? " | grep '#{pattern}'" : ""
14-
branch_name = CodeclimateDiff.configuration["main_branch_name"]
14+
branch_name = CodeclimateDiff.configuration["main_branch_name"] || "main"
1515
files_changed_str = `git diff --name-only #{branch_name} | grep --invert-match spec/ | grep --extended-regexp '.js$|.rb$'#{extra_grep_filter}`
1616
puts "Files changed on branch: #{files_changed_str}"
1717

0 commit comments

Comments
 (0)