@@ -8,19 +8,34 @@ def self.refresh_baseline_if_configured
8
8
return unless CodeclimateDiff . configuration [ "gitlab" ]
9
9
return unless CodeclimateDiff . configuration [ "gitlab" ] [ "download_baseline_from_pipeline" ]
10
10
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..."
12
19
branch_name = CodeclimateDiff . configuration [ "main_branch_name" ] || "main"
13
20
project_id = CodeclimateDiff . configuration [ "gitlab" ] [ "project_id" ]
14
21
host = CodeclimateDiff . configuration [ "gitlab" ] [ "host" ]
15
22
baseline_filename = CodeclimateDiff . configuration [ "gitlab" ] [ "baseline_filename" ]
16
- personal_access_token = ENV . fetch ( "CODECLIMATE_DIFF_GITLAB_PERSONAL_ACCESS_TOKEN" )
17
23
18
24
# 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"
19
25
url = "#{ host } /api/v4/projects/#{ project_id } /jobs/artifacts/#{ branch_name } /raw/#{ baseline_filename } ?job=code_quality"
20
26
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
+
22
36
rescue StandardError => e
23
37
puts e
38
+ puts "Using current baseline."
24
39
end
25
40
end
26
41
end
0 commit comments