Class: Danger::RequestSources::VSTS

Inherits:
RequestSource show all
Includes:
Helpers::CommentsHelper
Defined in:
lib/danger/request_sources/vsts.rb

Constant Summary

Constants inherited from RequestSource

RequestSource::DANGER_REPO_NAME

Instance Attribute Summary collapse

Attributes inherited from RequestSource

#ci_source, #ignored_violations

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers::CommentsHelper

#apply_template, #generate_comment, #generate_description, #generate_inline_comment_body, #generate_inline_markdown_body, #generate_message_group_comment, #markdown_link_to_message, #markdown_parser, #process_markdown, #random_compliment, #table

Methods included from Helpers::CommentsParsingHelper

#parse_comment, #parse_message_from_row, #parse_tables_from_comment, #table_kind_from_title, #violations_from_table

Methods inherited from RequestSource

available_request_sources, available_source_names_and_envs, #file_url, inherited, #inspect, source_name, #update_build_status

Constructor Details

#initialize(ci_source, environment) ⇒ VSTS

Returns a new instance of VSTS.



23
24
25
26
27
28
29
# File 'lib/danger/request_sources/vsts.rb', line 23

def initialize(ci_source, environment)
  self.ci_source = ci_source

  @is_vsts_ci = environment.key? "DANGER_VSTS_HOST"

  @api = VSTSAPI.new(ci_source.repo_slug, ci_source.pull_request_id, environment)
end

Instance Attribute Details

#pr_jsonObject

Returns the value of attribute pr_json.



8
9
10
# File 'lib/danger/request_sources/vsts.rb', line 8

def pr_json
  @pr_json
end

Class Method Details

.env_varsObject



10
11
12
13
14
15
# File 'lib/danger/request_sources/vsts.rb', line 10

def self.env_vars
  [
    "DANGER_VSTS_API_TOKEN",
    "DANGER_VSTS_HOST"
  ]
end

.optional_env_varsObject



17
18
19
20
21
# File 'lib/danger/request_sources/vsts.rb', line 17

def self.optional_env_vars
  [
    "DANGER_VSTS_API_VERSION"
  ]
end

Instance Method Details

#clientObject



43
44
45
# File 'lib/danger/request_sources/vsts.rb', line 43

def client
  @api
end

#fetch_detailsObject



51
52
53
# File 'lib/danger/request_sources/vsts.rb', line 51

def fetch_details
  self.pr_json = @api.fetch_pr_json
end

#hostObject



47
48
49
# File 'lib/danger/request_sources/vsts.rb', line 47

def host
  @host ||= @api.host
end

#messages_are_equivalent(m1, m2) ⇒ Object



177
178
179
180
181
# File 'lib/danger/request_sources/vsts.rb', line 177

def messages_are_equivalent(m1, m2)
  blob_regexp = %r{blob/[0-9a-z]+/}
  m1.file == m2.file && m1.line == m2.line &&
    m1.message.sub(blob_regexp, "") == m2.message.sub(blob_regexp, "")
end

#organisationObject



71
72
73
# File 'lib/danger/request_sources/vsts.rb', line 71

def organisation
  nil
end

#post_new_comment(comment) ⇒ Object



117
118
119
# File 'lib/danger/request_sources/vsts.rb', line 117

def post_new_comment(comment)
  @api.post_comment(comment)
end

#scmObject



39
40
41
# File 'lib/danger/request_sources/vsts.rb', line 39

def scm
  @scm ||= GitRepo.new
end

#setup_danger_branchesObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/danger/request_sources/vsts.rb', line 55

def setup_danger_branches
  base_branch = self.pr_json[:targetRefName].sub("refs/heads/", "")
  base_commit = self.pr_json[:lastMergeTargetCommit][:commitId]
  head_branch = self.pr_json[:sourceRefName].sub("refs/heads/", "")
  head_commit = self.pr_json[:lastMergeSourceCommit][:commitId]

  # Next, we want to ensure that we have a version of the current branch at a known location
  scm.ensure_commitish_exists_on_branch! base_branch, base_commit
  self.scm.exec "branch #{EnvironmentManager.danger_base_branch} #{base_commit}"

  # OK, so we want to ensure that we have a known head branch, this will always represent
  # the head of the PR ( e.g. the most recent commit that will be merged. )
  scm.ensure_commitish_exists_on_branch! head_branch, head_commit
  self.scm.exec "branch #{EnvironmentManager.danger_head_branch} #{head_commit}"
end

#submit_inline_comments!(warnings: [], errors: [], messages: [], markdowns: [], previous_violations: [], danger_id: "danger") ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/danger/request_sources/vsts.rb', line 141

def submit_inline_comments!(warnings: [], errors: [], messages: [], markdowns: [], previous_violations: [], danger_id: "danger")
  # Avoid doing any fetchs if there's no inline comments
  return {} if (warnings + errors + messages + markdowns).select(&:inline?).empty?

  pr_threads = @api.fetch_last_comments
  danger_threads = pr_threads.select do |thread|
    comment = thread[:comments].first
    comment_content = comment[:content].nil? ? "" : comment[:content]

    next comment_content.include?("generated_by_#{danger_id}")
  end
  non_danger_threads = pr_threads - danger_threads

  warnings = submit_inline_comments_for_kind!(:warning, warnings, danger_threads, previous_violations["warning"], danger_id: danger_id)
  errors = submit_inline_comments_for_kind!(:error, errors, danger_threads, previous_violations["error"], danger_id: danger_id)
  messages = submit_inline_comments_for_kind!(:message, messages, danger_threads, previous_violations["message"], danger_id: danger_id)
  markdowns = submit_inline_comments_for_kind!(:markdown, markdowns, danger_threads, [], danger_id: danger_id)

  # submit removes from the array all comments that are still in force
  # so we strike out all remaining ones
  danger_threads.each do |thread|
    violation = violations_from_table(thread[:comments].first[:content]).first
    if !violation.nil? && violation.sticky
      body = generate_inline_comment_body("white_check_mark", violation, danger_id: danger_id, resolved: true, template: "github")
      @api.update_comment(thread[:id], thread[:comments].first[:id], body)
    end
  end

  {
    warnings: warnings,
    errors: errors,
    messages: messages,
    markdowns: markdowns
  }
