Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error on unexpected commit message #67

Merged
merged 1 commit into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/change_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ def internal?
end
end

class UnexpectedCommitMessage < StandardError; end

Change = Struct.new(:dependency, :type) do
def self.type_from_dependabot_type(dependabot_type)
case dependabot_type
Expand All @@ -21,7 +23,7 @@ def self.type_from_dependabot_type(dependabot_type)
else
# As of March 2024, these are the only options Dependabot can return
# If they add more in the future, we will need to update this
raise "Unrecognised update-type: #{dependabot_type}"
raise(UnexpectedCommitMessage, "Unrecognised update-type: #{dependabot_type}")
end
end
end
Expand All @@ -47,6 +49,6 @@ def self.from_commit_message(commit_message)
}
.then { |changes| ChangeSet.new changes }
rescue StandardError
raise "Commit message is not in the expected format"
raise(UnexpectedCommitMessage, "Commit message is not in the expected format")
end
end
2 changes: 2 additions & 0 deletions lib/policy_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ def reasons_not_to_merge(pull_request)
end

reasons_not_to_merge
rescue UnexpectedCommitMessage => e
[e.message]
end

def change_allowed?(dependency_name, change_type)
Expand Down
6 changes: 3 additions & 3 deletions spec/lib/change_set_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
expect(Change.type_from_dependabot_type("version-update:semver-major")).to eq(:major)
expect(Change.type_from_dependabot_type("version-update:semver-minor")).to eq(:minor)
expect(Change.type_from_dependabot_type("version-update:semver-patch")).to eq(:patch)
expect { Change.type_from_dependabot_type("foo") }.to raise_error(RuntimeError, "Unrecognised update-type: foo")
expect { Change.type_from_dependabot_type("foo") }.to raise_error(UnexpectedCommitMessage, "Unrecognised update-type: foo")
end
end
end
Expand Down Expand Up @@ -124,11 +124,11 @@ def multiple_dependencies_commit
it "raises an error if the commit message is not in the expected format" do
expect {
ChangeSet.from_commit_message("Hello world!")
}.to raise_error(RuntimeError, "Commit message is not in the expected format")
}.to raise_error(UnexpectedCommitMessage, "Commit message is not in the expected format")

expect {
ChangeSet.from_commit_message("foo\n---\nsyntax: error:\n...\nbar")
}.to raise_error(RuntimeError, "Commit message is not in the expected format")
}.to raise_error(UnexpectedCommitMessage, "Commit message is not in the expected format")
end
end
end
17 changes: 17 additions & 0 deletions spec/lib/policy_manager_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,23 @@
])
end

it "should return reasons not to merge when commit message is not in the expected format" do
mock_pr = instance_double("PullRequest")
allow(mock_pr).to receive(:commit_message).and_return(
<<~COMMIT_MESSAGE,
---
updated-dependencies:
- dependency-name: #{external_dependency}
dependency-type: direct:production
COMMIT_MESSAGE
)

expect(PolicyManager.new(remote_config).is_auto_mergeable?(mock_pr)).to eq(false)
expect(PolicyManager.new(remote_config).reasons_not_to_merge(mock_pr)).to eq([
"Commit message is not in the expected format",
])
end

it "should return empty array if nothing wrong" do
mock_pr = instance_double("PullRequest")
allow(mock_pr).to receive(:commit_message).and_return(
Expand Down
Loading