-
Notifications
You must be signed in to change notification settings - Fork 475
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from danger/local_test
Support a `danger local` command
- Loading branch information
Showing
11 changed files
with
155 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# For more info see: https://github.com/schacon/ruby-git | ||
|
||
require 'git' | ||
require 'uri' | ||
|
||
module Danger | ||
module CISource | ||
class LocalGitRepo < CI | ||
attr_accessor :base_commit, :head_commit | ||
|
||
def self.validates?(env) | ||
return !env["DANGER_USE_LOCAL_GIT"].nil? | ||
end | ||
|
||
def initialize(*) | ||
git = Git.open(".") | ||
if git.remote("origin") | ||
url = git.remote("origin").url | ||
# deal with https:// | ||
if url.start_with? "https://github.com/" | ||
self.repo_slug = url.gsub("https://github.com/", "").gsub(".git", '') | ||
|
||
# deal with SSH origin | ||
elsif url.start_with? "git@github.com:" | ||
self.repo_slug = url.gsub("git@github.com:", "").gsub(".git", '') | ||
end | ||
end | ||
|
||
logs = git.log.since('2 weeks ago') | ||
# Look for something like | ||
# "Merge pull request #38 from KrauseFx/funky_circles\n\nAdd support for GitHub compare URLs that don't conform | ||
pr_merge = logs.detect { |log| (/Merge pull request #[0-9]* from/ =~ log.message) == 0 } | ||
if pr_merge | ||
# then pull out the 38, to_i | ||
self.pull_request_id = pr_merge.message.gsub("Merge pull request #", "").to_i | ||
self.base_commit = pr_merge.parents[0].sha | ||
self.head_commit = pr_merge.parents[1].sha | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
module Danger | ||
class Local < Runner | ||
self.description = 'Run the Dangerfile locally.' | ||
self.command = 'local' | ||
|
||
def initialize(argv) | ||
@dangerfile_path = "Dangerfile" if File.exist? "Dangerfile" | ||
super | ||
end | ||
|
||
def validate! | ||
super | ||
unless @dangerfile_path | ||
help! "Could not find a Dangerfile." | ||
end | ||
end | ||
|
||
def run | ||
ENV["DANGER_USE_LOCAL_GIT"] = "YES" | ||
|
||
dm = Dangerfile.new | ||
dm.env = EnvironmentManager.new(ENV) | ||
|
||
source = dm.env.ci_source | ||
unless source.repo_slug | ||
puts "danger local".red " failed because it only works with GitHub projects at the moment. Sorry." | ||
exit 0 | ||
end | ||
|
||
puts "Running your Dangerfile against this PR - https://github.com/#{source.repo_slug}/pulls/#{source.pull_request_id}" | ||
|
||
if verbose != true | ||
puts "Turning on --verbose" | ||
dm.verbose = true | ||
end | ||
|
||
puts "" | ||
|
||
gh = GitHub.new(dm.env.ci_source, ENV) | ||
# We can use tokenless here, as it's running on someone's computer | ||
# and is IP locked, as opposed to on the CI. | ||
gh.support_tokenless_auth = true | ||
gh.fetch_details | ||
|
||
dm.env.request_source = gh | ||
|
||
dm.env.scm = GitRepo.new | ||
dm.env.scm.diff_for_folder(".", dm.env.ci_source.base_commit, dm.env.ci_source.head_commit) | ||
dm.parse Pathname.new(@dangerfile_path) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters