Class: PrComet

Inherits:
Object
  • Object
show all
Defined in:
lib/pr_comet.rb,
lib/pr_comet/errors.rb,
lib/pr_comet/version.rb,
lib/pr_comet/rspec/stub.rb,
lib/pr_comet/git/command.rb,
lib/pr_comet/command_line.rb,
lib/pr_comet/github/client.rb

Overview

Helps to create a pull request

Defined Under Namespace

Modules: CommandLine, Errors, Git, Github, Stub

Constant Summary collapse

VERSION =
'0.7.0'

Instance Method Summary collapse

Constructor Details

#initialize(base:, branch:, user_name: nil, user_email: nil, verbose: false) ⇒ PrComet

Note:

You have to set ENV

Returns a new instance of PrComet.

Parameters:

  • base (String)

    The branch you want your changes pulled into

  • branch (String)

    The branch where your changes are going to implement.

  • user_name (String) (defaults to: nil)

    The username to use for committer and author

  • user_email (String) (defaults to: nil)

    The email to use for committer and author

  • verbose (Boolean) (defaults to: false)

    Displays executing commands



20
21
22
23
24
25
26
27
28
# 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

Instance Method Details

#commit(message) { ... } ⇒ Object

Add and commit local files to this branch

Parameters:

  • message (String)

    The commit message

Yields:

  • Some commands where modify local files

Returns:

  • (Object)

    Return result of yield if you use &block

Raises:

  • (RubocopChallenger::Errors::ExistUncommittedModify)

    Raise error if you use &block and exists someuncommitted files



37
38
39
40
41
42
43
44
45
# 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
rescue PrComet::Errors::ExistUncommittedModify
  raise `git status`
end

#create!(**options) ⇒ Boolean

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.

rubocop:disable Metrics/AbcSize

Parameters:

  • options (Hash)

    Options for the PR creation.

  • title (Hash)

    a customizable set of options

  • body (Hash)

    a customizable set of options

  • labels (Hash)

    a customizable set of options

  • project_column_name (Hash)

    a customizable set of options

  • project_id (Hash)

    a customizable set of options

  • validate (Hash)

    a customizable set of options

Returns:

  • (Boolean)

    Return true if it is successed.



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/pr_comet.rb', line 74

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