Class: Geet::Services::CreatePr

Inherits:
AbstractCreateIssue show all
Includes:
Geet::Shared::RepoPermissions, Geet::Shared::Selection
Defined in:
lib/geet/services/create_pr.rb

Constant Summary collapse

DEFAULT_GIT_CLIENT =
Geet::Utils::GitClient.new

Constants included from Geet::Shared::Selection

Geet::Shared::Selection::MANUAL_LIST_SELECTION_FLAG, Geet::Shared::Selection::SELECTION_MULTIPLE, Geet::Shared::Selection::SELECTION_SINGLE, Geet::Shared::Selection::SKIP_LIST_SELECTION_FLAG

Constants included from Geet::Shared::RepoPermissions

Geet::Shared::RepoPermissions::ALL_PERMISSIONS, Geet::Shared::RepoPermissions::PERMISSION_ADMIN, Geet::Shared::RepoPermissions::PERMISSION_NONE, Geet::Shared::RepoPermissions::PERMISSION_READ, Geet::Shared::RepoPermissions::PERMISSION_WRITE

Instance Method Summary collapse

Methods included from Geet::Shared::RepoPermissions

#permission_greater_or_equal_to?

Methods included from Helpers::OsHelper

#execute_command, #open_file_with_default_application

Constructor Details

#initialize(repository, out: $stdout, git_client: DEFAULT_GIT_CLIENT) ⇒ CreatePr

Returns a new instance of CreatePr.



18
19
20
21
22
# File 'lib/geet/services/create_pr.rb', line 18

def initialize(repository, out: $stdout, git_client: DEFAULT_GIT_CLIENT)
  super(repository)
  @git_client = git_client
  @out = out
end

Instance Method Details

#execute(title, description, labels: nil, milestone: nil, reviewers: nil, base: nil, draft: false, open_browser: false) ⇒ Object

options:

:labels
:reviewers
:open_browser


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/geet/services/create_pr.rb', line 29

def execute(
  title, description, labels: nil, milestone: nil, reviewers: nil,
  base: nil, draft: false, open_browser: false, **
)
  ensure_clean_tree

  if @repository.upstream? && !@git_client.remote_defined?(Utils::GitClient::UPSTREAM_NAME)
    @out.puts "Upstream not found; adding it to the repository remotes..."

    AddUpstreamRepo.new(@repository.downstream, out: @out, git_client: @git_client).execute
  end

  # See CreateIssue#execute for notes about performance.
  user_has_write_permissions = @repository.authenticated_user.is_collaborator? &&
                               @repository.authenticated_user.has_permission?(PERMISSION_WRITE)

  if user_has_write_permissions
    selected_labels, selected_milestone, selected_reviewers = find_and_select_attributes(labels, milestone, reviewers)
  end

  sync_with_remote_branch

  pr = create_pr(title, description, base: base, draft: draft)

  if user_has_write_permissions
    edit_pr(pr, selected_labels, selected_milestone, selected_reviewers)
  end

  if open_browser
    open_file_with_default_application(pr.link)
  else
    @out.puts "PR address: #{pr.link}"
  end

  pr
end