danger 2.1.4 → 2.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 36950c7276cc765b1f5994828da871b8752468d0
4
- data.tar.gz: ddf1f42d8808342587f4aad220b1dede2166deec
3
+ metadata.gz: f2e2226e12824fc11ac62cf2a5db48a4add6ec31
4
+ data.tar.gz: 2a763da22a526ab15228e3bdb7b57702c3ac94e2
5
5
  SHA512:
6
- metadata.gz: cd56f33b2e8477a1e3fb7eba37b60397b6863fa53b2d8cecd9dd700688ead5a409095b57a97266dc20682b575806b700fb835da48e266bceeea2bc2ffc00a040
7
- data.tar.gz: 70dc44e76ee01815baaaa241417e65fecbf33863d97240c6cafe1eb299af66925db266a96aacbebbc205ce3b7f693b6a7f3e50e6a7fa09a43e0869f2a311c79a
6
+ metadata.gz: 21209a7ef5de1d76336cd132793d334b25ab2cd16ee9946241cc56ff61f954c77a40b75f3af6d46ae40de0c4550e4dc1710bb5341e4c658c9ca955c4fc59f994
7
+ data.tar.gz: 99b9dbe5cc93c644168525024913453ce958af27c5a0735ba4b41fc94bead9fa689fb46571236cd31737c81281c464b06f936577aeffcb22318a1c62776392ea
@@ -22,7 +22,18 @@ module Danger
22
22
  #
23
23
  # There is no difference here for OSS vs Closed, add your `DANGER_GITHUB_API_TOKEN` to the Environment variable settings page.
24
24
  #
25
+ # ### I still want to run commit builds
26
+ #
27
+ # OK, alright. So, if you add a `DANGER_CIRCLE_CI_API_TOKEN` then Danger will use the Circle API to look up
28
+ # the status of whether a commit is inside a PR or not. You can generate a token from inside the project set_trace_func
29
+ # then go to Permissions > "API Permissions" and generate a token with access to Status. Take that token and add
30
+ # it to Build Settings > "Environment Variables".
31
+ #
25
32
  class CircleCI < CI
33
+ # Side note: CircleCI is complicated. The env vars for PRs are not guaranteed to exist
34
+ # if the build was triggered from a commit, to look at examples of the different types
35
+ # of CI states, see this repo: https://github.com/orta/show_circle_env
36
+
26
37
  def self.validates_as_ci?(env)
27
38
  env.key? "CIRCLE_BUILD_NUM"
28
39
  end
@@ -32,53 +43,42 @@ module Danger
32
43
  return true unless env["CI_PULL_REQUEST"].empty?
33
44
 
34
45
  # Real-world talk, it should be worrying if none of these are in the environment
35
- return false unless ["CIRCLE_CI_API_TOKEN", "CIRCLE_PROJECT_USERNAME", "CIRCLE_PROJECT_REPONAME", "CIRCLE_BUILD_NUM"].all? { |x| env[x] }
46
+ return false unless ["CIRCLE_CI_API_TOKEN", "CIRCLE_PROJECT_USERNAME", "CIRCLE_PROJECT_REPONAME", "CIRCLE_BUILD_NUM"].all? { |x| env[x] && !env[x].empty? }
36
47
 
37
- # Uses the Circle API to determine if it's a PR otherwose
38
- @circle_token = env["CIRCLE_CI_API_TOKEN"]
39
- !pull_request_url(env).nil?
48
+ # Uses the Circle API to determine if it's a PR otherwise
49
+ api = CircleAPI.new
50
+ api.pull_request?(env)
40
51
  end
41
52
 
42
53
  def supported_request_sources
43
54
  @supported_request_sources ||= [Danger::RequestSources::GitHub]
44
55
  end
45
56
 
46
- def pull_request_url(env)
47
- url = env["CI_PULL_REQUEST"]
48
-
49
- if url.nil? && !env["CIRCLE_PROJECT_USERNAME"].nil? && !env["CIRCLE_PROJECT_REPONAME"].nil?
50
- repo_slug = env["CIRCLE_PROJECT_USERNAME"] + "/" + env["CIRCLE_PROJECT_REPONAME"]
51
- url = fetch_pull_request_url(repo_slug, env["CIRCLE_BUILD_NUM"])
52
- end
53
-
54
- url
55
- end
56
-
57
- def client
58
- @client ||= CircleAPI.new(@circle_token)
59
- end
60
-
61
- def fetch_pull_request_url(repo_slug, build_number)
62
- build_json = client.fetch_build(repo_slug, build_number)
63
- build_json[:pull_request_urls].first
64
- end
65
-
66
57
  def initialize(env)
