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/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

Constant Summary collapse

VERSION =
'0.1.1'

Instance Method Summary collapse

Constructor Details

#initialize(base:, branch:, user_name: nil, user_email: nil) ⇒ 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



19
20
21
22
23
24
25
26
27
# File 'lib/pr_comet.rb', line 19

def initialize(base:, branch:, user_name: nil, user_email: nil)
  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)
  @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



36
37
38
39
40
41
42
# File 'lib/pr_comet.rb', line 36

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!(title:, body:, labels: nil) ⇒ Boolean

Create a pull request You should call #commit before calling this method

Parameters:

  • title (String)

    Title for the pull request

  • body (String)

    The body for the pull request

  • labels (Array<String>) (defaults to: nil)

    An array of labels to apply to this PR

Returns:

  • (Boolean)

    Return true if its successed



51
52
53
54
55
56
57
58
59
60
# File 'lib/pr_comet.rb', line 51

def create!(title:, body:, labels: nil)
  return false unless git_condition_valid?

  git.push(github_token_url, topic_branch)
  pr_number = github.create_pull_request(
    base: base_branch, head: topic_branch, title: title, body: body
  )
  github.add_labels(pr_number, *labels) unless labels.nil?
  true
end