danger 2.0.1 → 2.1.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
  SHA1:
3
- metadata.gz: 2462871258d80d80ee0a85ef7c159dbf42e2b665
4
- data.tar.gz: 36396f398f136931f0e02b56c035ad01f40ccfa1
3
+ metadata.gz: f22b32cdbaacb907e107d068b7533f4721b0ea9a
4
+ data.tar.gz: 8d7d99a8229047d542486c2e6a5208cee52d027b
5
5
  SHA512:
6
- metadata.gz: 8018e4545d0bb82cb32c81d711f893d5dc8f03107e8f4f8cc82b59f8985791211e088e3fce61528ee4838afc133b0a24e96db2af604fed1640cb8c98683cf6b7
7
- data.tar.gz: e943625c0c8d6215f0cc282f2a5f7b80900e6dd36c416e9c060ec524fa87a5eae71aaebf84fe2f538f3312e43f203e78a1ee50a934de0ff5d53de3f25087cb66
6
+ metadata.gz: 917ba759a6a42184ce38f1dc72bb543981830896fb15757bb8a0619a71a411922583a4f8d2610f04d0a37bacd338192a9b8539bec922d873f5e88764ac3d455c
7
+ data.tar.gz: fd38930629b2e39026f07b9371dc15ba81582223b141a05cc2c985f5085d719265d2e36984f56dad351a54999a98f2783ab5f5426f6827ed854a94d151b09df4
@@ -9,5 +9,5 @@ warn("PR is classed as Work in Progress") if github.pr_title.include? "[WIP]"
9
9
  warn("Big PR") if git.lines_of_code > 500
10
10
 
11
11
  # Don't let testing shortcuts get into master by accident
12
- fail("fdescribe left in tests") if `grep -r fdescribe specs/`.length > 1
13
- fail("fit left in tests") if `grep -r "fit specs/ `.length > 1
12
+ fail("fdescribe left in tests") if `grep -r fdescribe specs/ `.length > 1
13
+ fail("fit left in tests") if `grep -r fit specs/ `.length > 1
@@ -2,28 +2,40 @@
2
2
  # https://buildkite.com/docs/guides/environment-variables
3
3
 
4
4
  module Danger
5
- module CISource
6
- # https://buildkite.com
7
- class Buildkite < CI
8
- def self.validates?(env)
9
- return false unless env["BUILDKITE"]
10
- return false unless env["BUILDKITE_PULL_REQUEST_REPO"] && !env["BUILDKITE_PULL_REQUEST_REPO"].empty?
11
- return false unless env["BUILDKITE_PULL_REQUEST"]
5
+ # ### CI Setup
6
+ #
7
+ # With BuildKite you run the server yourself, so you will want to run it as a part of your build process.
8
+ # It is common to have build steps, so we would recommend adding this to your scrip:
9
+ #
10
+ # ``` shell
11
+ # echo "--- Running Danger"
12
+ # bundle exec danger
13
+ # ```
14
+ #
15
+ # ### Token Setup
16
+ #
17
+ # As this is self-hosted, you will need to add the `DANGER_GITHUB_API_TOKEN` to your build user's ENV. The alternative
18
+ # is to pass in the token as a prefix to the command `DANGER_GITHUB_API_TOKEN="123" bundle exec danger`.
19
+ #
20
+ class Buildkite < CI
21
+ def self.validates_as_ci?(env)
22
+ env.key? "BUILDKITE"
23
+ end
12
24
 
13
- return true
14
- end
25
+ def self.validates_as_pr?(env)
26
+ ["BUILDKITE_PULL_REQUEST_REPO", "BUILDKITE_PULL_REQUEST"].all? { |x| env[x] }
27
+ end
15
28
 
16
- def initialize(env)
17
- self.repo_url = env["BUILDKITE_PULL_REQUEST_REPO"]
18
- self.pull_request_id = env["BUILDKITE_PULL_REQUEST"]
29
+ def initialize(env)
30
+ self.repo_url = env["BUILDKITE_PULL_REQUEST_REPO"]
31
+ self.pull_request_id = env["BUILDKITE_PULL_REQUEST"]
19
32
 
