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.



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

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.



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

def host
  @host
end

#min_api_version_for_commentsObject

Returns the value of attribute min_api_version_for_comments.



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

def min_api_version_for_comments
  @min_api_version_for_comments
end

#pr_api_endpointObject

Returns the value of attribute pr_api_endpoint.



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

def pr_api_endpoint
  @pr_api_endpoint
end

Instance Method Details

#credentials_given?Boolean

Returns:

  • (Boolean)


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

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

#fetch_last_commentsObject



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

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

#fetch_pr_jsonObject



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

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

#inspectObject



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

def inspect
  inspected = super

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

  inspected
end

#post_comment(text) ⇒ Object



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

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



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
# File 'lib/danger/request_sources/vsts_api.rb', line 79

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

#supports_comments?Boolean

Returns:

  • (Boolean)


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

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



111
112
113
114
115
116
117
# File 'lib/danger/request_sources/vsts_api.rb', line 111

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