Class: Wagemage::CLI

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/wagemage/cli.rb

Instance Method Summary collapse

Methods included from Helpers

#ask, #command, #say, #warning

Constructor Details

#initialize(args) ⇒ CLI

Returns a new instance of CLI.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/wagemage/cli.rb', line 5

def initialize(args)
  @options = Slop.parse(args) do |option|
    option.bool '-h', '--help', 'print this help'
    option.on '-v', '--version', 'print the version'

    option.string '-o', '--org', 'github org'
    option.string '-r', '--repo', 'regex against which to match repo names'
    option.string '-b', '--branch', 'regex against which to match branches'

    option.path '-s', '--script', "the script to run on each repo's branch"

    option.bool '--first-branch', 'operate only on the "oldest" branch'
    option.array '--reviewers', 'array of github users to put on the PR'
    option.string(
      '--branch-prefix',
      'prefix of the new branch',
      default: 'wagemage'
    )

    option.bool '--debug', "don't push or issue PR, keep the tmp directory"
  end

  validate_options!

  token = ENV['WAGEMAGE_GITHUB_TOKEN'] || request_token
  @okclient = Octokit::Client.new(access_token: token)

  @tmpdir = Dir.mktmpdir
  @script_path = @options[:script].expand_path.to_s
end

Instance Method Details

#runObject

Raises:



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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/wagemage/cli.rb', line 36

def run
  raise Error, 'No repos found' if repos.empty?

  say "#{repos.size} repo(s) found:", space: true
  repos.each { |r| say "* #{r.name}", color: :green }

  say 'Would you like to clone these repos? (Y/n)', space: true
  abort if ask.casecmp?('n')

  begin
    clone_repos
    display_branch_list

    say "      You're about to run this script against the aforementioned list:\n      => \#{@script_path}\n    MESSAGE\n\n    if @options[:debug]\n      say \"(--debug flag enabled. No code will be pushed.)\", color: :green\n    else\n      say \"(--debug flag NOT enabled. Code may be pushed.)\", color: :red\n    end\n\n    say \"Would you like to execute this script? (Y/n)\"\n    abort if ask.casecmp?('n')\n\n    repos.each do |repo|\n      repo.branches.each_with_index do |branch, index|\n        if index > 0\n          say \"=> Skipping \#{repo.name}:\#{branch}\", color: :yellow\n          next\n        end\n\n        new_branch = [\n          @options[:branch_prefix],\n          branch,\n          Time.now.to_i\n        ].join('/')\n\n        repo.checkout! branch\n        repo.checkout! new_branch, create: true\n\n        say \"=> Running script on \#{repo.name}:\#{new_branch}\"\n        script_result = command(\n          [@script_path, repo.clone_dir, repo.name, branch].join(' ')\n        )\n\n        unless script_result[:stderr].empty?\n          say script_result[:stderr], color: :red\n        end\n\n        if script_result[:status].success? && !repo.has_changed?\n          say 'SCRIPT SUCCEEDED BUT NO CHANGES TO COMMIT!', color: :yellow\n          next\n        elsif !script_result[:status].success?\n          say 'SCRIPT FAILED!', color: :red\n          next\n        end\n\n        say 'SCRIPT SUCCEEDED! COMMITTING CHANGES!', color: :green\n\n        repo.add_all!\n        repo.commit! script_result[:stdout]\n\n        if @options[:debug]\n          say 'DEBUG ENABLED! SKIPPING PUSH & PULL REQUEST!', color: :yellow\n          next\n        end\n\n        repo.push!\n\n        pr_result = repo.pull_request!(branch, @options[:reviewers])\n\n        if pr_result[:status].success?\n          say \"=> PULL REQUEST URL: \#{pr_result[:stdout]}\", color: :green\n        else\n          say \"=> PULL REQUEST FAILED!\", color: :red\n        end\n      end\n    end\n  ensure\n    if @options[:debug]\n      say <<~MESSAGE, space: :true, color: :yellow\n        The temporary directory has been retained because you have specified\n        the --debug flag. You can view it here:\n        => \#{@tmpdir}\n      MESSAGE\n    else\n      FileUtils.remove_entry(@tmpdir)\n    end\n  end\nend\n", space: true, color: :yellow