20
- repo_matches = self.repo_url.match(%r{([\/:])([^\/]+\/[^\/.]+)(?:.git)?$})
21
- self.repo_slug = repo_matches[2] unless repo_matches.nil?
22
- end
33
+ repo_matches = self.repo_url.match(%r{([\/:])([^\/]+\/[^\/.]+)(?:.git)?$})
34
+ self.repo_slug = repo_matches[2] unless repo_matches.nil?
35
+ end
23
36
 
24
- def supported_request_sources
25
- @supported_request_sources ||= [Danger::RequestSources::GitHub]
26
- end
37
+ def supported_request_sources
38
+ @supported_request_sources ||= [Danger::RequestSources::GitHub]
27
39
  end
28
40
  end
29
41
  end
@@ -1,35 +1,37 @@
1
1
  require "set"
2
2
 
3
3
  module Danger
4
- module CISource
5
- # "abstract" CI class
6
- class CI
7
- attr_accessor :repo_slug, :pull_request_id, :repo_url, :supported_request_sources
8
-
9
- def self.inherited(child_class)
10
- available_ci_sources.add child_class
11
- super
12
- end
13
-
14
- def self.available_ci_sources
15
- @available_ci_sources ||= Set.new
16
- end
17
-
18
- def supported_request_sources
19
- raise "CISource subclass must specify the supported request sources"
20
- end
21
-
22
- def supports?(request_source)
23
- supported_request_sources.include? request_source
24
- end
25
-
26
- def self.validates?(_env)
27
- false
28
- end
29
-
30
- def initialize(_env)
31
- raise "Subclass and overwrite initialize"
32
- end
4
+ # "abstract" CI class
5
+ class CI
6
+ attr_accessor :repo_slug, :pull_request_id, :repo_url, :supported_request_sources
7
+
8
+ def self.inherited(child_class)
9
+ available_ci_sources.add child_class
10
+ super
11
+ end
12
+
13
+ def self.available_ci_sources
14
+ @available_ci_sources ||= Set.new
15
+ end
16
+
17
+ def supported_request_sources
18
+ raise "CISource subclass must specify the supported request sources"
19
+ end
20
+
21
+ def supports?(request_source)
22
+ supported_request_sources.include? request_source
23
+ end
24
+
25
+ def self.validates_as_ci?(_env)
26
+ abort "You need to include a function for #{self} for validates_as_ci?"
27
+ end
28
+
29
+ def self.validates_as_pr?(_env)
30
+ abort "You need to include a function for #{self} for validates_as_pr?"
31
+ end
32
+
33
+ def initialize(_env)
34
+ raise "Subclass and overwrite initialize"
33
35
  end
34
36
  end
35
37
  end
@@ -3,53 +3,73 @@ require "uri"
3
3
  require "danger/ci_source/circle_api"
4
4
 
5
5
  module Danger
6
- module CISource
7
- # https://circleci.com
8
- class CircleCI < CI
9
- def self.validates?(env)
10
- return false unless env["CIRCLE_BUILD_NUM"]
11
- return false unless env["CIRCLE_PROJECT_USERNAME"]
12
- return false unless env["CIRCLE_PROJECT_REPONAME"]
13
-
14
- return true
15
- end
6
+ # ### CI Setup
7
+ #
8
+ # For setting up Circle CI, we recommend turning on "Only Build pull requests." in "Advanced Setting." Without this enabled,
9
+ # it is _really_ tricky for Danger to know whether you are in a pull request or not, as the environment metadata
10
+ # isn't reliable.
11
+ #
12
+ # With that set up, you can you add `bundle exec danger` to your `circle.yml`. If you override the default
13
+ # `test:` section, then add it as an extra step. Otherwise add a new `pre` section to the test:
14
+ #
15
+ # ``` ruby
16
+ # test:
17
+ # override:
18
+ # - bundle exec danger
19
+ # ```
20
+ #
21
+ # ### Token Setup
22
+ #
23
+ # There is no difference here for OSS vs Closed, add your `DANGER_GITHUB_API_TOKEN` to the Environment variable settings page.
24
+ #
25
+ class CircleCI < CI
26
+ def self.validates_as_ci?(env)
27
+ env.key? "CIRCLE_BUILD_NUM"
28
+ end
16
29
 
17
- def supported_request_sources
18
- @supported_request_sources ||= [Danger::RequestSources::GitHub]
19
- end
30
+ def self.validates_as_pr?(env)
31
+ return true if env["CI_PULL_REQUEST"]
20
32
 
