class Danger::RequestSources::VSTSAPI

Attributes

host[RW]
min_api_version_for_comments[RW]
pr_api_endpoint[RW]

Public Class Methods

new(slug, pull_request_id, environment) click to toggle source
# File lib/danger/request_sources/vsts_api.rb, line 11
def initialize(slug, pull_request_id, environment)
  self.min_api_version_for_comments = "3.0"

  user_name = ""
  personal_access_token = environment["DANGER_VSTS_API_TOKEN"]

  @token = Base64.strict_encode64("#{user_name}:#{personal_access_token}")
  @api_version = environment["DANGER_VSTS_API_VERSION"] ||= self.min_api_version_for_comments

  self.host = environment["DANGER_VSTS_HOST"]
  if self.host && !(self.host.include? "http://") && !(self.host.include? "https://")
    self.host = "https://" + self.host
  end

  self.pr_api_endpoint = "#{host}/_apis/git/repositories/#{slug}/pullRequests/#{pull_request_id}"
end

Public Instance Methods

credentials_given?() click to toggle source
# File lib/danger/request_sources/vsts_api.rb, line 43
def credentials_given?
  @token && !@token.empty?
end
fetch_last_comments() click to toggle source
# File lib/danger/request_sources/vsts_api.rb, line 56
def fetch_last_comments
  uri = URI("#{pr_api_endpoint}/threads?api-version=#{@api_version}")
  fetch_json(uri)[:value]
end
fetch_pr_json() click to toggle source
# File lib/danger/request_sources/vsts_api.rb, line 51
def fetch_pr_json
  uri = URI("#{pr_api_endpoint}?api-version=#{@api_version}")
  fetch_json(uri)
end
inspect() click to toggle source
Calls superclass method
# File lib/danger/request_sources/vsts_api.rb, line 35
def inspect
  inspected = super

  inspected.gsub!(@token, "********") if @token

  inspected
end
post_comment(text) click to toggle source
# File lib/danger/request_sources/vsts_api.rb, line 61
def post_comment(text)
  uri = URI("#{pr_api_endpoint}/threads?api-version=#{@api_version}")
  body = {
    "comments" => [
      {
        "parentCommentId" => 0,
        "content" => text,
        "commentType" => 1
      }
    ],
    "properties" => {
      "Microsoft.TeamFoundation.Discussion.SupportsMarkdown" => {
        "type" => "System.Int32",
        "value" => 1
      }
    },
    "status" => 1
  }.to_json
  post(uri, body)
end
post_inline_comment(text, file, line) click to toggle source
# File lib/danger/request_sources/vsts_api.rb, line 82
def post_inline_comment(text, file, line)
  uri = URI("#{pr_api_endpoint}/threads?api-version=#{@api_version}")
  body = {
    "comments" => [
      {
        "parentCommentId" => 0,
        "content" => text,
        "commentType" => 1
      }
    ],
    "properties" => {
      "Microsoft.TeamFoundation.Discussion.SupportsMarkdown" => {
        "type" => "System.Int32",
        "value" => 1
      }
    },
    "status" => 1,
    "threadContext" => {
      "filePath" => file,
      "rightFileEnd" => {
        "line" => line + 1,
        "offset" => 1
      },
      "rightFileStart" => {
        "line" => line,
        "offset" => 1
      }
    }
  }.to_json
  post(uri, body)
end
pull_request(*) click to toggle source
# File lib/danger/request_sources/vsts_api.rb, line 47
def pull_request(*)
  fetch_pr_json
end
supports_comments?() click to toggle source
# File lib/danger/request_sources/vsts_api.rb, line 28
def supports_comments?
  major_version = @api_version.split(".").first.to_i
  minimum_version_for_comments = self.min_api_version_for_comments.split(".").first.to_i

  major_version >= minimum_version_for_comments
end
update_comment(thread, id, new_comment) click to toggle source
# File lib/danger/request_sources/vsts_api.rb, line 114
def update_comment(thread, id, new_comment)
  uri = URI("#{pr_api_endpoint}/threads/#{thread}/comments/#{id}?api-version=#{@api_version}")
  body = {
    "content" => new_comment
  }.to_json
  patch(uri, body)
end

Private Instance Methods

fetch_json(uri) click to toggle source
# File lib/danger/request_sources/vsts_api.rb, line 128
def fetch_json(uri)
  req = Net::HTTP::Get.new(uri.request_uri, { "Content-Type" => "application/json", "Authorization" => "Basic #{@token}" })
  res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: use_ssl) do |http|
    http.request(req)
  end
  JSON.parse(res.body, symbolize_names: true)
end
patch(uri, body) click to toggle source
# File lib/danger/request_sources/vsts_api.rb, line 152
def patch(uri, body)
  puts uri
  puts body

  req = Net::HTTP::Patch.new(uri.request_uri, { "Content-Type" => "application/json", "Authorization" => "Basic #{@token}" })
  req.body = body

  res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: use_ssl) do |http|
    http.request(req)
  end

  # show error to the user when VSTS returned an error
  case res
  when Net::HTTPClientError, Net::HTTPServerError
    # HTTP 4xx - 5xx
    abort "\nError updating comment on VSTS: #{res.code} (#{res.message})\n\n"
  end
end
post(uri, body) click to toggle source
# File lib/danger/request_sources/vsts_api.rb, line 136
def post(uri, body)
  req = Net::HTTP::Post.new(uri.request_uri, { "Content-Type" => "application/json", "Authorization" => "Basic #{@token}" })
  req.body = body

  res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: use_ssl) do |http|
    http.request(req)
  end

  # show error to the user when VSTS returned an error
  case res
  when Net::HTTPClientError, Net::HTTPServerError
    # HTTP 4xx - 5xx
    abort "\nError posting comment to VSTS: #{res.code} (#{res.message})\n\n"
  end
end
use_ssl() click to toggle source
# File lib/danger/request_sources/vsts_api.rb, line 124
def use_ssl
  return self.pr_api_endpoint.include? "https://"
end