diff --git a/lib/ruboty/github/actions/create_deploy_pull_request.rb b/lib/ruboty/github/actions/create_deploy_pull_request.rb index 78025c4..2e205bd 100644 --- a/lib/ruboty/github/actions/create_deploy_pull_request.rb +++ b/lib/ruboty/github/actions/create_deploy_pull_request.rb @@ -4,37 +4,45 @@ module Actions class CreateDeployPullRequest < CreatePullRequest private + def create + super + + if database_schema_changed? + message.reply("@here :warning: This deployment includes some database migrations") + end + end + # e.g. master def to_branch to.split(":").last end def body - body = "## Pull Requests to deploy\n\n" - pull_requests_to_deploy(repository, to_branch, from_branch).each do |pr| - body = body + [ - "-", - "[##{pr[:number]}](#{pr[:html_url]}):", - pr[:title], - "by", - "@#{pr[:user][:login]}\n", - ].join(' ') + lines = included_pull_requests.map do |pr| + "- [##{pr[:number]}](#{pr[:html_url]}): #{pr[:title]} by @#{pr[:user][:login]}" end - body + lines.unshift('## Pull Requests to deploy', '') + lines.join("\n") end - def pull_requests_to_deploy(repository, to_branch, from_branch) - numbers = compare(repository, to_branch, from_branch).commits.map do |commit| + def included_pull_requests + numbers = comparison.commits.map do |commit| /^Merge pull request #(\d+) from/ =~ commit[:commit][:message] $1 end - numbers.compact.map { |n| client.pull_request(repository, n.to_i) } + numbers.compact.map { |number| client.pull_request(repository, number.to_i) } end - def compare(repository, to_branch, from_branch) - start = client.branch(repository, to_branch)[:commit][:sha] - endd = client.branch(repository, from_branch)[:commit][:sha] - client.compare(repository, start, endd) + def database_schema_changed? + comparison.files.any? { |file| file.filename == 'db/schema.rb' } + end + + def comparison + @comparison ||= begin + start = client.branch(repository, to_branch)[:commit][:sha] + endd = client.branch(repository, from_branch)[:commit][:sha] + client.compare(repository, start, endd) + end end end end diff --git a/lib/ruboty/github/actions/create_pull_request.rb b/lib/ruboty/github/actions/create_pull_request.rb index 04bb06a..65aadcd 100644 --- a/lib/ruboty/github/actions/create_pull_request.rb +++ b/lib/ruboty/github/actions/create_pull_request.rb @@ -4,7 +4,7 @@ module Actions class CreatePullRequest < Base def call if has_access_token? - create + create_with_error_handling else require_access_token end @@ -12,8 +12,8 @@ def call private - def create - message.reply("Created #{pull_request.html_url}") + def create_with_error_handling + create rescue Octokit::Unauthorized message.reply("Failed in authentication (401)") rescue Octokit::NotFound @@ -22,6 +22,10 @@ def create message.reply("Failed by #{exception.class} #{exception}") end + def create + message.reply("Created #{pull_request.html_url}") + end + def pull_request client.create_pull_request(repository, base, head, title, body) end