@@ -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
2641end
0 commit comments