danger 5.11.1 → 5.12.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f393145687ffadd684dd2202482cc478346a2a8e2d82df81e11b30a373292dc6
4
- data.tar.gz: c91d3ec700cb5402ad87a4e49b6e898811610df81d6274e42dcb39e1c74cbcc1
3
+ metadata.gz: 073d0e00dcf8fab23ef166afb9084ec50c86e7f739616650f716e2d3f8074a6f
4
+ data.tar.gz: 6122124ff2f3cbad49516ff8a883917f91132f6e97926413508f856c322d766b
5
5
  SHA512:
6
- metadata.gz: 184da256cc00896440990935eaecb0ad1b9b917cba1816cc0e95b2d48bdbd68f6157aa89d3297f4e538904038d3672124a05dc29ce89360bda6d1caf606bb9e2
7
- data.tar.gz: c2b41799bec19df9439289d0430365af21ab1aaef8dc76114b87d7f92848fd654a7bc133c7ea3dc53f91b1f5254259fde32e3a8b38ffc916609420f077f2e31b
6
+ metadata.gz: 46c3511779f86f30194e3246f1e8ca3fb6c7b879c3728b511b3d5df9935dfd83f5c7b86bd31502550d6820f5a8ca7533966f9d849332b9078e639cd5abd32b48
7
+ data.tar.gz: 29d0f8f813403dc039f6e2eef1b7170de5f3161b4d61702f7716387b9d58360b173fef1fbf636014e69bfa04cfbb26a80623d43bb50172b928c68cafe9d72189
data/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2018 Orta, Felix Krause
3
+ Copyright (c) 2019 Orta, Felix Krause
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -3,11 +3,11 @@ require "danger/request_sources/github/github"
3
3
  module Danger
4
4
  # ### CI Setup
5
5
  #
6
- # You can use `duck8823/actions/danger` Action in your .github/main.workflow.
6
+ # You can use `danger/danger` Action in your .github/main.workflow.
7
7
  #
8
8
  # ```
9
9
  # action "Danger" {
10
- # uses = "duck8823/actions/danger"
10
+ # uses = "danger/danger"
11
11
  # }
12
12
  # ```
13
13
  #
@@ -17,7 +17,7 @@ module Danger
17
17
  #
18
18
  # ```
19
19
  # action "Danger" {
20
- # uses = "duck8823/actions/danger"
20
+ # uses = "danger/danger"
21
21
  # secrets = ["GITHUB_TOKEN"]
22
22
  # }
23
23
  # ```
@@ -32,10 +32,10 @@ module Danger
32
32
  end
33
33
 
34
34
  def self.determine_merge_request_id(env)
35
- return env["CI_MERGE_REQUEST_ID"] if env["CI_MERGE_REQUEST_ID"]
35
+ return env["CI_MERGE_REQUEST_IID"] if env["CI_MERGE_REQUEST_IID"]
36
36
  return 0 unless env["CI_COMMIT_SHA"]
37
37
 
38
- project_path = env["CI_PROJECT_PATH"]
38
+ project_path = env["CI_MERGE_REQUEST_PROJECT_PATH"] || env["CI_PROJECT_PATH"]
39
39
  base_commit = env["CI_COMMIT_SHA"]
40
40
  client = RequestSources::GitLab.new(nil, env).client
41
41
 
@@ -49,8 +49,8 @@ module Danger
49
49
 
50
50
  def initialize(env)
51
51
  @env = env
52
- @repo_slug = env["CI_PROJECT_PATH"]
53
- @project_url = env["CI_PROJECT_URL"]
52
+ @repo_slug = env["CI_MERGE_REQUEST_PROJECT_PATH"] || env["CI_PROJECT_PATH"]
53
+ @project_url = env["CI_MERGE_REQUEST_PROJECT_URL"] || env["CI_PROJECT_URL"]
54
54
  end
55
55
 
56
56
  def supported_request_sources
@@ -33,7 +33,7 @@ module Danger
33
33
  end
