danger-additional-logging 0.0.1

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.
Files changed (127) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +22 -0
  3. data/README.md +93 -0
  4. data/bin/danger +5 -0
  5. data/lib/assets/DangerfileTemplate +13 -0
  6. data/lib/danger/ci_source/appcenter.rb +55 -0
  7. data/lib/danger/ci_source/appcircle.rb +83 -0
  8. data/lib/danger/ci_source/appveyor.rb +64 -0
  9. data/lib/danger/ci_source/azure_pipelines.rb +61 -0
  10. data/lib/danger/ci_source/bamboo.rb +41 -0
  11. data/lib/danger/ci_source/bitbucket_pipelines.rb +37 -0
  12. data/lib/danger/ci_source/bitrise.rb +78 -0
  13. data/lib/danger/ci_source/buddybuild.rb +62 -0
  14. data/lib/danger/ci_source/buildkite.rb +51 -0
  15. data/lib/danger/ci_source/ci_source.rb +37 -0
  16. data/lib/danger/ci_source/circle.rb +94 -0
  17. data/lib/danger/ci_source/circle_api.rb +51 -0
  18. data/lib/danger/ci_source/cirrus.rb +31 -0
  19. data/lib/danger/ci_source/code_build.rb +71 -0
  20. data/lib/danger/ci_source/codefresh.rb +47 -0
  21. data/lib/danger/ci_source/codemagic.rb +58 -0
  22. data/lib/danger/ci_source/codeship.rb +44 -0
  23. data/lib/danger/ci_source/concourse.rb +60 -0
  24. data/lib/danger/ci_source/custom_ci_with_github.rb +49 -0
  25. data/lib/danger/ci_source/dotci.rb +50 -0
  26. data/lib/danger/ci_source/drone.rb +71 -0
  27. data/lib/danger/ci_source/github_actions.rb +44 -0
  28. data/lib/danger/ci_source/gitlab_ci.rb +89 -0
  29. data/lib/danger/ci_source/jenkins.rb +148 -0
  30. data/lib/danger/ci_source/local_git_repo.rb +117 -0
  31. data/lib/danger/ci_source/local_only_git_repo.rb +44 -0
  32. data/lib/danger/ci_source/screwdriver.rb +48 -0
  33. data/lib/danger/ci_source/semaphore.rb +37 -0
  34. data/lib/danger/ci_source/support/commits.rb +19 -0
  35. data/lib/danger/ci_source/support/find_repo_info_from_logs.rb +35 -0
  36. data/lib/danger/ci_source/support/find_repo_info_from_url.rb +43 -0
  37. data/lib/danger/ci_source/support/local_pull_request.rb +14 -0
  38. data/lib/danger/ci_source/support/no_pull_request.rb +7 -0
  39. data/lib/danger/ci_source/support/no_repo_info.rb +5 -0
  40. data/lib/danger/ci_source/support/pull_request_finder.rb +190 -0
  41. data/lib/danger/ci_source/support/remote_pull_request.rb +15 -0
  42. data/lib/danger/ci_source/support/repo_info.rb +10 -0
  43. data/lib/danger/ci_source/surf.rb +37 -0
  44. data/lib/danger/ci_source/teamcity.rb +163 -0
  45. data/lib/danger/ci_source/travis.rb +51 -0
  46. data/lib/danger/ci_source/xcode_cloud.rb +38 -0
  47. data/lib/danger/ci_source/xcode_server.rb +48 -0
  48. data/lib/danger/clients/rubygems_client.rb +14 -0
  49. data/lib/danger/commands/dangerfile/gem.rb +43 -0
  50. data/lib/danger/commands/dangerfile/init.rb +30 -0
  51. data/lib/danger/commands/dry_run.rb +54 -0
  52. data/lib/danger/commands/init.rb +297 -0
  53. data/lib/danger/commands/init_helpers/interviewer.rb +92 -0
  54. data/lib/danger/commands/local.rb +83 -0
  55. data/lib/danger/commands/local_helpers/http_cache.rb +38 -0
  56. data/lib/danger/commands/local_helpers/local_setup.rb +48 -0
  57. data/lib/danger/commands/local_helpers/pry_setup.rb +32 -0
  58. data/lib/danger/commands/plugins/plugin_json.rb +44 -0
  59. data/lib/danger/commands/plugins/plugin_lint.rb +52 -0
  60. data/lib/danger/commands/plugins/plugin_readme.rb +42 -0
  61. data/lib/danger/commands/pr.rb +93 -0
  62. data/lib/danger/commands/runner.rb +94 -0
  63. data/lib/danger/commands/staging.rb +53 -0
  64. data/lib/danger/commands/systems.rb +41 -0
  65. data/lib/danger/comment_generators/bitbucket_server.md.erb +20 -0
  66. data/lib/danger/comment_generators/bitbucket_server_inline.md.erb +15 -0
  67. data/lib/danger/comment_generators/bitbucket_server_message_group.md.erb +12 -0
  68. data/lib/danger/comment_generators/github.md.erb +55 -0
  69. data/lib/danger/comment_generators/github_inline.md.erb +26 -0
  70. data/lib/danger/comment_generators/gitlab.md.erb +40 -0
  71. data/lib/danger/comment_generators/gitlab_inline.md.erb +21 -0
  72. data/lib/danger/comment_generators/vsts.md.erb +20 -0
  73. data/lib/danger/comment_generators/vsts_inline.md.erb +17 -0
  74. data/lib/danger/core_ext/file_list.rb +18 -0
  75. data/lib/danger/core_ext/string.rb +20 -0
  76. data/lib/danger/danger_core/dangerfile.rb +348 -0
  77. data/lib/danger/danger_core/dangerfile_dsl.rb +29 -0
  78. data/lib/danger/danger_core/dangerfile_generator.rb +11 -0
  79. data/lib/danger/danger_core/environment_manager.rb +126 -0
  80. data/lib/danger/danger_core/executor.rb +91 -0
  81. data/lib/danger/danger_core/message_aggregator.rb +50 -0
  82. data/lib/danger/danger_core/message_group.rb +68 -0
  83. data/lib/danger/danger_core/messages/base.rb +57 -0
  84. data/lib/danger/danger_core/messages/markdown.rb +41 -0
  85. data/lib/danger/danger_core/messages/violation.rb +53 -0
  86. data/lib/danger/danger_core/plugins/dangerfile_bitbucket_cloud_plugin.rb +142 -0
  87. data/lib/danger/danger_core/plugins/dangerfile_bitbucket_server_plugin.rb +211 -0
  88. data/lib/danger/danger_core/plugins/dangerfile_danger_plugin.rb +274 -0
  89. data/lib/danger/danger_core/plugins/dangerfile_git_plugin.rb +159 -0
  90. data/lib/danger/danger_core/plugins/dangerfile_github_plugin.rb +264 -0
  91. data/lib/danger/danger_core/plugins/dangerfile_gitlab_plugin.rb +275 -0
  92. data/lib/danger/danger_core/plugins/dangerfile_local_only_plugin.rb +43 -0
  93. data/lib/danger/danger_core/plugins/dangerfile_messaging_plugin.rb +220 -0
  94. data/lib/danger/danger_core/plugins/dangerfile_vsts_plugin.rb +191 -0
  95. data/lib/danger/danger_core/standard_error.rb +142 -0
  96. data/lib/danger/helpers/array_subclass.rb +61 -0
  97. data/lib/danger/helpers/comment.rb +32 -0
  98. data/lib/danger/helpers/comments_helper.rb +179 -0
  99. data/lib/danger/helpers/comments_parsing_helper.rb +71 -0
  100. data/lib/danger/helpers/emoji_mapper.rb +41 -0
  101. data/lib/danger/helpers/find_max_num_violations.rb +31 -0
  102. data/lib/danger/helpers/message_groups_array_helper.rb +31 -0
  103. data/lib/danger/plugin_support/gems_resolver.rb +77 -0
  104. data/lib/danger/plugin_support/plugin.rb +52 -0
  105. data/lib/danger/plugin_support/plugin_file_resolver.rb +30 -0
  106. data/lib/danger/plugin_support/plugin_linter.rb +162 -0
  107. data/lib/danger/plugin_support/plugin_parser.rb +199 -0
  108. data/lib/danger/plugin_support/templates/readme_table.html.erb +26 -0
  109. data/lib/danger/request_sources/bitbucket_cloud.rb +169 -0
  110. data/lib/danger/request_sources/bitbucket_cloud_api.rb +181 -0
  111. data/lib/danger/request_sources/bitbucket_server.rb +210 -0
  112. data/lib/danger/request_sources/bitbucket_server_api.rb +129 -0
  113. data/lib/danger/request_sources/code_insights_api.rb +142 -0
  114. data/lib/danger/request_sources/github/github.rb +535 -0
  115. data/lib/danger/request_sources/github/github_review.rb +127 -0
  116. data/lib/danger/request_sources/github/github_review_resolver.rb +17 -0
  117. data/lib/danger/request_sources/github/github_review_unsupported.rb +23 -0
  118. data/lib/danger/request_sources/gitlab.rb +557 -0
  119. data/lib/danger/request_sources/local_only.rb +50 -0
  120. data/lib/danger/request_sources/request_source.rb +97 -0
  121. data/lib/danger/request_sources/support/get_ignored_violation.rb +17 -0
  122. data/lib/danger/request_sources/vsts.rb +278 -0
  123. data/lib/danger/request_sources/vsts_api.rb +172 -0
  124. data/lib/danger/scm_source/git_repo.rb +198 -0
  125. data/lib/danger/version.rb +4 -0
  126. data/lib/danger.rb +45 -0
  127. metadata +351 -0
