danger 6.0.2 → 6.0.3

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: 6d812582b1d50574cecbc874f8e746a7e070f97decce7470b1616abd8871dd3d
4
- data.tar.gz: 44a6746b63eef45617e9ba0057b1319539bfcdaabd3a359b561f3dd2ff4d3c65
3
+ metadata.gz: 5ed2cdc61eed91524dbc037cc0b4109298a59f79bae857ab9ab4fb91f9f16d17
4
+ data.tar.gz: 59ddea79d36ee0dca6664f1ed3ce654e88bebfb70ed9f43ed5948d7bee11349f
5
5
  SHA512:
6
- metadata.gz: 54479ef3587f4486a282323894e32778e5dcc4c21d41410fddc25f2d02e581c692dfe488755828aff27e04115076031d032261f1e45ab60ff41df6ba429dd12a
7
- data.tar.gz: 28b3b370371b9426168e57ebbce738bc5e7771b8dc7f223f49aa19d5de4bc62047c845720316074b677ca1e1ee387d65b24406077d0d532b764edc0c84f07ad8
6
+ metadata.gz: 1ed19550308a3957a370beab7524037eec4760f4c08664c7f4283ca647b2c10a909a9cf7aab934dd65b9ff518cb7f72d2ce51b2c1f4703c2c6b7fd1f061dd1b6
7
+ data.tar.gz: 99784fe747487d86ccbe6e65ac320ac5fbdcfab8818128f4c453c1161a7517b738f7611da988e1825e61d7613b96f946d412e8ea0ba71cf80c38cd784adfdac5
@@ -8,9 +8,11 @@ module Danger
8
8
  # ```
9
9
  #
10
10
  # #### Token Setup
11
- #
12
- # Add `DANGER_BITBUCKETCLOUD_USERNAME` and `DANGER_BITBUCKETCLOUD_USERNAME` to your pipeline repository variable.
13
- # Settings > Pipelines > Repository Variables
11
+ #
12
+ # Add `DANGER_BITBUCKETCLOUD_USERNAME` and `DANGER_BITBUCKETCLOUD_USERNAME` to your pipeline repository variable
13
+ # or instead using `DANGER_BITBUCKETCLOUD_OAUTH_KEY` and `DANGER_BITBUCKETCLOUD_OAUTH_SECRET`.
14
+ #
15
+ # You can find them in Settings > Pipelines > Repository Variables
14
16
 
15
17
  class BitbucketPipelines < CI
16
18
 
@@ -42,11 +42,19 @@ module Danger
42
42
  #
43
43
  # You will need to add the following environment variables as build parameters or by exporting them inside your
44
44
  # Simple Command Runner.
45
- # - `DANGER_BITBUCKETCLOUD_USERNAME`
46
- # - `DANGER_BITBUCKETCLOUD_PASSWORD`
45
+ #
46
+ #
47
47
  # - `BITBUCKET_REPO_SLUG`
48
48
  # - `BITBUCKET_REPO_URL`
49
49
  #
50
+ # - `DANGER_BITBUCKETCLOUD_USERNAME`
51
+ # - `DANGER_BITBUCKETCLOUD_PASSWORD`
52
+ #
53
+ # or
54
+ #
55
+ # - `DANGER_BITBUCKETCLOUD_OAUTH_KEY`
56
+ # - `DANGER_BITBUCKETCLOUD_OAUTH_SECRET`
57
+ #
50
58
  # You will also need to set the `BITBUCKET_BRANCH_NAME` environment variable.
51
59
  # TeamCity provides `%teamcity.build.branch%`, which you can use at the top of your Simple Command Runner:
52
60
  # ```sh
@@ -57,6 +65,7 @@ module Danger
57
65
  #
58
66
  # You will need to add the following environment variables as build parameters or by exporting them inside your
59
67
  # Simple Command Runner.
68
+ #
60
69
  # - `DANGER_BITBUCKETSERVER_USERNAME`
61
70
  # - `DANGER_BITBUCKETSERVER_PASSWORD`