34
34
 
35
35
  def supported_request_sources
36
- @supported_request_sources ||= [Danger::RequestSources::GitHub, Danger::RequestSources::BitbucketServer]
36
+ @supported_request_sources ||= [Danger::RequestSources::GitHub, Danger::RequestSources::BitbucketServer, Danger::RequestSources::BitbucketCloud]
37
37
  end
38
38
 
39
39
  def initialize(env = {})
@@ -62,18 +62,29 @@ module Danger
62
62
  end
63
63
 
64
64
  def generate_remote_pull_request
65
- if remote_url =~ %r{/pull-requests/}
65
+ scm_provider = find_scm_provider(remote_url)
66
+
67
+ case scm_provider
68
+ when :bitbucket_cloud
69
+ RemotePullRequest.new(
70
+ remote_pull_request[:id].to_s,
71
+ remote_pull_request[:source][:commit][:hash],
72
+ remote_pull_request[:destination][:commit][:hash]
73
+ )
74
+ when :bitbucket_server
66
75
  RemotePullRequest.new(
67
76
  remote_pull_request[:id].to_s,
68
77
  remote_pull_request[:fromRef][:latestCommit],
69
78
  remote_pull_request[:toRef][:latestCommit]
70
79
  )
71
- else
80
+ when :github
72
81
  RemotePullRequest.new(
73
82
  remote_pull_request.number.to_s,
74
83
  remote_pull_request.head.sha,
75
84
  remote_pull_request.base.sha
76
85
  )
86
+ else
87
+ raise "SCM provider not supported: #{scm_provider}"
77
88
  end
78
89
  end
79
90
 
@@ -125,13 +136,25 @@ module Danger
125
136
  end
126
137
 
127
138
  def client
128
- if remote_url =~ %r{/pull-requests/}
139
+ scm_provider = find_scm_provider(remote_url)
140
+
141
+ case scm_provider
142
+ when :bitbucket_cloud
143
+ require "danger/request_sources/bitbucket_cloud_api"
144
+ branch_name = ENV["DANGER_BITBUCKET_TARGET_BRANCH"] # Optional env variable (specifying the target branch) to help find the PR.
145
+ RequestSources::BitbucketCloudAPI.new(repo_slug, specific_pull_request_id, branch_name, env)
146
+
147
+ when :bitbucket_server
129
148
  require "danger/request_sources/bitbucket_server_api"
130
149
  project, slug = repo_slug.split("/")
131
150
  RequestSources::BitbucketServerAPI.new(project, slug, specific_pull_request_id, env)
132
- else
151
+
152
+ when :github
133
153
  require "octokit"
134
154
  Octokit::Client.new(access_token: ENV["DANGER_GITHUB_API_TOKEN"], api_endpoint: api_url)
155
+
156
+ else
157
+ raise "SCM provider not supported: #{scm_provider}"
135
158
  end
136
159
  end
137
160
 
@@ -142,5 +165,15 @@ module Danger
142
165
  end
143
166
  end
144
167
  end
168
+
169
+ def find_scm_provider(remote_url)
170
+ if remote_url =~ %r{/bitbucket.org/}
171
+ :bitbucket_cloud
172
+ elsif remote_url =~ %r{/pull-requests/}
173
+ :bitbucket_server
174
+ else
175
+ :github
176
+ end
177
+ end
145
178
  end
146
179
  end
@@ -8,7 +8,7 @@ require "no_proxy_fix"
8
8
 
9
9
  module Danger
10
10
  class PR < Runner
11
- self.summary = "Run the Dangerfile locally against Pull Requests (works with forks, too). Does not post to the PR.".freeze
11
+ self.summary = "Run the Dangerfile locally against Pull Requests (works with forks, too). Does not post to the PR. Usage: danger pr <URL>".freeze
12
12
  self.command = "pr".freeze
13
13
 
14
14
  def self.options
@@ -46,6 +46,9 @@ module Danger
46
46
  unless @dangerfile_path