@@ -0,0 +1,43 @@
1
+ require "danger/ci_source/support/repo_info"
2
+
3
+ module Danger
4
+ class FindRepoInfoFromURL
5
+ REGEXP = %r{
6
+ ://[^/]+/
7
+ (([^/]+/){1,2}_git/)?
8
+ (?<slug>[^/]+(/[^/]+){0,2})
9
+ (/(pull|pullrequest|merge_requests|pull-requests)/)
10
+ (?<id>\d+)
11
+ }x.freeze
12
+
13
+ # Regex used to extract info from Bitbucket server URLs, as they use a quite different format
14
+ REGEXPBB = %r{
15
+ (?:[/:])projects
16
+ /([^/.]+)
17
+ /repos/([^/.]+)
18
+ /pull-requests
19
+ /(\d+)
20
+ }x.freeze
21
+
22
+ def initialize(url)
23
+ @url = url
24
+ end
25
+
26
+ def call
27
+ matched = url.match(REGEXPBB)
28
+
29
+ if matched
30
+ RepoInfo.new("#{matched[1]}/#{matched[2]}", matched[3])
31
+ else
32
+ matched = url.match(REGEXP)
33
+ if matched
34
+ RepoInfo.new(matched[:slug], matched[:id])
35
+ end
36
+ end
37
+ end
38
+
39
+ private
40
+
41
+ attr_reader :url
42
+ end
43
+ end
@@ -0,0 +1,14 @@
1
+ module Danger
2
+ class LocalPullRequest
3
+ attr_reader :pull_request_id, :sha
4
+
5
+ def initialize(log_line)
6
+ @pull_request_id = log_line.match(/#(?<id>[0-9]+)/)[:id]
7
+ @sha = log_line.split(" ".freeze).first
8
+ end
9
+
10
+ def valid?
11
+ pull_request_id && sha
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,7 @@
1
+ module Danger
2
+ class NoPullRequest
3
+ def valid?
4
+ false
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ module Danger
2
+ class NoRepoInfo
3
+ attr_reader :slug, :id
4
+ end
5
+ end
@@ -0,0 +1,190 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "danger/ci_source/support/local_pull_request"
4
+ require "danger/ci_source/support/remote_pull_request"
5
+ require "danger/ci_source/support/no_pull_request"
6
+
7
+ module Danger
8
+ class PullRequestFinder
9
+ def initialize(specific_pull_request_id, repo_slug = nil, remote: false, git_logs: "", remote_url: "")
10
+ @specific_pull_request_id = specific_pull_request_id
11
+ @git_logs = git_logs
12
+ @repo_slug = repo_slug
13
+ @remote = to_boolean(remote)
14
+ @remote_url = remote_url
15
+ end
16
+
17
+ def call(env: nil)
18
+ find_pull_request(env).tap do |pull_request|
19
+ raise_pull_request_not_found!(pull_request) unless pull_request.valid?
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ attr_reader :specific_pull_request_id, :git_logs, :repo_slug, :remote, :remote_url
26
+
27
+ def to_boolean(maybe_string)
28
+ ["true", "1", "yes", "y", true].include?(maybe_string)
29
+ end
30
+
31
+ def raise_pull_request_not_found!(_pull_request)
32
+ if specific_pull_request_id.empty?
33
+ raise "No recent Pull Requests found for this repo, danger requires at least one Pull Request for the local mode."
34
+ else
35
+ raise "Could not find the Pull Request (#{specific_pull_request_id}) inside the git history for this repo."
36
+ end
37
+ end
38
+
39
+ # @return [String] Log line of most recent merged Pull Request
40
+ def find_pull_request(env)
41
+ return if pull_request_ref.empty?
42
+
43
+ if both_present?
44
+ LocalPullRequest.new(pick_the_most_recent_one_from_two_matches)
45
+ elsif only_merged_pull_request_present?
46
+ LocalPullRequest.new(most_recent_merged_pull_request)
47
+ elsif only_squash_and_merged_pull_request_present?
48
+ LocalPullRequest.new(most_recent_squash_and_merged_pull_request)
49
+ elsif remote
50
+ remote_pull_request = find_remote_pull_request(env)
51
+ remote_pull_request ? generate_remote_pull_request(remote_pull_request) : NoPullRequest.new
52
+ else
53
+ NoPullRequest.new
54
+ end
55
+ end
56
+
57
+ # @return [String] "#42"
58
+ def pull_request_ref
59
+ !specific_pull_request_id.empty? ? "##{specific_pull_request_id}" : "#\\d+"
60
+ end
61
+
62
+ def generate_remote_pull_request(remote_pull_request)
63
+ scm_provider = find_scm_provider(remote_url)
64
+
65
+ case scm_provider
66
+ when :bitbucket_cloud
67
+ RemotePullRequest.new(
68
+ remote_pull_request[:id].to_s,
69
+ remote_pull_request[:source][:commit][:hash],
70
+ remote_pull_request[:destination][:commit][:hash]
71
+ )
72
+ when :bitbucket_server
73
+ RemotePullRequest.new(
74
+ remote_pull_request[:id].to_s,
75
+ remote_pull_request[:fromRef][:latestCommit],
76
+ remote_pull_request[:toRef][:latestCommit]
77
+ )
78
+ when :github
79
+ RemotePullRequest.new(
80
+ remote_pull_request.number.to_s,
81
+ remote_pull_request.head.sha,
82
+ remote_pull_request.base.sha
83
+ )
84
+ when :vsts
85
+ RemotePullRequest.new(
86
+ remote_pull_request[:pullRequestId].to_s,
87
+ remote_pull_request[:lastMergeSourceCommit][:commitId],
88
+ remote_pull_request[:lastMergeTargetCommit][:commitId]
89
+ )
90
+ else
91
+ raise "SCM provider not supported: #{scm_provider}"
92
+ end
93
+ end
94
+
95
+ def find_remote_pull_request(env)
96
+ client(env).pull_request(repo_slug, specific_pull_request_id)
97
+ end
98
+
99
+ def both_present?
100
+ most_recent_merged_pull_request && most_recent_squash_and_merged_pull_request
101
+ end
102
+
103
+ # @return [String] Log line of format: "Merge pull request #42"
104
+ def most_recent_merged_pull_request
105
+ @most_recent_merged_pull_request ||= git_logs.lines.grep(/Merge pull request #{pull_request_ref} from/)[0]
106
+ end
107
+
108
+ # @return [String] Log line of format: "description (#42)"
109
+ def most_recent_squash_and_merged_pull_request
110
+ @most_recent_squash_and_merged_pull_request ||= git_logs.lines.grep(/\(#{pull_request_ref}\)/)[0]
111
+ end
112
+
113
+ def pick_the_most_recent_one_from_two_matches
114
+ merged_index = git_logs.lines.index(most_recent_merged_pull_request)
115
+ squash_and_merged_index = git_logs.lines.index(most_recent_squash_and_merged_pull_request)
116
+
117
+ if merged_index > squash_and_merged_index # smaller index is more recent
118
+ most_recent_squash_and_merged_pull_request
119
+ else
120
+ most_recent_merged_pull_request
121
+ end
122
+ end
123
+
124
+ def only_merged_pull_request_present?
125
+ return false if most_recent_squash_and_merged_pull_request
126
+
127
+ !most_recent_merged_pull_request.nil? && !most_recent_merged_pull_request.empty?
128
+ end
129
+
130
+ def only_squash_and_merged_pull_request_present?
131
+ return false if most_recent_merged_pull_request
132
+
133
+ !most_recent_squash_and_merged_pull_request.nil? && !most_recent_squash_and_merged_pull_request.empty?
134
+ end
135
+
136
+ def client(env)
137
+ scm_provider = find_scm_provider(remote_url)
138
+
139
+ case scm_provider
140
+ when :bitbucket_cloud
141
+ require "danger/request_sources/bitbucket_cloud_api"
142
+ branch_name = ENV["DANGER_BITBUCKET_TARGET_BRANCH"] # Optional env variable (specifying the target branch) to help find the PR.
143
+ RequestSources::BitbucketCloudAPI.new(repo_slug, specific_pull_request_id, branch_name, env)
144
+
145
+ when :bitbucket_server
146
+ require "danger/request_sources/bitbucket_server_api"
147
+ project, slug = repo_slug.split("/")
148
+ RequestSources::BitbucketServerAPI.new(project, slug, specific_pull_request_id, env)
149
+
150
+ when :vsts
151
+ require "danger/request_sources/vsts_api"
152
+ RequestSources::VSTSAPI.new(repo_slug, specific_pull_request_id, env)
153
+
154
+ when :github
155
+ require "octokit"
156
+ access_token = ENV["DANGER_GITHUB_API_TOKEN"]
157
+ bearer_token = ENV["DANGER_GITHUB_BEARER_TOKEN"]
158
+ if bearer_token && !bearer_token.empty?
159
+ Octokit::Client.new(bearer_token: bearer_token, api_endpoint: api_url)
160
+ elsif access_token && !access_token.empty?
161
+ Octokit::Client.new(access_token: access_token, api_endpoint: api_url)
162
+ else
163
+ raise "No API token given, please provide one using `DANGER_GITHUB_API_TOKEN` or `DANGER_GITHUB_BEARER_TOKEN`"
164
+ end
165
+ else
166
+ raise "SCM provider not supported: #{scm_provider}"
167
+ end
168
+ end
169
+
170
+ def api_url
171
+ ENV.fetch("DANGER_GITHUB_API_HOST") do
172
+ ENV.fetch("DANGER_GITHUB_API_BASE_URL") do
173
+ "https://api.github.com/"
174
+ end
175
+ end
176
+ end
177
+
178
+ def find_scm_provider(remote_url)
179
+ if remote_url =~ %r{/bitbucket.org/}
180
+ :bitbucket_cloud
181
+ elsif remote_url =~ %r{/pull-requests/}
182
+ :bitbucket_server
183
+ elsif remote_url =~ /\.visualstudio\.com/i || remote_url =~ /dev\.azure\.com/i
184
+ :vsts
185
+ else
186
+ :github
187
+ end
188
+ end
189
+ end
190
+ end
@@ -0,0 +1,15 @@
1
+ module Danger
2
+ class RemotePullRequest
3
+ attr_reader :pull_request_id, :sha, :head, :base
4
+
5
+ def initialize(pull_request_id, head, base)
6
+ @pull_request_id = pull_request_id
7
+ @head = head
8
+ @base = base
9
+ end
10
+
11
+ def valid?
12
+ pull_request_id && head && base
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,10 @@
1
+ module Danger
2
+ class RepoInfo
3
+ attr_reader :slug, :id
4
+
5
+ def initialize(slug, id)
6
+ @slug = slug
7
+ @id = id
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,37 @@
1
+ # http://github.com/surf-build/surf
2
+ require "danger/request_sources/github/github"
3
+
4
+ module Danger
5
+ # ### CI Setup
6
+ #
7
+ # You want to add `bundle exec danger` to your `build.sh` file to run Danger at the
8
+ # end of your build.
9
+ #
10
+ # ### Token Setup
11
+ #
12
+ # As this is self-hosted, you will need to add the `DANGER_GITHUB_API_TOKEN` to your build user's ENV. The alternative
13
+ # is to pass in the token as a prefix to the command `DANGER_GITHUB_API_TOKEN="123" bundle exec danger`.
14
+ #
15
+ class Surf < CI
16
+ def self.validates_as_ci?(env)
17
+ return ["SURF_REPO", "SURF_NWO"].all? { |x| env[x] && !env[x].empty? }
18
+ end
19
+
20
+ def self.validates_as_pr?(env)
21
+ validates_as_ci?(env)
22
+ end
23
+
24
+ def supported_request_sources
25
+ @supported_request_sources ||= [Danger::RequestSources::GitHub]
26
+ end
27
+
28
+ def initialize(env)
29
+ self.repo_slug = env["SURF_NWO"]
30
+ if env["SURF_PR_NUM"].to_i > 0
31
+ self.pull_request_id = env["SURF_PR_NUM"]
32
+ end
33
+
34
+ self.repo_url = env["SURF_REPO"]
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,163 @@
1
+ # https://www.jetbrains.com/teamcity/
2
+ require "danger/request_sources/github/github"
3
+ require "danger/request_sources/gitlab"
4
+
5
+ module Danger
6
+ # ### CI Setup
7
+ #
8
+ # You need to go to your project settings. Then depending on the type of your build settings, you may need
9
+ # to add a new build step for Danger. You want to be able to run the command `bundle exec danger`, so
10
+ # the "Simple Command Runner" should be all you need to do that.
11
+ #
12
+ # ### Token + Environment Setup
13
+ #
14
+ # #### GitHub
15
+ #
16
+ # As this is self-hosted, you will need to add the `DANGER_GITHUB_API_TOKEN` to your build user's ENV. The alternative
17
+ # is to pass in the token as a prefix to the command `DANGER_GITHUB_API_TOKEN="123" bundle exec danger`.
18
+ #
19
+ # However, you will need to find a way to add the environment vars: `GITHUB_REPO_SLUG`, `GITHUB_PULL_REQUEST_ID` and
20
+ # `GITHUB_REPO_URL`. These are not added by default. You can manually add `GITHUB_REPO_SLUG` and `GITHUB_REPO_URL`
21
+ # as build parameters or by exporting them inside your Simple Command Runner.
22
+ #
23
+ # As for `GITHUB_PULL_REQUEST_ID`, TeamCity provides the `%teamcity.build.branch%` variable which is in the format
24
+ # `PR_NUMBER/merge`. You can slice the Pull Request ID out by doing the following:
25
+ #
26
+ # ```sh
27
+ # branch="%teamcity.build.branch%"
28
+ # export GITHUB_PULL_REQUEST_ID=(${branch//\// })
29
+ # ```
30
+ #
31
+ # Or if you are using the pull request feature you can set an environment parameter called `GITHUB_PULL_REQUEST_ID`
32
+ # to the value of: `%teamcity.pullRequest.number`
33
+ #
34
+ # #### GitLab
35
+ #
36
+ # As this is self-hosted, you will need to add the `DANGER_GITLAB_API_TOKEN` to your build user's ENV. The alternative
37
+ # is to pass in the token as a prefix to the command `DANGER_GITLAB_API_TOKEN="123" bundle exec danger`.
38
+ #
39
+ # However, you will need to find a way to add the environment vars: `GITLAB_REPO_SLUG`, `GITLAB_PULL_REQUEST_ID` and
40
+ # `GITLAB_REPO_URL`. These are not added by default. You could do this via the GitLab API potentially.
41
+ #
42
+ # We would love some advice on improving this setup.
43
+ #
44
+ # #### BitBucket Cloud
45
+ #
46
+ # You will need to add the following environment variables as build parameters or by exporting them inside your
47
+ # Simple Command Runner.
48
+ #
49
+ #
50
+ # - `BITBUCKET_REPO_SLUG`
51
+ # - `BITBUCKET_REPO_URL`
52
+ #
53
+ # - `DANGER_BITBUCKETCLOUD_USERNAME`
54
+ # - `DANGER_BITBUCKETCLOUD_PASSWORD`
55
+ #
56
+ # or
57
+ #
58
+ # - `DANGER_BITBUCKETCLOUD_OAUTH_KEY`
59
+ # - `DANGER_BITBUCKETCLOUD_OAUTH_SECRET`
60
+ #
61
+ # You will also need to set the `BITBUCKET_BRANCH_NAME` environment variable.
62
+ # TeamCity provides `%teamcity.build.branch%`, which you can use at the top of your Simple Command Runner:
63
+ #
64
+ # ```sh
65
+ # export BITBUCKET_BRANCH_NAME="%teamcity.build.branch%"
66
+ # ```
67
+ #
68
+ # #### BitBucket Server
69
+ #
70
+ # You will need to add the following environment variables as build parameters or by exporting them inside your
71
+ # Simple Command Runner.
72
+ #
73
+ # - `DANGER_BITBUCKETSERVER_USERNAME`
74
+ # - `DANGER_BITBUCKETSERVER_PASSWORD`
75
+ # - `DANGER_BITBUCKETSERVER_HOST`
76
+ # - `BITBUCKETSERVER_REPO_SLUG`
77
+ # - `BITBUCKETSERVER_PULL_REQUEST_ID`
78
+ # - `BITBUCKETSERVER_REPO_URL`
79
+ #
80
+ class TeamCity < CI
81
+ class << self
82
+ def validates_as_github_pr?(env)
83
+ ["GITHUB_PULL_REQUEST_ID", "GITHUB_REPO_URL"].all? { |x| env[x] && !env[x].empty? }
84
+ end
85
+
86
+ def validates_as_gitlab_pr?(env)
87
+ ["GITLAB_REPO_SLUG", "GITLAB_PULL_REQUEST_ID", "GITLAB_REPO_URL"].all? { |x| env[x] && !env[x].empty? }
88
+ end
89
+
90
+ def validates_as_bitbucket_cloud_pr?(env)
91
+ ["BITBUCKET_REPO_SLUG", "BITBUCKET_BRANCH_NAME", "BITBUCKET_REPO_URL"].all? { |x| env[x] && !env[x].empty? }
92
+ end
93
+
94
+ def validates_as_bitbucket_server_pr?(env)
95
+ ["BITBUCKETSERVER_REPO_SLUG", "BITBUCKETSERVER_PULL_REQUEST_ID", "BITBUCKETSERVER_REPO_URL"].all? { |x| env[x] && !env[x].empty? }
96
+ end
97
+ end
98
+
99
+ def self.validates_as_ci?(env)
100
+ env.key? "TEAMCITY_VERSION"
101
+ end
102
+
103
+ def self.validates_as_pr?(env)
104
+ validates_as_github_pr?(env) || validates_as_gitlab_pr?(env) || validates_as_bitbucket_cloud_pr?(env) || validates_as_bitbucket_server_pr?(env)
105
+ end
106
+
107
+ def supported_request_sources
108
+ @supported_request_sources ||= [Danger::RequestSources::GitHub, Danger::RequestSources::GitLab, Danger::RequestSources::BitbucketCloud, Danger::RequestSources::BitbucketServer]
109
+ end
110
+
111
+ def initialize(env)
112
+ # NB: Unfortunately TeamCity doesn't provide these variables
113
+ # automatically so you have to add these variables manually to your
114
+ # project or build configuration
115
+ if self.class.validates_as_github_pr?(env)
116
+ extract_github_variables!(env)
117
+ elsif self.class.validates_as_gitlab_pr?(env)
118
+ extract_gitlab_variables!(env)
119
+ elsif self.class.validates_as_bitbucket_cloud_pr?(env)
120
+ extract_bitbucket_variables!(env)
121
+ elsif self.class.validates_as_bitbucket_server_pr?(env)
122
+ extract_bitbucket_server_variables!(env)
123
+ end
124
+ end
125
+
126
+ private
127
+
128
+ def extract_github_variables!(env)
129
+ self.repo_slug = env["GITHUB_REPO_SLUG"]
130
+ self.pull_request_id = env["GITHUB_PULL_REQUEST_ID"].to_i
131
+ self.repo_url = env["GITHUB_REPO_URL"]
132
+ end
133
+
134
+ def extract_gitlab_variables!(env)
135
+ self.repo_slug = env["GITLAB_REPO_SLUG"]
136
+ self.pull_request_id = env["GITLAB_PULL_REQUEST_ID"].to_i
137
+ self.repo_url = env["GITLAB_REPO_URL"]
138
+ end
139
+
140
+ def extract_bitbucket_variables!(env)
141
+ self.repo_slug = env["BITBUCKET_REPO_SLUG"]
142
+ self.pull_request_id = bitbucket_pr_from_env(env)
143
+ self.repo_url = env["BITBUCKET_REPO_URL"]
144
+ end
145
+
146
+ def extract_bitbucket_server_variables!(env)
147
+ self.repo_slug = env["BITBUCKETSERVER_REPO_SLUG"]
148
+ self.pull_request_id = env["BITBUCKETSERVER_PULL_REQUEST_ID"].to_i
149
+ self.repo_url = env["BITBUCKETSERVER_REPO_URL"]
150
+ end
151
+
152
+ # This is a little hacky, because Bitbucket doesn't provide us a PR id
153
+ def bitbucket_pr_from_env(env)
154
+ branch_name = env["BITBUCKET_BRANCH_NAME"]
155
+ repo_slug = env["BITBUCKET_REPO_SLUG"]
156
+ begin
157
+ Danger::RequestSources::BitbucketCloudAPI.new(repo_slug, nil, branch_name, env).pull_request_id
158
+ rescue StandardError
159
+ raise "Failed to find a pull request for branch \"#{branch_name}\" on Bitbucket."
160
+ end
161
+ end
162
+ end
163
+ end
@@ -0,0 +1,51 @@
1
+ # http://docs.travis-ci.com/user/osx-ci-environment/
2
+ # http://docs.travis-ci.com/user/environment-variables/
3
+ require "danger/request_sources/github/github"
4
+
5
+ module Danger
6
+ # ### CI Setup
7
+ # You need to edit your `.travis.yml` to include `bundle exec danger`. If you already have
8
+ # a `script:` section then we recommend adding this command at the end of the script step: `- bundle exec danger`.
9
+ #
10
+ # Otherwise, add a `before_script` step to the root of the `.travis.yml` with `bundle exec danger`
11
+ #
12
+ # ```ruby
13
+ # before_script:
14
+ # - bundle exec danger
15
+ # ```
16
+ #
17
+ # Adding this to your `.travis.yml` allows Danger to fail your build, both on the TravisCI website and within your Pull Request.
18
+ # With that set up, you can edit your job to add `bundle exec danger` at the build action.
19
+ #
20
+ # _Note:_ Travis CI defaults to using an older version of Ruby, so you may need to add `rvm: 2.0.0` to the root your `.travis.yml`.
21
+ #
22
+ # ### Token Setup
23
+ #
24
+ # You need to add the `DANGER_GITHUB_API_TOKEN` environment variable, to do this,
25
+ # go to your repo's settings, which should look like: `https://travis-ci.org/[user]/[repo]/settings`.
26
+ #
27
+ # If you have an open source project, you should ensure "Display value in build log" enabled, so that PRs from forks work.
28
+ #
29
+ class Travis < CI
30
+ def self.validates_as_ci?(env)
31
+ env.key? "HAS_JOSH_K_SEAL_OF_APPROVAL"
32
+ end
33
+
34
+ def self.validates_as_pr?(env)
35
+ exists = ["TRAVIS_PULL_REQUEST", "TRAVIS_REPO_SLUG"].all? { |x| env[x] && !env[x].empty? }
36
+ exists && env["TRAVIS_PULL_REQUEST"].to_i > 0
37
+ end
38
+
39
+ def supported_request_sources
40
+ @supported_request_sources ||= [Danger::RequestSources::GitHub]
41
+ end
42
+
43
+ def initialize(env)
44
+ self.repo_slug = env["TRAVIS_REPO_SLUG"]
45
+ if env["TRAVIS_PULL_REQUEST"].to_i > 0
46
+ self.pull_request_id = env["TRAVIS_PULL_REQUEST"]
47
+ end
48
+ self.repo_url = GitRepo.new.origins # Travis doesn't provide a repo url env variable :/
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,38 @@
1
+ module Danger
2
+ # ### CI Setup
3
+ #
4
+ # In order to work with Xcode Cloud and Danger, you will need to add `bundle exec danger` to
5
+ # the `ci_scripts/ci_post_xcodebuild.sh` (Xcode Cloud's expected filename for a post-action build script).
6
+ # More details and documentation on Xcode Cloud configuration can be found [here](https://developer.apple.com/documentation/xcode/writing-custom-build-scripts).
7
+ #
8
+ # ### Token Setup
9
+ #
10
+ # You will need to add the `DANGER_GITHUB_API_TOKEN` to your build environment.
11
+ # If running on GitHub Enterprise, make sure you also set the expected values for
12
+ # both `DANGER_GITHUB_API_HOST` and `DANGER_GITHUB_HOST`.
13
+ #
14
+ class XcodeCloud < CI
15
+ def self.validates_as_ci?(env)
16
+ env.key? "CI_XCODEBUILD_ACTION"
17
+ end
18
+
19
+ def self.validates_as_pr?(env)
20
+ env.key? "CI_PULL_REQUEST_NUMBER"
21
+ end
22
+
23
+ def supported_request_sources
24
+ @supported_request_sources ||= [
25
+ Danger::RequestSources::GitHub,
26
+ Danger::RequestSources::GitLab,
27
+ Danger::RequestSources::BitbucketCloud,
28
+ Danger::RequestSources::BitbucketServer
29
+ ]
30
+ end
31
+
32
+ def initialize(env)
33
+ self.repo_slug = env["CI_PULL_REQUEST_SOURCE_REPO"]
34
+ self.pull_request_id = env["CI_PULL_REQUEST_NUMBER"]
35
+ self.repo_url = env["CI_PULL_REQUEST_HTML_URL"]
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,48 @@
1
+ # Following the advice from @czechboy0 https://github.com/danger/danger/issues/171
2
+ # https://github.com/czechboy0/Buildasaur
3
+ require "danger/request_sources/github/github"
4
+
5
+ module Danger
6
+ # ### CI Setup
7
+ #
8
+ # If you're bold enough to use Xcode Bots. You will need to use [Buildasaur](https://github.com/czechboy0/Buildasaur)
9
+ # in order to work with Danger. This will set up your build environment for you, as the name of the bot contains all
10
+ # of the environment variables that Danger needs to work.
11
+ #
12
+ # With Buildasaur set up, you can edit your job to add `bundle exec danger` as a post-action build script.
13
+ #
14
+ # ### Token Setup
15
+ #
16
+ # As this is self-hosted, you will need to add the `DANGER_GITHUB_API_TOKEN` to your build user's ENV. The alternative
17
+ # is to pass in the token as a prefix to the command `DANGER_GITHUB_API_TOKEN="123" bundle exec danger`.`.
18
+ #
19
+ class XcodeServer < CI
20
+ def self.validates_as_ci?(env)
21
+ env.key? "XCS_BOT_NAME"
22
+ end
23
+
24
+ def self.validates_as_pr?(env)
25
+ value = env["XCS_BOT_NAME"]
26
+ !value.nil? && value.include?("BuildaBot")
27
+ end
28
+
29
+ def supported_request_sources
30
+ @supported_request_sources ||= [
31
+ Danger::RequestSources::GitHub,
32
+ Danger::RequestSources::BitbucketServer,
33
+ Danger::RequestSources::BitbucketCloud
34
+ ]
35
+ end
36
+
37
+ def initialize(env)
38
+ bot_name = env["XCS_BOT_NAME"]
39
+ return if bot_name.nil?
40
+
41
+ repo_matches = bot_name.match(/\[(.+)\]/)
42
+ self.repo_slug = repo_matches[1] unless repo_matches.nil?
43
+ pull_request_id_matches = bot_name.match(/#(\d+)/)
44
+ self.pull_request_id = pull_request_id_matches[1] unless pull_request_id_matches.nil?
45
+ self.repo_url = GitRepo.new.origins # Xcode Server doesn't provide a repo url env variable :/
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,14 @@
1
+ module Danger
2
+ class RubyGemsClient
3
+ API_URL = "https://rubygems.org/api/v1/versions/danger/latest.json".freeze
4
+ DUMMY_VERSION = "0.0.0".freeze
5
+
6
+ def self.latest_danger_version
7
+ require "json"
8
+ json = JSON.parse(Faraday.get(API_URL).body)
9
+ json.fetch("version") { DUMMY_VERSION }
10
+ rescue StandardError => _e
11
+ DUMMY_VERSION
12
+ end
13
+ end
14
+ end