21
- def client
22
- @client ||= CircleAPI.new(@circle_token)
23
- end
33
+ # Uses the Circle API to determine if it's a PR otherwose
34
+ @circle_token = env["CIRCLE_CI_API_TOKEN"]
35
+ !pull_request_url(env).nil?
36
+ end
24
37
 
25
- def fetch_pull_request_url(repo_slug, build_number)
26
- build_json = client.fetch_build(repo_slug, build_number)
27
- build_json[:pull_request_urls].first
28
- end
38
+ def supported_request_sources
39
+ @supported_request_sources ||= [Danger::RequestSources::GitHub]
40
+ end
41
+
42
+ def client
43
+ @client ||= CircleAPI.new(@circle_token)
44
+ end
29
45
 
30
- def pull_request_url(env)
31
- url = env["CI_PULL_REQUEST"]
46
+ def fetch_pull_request_url(repo_slug, build_number)
47
+ build_json = client.fetch_build(repo_slug, build_number)
48
+ build_json[:pull_request_urls].first
49
+ end
32
50
 
33
- if url.nil? && !env["CIRCLE_PROJECT_USERNAME"].nil? && !env["CIRCLE_PROJECT_REPONAME"].nil?
34
- repo_slug = env["CIRCLE_PROJECT_USERNAME"] + "/" + env["CIRCLE_PROJECT_REPONAME"]
35
- url = fetch_pull_request_url(repo_slug, env["CIRCLE_BUILD_NUM"])
36
- end
51
+ def pull_request_url(env)
52
+ url = env["CI_PULL_REQUEST"]
37
53
 
38
- url
54
+ if url.nil? && !env["CIRCLE_PROJECT_USERNAME"].nil? && !env["CIRCLE_PROJECT_REPONAME"].nil?
55
+ repo_slug = env["CIRCLE_PROJECT_USERNAME"] + "/" + env["CIRCLE_PROJECT_REPONAME"]
56
+ url = fetch_pull_request_url(repo_slug, env["CIRCLE_BUILD_NUM"])
39
57
  end
40
58
 
41
- def initialize(env)
42
- self.repo_url = GitRepo.new.origins # CircleCI doesn't provide a repo url env variable :/
59
+ url
60
+ end
61
+
62
+ def initialize(env)
63
+ self.repo_url = GitRepo.new.origins # CircleCI doesn't provide a repo url env variable :/
43
64
 
44
- @circle_token = env["CIRCLE_CI_API_TOKEN"]
45
- url = pull_request_url(env)
65
+ @circle_token = env["CIRCLE_CI_API_TOKEN"]
66
+ url = pull_request_url(env)
46
67
 
47
- if URI.parse(url).path.split("/").count == 5
48
- paths = URI.parse(url).path.split("/")
49
- # The first one is an extra slash, ignore it
50
- self.repo_slug = paths[1] + "/" + paths[2]
51
- self.pull_request_id = paths[4]
52
- end
68
+ if URI.parse(url).path.split("/").count == 5
69
+ paths = URI.parse(url).path.split("/")
70
+ # The first one is an extra slash, ignore it
71
+ self.repo_slug = paths[1] + "/" + paths[2]
72
+ self.pull_request_id = paths[4]
53
73
  end
54
74
  end
55
75
  end
@@ -1,26 +1,41 @@
1
1
  # http://readme.drone.io/usage/variables/
2
2
 
3
3
  module Danger
4
- module CISource
5
- # https://drone.io
6
- class Drone < CI
7
- def self.validates?(env)
8
- return false unless env["DRONE"]
9
- return false unless env["DRONE_REPO"]
10
- return false unless env["DRONE_PULL_REQUEST"].to_i > 0
4
+ # ### CI Setup
5
+ #
6
+ # With Drone you run the docker images yourself, so you will want to add `bundle exec danger` at the end of
7
+ # your `.drone.yml`.
8
+ #
9
+ # ``` shell
10
+ # build:
11
+ # image: golang
12
+ # commands:
13
+ # - ...
14
+ # - bundle exec danger
15
+ # ```
16
+ #
17
+ # ### Token Setup
18
+ #
19
+ # As this is self-hosted, you will need to add the `DANGER_GITHUB_API_TOKEN` to your build user's ENV. The alternative
20
+ # is to pass in the token as a prefix to the command `DANGER_GITHUB_API_TOKEN="123" bundle exec danger`.
21
+ #
22
+ class Drone < CI
23
+ def self.validates_as_ci?(env)
24
+ env.key? "DRONE_REPO"
25
+ end
11
26
 