62
71
  # - `DANGER_BITBUCKETSERVER_HOST`
@@ -16,6 +16,10 @@ module Danger
16
16
  ]
17
17
  end
18
18
 
19
+ def self.optional_env_vars
20
+ ["DANGER_BITBUCKETCLOUD_OAUTH_KEY", "DANGER_BITBUCKETCLOUD_OAUTH_SECRET"]
21
+ end
22
+
19
23
  def initialize(ci_source, environment)
20
24
  self.ci_source = ci_source
21
25
  self.environment = environment
@@ -5,12 +5,13 @@ require "danger/helpers/comments_helper"
5
5
  module Danger
6
6
  module RequestSources
7
7
  class BitbucketCloudAPI
8
- attr_accessor :host, :project, :slug, :pull_request_id
8
+ attr_accessor :host, :project, :slug, :access_token, :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
+ self.access_token = fetch_access_token(environment)
14
15
  self.pull_request_id = pull_request_id || fetch_pr_from_branch(branch_name)
15
16
  self.host = "https://bitbucket.org/"
16
17
  end
@@ -77,11 +78,32 @@ module Danger
77
78
  fetch_json(uri)[:values][0][:id]
78
79
  end
79
80
 
81
+ def fetch_access_token(environment)
82
+ oauth_key = environment["DANGER_BITBUCKETCLOUD_OAUTH_KEY"]
83
+ oauth_secret = environment["DANGER_BITBUCKETCLOUD_OAUTH_SECRET"]
84
+ return nil if oauth_key.nil?
85
+ return nil if oauth_secret.nil?
86
+
87
+ uri = URI.parse("https://bitbucket.org/site/oauth2/access_token")
88
+ req = Net::HTTP::Post.new(uri.request_uri, { "Content-Type" => "application/json" })
89
+ req.basic_auth oauth_key, oauth_secret
90
+ req.set_form_data({'grant_type' => 'client_credentials'})
91
+ res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
92
+ http.request(req)
93
+ end
94
+
95
+ JSON.parse(res.body, symbolize_names: true)[:access_token]
96
+ end
97
+
80
98
  def fetch_json(uri)
81
99
  raise credentials_not_available unless credentials_given?
82
100
 
83
101
  req = Net::HTTP::Get.new(uri.request_uri, { "Content-Type" => "application/json" })
84
- req.basic_auth @username, @password
102
+ if access_token.nil?
103
+ req.basic_auth @username, @password
104
+ else
105
+ req["Authorization"] = "Bearer #{access_token}"
106
+ end
85
107
  res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
86
108
  http.request(req)
87
109
  end
@@ -94,7 +116,11 @@ module Danger
94
116
  raise credentials_not_available unless credentials_given?
95
117
 
96
118
  req = Net::HTTP::Post.new(uri.request_uri, { "Content-Type" => "application/json" })
97
- req.basic_auth @username, @password
119
+ if access_token.nil?
120
+ req.basic_auth @username, @password
121
+ else
122
+ req["Authorization"] = "Bearer #{access_token}"
123
+ end
98
124
  req.body = body
99
125
  Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
100
126
  http.request(req)
@@ -105,7 +131,11 @@ module Danger
105
131
  raise credentials_not_available unless credentials_given?
106
132
 
107
133
  req = Net::HTTP::Delete.new(uri.request_uri, { "Content-Type" => "application/json" })
108
- req.basic_auth @username, @password
134
+ if access_token.nil?
135
+ req.basic_auth @username, @password
136
+ else
137
+ req["Authorization"] = "Bearer #{access_token}"
138
+ end
109
139
  Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
110
140
  http.request(req)
111
141
  end
@@ -1,4 +1,4 @@
1
1
  module Danger
2
- VERSION = "6.0.2".freeze
2
+ VERSION = "6.0.3".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: 6.0.2
4
+ version: 6.0.3
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-03-30 00:00:00.000000000 Z
12
+ date: 2019-04-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: claide