Skip to content

Commit

Permalink
Merge pull request #1354 from rymai/1352-dont-post-error-as-comment
Browse files Browse the repository at this point in the history
Don't post Dangefile errors as comment on PR when the `DANGER_DO_NOT_POST_INVALID_DANGERFILE_ERROR` env var is set
  • Loading branch information
orta committed Feb 28, 2022
2 parents b220c40 + 12e0a49 commit 89923b3
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 31 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

## master
<!-- Your comment below here -->

* Don't post Dangefile errors as comment on PR when the `DANGER_DO_NOT_POST_INVALID_DANGERFILE_ERROR` env var is set - [@rymai](https://github.com/rymai) [#1354](https://github.com/danger/danger/pull/1354)
<!-- Your comment above here -->


Expand Down
5 changes: 4 additions & 1 deletion lib/danger/danger_core/dangerfile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ def run(base_branch, head_branch, dangerfile_path, danger_id, new_comment, remov
# Push results to the API
# Pass along the details of the run to the request source
# to send back to the code review site.
post_results(danger_id, new_comment, remove_previous_comments) unless danger_id.nil?
post_results(danger_id, new_comment, remove_previous_comments)

# Print results in the terminal
print_results
Expand Down Expand Up @@ -335,6 +335,9 @@ def wrap_text(text, width = 80)
end

def post_exception(ex, danger_id, new_comment)
return if ENV["DANGER_DO_NOT_POST_INVALID_DANGERFILE_ERROR"]
return if danger_id.nil?

env.request_source.update_pull_request!(
danger_id: danger_id,
new_comment: new_comment,
Expand Down
65 changes: 36 additions & 29 deletions spec/lib/danger/danger_core/dangerfile_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -303,56 +303,63 @@ def something

describe "#run" do
context "when exception occurred" do
before { allow(Danger).to receive(:danger_outdated?).and_return(false) }

it "updates PR with an error" do
path = Pathname.new(File.join("spec", "fixtures", "dangerfile_with_error"))
env_manager = double("Danger::EnvironmentManager", {
let(:dangerfile_fixture_filename) { "dangerfile_with_error" }
let(:dangerfile_path) { Pathname.new(File.join("spec", "fixtures", dangerfile_fixture_filename)) }
let(:env_manager) do
double("Danger::EnvironmentManager", {
pr?: false,
clean_up: true,
fill_environment_vars: true,
ensure_danger_branches_are_setup: false
})
scm = double("Danger::GitRepo", {
end
let(:scm) do
double("Danger::GitRepo", {
class: Danger::GitRepo,
diff_for_folder: true
})
request_source = double("Danger::RequestSources::GitHub")
dm = Danger::Dangerfile.new(env_manager, testing_ui)
end
let(:request_source) { double("Danger::RequestSources::GitHub") }
let(:dm) { Danger::Dangerfile.new(env_manager, testing_ui) }

before do
allow(Danger).to receive(:danger_outdated?).and_return(false)
allow(env_manager).to receive(:scm) { scm }
allow(env_manager).to receive(:request_source) { request_source }
end

it "does not updates PR with an error" do
expect(request_source).to receive(:update_pull_request!)

expect do
dm.run("custom_danger_base", "custom_danger_head", path, 1, false, false)
dm.run("custom_danger_base", "custom_danger_head", dangerfile_path, 1, false, false)
end.to raise_error(Danger::DSLError)
end

it "doesn't crash if path is reassigned" do
path = Pathname.new(File.join("spec", "fixtures", "dangerfile_with_error_and_path_reassignment"))
env_manager = double("Danger::EnvironmentManager", {
pr?: false,
clean_up: true,
fill_environment_vars: true,
ensure_danger_branches_are_setup: false
})
scm = double("Danger::GitRepo", {
class: Danger::GitRepo,
diff_for_folder: true
})
request_source = double("Danger::RequestSources::GitHub")
dm = Danger::Dangerfile.new(env_manager, testing_ui)
context "when path is reassigned" do
let(:dangerfile_fixture_filename) { "dangerfile_with_error_and_path_reassignment" }

allow(env_manager).to receive(:scm) { scm }
allow(env_manager).to receive(:request_source) { request_source }
it "doesn't crash" do
expect(request_source).to receive(:update_pull_request!)

expect(request_source).to receive(:update_pull_request!)
expect do
dm.run("custom_danger_base", "custom_danger_head", dangerfile_path, 1, false, false)
end.to raise_error(Danger::DSLError)
end
end

expect do
dm.run("custom_danger_base", "custom_danger_head", path, 1, false, false)
end.to raise_error(Danger::DSLError)
context "when ENV['DANGER_DO_NOT_POST_INVALID_DANGERFILE_ERROR'] is set" do
before do
allow(ENV).to receive(:[]).with("DANGER_DO_NOT_POST_INVALID_DANGERFILE_ERROR") { "" }
end

it "does not updates PR with an error" do
expect(request_source).not_to receive(:update_pull_request!)

expect do
dm.run("custom_danger_base", "custom_danger_head", dangerfile_path, 1, false, false)
end.to raise_error(Danger::DSLError)
end
end

after { allow(Danger).to receive(:danger_outdated?).and_call_original }
Expand Down

0 comments on commit 89923b3

Please sign in to comment.