end

#submit_inline_comments_for_kind!(kind, messages, danger_threads, previous_violations, danger_id: "danger") ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/danger/request_sources/vsts.rb', line 183

def submit_inline_comments_for_kind!(kind, messages, danger_threads, previous_violations, danger_id: "danger")
  previous_violations ||= []
  is_markdown_content = kind == :markdown
  emoji = { warning: "warning", error: "no_entry_sign", message: "book" }[kind]

  messages.reject do |m|
    next false unless m.file && m.line

    # Once we know we're gonna submit it, we format it
    if is_markdown_content
      body = generate_inline_markdown_body(m, danger_id: danger_id, template: "vsts")
    else
      # Hide the inline link behind a span
      m.message = m.message.gsub("\n", "<br />")
      m = process_markdown(m, true)
      body = generate_inline_comment_body(emoji, m, danger_id: danger_id, template: "vsts")
      # A comment might be in previous_violations because only now it's part of the unified diff
      # We remove from the array since it won't have a place in the table anymore
      previous_violations.reject! { |v| messages_are_equivalent(v, m) }
    end

    matching_threads = danger_threads.select do |comment_data|
      if comment_data.key?(:threadContext) && !comment_data[:threadContext].nil? &&
         comment_data[:threadContext][:filePath] == m.file &&
         comment_data[:threadContext].key?(:rightFileStart) &&
         comment_data[:threadContext][:rightFileStart][:line] == m.line
        # Parse it to avoid problems with strikethrough
        violation = violations_from_table(comment_data[:comments].first[:content]).first
        if violation
          messages_are_equivalent(violation, m)
        else
          blob_regexp = %r{blob/[0-9a-z]+/}
          comment_data[:comments].first[:content].sub(blob_regexp, "") == body.sub(blob_regexp, "")
        end
      else
        false
      end
    end

    if matching_threads.empty?
      @api.post_inline_comment(body, m.file, m.line)

      # Not reject because this comment has not completed
      next false
    else
      # Remove the surviving comment so we don't strike it out
      danger_threads.reject! { |c| matching_threads.include? c }

      # Update the comment to remove the strikethrough if present
      thread = matching_threads.first
      @api.update_comment(thread[:id], thread[:comments].first[:id], body)
    end

    # Remove this element from the array
    next true
  end
end

#update_old_comment(new_comment, danger_id: "danger") ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/danger/request_sources/vsts.rb', line 121

def update_old_comment(new_comment, danger_id: "danger")
  comment_updated = false
  @api.fetch_last_comments.each do |c|
    thread_id = c[:id]
    comment = c[:comments].first
    comment_id = comment[:id]
    comment_content = comment[:content].nil? ? "" : comment[:content]
    # Skip the comment if it wasn't posted by danger
    next unless comment_content.include?("generated_by_#{danger_id}")
    # Skip the comment if it's an inline comment
    next unless c[:threadContext].nil?

    # Updated the danger posted comment
    @api.update_comment(thread_id, comment_id, new_comment)
    comment_updated = true
  end
  # If no comment was updated, post a new one
  post_new_comment(new_comment) unless comment_updated
end

#update_pull_request!(warnings: [], errors: [], messages: [], markdowns: [], danger_id: "danger", new_comment: false, remove_previous_comments: false) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/danger/request_sources/vsts.rb', line 75

def update_pull_request!(warnings: [], errors: [], messages: [], markdowns: [], danger_id: "danger", new_comment: false, remove_previous_comments: false)
  unless @api.supports_comments?
    return
  end

  regular_violations = regular_violations_group(
    warnings: warnings,
    errors: errors,
    messages: messages,
    markdowns: markdowns
  )

  inline_violations = inline_violations_group(
    warnings: warnings,
    errors: errors,
    messages: messages,
    markdowns: markdowns
  )

  rest_inline_violations = submit_inline_comments!(**{
    danger_id: danger_id,
    previous_violations: {}
  }.merge(inline_violations))

  main_violations = merge_violations(
    regular_violations, rest_inline_violations
  )

  comment = generate_description(warnings: main_violations[:warnings], errors: main_violations[:errors])
  comment += "\n\n"
  comment += generate_comment(**{
    previous_violations: {},
    danger_id: danger_id,
    template: "vsts"
  }.merge(main_violations))
  if new_comment || remove_previous_comments
    post_new_comment(comment)
  else
    update_old_comment(comment, danger_id: danger_id)
  end
end

#validates_as_api_source?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/danger/request_sources/vsts.rb', line 35

def validates_as_api_source?
  @api.credentials_given?
end

#validates_as_ci?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/danger/request_sources/vsts.rb', line 31

def validates_as_ci?
  @is_vsts_ci
end