67
- self.repo_url = GitRepo.new.origins # CircleCI doesn't provide a repo url env variable :/
68
-
58
+ self.repo_url = env["CIRCLE_REPOSITORY_URL"]
69
59
  pr_url = env["CI_PULL_REQUEST"]
70
60
 
71
61
  # If it's not a real URL, use the Circle API
72
62
  unless pr_url && URI.parse(pr_url).kind_of?(URI::HTTP)
73
- @circle_token = env["CIRCLE_CI_API_TOKEN"]
74
- pr_url = pull_request_url(env)
63
+ api = CircleAPI.new
64
+ pr_url = api.pull_request_url(env)
75
65
  end
76
66
 
67
+ # We should either have got it via the API, or
68
+ # an ENV var.
77
69
  pr_path = URI.parse(pr_url).path.split("/")
78
70
  if pr_path.count == 5
79
71
  # The first one is an extra slash, ignore it
80
72
  self.repo_slug = pr_path[1] + "/" + pr_path[2]
81
73
  self.pull_request_id = pr_path[4]
74
+
75
+ else
76
+ message = "Danger::Circle.rb considers this a PR, " \
77
+ "but did not get enough information to get a repo slug" \
78
+ "and PR id.\n\n" \
79
+ "PR path: #{pr_url}\n" \
80
+ "Keys: #{env.keys}"
81
+ raise message.red
82
82
  end
83
83
  end
84
84
  end
@@ -2,19 +2,40 @@ require "faraday"
2
2
 
3
3
  module Danger
4
4
  class CircleAPI
5
- attr_accessor :circle_token
5
+ # Determine if there's a PR attached to this commit,
6
+ # and return a bool
7
+ def pull_request?(env)
8
+ url = pull_request_url(env)
9
+ return !url.nil?
10
+ end
11
+
12
+ # Determine if there's a PR attached to this commit,
13
+ # and return the url if so
14
+ def pull_request_url(env)
15
+ url = env["CI_PULL_REQUEST"]
6
16
 
7
- def initialize(circle_token = nil)
8
- self.circle_token = circle_token
17
+ if url.nil? && !env["CIRCLE_PROJECT_USERNAME"].nil? && !env["CIRCLE_PROJECT_REPONAME"].nil?
18
+ repo_slug = env["CIRCLE_PROJECT_USERNAME"] + "/" + env["CIRCLE_PROJECT_REPONAME"]
19
+ token = env["DANGER_CIRCLE_CI_API_TOKEN"] || env["CIRCLE_CI_API_TOKEN"]
20
+ url = fetch_pull_request_url(repo_slug, env["CIRCLE_BUILD_NUM"], token)
21
+ end
22
+ url
9
23
  end
10
24
 
11
25
  def client
12
26
  @client ||= Faraday.new(url: "https://circleci.com/api/v1")
13
27
  end
14
28
 
15
- def fetch_build(repo_slug, build_number)
29
+ # Ask the API if the commit is inside a PR
30
+ def fetch_pull_request_url(repo_slug, build_number, token)
31
+ build_json = fetch_build(repo_slug, build_number, token)
32
+ build_json[:pull_request_urls].first
33
+ end
34
+
35
+ # Make the API call, and parse the JSON
36
+ def fetch_build(repo_slug, build_number, token)
16
37
  url = "project/#{repo_slug}/#{build_number}"
17
- params = { "circle-token" => circle_token }
38
+ params = { "circle-token" => token }
18
39
  response = client.get url, params, accept: "application/json"
19
40
  json = JSON.parse(response.body, symbolize_names: true)
20
41
  json
@@ -1,4 +1,4 @@
1
1
  module Danger
2
- VERSION = "2.1.4".freeze
2
+ VERSION = "2.1.5".freeze
3
3
  DESCRIPTION = "Automate your PR etiquette.".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: 2.1.4
4
+ version: 2.1.5
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: 2016-08-17 00:00:00.000000000 Z
12
+ date: 2016-08-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: claide
@@ -398,9 +398,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
398
398
  version: '0'
399
399
  requirements: []
400
400
  rubyforge_project:
401
- rubygems_version: 2.4.8
401
+ rubygems_version: 2.2.2
402
402
  signing_key:
403
403
  specification_version: 4
404
404
  summary: Automate your PR etiquette.
405
405
  test_files: []
406
- has_rdoc: