Class: CodeclimateDiff::Downloader

Inherits:
Object
  • Object
show all
Defined in:
lib/codeclimate_diff/downloader.rb

Class Method Summary collapse

Class Method Details

.refresh_baseline_if_configuredObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/codeclimate_diff/downloader.rb', line 7

def self.refresh_baseline_if_configured
  return unless CodeclimateDiff.configuration["gitlab"]
  return unless CodeclimateDiff.configuration["gitlab"]["download_baseline_from_pipeline"]

  personal_access_token = ENV.fetch("CODECLIMATE_DIFF_GITLAB_PERSONAL_ACCESS_TOKEN")

  if !personal_access_token
    puts "Missing environment variable 'CODECLIMATE_DIFF_GITLAB_PERSONAL_ACCESS_TOKEN'. Using current baseline."
    return
  end

  puts "Downloading baseline file from gitlab..."
  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"]

  # 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"
  url = "#{host}/api/v4/projects/#{project_id}/jobs/artifacts/#{branch_name}/raw/#{baseline_filename}?job=code_quality"
  response = RestClient.get(url, { "PRIVATE-TOKEN": personal_access_token })

  if response.code < 300
    File.write("codeclimate_diff_baseline.json", response.body)
    puts "Successfully updated the baseline."
  else
    puts "Downloading baseline file failed with status code #{response.code}: #{response.body}"
    puts "Using current baseline."
  end

rescue StandardError => e
  puts e
  puts "Using current baseline."
end