Skip to content

Commit 039d6f2

Browse files
feat: started adding download functionality
1 parent e70ac10 commit 039d6f2

File tree

6 files changed

+49
-0
lines changed

6 files changed

+49
-0
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ gemspec
88
gem "colorize"
99
gem "json"
1010
gem "rake", "~> 13.0"
11+
gem "rest-client"
1112
gem "rspec", "~> 3.0"
1213
gem "rubocop", "~> 1.21"

Gemfile.lock

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,36 @@ PATH
44
codeclimate_diff (0.1.1)
55
json
66
optparse
7+
rest-client
78

89
GEM
910
remote: https://rubygems.org/
1011
specs:
1112
ast (2.4.2)
1213
colorize (0.8.1)
1314
diff-lcs (1.5.0)
15+
domain_name (0.5.20190701)
16+
unf (>= 0.0.5, < 1.0.0)
17+
http-accept (1.7.0)
18+
http-cookie (1.0.5)
19+
domain_name (~> 0.5)
1420
json (2.6.2)
21+
mime-types (3.4.1)
22+
mime-types-data (~> 3.2015)
23+
mime-types-data (3.2022.0105)
24+
netrc (0.11.0)
1525
optparse (0.2.0)
1626
parallel (1.22.1)
1727
parser (3.1.2.1)
1828
ast (~> 2.4.1)
1929
rainbow (3.1.1)
2030
rake (13.0.6)
2131
regexp_parser (2.6.0)
32+
rest-client (2.1.0)
33+
http-accept (>= 1.7.0, < 2.0)
34+
http-cookie (>= 1.0.2, < 2.0)
35+
mime-types (>= 1.16, < 4.0)
36+
netrc (~> 0.8)
2237
rexml (3.2.5)
2338
rspec (3.11.0)
2439
rspec-core (~> 3.11.0)
@@ -46,6 +61,9 @@ GEM
4661
rubocop-ast (1.22.0)
4762
parser (>= 3.1.1.0)
4863
ruby-progressbar (1.11.0)
64+
unf (0.1.4)
65+
unf_ext
66+
unf_ext (0.0.8.2)
4967
unicode-display_width (2.3.0)
5068

5169
PLATFORMS
@@ -57,6 +75,7 @@ DEPENDENCIES
5775
colorize
5876
json
5977
rake (~> 13.0)
78+
rest-client
6079
rspec (~> 3.0)
6180
rubocop (~> 1.21)
6281

codeclimate_diff.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Gem::Specification.new do |spec|
2828
# Uncomment to register a new dependency of your gem
2929
spec.add_dependency("json")
3030
spec.add_dependency("optparse")
31+
spec.add_dependency("rest-client")
3132

3233
# For more information and examples about making a new gem, check out our
3334
# guide at: https://bundler.io/guides/creating_gem.html

lib/codeclimate_diff/configuration.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class Configuration
88
def initialize
99
@gitlab = {
1010
main_branch_name: "main",
11+
download_baseline_from_pipeline: false,
1112
project_id: nil,
1213
host: nil,
1314
personal_access_token: nil

lib/codeclimate_diff/downloader.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# frozen_string_literal: true
2+
3+
require "rest-client"
4+
5+
module CodeclimateDiff
6+
class Downloader
7+
def self.refresh_baseline_if_configured
8+
should_download = CodeclimateDiff.configuration.gitlab[:download_baseline_from_pipeline]
9+
return unless should_download
10+
11+
branch_name = CodeclimateDiff.configuration.gitlab[:main_branch_name]
12+
project_id = CodeclimateDiff.configuration.gitlab[:project_id]
13+
host = CodeclimateDiff.configuration.gitlab[:host]
14+
personal_access_token = CodeclimateDiff.configuration.gitlab[:personal_access_token]
15+
16+
# 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"
17+
url = "#{host}/api/v4/projects/#{project_id}/jobs/artifacts/#{branch_name}/raw/codeclimate_diff_baseline.json?job=code_quality"
18+
response = RestClient.get(url, { "PRIVATE-TOKEN": personal_access_token })
19+
File.write("codeclimate_diff_baseline.json", response.body)
20+
rescue StandardError => e
21+
puts e
22+
end
23+
end
24+
end

lib/codeclimate_diff/runner.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
require_relative "./codeclimate_wrapper"
66
require_relative "./result_printer"
77
require_relative "./issue_sorter"
8+
require_relative "./downloader"
89

910
module CodeclimateDiff
1011
class Runner
@@ -33,6 +34,8 @@ def self.calculate_issues_in_changed_files(changed_filenames)
3334
end
3435

3536
def self.calculate_preexisting_issues_in_changed_files(changed_filenames)
37+
Downloader.refresh_baseline_if_configured
38+
3639
puts "Extracting relevant preexisting issues..."
3740
all_issues = JSON.parse(File.read("./codeclimate_diff_baseline.json"))
3841

0 commit comments

Comments
 (0)