Class: CompareLinkerCommand::Executor

Inherits:
Object
  • Object
show all
Defined in:
lib/compare_linker_command/executor.rb

Instance Method Summary collapse

Constructor Details

#initialize(current_dir, params) ⇒ Executor

Returns a new instance of Executor.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/compare_linker_command/executor.rb', line 7

def initialize(current_dir, params)
  @current_dir = current_dir

  @repo_name = params[:repo_name]
  @pr_body = params[:pr_body_file] ? File.read(params[:pr_body_file]) : ""

  @local = Git.open(current_dir)

  @token = params[:token]
  @remote = Octokit::Client.new(access_token: @token)
end

Instance Method Details

#execObject



19
20
21
22
23
24
25
26
27
28
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
# File 'lib/compare_linker_command/executor.rb', line 19

def exec
  $stdout.print "Create PR for '#{@current_dir}' (Ctrl-C for Cancel) :"
  $stdin.gets

  branch_name    = "bundle_update_" + Date.today.strftime("%Y_%m_%d")
  commit_message = "bundle update " + Date.today.strftime("%Y.%m.%d")
  pr_title       = commit_message

  $stdout.puts "Stash"
  $stdout.puts `git stash`

  $stdout.puts "Checkout master & pull"
  @local.branch("master").checkout()
  @local.pull()

  $stdout.puts "Create '#{branch_name}' branch in local"
  @local.branch(branch_name).checkout()

  $stdout.puts "Exec bundle update..."
  $stdout.puts `bundle update`
  $stdout.puts "bundle update Done."
  $stdout.puts
  $stdout.puts "Commit as '#{commit_message}'"
  @local.add(["Gemfile", "Gemfile.lock"])
  @local.commit(commit_message)

  $stdout.print "Push '#{branch_name}' to origin ? (Ctrl-C for Cancel) :"
  $stdin.gets

  $stdout.puts "Push to remote"
  @local.push("origin", branch_name)

  $stdout.print "Create PR ? (Ctrl-C for Cancel) :"
  $stdin.gets

  $stdout.puts "Create PR"
  pr = @remote.create_pull_request(@repo_name, "master", branch_name, pr_title, @pr_body)

  $stdout.puts "#{pr[:html_url]} was created."

  run_compare_linker(pr.number)

  $stdout.puts "All Done."
end

#run_compare_linker(pr_number) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/compare_linker_command/executor.rb', line 64

def run_compare_linker(pr_number)
  # NOTE: CompareLinker requires ENV["OCTOKIT_ACCESS_TOKEN"]
  # c.f. https://github.com/masutaka/compare_linker/blob/234719c8e679fa59afa767fd149d2ca7ee51e5d3/lib/compare_linker.rb#L18
  ENV["OCTOKIT_ACCESS_TOKEN"] = @token

  $stdout.print "Run compare-linker for '#{@repo_name}##{pr_number}' (Ctrl-C for Cancel) :"
  $stdin.gets

  compare_linker = CompareLinker.new(@repo_name, pr_number)
  compare_linker.formatter = CompareLinker::Formatter::Markdown.new
  comment = compare_linker.make_compare_links.to_a.join("\n")
  compare_linker.add_comment(@repo_name, pr_number, comment)
end