47
47
  help! "Could not find a Dangerfile."
48
48
  end
49
+ unless @pr_url
50
+ help! "Could not find a pull-request. Usage: danger pr <URL>"
51
+ end
49
52
  end
50
53
 
51
54
  def run
@@ -97,7 +97,7 @@ module Danger
97
97
  # @return [String]
98
98
  #
99
99
  def pr_author
100
- @bs.pr_json[:author][:user][:slug].to_s
100
+ @bs.pr_json[:author][:username]
101
101
  end
102
102
 
103
103
  # @!group PR Commit Metadata
@@ -105,7 +105,7 @@ module Danger
105
105
  # @return [String]
106
106
  #
107
107
  def branch_for_base
108
- @bs.pr_json[:toRef][:displayId].to_s
108
+ @bs.pr_json[:destination][:branch][:name]
109
109
  end
110
110
 
111
111
  # @!group PR Commit Metadata
@@ -113,7 +113,7 @@ module Danger
113
113
  # @return [String]
114
114
  #
115
115
  def pr_link
116
- @bs.pr_json[:links][:self].flat_map { |l| l[:href] }.first.to_s
116
+ @bs.pr_json[:links][:self][:href]
117
117
  end
118
118
 
119
119
  # @!group PR Commit Metadata
@@ -121,7 +121,7 @@ module Danger
121
121
  # @return [String]
122
122
  #
123
123
  def branch_for_head
124
- @bs.pr_json[:fromRef][:displayId].to_s
124
+ @bs.pr_json[:destination][:branch][:name]
125
125
  end
126
126
 
127
127
  # @!group PR Commit Metadata
@@ -129,7 +129,7 @@ module Danger
129
129
  # @return [String]
130
130
  #
131
131
  def base_commit
132
- @bs.pr_json[:toRef][:latestCommit].to_s
132
+ @bs.pr_json[:destination][:commit][:hash]
133
133
  end
134
134
 
135
135
  # @!group PR Commit Metadata
@@ -137,63 +137,8 @@ module Danger
137
137
  # @return [String]
138
138
  #
139
139
  def head_commit
140
- @bs.pr_json[:fromRef][:latestCommit].to_s
140
+ @bs.pr_json[:source][:commit][:hash]
141
141
  end
142
142
 
143
- # @!group Bitbucket Cloud Misc
144
- # Returns a list of HTML anchors for a file, or files in the head repository.
145
- # It returns a string of multiple anchors if passed an array.
146
- # @param [String or Array<String>] paths
147
- # A list of strings to convert to github anchors
148
- # @param [Bool] full_path
149
- # Shows the full path as the link's text, defaults to `true`.
150
- #
151
- # @return [String]
152
- #
153
- def html_link(paths, full_path: true)
154
- create_link(paths, full_path) { |href, text| create_html_link(href, text) }
155
- end
156
-
157
- # @!group Bitbucket Cloud Misc
158
- # Returns a list of Markdown links for a file, or files in the head repository.
159
- # It returns a string of multiple links if passed an array.
160
- # @param [String or Array<String>] paths
161
- # A list of strings to convert to Markdown links
162
- # @param [Bool] full_path
163
- # Shows the full path as the link's text, defaults to `true`.
164
- #
165
- # @return [String]
166
- #
167
- def markdown_link(paths, full_path: true)
168
- create_link(paths, full_path) { |href, text| create_markdown_link(href, text) }
169
- end
170
-
171
- private
172
-
173
- def create_link(paths, full_path)
174
- paths = [paths] unless paths.kind_of?(Array)
175
- commit = head_commit
176
- repo = pr_json[:fromRef][:repository][:links][:self].flat_map { |l| l[:href] }.first
177
-
178
- paths = paths.map do |path|
179
- path, line = path.split("#")
180
- url_path = path.start_with?("/") ? path : "/#{path}"
181
- text = full_path ? path : File.basename(path)
182
- url_path.gsub!(" ", "%20")
183
- line_ref = line ? "##{line}" : ""
184
- yield("#{repo}#{url_path}?at=#{commit}#{line_ref}", text)
185
- end
186
-
187
- return paths.first if paths.count < 2
188
- paths.first(paths.count - 1).join(", ") + " & " + paths.last
189
- end
190
-
191
- def create_html_link(href, text)
192
- "<a href='#{href}'>#{text}</a>"
193
- end
194
-
195
- def create_markdown_link(href, text)
196
- "[#{text}](#{href})"
197
- end
198
143
  end
