Skip to content

Warn when deployment pull request includes database migration #5

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

Merged
merged 2 commits into from
Jun 27, 2018
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
42 changes: 25 additions & 17 deletions lib/ruboty/github/actions/create_deploy_pull_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 7 additions & 3 deletions lib/ruboty/github/actions/create_pull_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ module Actions
class CreatePullRequest < Base
def call
if has_access_token?
create
create_with_error_handling
else
require_access_token
end
end

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
Expand All @@ -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
Expand Down