Class: Danger::RequestSources::VSTSAPI

Inherits:
Object
  • Object
show all
Defined in:
lib/danger/request_sources/vsts_api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_project, slug, pull_request_id, environment) ⇒ VSTSAPI

Returns a new instance of VSTSAPI.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/danger/request_sources/vsts_api.rb', line 11

def initialize(_project, 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

Instance Attribute Details

#hostObject

Returns the value of attribute host.



9
10
11
# File 'lib/danger/request_sources/vsts_api.rb', line 9

def host
  @host
end

#min_api_version_for_commentsObject

Returns the value of attribute min_api_version_for_comments.



9
10
11
# File 'lib/danger/request_sources/vsts_api.rb', line 9

def min_api_version_for_comments
  @min_api_version_for_comments
end

#pr_api_endpointObject

Returns the value of attribute pr_api_endpoint.



9
10
11
# File 'lib/danger/request_sources/vsts_api.rb', line 9

def pr_api_endpoint
  @pr_api_endpoint
end

Instance Method Details

#credentials_given?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/danger/request_sources/vsts_api.rb', line 45

def credentials_given?
  @token && !@token.empty?
end

#fetch_last_commentsObject



54
55
56
57
# File 'lib/danger/request_sources/vsts_api.rb', line 54

def fetch_last_comments
  uri = URI("#{pr_api_endpoint}/threads?api-version=#{@api_version}")
  fetch_json(uri)[:value]
end

#fetch_pr_jsonObject



49
50
51
52
# File 'lib/danger/request_sources/vsts_api.rb', line 49

def fetch_pr_json
  uri = URI("#{pr_api_endpoint}?api-version=#{@api_version}")
  fetch_json(uri)
end

#inspectObject



35
36
37
38
39
40
41
42
43
# File 'lib/danger/request_sources/vsts_api.rb', line 35

def inspect
  inspected = super

  if @token
    inspected = inspected.sub! @token, "********".freeze
  end

  inspected
end

#post_comment(text) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/danger/request_sources/vsts_api.rb', line 59

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

#supports_comments?Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
# File 'lib/danger/request_sources/vsts_api.rb', line 28

def supports_comments?
  major_version = @api_version.split(".").first.to_i
  minimun_version_for_comments = self.min_api_version_for_comments.split(".").first.to_i

  major_version >= minimun_version_for_comments
end

#update_comment(thread, id, new_comment) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/danger/request_sources/vsts_api.rb', line 80

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