module WerckerBundleUpdatePr

An automation script to `bundle update` and send pull request via Wercker's Trigger Build API

Constants

VERSION

Public Class Methods

run(username: nil, email: nil, branches: ['master']) click to toggle source

Main process @param [String] username username for Git commitment @param [String] email email for Git commitment @param [Array] branches list of target branches

# File lib/wercker_bundle_update_pr.rb, line 11
def self.run(username: nil, email: nil, branches: ['master'])
  raise '$WERCKER_GIT_OWNER not set' unless ENV['WERCKER_GIT_OWNER']
  raise '$WERCKER_GIT_REPOSITORY not set' unless ENV['WERCKER_GIT_REPOSITORY']
  raise '$GITHUB_ACCESS_TOKEN not set' unless ENV['GITHUB_ACCESS_TOKEN']

  username ||= client.user.login
  email ||= "#{username}@users.noreply.github.com"

  return unless bundle_update?(branches)

  repository = "#{ENV['WERCKER_GIT_OWNER']}/#{ENV['WERCKER_GIT_REPOSITORY']}"
  now = Time.now
  branch = "bundle-update-#{now.strftime('%Y%m%d%H%M%S')}"

  create_branch(username, email, branch)
  pull_request = create_pull_request(repository, branch, now)
  add_compare_linker_comment(repository, pull_request[:number])
end

Private Class Methods

add_compare_linker_comment(repository, pr_number) click to toggle source
# File lib/wercker_bundle_update_pr.rb, line 55
  def self.add_compare_linker_comment(repository, pr_number)
    ENV["OCTOKIT_ACCESS_TOKEN"] = ENV["GITHUB_ACCESS_TOKEN"]
    compare_linker = CompareLinker.new(repository, pr_number)
    compare_linker.formatter = CompareLinker::Formatter::Markdown.new

    comment = <<-EOC
#{compare_linker.make_compare_links.to_a.join("\n")}

Powered by [compare_linker](https://rubygems.org/gems/compare_linker)
          EOC

    compare_linker.add_comment(repository, pr_number, comment)
  end
bundle_update?(branches) click to toggle source
# File lib/wercker_bundle_update_pr.rb, line 30
def self.bundle_update?(branches)
  return false unless branches.include?(ENV['WERCKER_GIT_BRANCH'])

  raise "Failed to run `bundle update`" unless system("bundle update")
  `git status -sb 2> /dev/null`.include?("Gemfile.lock")
end
client() click to toggle source
# File lib/wercker_bundle_update_pr.rb, line 70
def self.client
  Octokit::Client.new(access_token: ENV["GITHUB_ACCESS_TOKEN"])
end
create_branch(username, email, branch) click to toggle source
# File lib/wercker_bundle_update_pr.rb, line 38
def self.create_branch(username, email, branch)
  `git config user.name #{username}`
  `git config user.email #{email}`
  `git checkout -b #{branch}`
  `git add Gemfile.lock`
  `git commit -m'bundle update'`
  `git push origin #{branch}`
end
create_pull_request(repository, branch, now) click to toggle source
# File lib/wercker_bundle_update_pr.rb, line 48
def self.create_pull_request(repository, branch, now)
  title = "bundle update at #{now.strftime('%Y-%m-%d %H:%M:%S %Z')}"
  body = "Auto generated by [Wercker](#{ENV['WERCKER_APPLICATION_URL']})"
  client.create_pull_request(repository, ENV['WERCKER_GIT_BRANCH'], branch, title, body)
end