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,71 @@
1
+ # http://readme.drone.io/usage/variables/
2
+ require "danger/request_sources/github/github"
3
+ require "danger/request_sources/gitlab"
4
+
5
+ module Danger
6
+ # ### CI Setup
7
+ #
8
+ # With Drone you run the docker images yourself, so you will want to add `bundle exec danger` at the end of
9
+ # your `.drone.yml`.
10
+ #
11
+ # ```shell
12
+ # build:
13
+ # image: golang
14
+ # commands:
15
+ # - ...
16
+ # - bundle exec danger
17
+ # ```
18
+ #
19
+ # ### Token Setup
20
+ #
21
+ # As this is self-hosted, you will need to expose the `DANGER_GITHUB_API_TOKEN` as a secret to your
22
+ # builds:
23
+ #
24
+ # Drone secrets: http://readme.drone.io/usage/secret-guide/
25
+ # NOTE: This is a new syntax in DroneCI 0.6+
26
+ #
27
+ # ```yml
28
+ # build:
29
+ # image: golang
30
+ # secrets:
31
+ # - DANGER_GITHUB_API_TOKEN
32
+ # commands:
33
+ # - ...
34
+ # - bundle exec danger
35
+ # ```
36
+ class Drone < CI
37
+ def self.validates_as_ci?(env)
38
+ validates_as_ci_post_06?(env) or validates_as_ci_pre_06?(env)
39
+ end
40
+
41
+ def self.validates_as_pr?(env)
42
+ env["DRONE_PULL_REQUEST"].to_i > 0
43
+ end
44
+
45
+ def supported_request_sources
46
+ @supported_request_sources ||= [Danger::RequestSources::GitHub, Danger::RequestSources::GitLab]
47
+ end
48
+
49
+ def initialize(env)
50
+ if self.class.validates_as_ci_post_06?(env)
51
+ self.repo_slug = "#{env['DRONE_REPO_OWNER']}/#{env['DRONE_REPO_NAME']}"
52
+ self.repo_url = env["DRONE_REPO_LINK"] if self.class.validates_as_ci_post_06?(env)
53
+ elsif self.class.validates_as_ci_pre_06?(env)
54
+ self.repo_slug = env["DRONE_REPO"]
55
+ self.repo_url = GitRepo.new.origins
56
+ end
57
+
58
+ self.pull_request_id = env["DRONE_PULL_REQUEST"]
59
+ end
60
+
61
+ # Check if this build is valid for CI with drone 0.6 or later
62
+ def self.validates_as_ci_post_06?(env)
63
+ env.key? "DRONE_REPO_OWNER" and env.key? "DRONE_REPO_NAME"
64
+ end
65
+
66
+ # Checks if this build is valid for CI with drone 0.5 or earlier
67
+ def self.validates_as_ci_pre_06?(env)
68
+ env.key? "DRONE_REPO"
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,44 @@
1
+ require "danger/request_sources/github/github"
2
+
3
+ module Danger
4
+ # ### CI Setup
5
+ #
6
+ # You can use `danger/danger` Action in your `.github/workflows/xxx.yml`.
7
+ # And so, you can use GITHUB_TOKEN secret as `DANGER_GITHUB_API_TOKEN` environment variable.
8
+ #
9
+ # ```yml
10
+ # ...
11
+ # steps:
12
+ # - uses: actions/checkout@v3
13
+ # - uses: danger/danger@master
14
+ # env:
15
+ # DANGER_GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
16
+ # ```
17
+ #
18
+ class GitHubActions < CI
19
+ def self.validates_as_ci?(env)
20
+ env.key? "GITHUB_ACTION"
21
+ end
22
+
23
+ def self.validates_as_pr?(env)
24
+ value = env["GITHUB_EVENT_NAME"]
25
+ ["pull_request", "pull_request_target"].include?(value)
26
+ end
27
+
28
+ def supported_request_sources
29
+ @supported_request_sources ||= [Danger::RequestSources::GitHub]
30
+ end
31
+
32
+ def initialize(env)
33
+ self.repo_slug = env["GITHUB_REPOSITORY"]
34
+ pull_request_event = JSON.parse(File.read(env["GITHUB_EVENT_PATH"]))
35
+ self.pull_request_id = pull_request_event["number"]
36
+ self.repo_url = pull_request_event["repository"]["clone_url"]
37
+
38
+ # if environment variable DANGER_GITHUB_API_TOKEN is not set, use env GITHUB_TOKEN
39
+ if (env.key? "GITHUB_ACTION") && (!env.key? "DANGER_GITHUB_API_TOKEN")
40
+ env["DANGER_GITHUB_API_TOKEN"] = env["GITHUB_TOKEN"]
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,89 @@
1
+ # http://docs.gitlab.com/ce/ci/variables/README.html
2
+ require "uri"
3
+ require "danger/request_sources/github/github"
4
+ require "danger/request_sources/gitlab"
5
+
6
+ module Danger
7
+ # ### CI Setup
8
+ #
9
+ # Install dependencies and add a danger step to your .gitlab-ci.yml:
10
+ #
11
+ # ```yml
12
+ # before_script:
13
+ # - bundle install
14
+ # danger:
15
+ # script:
16
+ # - bundle exec danger
17
+ # ```
18
+ #
19
+ # ### Token Setup
20
+ #
21
+ # Add the `DANGER_GITLAB_API_TOKEN` to your pipeline env variables if you
22
+ # are hosting your code on GitLab. If you are using GitLab as a mirror
23
+ # for the purpose of CI/CD, while hosting your repo on GitHub, set the
24
+ # `DANGER_GITHUB_API_TOKEN` as well as the project repo URL to
25
+ # `DANGER_PROJECT_REPO_URL`.
26
+
27
+ class GitLabCI < CI
28
+ def self.validates_as_ci?(env)
29
+ env.key? "GITLAB_CI"
30
+ end
31
+
32
+ def self.validates_as_pr?(env)
33
+ exists = [
34
+ "GITLAB_CI", "CI_PROJECT_PATH"
35
+ ].all? { |x| env[x] }
36
+
37
+ exists && determine_pull_or_merge_request_id(env).to_i > 0
38
+ end
39
+
40
+ def self.determine_pull_or_merge_request_id(env)
41
+ return env["CI_MERGE_REQUEST_IID"] if env["CI_MERGE_REQUEST_IID"]
42
+ return env["CI_EXTERNAL_PULL_REQUEST_IID"] if env["CI_EXTERNAL_PULL_REQUEST_IID"]
43
+ return 0 unless env["CI_COMMIT_SHA"]
44
+
45
+ project_path = env["CI_MERGE_REQUEST_PROJECT_PATH"] || env["CI_PROJECT_PATH"]
46
+ base_commit = env["CI_COMMIT_SHA"]
47
+ client = RequestSources::GitLab.new(nil, env).client
48
+
49
+ client_version = Gem::Version.new(client.version.version)
50
+ if client_version >= Gem::Version.new("10.7")
51
+ # Use the 'list merge requests associated with a commit' API, for speed
52
+ # (GET /projects/:id/repository/commits/:sha/merge_requests) available for GitLab >= 10.7
53
+ merge_request = client.commit_merge_requests(project_path, base_commit, state: :opened).first
54
+ if client_version >= Gem::Version.new("13.8")
55
+ # Gitlab 13.8.0 started returning merge requests for merge commits and squashed commits
56
+ # By checking for merge_request.state, we can ensure danger only comments on MRs which are open
57
+ return 0 if merge_request.nil?
58
+ return 0 unless merge_request.state == "opened"
59
+ end
60
+ else
61
+ merge_requests = client.merge_requests(project_path, state: :opened)
62
+ merge_request = merge_requests.auto_paginate.find do |mr|
63
+ mr.sha == base_commit
64
+ end
65
+ end
66
+ merge_request.nil? ? 0 : merge_request.iid
67
+ end
68
+
69
+ def self.slug_from(env)
70
+ if env["DANGER_PROJECT_REPO_URL"]
71
+ env["DANGER_PROJECT_REPO_URL"].split("/").last(2).join("/")
72
+ else
73
+ env["CI_MERGE_REQUEST_PROJECT_PATH"] || env["CI_PROJECT_PATH"]
74
+ end
75
+ end
76
+
77
+ def initialize(env)
78
+ self.repo_slug = self.class.slug_from(env)
79
+ self.pull_request_id = self.class.determine_pull_or_merge_request_id(env)
80
+ end
81
+
82
+ def supported_request_sources
83
+ @supported_request_sources ||= [
84
+ Danger::RequestSources::GitHub,
85
+ Danger::RequestSources::GitLab
86
+ ]
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,148 @@
1
+ # https://wiki.jenkins-ci.org/display/JENKINS/Building+a+software+project#Buildingasoftwareproject-JenkinsSetEnvironmentVariables
2
+ # https://wiki.jenkins-ci.org/display/JENKINS/GitHub+pull+request+builder+plugin
3
+ require "danger/request_sources/github/github"
4
+ require "danger/request_sources/gitlab"
5
+ require "danger/request_sources/bitbucket_server"
6
+ require "danger/request_sources/bitbucket_cloud"
7
+
8
+ module Danger
9
+ # https://jenkins-ci.org
10
+
11
+ # ### CI Setup
12
+ # Ah Jenkins, so many memories. So, if you're using Jenkins, you're hosting your own environment.
13
+ #
14
+ # #### GitHub
15
+ # You will want to be using the [GitHub pull request builder plugin](https://wiki.jenkins-ci.org/display/JENKINS/GitHub+pull+request+builder+plugin)
16
+ # in order to ensure that you have the build environment set up for PR integration.
17
+ #
18
+ # With that set up, you can edit your job to add `bundle exec danger` at the build action.
19
+ #
20
+ # ##### Pipeline
21
+ # If your're using [pipelines](https://jenkins.io/solutions/pipeline/) you should be using the [GitHub branch source plugin](https://wiki.jenkins-ci.org/display/JENKINS/GitHub+Branch+Source+Plugin)
22
+ # for easy setup and handling of PRs.
23
+ #
24
+ # After you've set up the plugin, add a `sh 'bundle exec danger'` line in your pipeline script and make sure that build PRs is enabled.
25
+ #
26
+ # #### GitLab
27
+ # You will want to be using the [GitLab Plugin](https://github.com/jenkinsci/gitlab-plugin)
28
+ # in order to ensure that you have the build environment set up for MR integration.
29
+ #
30
+ # With that set up, you can edit your job to add `bundle exec danger` at the build action.
31
+ #
32
+ # #### General
33
+ #
34
+ # People occasionally see issues with Danger not classing your CI runs as a PR, to give you visibility
35
+ # the Jenkins side of Danger expects to see one of these env vars:
36
+ # - ghprbPullId
37
+ # - CHANGE_ID
38
+ # - gitlabMergeRequestIid
39
+ # - gitlabMergeRequestId
40
+ #
41
+ # ### Token Setup
42
+ #
43
+ # #### GitHub
44
+ # As you own the machine, it's up to you to add the environment variable for the `DANGER_GITHUB_API_TOKEN`.
45
+ #
46
+ # #### GitLab
47
+ # As you own the machine, it's up to you to add the environment variable for the `DANGER_GITLAB_API_TOKEN`.
48
+ #
49
+ class Jenkins < CI
50
+ attr_accessor :project_url
51
+
52
+ class EnvNotFound < StandardError
53
+ def initialize
54
+ super("ENV not found, please check your Jenkins. Related: https://stackoverflow.com/search?q=jenkins+env+null")
55
+ end
56
+ end
57
+
58
+ def self.validates_as_ci?(env)
59
+ env.key? "JENKINS_URL"
60
+ end
61
+
62
+ def self.validates_as_pr?(env)
63
+ id = pull_request_id(env)
64
+ !id.nil? && !id.empty? && !!id.match(/^\d+$/)
65
+ end
66
+
67
+ def supported_request_sources
68
+ @supported_request_sources ||= [
69
+ Danger::RequestSources::GitHub,
70
+ Danger::RequestSources::GitLab,
71
+ Danger::RequestSources::BitbucketServer,
72
+ Danger::RequestSources::BitbucketCloud
73
+ ]
74
+ end
75
+
76
+ def initialize(env)
77
+ raise EnvNotFound.new if env.nil? || env.empty?
78
+
79
+ self.repo_url = self.class.repo_url(env)
80
+ self.pull_request_id = self.class.pull_request_id(env)
81
+ self.repo_slug = self.class.repo_slug(self.repo_url)
82
+ self.project_url = env["CI_MERGE_REQUEST_PROJECT_URL"] || env["CI_PROJECT_URL"]
83
+ end
84
+
85
+ def self.repo_slug(repo_url)
86
+ slug = self.slug_ssh(repo_url)
87
+ slug ||= self.slug_http(repo_url)
88
+ slug ||= self.slug_bitbucket(repo_url)
89
+ slug ||= self.slug_fallback(repo_url)
90
+ return slug.gsub(/\.git$/, "") unless slug.nil?
91
+ end
92
+
93
+ def self.slug_bitbucket(repo_url)
94
+ repo_matches = repo_url.match(%r{(?:[/:])projects/([^/.]+)/repos/([^/.]+)})
95
+ return "#{repo_matches[1]}/#{repo_matches[2]}" if repo_matches
96
+ end
97
+
98
+ def self.slug_ssh(repo_url)
99
+ repo_matches = repo_url.match(/^git@.+:(.+)/)
100
+ return repo_matches[1] if repo_matches
101
+ end
102
+
103
+ def self.slug_http(repo_url)
104
+ repo_matches = repo_url.match(%r{^https?.+(?>\.\w*\d*/)(.+.git$)})
105
+ return repo_matches[1] if repo_matches
106
+ end
107
+
108
+ def self.slug_fallback(repo_url)
109
+ repo_matches = repo_url.match(%r{([/:])([^/]+/[^/]+)$})
110
+ return repo_matches[2]
111
+ end
112
+
113
+ def self.pull_request_id(env)
114
+ if env["ghprbPullId"]
115
+ env["ghprbPullId"]
116
+ elsif env["CHANGE_ID"]
117
+ env["CHANGE_ID"]
118
+ elsif env["gitlabMergeRequestIid"]
119
+ env["gitlabMergeRequestIid"]
120
+ else
121
+ env["gitlabMergeRequestId"]
122
+ end
123
+ end
124
+
125
+ def self.repo_url(env)
126
+ if env["GIT_URL_1"]
127
+ env["GIT_URL_1"]
128
+ elsif env["CHANGE_URL"]
129
+ change_url = env["CHANGE_URL"]
130
+ case change_url
131
+ when %r{/pull/} # GitHub
132
+ matches = change_url.match(%r{(.+)/pull/[0-9]+})
133
+ matches[1] unless matches.nil?
134
+ when %r{/merge_requests/} # GitLab
135
+ matches = change_url.match(%r{(.+?)(/-)?/merge_requests/[0-9]+})
136
+ matches[1] unless matches.nil?
137
+ when %r{/pull-requests/} # Bitbucket
138
+ matches = change_url.match(%r{(.+)/pull-requests/[0-9]+})
139
+ matches[1] unless matches.nil?
140
+ else
141
+ change_url
142
+ end
143
+ else
144
+ env["GIT_URL"]
145
+ end
146
+ end
147
+ end
148
+ end
@@ -0,0 +1,117 @@
1
+ # frozen_string_literal: true
2
+
3
+ # For more info see: https://github.com/schacon/ruby-git
4
+
5
+ require "git"
6
+ require "uri"
7
+
8
+ require "danger/request_sources/github/github"
9
+
10
+ require "danger/ci_source/support/find_repo_info_from_url"
11
+ require "danger/ci_source/support/find_repo_info_from_logs"
12
+ require "danger/ci_source/support/no_repo_info"
13
+ require "danger/ci_source/support/pull_request_finder"
14
+ require "danger/ci_source/support/commits"
15
+
16
+ module Danger
17
+ # ignore
18
+ class LocalGitRepo < CI
19
+ attr_accessor :base_commit, :head_commit
20
+
21
+ def self.validates_as_ci?(env)
22
+ env.key? "DANGER_USE_LOCAL_GIT"
23
+ end
24
+
25
+ def self.validates_as_pr?(_env)
26
+ false
27
+ end
28
+
29
+ def git
30
+ @git ||= GitRepo.new
31
+ end
32
+
33
+ def run_git(command)
34
+ git.exec(command).encode("UTF-8", "binary", invalid: :replace, undef: :replace, replace: "")
35
+ end
36
+
37
+ def supported_request_sources
38
+ @supported_request_sources ||= [
39
+ Danger::RequestSources::GitHub,
40
+ Danger::RequestSources::BitbucketServer,
41
+ Danger::RequestSources::BitbucketCloud,
42
+ Danger::RequestSources::VSTS
43
+ ]
44
+ end
45
+
46
+ def initialize(env = {})
47
+ @remote_info = find_remote_info(env)
48
+ @found_pull_request = find_pull_request(env)
49
+ self.repo_slug = remote_info.slug
50
+ raise_error_for_missing_remote if remote_info.kind_of?(NoRepoInfo)
51
+
52
+ self.pull_request_id = found_pull_request.pull_request_id
53
+
54
+ if sha
55
+ self.base_commit = commits.base
56
+ self.head_commit = commits.head
57
+ else
58
+ self.base_commit = found_pull_request.base
59
+ self.head_commit = found_pull_request.head
60
+ end
61
+ end
62
+
63
+ private
64
+
65
+ attr_reader :remote_info, :found_pull_request
66
+
67
+ def raise_error_for_missing_remote
68
+ raise missing_remote_error_message
69
+ end
70
+
71
+ def missing_remote_error_message
72
+ "danger cannot find your git remote, please set a remote. " \
73
+ "And the repository must host on GitHub.com or GitHub Enterprise."
74
+ end
75
+
76
+ def find_remote_info(env)
77
+ if given_pull_request_url?(env)
78
+ FindRepoInfoFromURL.new(env["LOCAL_GIT_PR_URL"]).call
79
+ else
80
+ FindRepoInfoFromLogs.new(
81
+ env["DANGER_GITHUB_HOST"] || "github.com",
82
+ run_git("remote show origin -n")
83
+ ).call
84
+ end || NoRepoInfo.new
85
+ end
86
+
87
+ def find_pull_request(env)
88
+ if given_pull_request_url?(env)
89
+ PullRequestFinder.new(
90
+ remote_info.id,
91
+ remote_info.slug,
92
+ remote: true,
93
+ remote_url: env["LOCAL_GIT_PR_URL"]
94
+ ).call(env: env)
95
+ else
96
+ PullRequestFinder.new(
97
+ env.fetch("LOCAL_GIT_PR_ID") { "" },
98
+ remote_info.slug,
99
+ remote: false,
100
+ git_logs: run_git("log --oneline -1000000")
101
+ ).call(env: env)
102
+ end
103
+ end
104
+
105
+ def given_pull_request_url?(env)
106
+ env["LOCAL_GIT_PR_URL"] && !env["LOCAL_GIT_PR_URL"].empty?
107
+ end
108
+
109
+ def sha
110
+ @_sha ||= found_pull_request.sha
111
+ end
112
+
113
+ def commits
114
+ @_commits ||= Commits.new(run_git("rev-list --parents -n 1 #{sha}"))
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "git"
4
+ require "danger/request_sources/local_only"
5
+
6
+ module Danger
7
+ # ### CI Setup
8
+ #
9
+ # For setting up LocalOnlyGitRepo there is not much needed. Either `--base` and `--head` need to be specified or
10
+ # origin/master is expected for base and HEAD for head
11
+ #
12
+ class LocalOnlyGitRepo < CI
13
+ attr_accessor :base_commit, :head_commit
14
+
15
+ HEAD_VAR = "DANGER_LOCAL_HEAD"
16
+ BASE_VAR = "DANGER_LOCAL_BASE"
17
+
18
+ def self.validates_as_ci?(env)
19
+ env.key? "DANGER_USE_LOCAL_ONLY_GIT"
20
+ end
21
+
22
+ def self.validates_as_pr?(_env)
23
+ false
24
+ end
25
+
26
+ def git
27
+ @git ||= GitRepo.new
28
+ end
29
+
30
+ def run_git(command)
31
+ git.exec(command).encode("UTF-8", "binary", invalid: :replace, undef: :replace, replace: "")
32
+ end
33
+
34
+ def supported_request_sources
35
+ @supported_request_sources ||= [Danger::RequestSources::LocalOnly]
36
+ end
37
+
38
+ def initialize(env = {})
39
+ # expects --base/--head specified OR origin/master to be base and HEAD head
40
+ self.base_commit = env[BASE_VAR] || run_git("rev-parse --abbrev-ref origin/master")
41
+ self.head_commit = env[HEAD_VAR] || run_git("rev-parse --abbrev-ref HEAD")
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,48 @@
1
+ # http://screwdriver.cd
2
+ # https://docs.screwdriver.cd/user-guide/environment-variables
3
+ require "danger/request_sources/github/github"
4
+
5
+ module Danger
6
+ # ### CI Setup
7
+ #
8
+ # Install dependencies and add a danger step to your screwdriver.yaml:
9
+ #
10
+ # ```yml
11
+ # jobs:
12
+ # danger:
13
+ # requires: [~pr, ~commit]
14
+ # steps:
15
+ # - setup: bundle install --path vendor
16
+ # - danger: bundle exec danger
17
+ # secrets:
18
+ # - DANGER_GITHUB_API_TOKEN
19
+ # ```
20
+ #
21
+ # ### Token Setup
22
+ #
23
+ # Add the `DANGER_GITHUB_API_TOKEN` to your pipeline env as a
24
+ # [build secret](https://docs.screwdriver.cd/user-guide/configuration/secrets)
25
+ #
26
+ class Screwdriver < CI
27
+ def self.validates_as_ci?(env)
28
+ env.key? "SCREWDRIVER"
29
+ end
30
+
31
+ def self.validates_as_pr?(env)
32
+ exists = ["SD_PULL_REQUEST", "SCM_URL"].all? { |x| env[x] && !env[x].empty? }
33
+ exists && env["SD_PULL_REQUEST"].to_i > 0
34
+ end
35
+
36
+ def supported_request_sources
37
+ @supported_request_sources ||= [Danger::RequestSources::GitHub]
38
+ end
39
+
40
+ def initialize(env)
41
+ self.repo_slug = env["SCM_URL"].split(":").last.gsub(".git", "").split("#", 2).first
42
+ self.repo_url = env["SCM_URL"].split("#", 2).first
43
+ if env["SD_PULL_REQUEST"].to_i > 0
44
+ self.pull_request_id = env["SD_PULL_REQUEST"]
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,37 @@
1
+ # https://docs.semaphoreci.com/article/12-environment-variables
2
+ require "danger/request_sources/github/github"
3
+
4
+ module Danger
5
+ # ### CI Setup
6
+ #
7
+ # For Semaphore you will want to go to the settings page of the project. Inside "Build Settings"
8
+ # you should add `bundle exec danger` to the Setup thread. Note that Semaphore only provides
9
+ # the build environment variables necessary for Danger on PRs across forks.
10
+ #
11
+ # ### Token Setup
12
+ #
13
+ # You can add your `DANGER_GITHUB_API_TOKEN` inside the "Environment Variables" section in the settings.
14
+ #
15
+ class Semaphore < CI
16
+ def self.validates_as_ci?(env)
17
+ env.key? "SEMAPHORE"
18
+ end
19
+
20
+ def self.validates_as_pr?(env)
21
+ one = ["SEMAPHORE_REPO_SLUG", "PULL_REQUEST_NUMBER"].all? { |x| env[x] && !env[x].empty? }
22
+ two = ["SEMAPHORE_GIT_REPO_SLUG", "SEMAPHORE_GIT_PR_NUMBER"].all? { |x| env[x] && !env[x].empty? }
23
+
24
+ one || two
25
+ end
26
+
27
+ def supported_request_sources
28
+ @supported_request_sources ||= [Danger::RequestSources::GitHub]
29
+ end
30
+
31
+ def initialize(env)
32
+ self.repo_slug = env["SEMAPHORE_GIT_REPO_SLUG"] || env["SEMAPHORE_REPO_SLUG"]
33
+ self.pull_request_id = env["SEMAPHORE_GIT_PR_NUMBER"] || env["PULL_REQUEST_NUMBER"]
34
+ self.repo_url = env["SEMAPHORE_GIT_URL"] || GitRepo.new.origins
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,19 @@
1
+ module Danger
2
+ class Commits
3
+ def initialize(base_head)
4
+ @base_head = base_head.strip.split(" ".freeze)
5
+ end
6
+
7
+ def base
8
+ base_head.first
9
+ end
10
+
11
+ def head
12
+ base_head.last
13
+ end
14
+
15
+ private
16
+
17
+ attr_reader :base_head
18
+ end
19
+ end
@@ -0,0 +1,35 @@
1
+ require "danger/ci_source/support/repo_info"
2
+
3
+ module Danger
4
+ class FindRepoInfoFromLogs
5
+ def initialize(github_host, remote_logs)
6
+ @github_host = github_host
7
+ @remote_logs = remote_logs
8
+ end
9
+
10
+ def call
11
+ matched = remote.match(regexp)
12
+
13
+ if matched
14
+ RepoInfo.new(matched["repo_slug"], nil)
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ attr_reader :remote_logs, :github_host
21
+
22
+ def remote
23
+ remote_logs.lines.grep(/Fetch URL/)[0].split(": ".freeze, 2)[1]
24
+ end
25
+
26
+ def regexp
27
+ %r{
28
+ #{Regexp.escape(github_host)}
29
+ (:|/|(:/))
30
+ (?<repo_slug>[^/]+/.+?)
31
+ (?:\.git)?$
32
+ }x
33
+ end
34
+ end
35
+ end