class PrComet

Helps to create a pull request

Constants

VERSION

Attributes

base_branch[R]

rubocop:enable Metrics/AbcSize

git[R]

rubocop:enable Metrics/AbcSize

github[R]

rubocop:enable Metrics/AbcSize

initial_sha1[R]

rubocop:enable Metrics/AbcSize

topic_branch[R]

rubocop:enable Metrics/AbcSize

Public Class Methods

new(base:, branch:, user_name: nil, user_email: nil, verbose: false) click to toggle source

@note You have to set ENV @param base [String] The branch you want your changes pulled into @param branch [String] The branch where your changes are going to implement. @param user_name [String] The username to use for committer and author @param user_email [String] The email to use for committer and author @param verbose [Boolean] Displays executing commands

# File lib/pr_comet.rb, line 20
def initialize(base:, branch:, user_name: nil, user_email: nil, verbose: false)
  raise "You have to set ENV['GITHUB_ACCESS_TOKEN']" if access_token.nil?

  @base_branch = base
  @topic_branch = branch
  @git = Git::Command.new(user_name: user_name, user_email: user_email, verbose: verbose)
  @github = Github::Client.new(access_token, git.remote_url('origin'))
  @initial_sha1 = git.current_sha1
end

Public Instance Methods

commit(message, &block) click to toggle source

Add and commit local files to this branch

@param message [String] The commit message @yield Some commands where modify local files @return [Object] Return result of yield if you use &block @raise [RubocopChallenger::Errors::ExistUncommittedModify]

Raise error if you use &block and exists someuncommitted files
# File lib/pr_comet.rb, line 37
def commit(message, &block)
  git.checkout_with(topic_branch) unless git.current_branch?(topic_branch)
  result = modify_files(&block) if block_given?
  git.add('.')
  git.commit(message)
  result
end
create!(**options) click to toggle source

Create a pull request. You should call commit before calling this method. If you want to create a blank PR, you can do it with `validate: false` option.

@param options [Hash]

Options for the PR creation.

@option title [String]

The title for the pull request

@option body [String]

The body for the pull request

@option labels [Array<String>]

List of labels. It is a optional parameter. You can add labels to the
created PR.

@option project_column_name [String]

A project column name. It is a optional parameter. You can add the created
PR to the GitHub project.

@option project_id [Integer]

A target project ID. It is a optional parameter. If does not supplied,
this method will find a project which associated the repository.
When the repository is associated with multiple projects, you should
supply this.

@option validate [Boolean]

Verifies the branch has commits and checkout to the topic branch. If you
want to create a blank PR, set "false" to this option. default: true.

@return [Boolean]

Return true if it is successed.

rubocop:disable Metrics/AbcSize

# File lib/pr_comet.rb, line 72
def create!(**options)
  options[:validate] = true if options[:validate].nil?
  return false if options[:validate] && !git_condition_valid?

  git.push(github_token_url, topic_branch)
  pr_number = github.create_pull_request(**generate_create_pr_options(**options))
  github.add_labels(pr_number, *options[:labels]) unless options[:labels].nil?
  unless options[:project_column_name].nil?
    github.add_to_project(pr_number, **generate_add_to_project_options(**options))
  end
  true
end

Private Instance Methods

access_token() click to toggle source
# File lib/pr_comet.rb, line 116
def access_token
  ENV['GITHUB_ACCESS_TOKEN']
end
generate_add_to_project_options(**options) click to toggle source
# File lib/pr_comet.rb, line 99
def generate_add_to_project_options(**options)
  {
    column_name: options[:project_column_name],
    project_id: options[:project_id]
  }
end
generate_create_pr_options(**options) click to toggle source
# File lib/pr_comet.rb, line 90
def generate_create_pr_options(**options)
  {
    base: base_branch,
    head: topic_branch,
    title: options[:title],
    body: options[:body]
  }
end
git_condition_valid?() click to toggle source
# File lib/pr_comet.rb, line 106
def git_condition_valid?
  !git.current_sha1?(initial_sha1) && git.current_branch?(topic_branch)
end
github_token_url() click to toggle source

@note You *MUST NOT* use `#access_token` in the URL because this string

will be output STDOUT via `RubocopChallenger::CommandLine` module.
# File lib/pr_comet.rb, line 122
def github_token_url
  "https://${GITHUB_ACCESS_TOKEN}@github.com/#{github.repository}"
end
modify_files() { || ... } click to toggle source
# File lib/pr_comet.rb, line 110
def modify_files
  raise Errors::ExistUncommittedModify if git.exist_uncommitted_modify?

  yield
end