Skip to content
Permalink
Browse files

Include semantic commit message prefix only if repo uses them

  • Loading branch information
greysteil committed Dec 8, 2017
1 parent 74926ed commit 77bd3b823d740c65d3a89f1e7027a3ca4fbf09e9
@@ -6,6 +6,7 @@
module Dependabot
class PullRequestCreator
class MessageBuilder
SEMANTIC_PREFIXES = %w(chore docs feat fix refactor style test).freeze
attr_reader :repo_name, :dependencies, :files, :github_client,
:pr_message_footer

@@ -19,7 +20,9 @@ def initialize(repo_name:, dependencies:, files:, github_client:,
end

def commit_message
"chore(dependencies): " + pr_name + "\n\n" + pr_message_without_footer
message = ""
message += "chore(dependencies): " if using_semantic_commit_messages?
message + pr_name + "\n\n" + pr_message_without_footer
end

def pr_name
@@ -34,6 +37,7 @@ def pr_name
names = dependencies.map(&:name)
"Bump #{names[0..-2].join(', ')} and #{names[-1]}"
end

return base if files.first.directory == "/"

base + " in #{files.first.directory}"
@@ -46,6 +50,24 @@ def pr_message

private

def using_semantic_commit_messages?
return false if recent_commit_messages.none?

semantic_messages = recent_commit_messages.select do |message|
SEMANTIC_PREFIXES.any? { |pre| message.downcase.start_with?(pre) }
end

semantic_messages.count.to_f / recent_commit_messages.count > 0.3
end

def recent_commit_messages
@recent_messages ||=
github_client.commits(repo_name).
map(&:commit).
map(&:message).
compact
end

def pr_message_without_footer
return requirement_pr_message if library?
version_pr_message
@@ -57,6 +57,8 @@
to_return(status: 200,
body: fixture("github", "bump_repo.json"),
headers: json_header)
stub_request(:get, watched_repo_url + "/commits").
to_return(status: 200, body: "[]", headers: json_header)
stub_request(:get, "#{watched_repo_url}/git/refs/heads/#{branch_name}").
to_return(status: 404,
body: fixture("github", "not_found.json"),
@@ -200,7 +202,7 @@
it "has the right commit message" do
creator.create

message = "chore(dependencies): Bump business from 1.4.0 to 1.5.0\n\n"\
message = "Bump business from 1.4.0 to 1.5.0\n\n"\
"Bumps [busines"

expect(WebMock).
@@ -701,7 +703,7 @@
it "has the right commit message" do
creator.create

message = "chore(dependencies): Update business requirement to >= 1"
message = "Update business requirement to >= 1"

expect(WebMock).
to have_requested(:post, "#{watched_repo_url}/git/commits").
@@ -916,6 +918,27 @@
end
end

context "with a repo using semantic commit messages" do
before do
stub_request(:get, watched_repo_url + "/commits").
to_return(status: 200,
body: fixture("github", "commits_semantic.json"),
headers: json_header)
end

it "includes a semantic commit message prefix" do
creator.create

expect(WebMock).
to have_requested(:post, "#{watched_repo_url}/git/commits").
with(body: {
parents: ["basecommitsha"],
tree: "cd8274d15fa3ae2ab983129fb037999f264ba9a7",
message: /\Achore\(dependencies\): Bump/
})
end
end

context "with a directory specified" do
let(:gemfile) do
Dependabot::DependencyFile.new(
@@ -960,8 +983,7 @@
it "includes the directory in the commit message" do
creator.create

message = "chore(dependencies): Bump business from 1.4.0 to 1.5.0 "\
"in /directory"
message = "Bump business from 1.4.0 to 1.5.0 in /directory"

expect(WebMock).
to have_requested(:post, "#{watched_repo_url}/git/commits").

0 comments on commit 77bd3b8

Please sign in to comment.
You can’t perform that action at this time.