Class: Danger::RequestSources::VSTSAPI
- Inherits:
-
Object
- Object
- Danger::RequestSources::VSTSAPI
- Defined in:
- lib/danger/request_sources/vsts_api.rb
Instance Attribute Summary collapse
-
#host ⇒ Object
Returns the value of attribute host.
-
#min_api_version_for_comments ⇒ Object
Returns the value of attribute min_api_version_for_comments.
-
#pr_api_endpoint ⇒ Object
Returns the value of attribute pr_api_endpoint.
Instance Method Summary collapse
- #credentials_given? ⇒ Boolean
- #fetch_last_comments ⇒ Object
- #fetch_pr_json ⇒ Object
-
#initialize(slug, pull_request_id, environment) ⇒ VSTSAPI
constructor
A new instance of VSTSAPI.
- #inspect ⇒ Object
- #post_comment(text) ⇒ Object
- #post_inline_comment(text, file, line) ⇒ Object
- #pull_request ⇒ Object
- #supports_comments? ⇒ Boolean
- #update_comment(thread, id, new_comment) ⇒ Object
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
#host ⇒ Object
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_comments ⇒ Object
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_endpoint ⇒ Object
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
43 44 45 |
# File 'lib/danger/request_sources/vsts_api.rb', line 43 def credentials_given? @token && !@token.empty? end |
#fetch_last_comments ⇒ Object
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_json ⇒ Object
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 |
#inspect ⇒ Object
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_request ⇒ Object
47 48 49 |
# File 'lib/danger/request_sources/vsts_api.rb', line 47 def pull_request(*) fetch_pr_json end |
#supports_comments? ⇒ 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 |