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
40
41
42
|
# File 'lib/terraspace_ci_gitlab/pr.rb', line 7
def (url)
return unless ENV['CI_PIPELINE_SOURCE'] == 'merge_request_event'
return unless gitlab_token?
repo = ENV['CI_PROJECT_PATH'] number = ENV['CI_MERGE_REQUEST_IID']
marker = "<!-- terraspace marker -->"
body = marker + "\n"
body << "Terraspace Cloud Url #{url}"
puts "Adding comment to repo #{repo} number #{number}"
project = client.project(ENV['CI_PROJECT_PATH'])
merge_request = ENV['CI_MERGE_REQUEST_IID']
notes = client.merge_request_notes(project.id, number)
found_note = notes.find do |note|
note.body.starts_with?(marker)
end
if found_note
client.edit_merge_request_note(project.id, merge_request, found_note.id, body) unless found_note.body == body
else
client.create_merge_request_note(project.id, merge_request, body)
end
rescue Gitlab::Error::Unauthorized => e
puts "WARN: #{e.message}. Unable to create merge request comment. Please double check your gitlab token"
rescue Gitlab::Error::Forbidden => e
puts "WARN: #{e.message}. Unable to create merge request comment. The token does not have the permission. Please double check your gitlab token"
end
|