module Jenkins::Bundle::Update::Pr

Constants

VERSION

Public Class Methods

create_if_needed(git_username: nil, git_email: nil) click to toggle source
# File lib/jenkins/bundle/update/pr.rb, line 73
def self.create_if_needed(git_username: nil, git_email: nil)
  raise "$GITHUB_PROJECT_USERNAME isn't set" unless ENV['GITHUB_PROJECT_USERNAME']
  raise "$GITHUB_PROJECT_REPONAME isn't set" unless ENV['GITHUB_PROJECT_REPONAME']
  raise "$GITHUB_ACCESS_TOKEN isn't set" unless ENV['GITHUB_ACCESS_TOKEN']

  return unless need?

  now = Time.now

  git_username ||= client.user.login
  git_email    ||= "#{git_username}@users.noreply.github.com"

  repo_full_name = "#{ENV['GITHUB_PROJECT_USERNAME']}/#{ENV['GITHUB_PROJECT_REPONAME']}"
  branch         = "bundle-update-#{now.strftime('%Y%m%d%H%M%S')}"

  create_branch(git_username, git_email, branch)
  pull_request = create_pull_request(repo_full_name, branch, now)
  add_comment_of_compare_linker(repo_full_name, pull_request[:number])
end

Private Class Methods

add_comment_of_compare_linker(repo_full_name, pr_number) click to toggle source
# File lib/jenkins/bundle/update/pr.rb, line 117
        def self.add_comment_of_compare_linker(repo_full_name, pr_number)
          ENV['OCTOKIT_ACCESS_TOKEN'] = ENV['GITHUB_ACCESS_TOKEN']
          compare_linker = CompareLinker.new(repo_full_name, pr_number)
          compare_linker.formatter = CompareLinker::Formatter::Markdown.new

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

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

          compare_linker.add_comment(repo_full_name, pr_number, comment)
        end
client() click to toggle source
# File lib/jenkins/bundle/update/pr.rb, line 132
def self.client
  Octokit::Client.new(access_token: ENV['GITHUB_ACCESS_TOKEN'])
end
create_branch(git_username, git_email, branch) click to toggle source
# File lib/jenkins/bundle/update/pr.rb, line 99
def self.create_branch(git_username, git_email, branch)
  system("git config user.name #{git_username}")
  system("git config user.email #{git_email}")
  system("git checkout -b #{branch}")
  system('git add Gemfile.lock') if File.exist?('Gemfile.lock')
  system('git add config/Gemfile.lock') if File.exist?('config/Gemfile.lock')
  system("git commit -m 'bundle update'")
  system("git push origin #{branch}")
end
create_pull_request(repo_full_name, branch, now) click to toggle source
# File lib/jenkins/bundle/update/pr.rb, line 110
def self.create_pull_request(repo_full_name, branch, now)
  title = "bundle update at #{now.strftime('%Y-%m-%d %H:%M:%S %Z')}"
  body  = "auto generated by [Jenkins of #{ENV['JOB_NAME']}](#{ENV['BUILD_URL']})"
  client.create_pull_request(repo_full_name, 'master', branch, title, body)
end
need?() click to toggle source
# File lib/jenkins/bundle/update/pr.rb, line 93
def self.need?
  system('bundle update') || raise
  `git status -s 2> /dev/null`.include?('Gemfile.lock')
end