199
144
  end
@@ -5,13 +5,14 @@ require "danger/helpers/comments_helper"
5
5
  module Danger
6
6
  module RequestSources
7
7
  class BitbucketCloudAPI
8
- attr_accessor :project, :slug, :pull_request_id
8
+ attr_accessor :host, :project, :slug, :pull_request_id
9
9
 
10
10
  def initialize(repo_slug, pull_request_id, branch_name, environment)
11
11
  @username = environment["DANGER_BITBUCKETCLOUD_USERNAME"]
12
12
  @password = environment["DANGER_BITBUCKETCLOUD_PASSWORD"]
13
13
  self.project, self.slug = repo_slug.split("/")
14
14
  self.pull_request_id = pull_request_id || fetch_pr_from_branch(branch_name)
15
+ self.host = "https://bitbucket.org/"
15
16
  end
16
17
 
17
18
  def inspect
@@ -28,6 +29,10 @@ module Danger
28
29
  @username && !@username.empty? && @password && !@password.empty?
29
30
  end
30
31
 
32
+ def pull_request(*)
33
+ fetch_pr_json
34
+ end
35
+
31
36
  def fetch_pr_json
32
37
  uri = URI(pr_api_endpoint)
33
38
  fetch_json(uri)
@@ -73,15 +78,21 @@ module Danger
73
78
  end
74
79
 
75
80
  def fetch_json(uri)
81
+ raise credentials_not_available unless credentials_given?
82
+
76
83
  req = Net::HTTP::Get.new(uri.request_uri, { "Content-Type" => "application/json" })
77
84
  req.basic_auth @username, @password
78
85
  res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
79
86
  http.request(req)
80
87
  end
88
+ raise error_fetching_json(uri.to_s, res.code) unless res.code == "200"
89
+
81
90
  JSON.parse(res.body, symbolize_names: true)
82
91
  end
83
92
 
84
93
  def post(uri, body)
94
+ raise credentials_not_available unless credentials_given?
95
+
85
96
  req = Net::HTTP::Post.new(uri.request_uri, { "Content-Type" => "application/json" })
86
97
  req.basic_auth @username, @password
87
98
  req.body = body
@@ -91,12 +102,23 @@ module Danger
91
102
  end
92
103
 
93
104
  def delete(uri)
105
+ raise credentials_not_available unless credentials_given?
106
+
94
107
  req = Net::HTTP::Delete.new(uri.request_uri, { "Content-Type" => "application/json" })
95
108
  req.basic_auth @username, @password
96
109
  Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
97
110
  http.request(req)
98
111
  end
99
112
  end
113
+
114
+ def credentials_not_available
115
+ "Credentials not available. Provide DANGER_BITBUCKETCLOUD_USERNAME and DANGER_BITBUCKETCLOUD_PASSWORD as environment variables."
116
+ end
117
+
118
+ def error_fetching_json(url, status_code)
119
+ "Error fetching json for: #{url}, status code: #{status_code}"
120
+ end
121
+
100
122
  end
101
123
  end
102
124
  end
@@ -1,4 +1,4 @@
1
1
  module Danger
2
- VERSION = "5.11.1".freeze
2
+ VERSION = "5.12.0".freeze
3
3
  DESCRIPTION = "Like Unit Tests, but for your Team Culture.".freeze
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: danger
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.11.1
4
+ version: 5.12.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Orta Therox
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2019-01-07 00:00:00.000000000 Z
12
+ date: 2019-01-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: claide