12
- return true
13
- end
27
+ def self.validates_as_pr?(env)
28
+ env["DRONE_PULL_REQUEST"].to_i > 0
29
+ end
14
30
 
15
- def supported_request_sources
16
- @supported_request_sources ||= [Danger::RequestSources::GitHub]
17
- end
31
+ def supported_request_sources
32
+ @supported_request_sources ||= [Danger::RequestSources::GitHub]
33
+ end
18
34
 
19
- def initialize(env)
20
- self.repo_slug = env["DRONE_REPO"]
21
- self.pull_request_id = env["DRONE_PULL_REQUEST"]
22
- self.repo_url = GitRepo.new.origins # Drone doesn't provide a repo url env variable :/
23
- end
35
+ def initialize(env)
36
+ self.repo_slug = env["DRONE_REPO"]
37
+ self.pull_request_id = env["DRONE_PULL_REQUEST"]
38
+ self.repo_url = GitRepo.new.origins # Drone doesn't provide a repo url env variable :/
24
39
  end
25
40
  end
26
41
  end
@@ -2,27 +2,39 @@
2
2
  # https://wiki.jenkins-ci.org/display/JENKINS/GitHub+pull+request+builder+plugin
3
3
 
4
4
  module Danger
5
- module CISource
6
- # https://jenkins-ci.org
7
- class Jenkins < CI
8
- def self.validates?(env)
9
- return false unless env["ghprbPullId"].to_i > 0
10
- return false unless env["GIT_URL"]
5
+ # https://jenkins-ci.org
11
6
 
12
- return true
13
- end
7
+ ### CI Setup
8
+ #
9
+ # Ah Jenkins, so many memories. So, if you're using Jenkins, you're hosting your own environment. You
10
+ # will want to be using the [GitHub pull request builder plugin](https://wiki.jenkins-ci.org/display/JENKINS/GitHub+pull+request+builder+plugin)
11
+ # in order to ensure that you have the build environment set up for PR integration.
12
+ #
13
+ # With that set up, you can edit your job to add `bundle exec danger` at the build action.
14
+ #
15
+ # ### Token Setup
16
+ #
17
+ # As you own the machine, it's up to you to add the enviroment variable for the `DANGER_GITHUB_API_TOKEN`.
18
+ #
19
+ class Jenkins < CI
20
+ def self.validates_as_ci?(env)
21
+ env.key? "JENKINS_URL"
22
+ end
23
+
24
+ def self.validates_as_pr?(env)
25
+ ["ghprbPullId"].all? { |x| env[x] }
26
+ end
14
27
 
15
- def supported_request_sources
16
- @supported_request_sources ||= [Danger::RequestSources::GitHub]
17
- end
28
+ def supported_request_sources
29
+ @supported_request_sources ||= [Danger::RequestSources::GitHub]
30
+ end
18
31
 
19
- def initialize(env)
20
- self.repo_url = env["GIT_URL"]
21
- self.pull_request_id = env["ghprbPullId"]
32
+ def initialize(env)
33
+ self.repo_url = env["GIT_URL"]
34
+ self.pull_request_id = env["ghprbPullId"]
22
35
 
23
- repo_matches = self.repo_url.match(%r{([\/:])([^\/]+\/[^\/.]+)(?:.git)?$})
24
- self.repo_slug = repo_matches[2] unless repo_matches.nil?
25
- end
36
+ repo_matches = self.repo_url.match(%r{([\/:])([^\/]+\/[^\/.]+)(?:.git)?$})
37
+ self.repo_slug = repo_matches[2] unless repo_matches.nil?
26
38
  end
27
39
  end
28
40
  end
@@ -4,62 +4,60 @@ require "git"
4
4
  require "uri"
5
5
 
6
6
  module Danger
7
- module CISource
8
- # ignore
9
- class LocalGitRepo < CI
10
- attr_accessor :base_commit, :head_commit
7
+ # ignore
8
+ class LocalGitRepo < CI
9
+ attr_accessor :base_commit, :head_commit
11
10
 
