Skip to content

Commit 2c65590

Browse files
feat: better error handling for baseline downloading
1 parent 2af98d0 commit 2c65590

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
codeclimate_diff (0.1.6)
4+
codeclimate_diff (0.1.7)
55
colorize
66
json
77
optparse

lib/codeclimate_diff/downloader.rb

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,34 @@ def self.refresh_baseline_if_configured
88
return unless CodeclimateDiff.configuration["gitlab"]
99
return unless CodeclimateDiff.configuration["gitlab"]["download_baseline_from_pipeline"]
1010

11-
puts "Downloading baseline file from gitlab"
11+
personal_access_token = ENV.fetch("CODECLIMATE_DIFF_GITLAB_PERSONAL_ACCESS_TOKEN")
12+
13+
if !personal_access_token
14+
puts "Missing environment variable 'CODECLIMATE_DIFF_GITLAB_PERSONAL_ACCESS_TOKEN'. Using current baseline."
15+
return
16+
end
17+
18+
puts "Downloading baseline file from gitlab..."
1219
branch_name = CodeclimateDiff.configuration["main_branch_name"] || "main"
1320
project_id = CodeclimateDiff.configuration["gitlab"]["project_id"]
1421
host = CodeclimateDiff.configuration["gitlab"]["host"]
1522
baseline_filename = CodeclimateDiff.configuration["gitlab"]["baseline_filename"]
16-
personal_access_token = ENV.fetch("CODECLIMATE_DIFF_GITLAB_PERSONAL_ACCESS_TOKEN")
1723

1824
# curl --output codeclimate_diff_baseline.json --header "PRIVATE-TOKEN: MYTOKEN" "https://gitlab.digitalnz.org/api/v4/projects/85/jobs/artifacts/main/raw/codeclimate_diff_baseline.json?job=code_quality"
1925
url = "#{host}/api/v4/projects/#{project_id}/jobs/artifacts/#{branch_name}/raw/#{baseline_filename}?job=code_quality"
2026
response = RestClient.get(url, { "PRIVATE-TOKEN": personal_access_token })
21-
File.write("codeclimate_diff_baseline.json", response.body)
27+
28+
if response.code < 300
29+
File.write("codeclimate_diff_baseline.json", response.body)
30+
puts "Successfully updated the baseline."
31+
else
32+
puts "Downloading baseline file failed with status code #{response.code}: #{response.body}"
33+
puts "Using current baseline."
34+
end
35+
2236
rescue StandardError => e
2337
puts e
38+
puts "Using current baseline."
2439
end
2540
end
2641
end

lib/codeclimate_diff/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module CodeclimateDiff
4-
VERSION = "0.1.6"
4+
VERSION = "0.1.7"
55
end

0 commit comments

Comments
 (0)