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(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(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)


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

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

#fetch_last_commentsObject



56
57
58
59
# 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_jsonObject



51
52
53
54
# 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

#inspectObject



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

def inspect
  inspected = super

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

  inspected
end

#post_comment(text) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# 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) ⇒ Object



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
# 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_requestObject



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

def pull_request(*)
  fetch_pr_json
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
  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) ⇒ Object



114
115
116
117
118
119
120
# 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