12
- def self.validates?(env)
13
- return !env["DANGER_USE_LOCAL_GIT"].nil?
14
- end
15
-
16
- def git
17
- @git ||= GitRepo.new
18
- end
11
+ def self.validates_as_ci?(env)
12
+ env.key? "DANGER_USE_LOCAL_GIT"
13
+ end
19
14
 
20
- def run_git(command)
21
- git.exec command
22
- end
15
+ def git
16
+ @git ||= GitRepo.new
17
+ end
23
18
 
24
- def supported_request_sources
25
- @supported_request_sources ||= [Danger::RequestSources::GitHub]
26
- end
19
+ def run_git(command)
20
+ git.exec command
21
+ end
27
22
 
28
- def initialize(env = {})
29
- github_host = env["DANGER_GITHUB_HOST"] || "github.com"
23
+ def supported_request_sources
24
+ @supported_request_sources ||= [Danger::RequestSources::GitHub]
25
+ end
30
26
 
31
- # get the remote URL
32
- remote = run_git("remote show origin -n").lines.grep(/Fetch URL/)[0].split(": ", 2)[1]
33
- if remote
34
- remote_url_matches = remote.match(%r{#{Regexp.escape github_host}(:|/)(?<repo_slug>.+/.+?)(?:\.git)?$})
35
- if !remote_url_matches.nil? and remote_url_matches["repo_slug"]
36
- self.repo_slug = remote_url_matches["repo_slug"]
37
- else
38
- puts "Danger local requires a repository hosted on GitHub.com or GitHub Enterprise."
39
- end
27
+ def initialize(env = {})
28
+ github_host = env["DANGER_GITHUB_HOST"] || "github.com"
29
+
30
+ # get the remote URL
31
+ remote = run_git("remote show origin -n").lines.grep(/Fetch URL/)[0].split(": ", 2)[1]
32
+ if remote
33
+ remote_url_matches = remote.match(%r{#{Regexp.escape github_host}(:|/)(?<repo_slug>.+/.+?)(?:\.git)?$})
34
+ if !remote_url_matches.nil? and remote_url_matches["repo_slug"]
35
+ self.repo_slug = remote_url_matches["repo_slug"]
36
+ else
37
+ puts "Danger local requires a repository hosted on GitHub.com or GitHub Enterprise."
40
38
  end
39
+ end
41
40
 
42
- specific_pr = env["LOCAL_GIT_PR_ID"]
43
- pr_ref = specific_pr ? "##{specific_pr}" : ""
44
- pr_command = "log --merges --oneline"
41
+ specific_pr = env["LOCAL_GIT_PR_ID"]
42
+ pr_ref = specific_pr ? "##{specific_pr}" : ""
43
+ pr_command = "log --merges --oneline"
45
44
 
46
- # get the most recent PR merge
47
- pr_merge = run_git(pr_command.strip).lines.grep(Regexp.new("Merge pull request " + pr_ref))[0]
45
+ # get the most recent PR merge
46
+ pr_merge = run_git(pr_command.strip).lines.grep(Regexp.new("Merge pull request " + pr_ref))[0]
48
47
 
49
- if pr_merge.to_s.empty?
50
- if specific_pr
51
- raise "Could not find the pull request (#{specific_pr}) inside the git history for this repo."
52
- else
53
- raise "No recent pull requests found for this repo, danger requires at least one PR for the local mode."
54
- end
48
+ if pr_merge.to_s.empty?
49
+ if specific_pr
50
+ raise "Could not find the pull request (#{specific_pr}) inside the git history for this repo."
51
+ else
52
+ raise "No recent pull requests found for this repo, danger requires at least one PR for the local mode."
55
53
  end
56
-
57
- self.pull_request_id = pr_merge.match("#([0-9]+)")[1]
58
- sha = pr_merge.split(" ")[0]
59
- parents = run_git("rev-list --parents -n 1 #{sha}").strip.split(" ")
60
- self.base_commit = parents[0]
61
- self.head_commit = parents[1]
62
54
  end
55
+
56
+ self.pull_request_id = pr_merge.match("#([0-9]+)")[1]
57
+ sha = pr_merge.split(" ")[0]
58
+ parents = run_git("rev-list --parents -n 1 #{sha}").strip.split(" ")
59
+ self.base_commit = parents[0]
60
+ self.head_commit = parents[1]
63
61
  end
64
62
  end
65
63
  end