Module: Zenflow::BranchCommands::Review

Included in:
Zenflow::BranchCommand
Defined in:
lib/zenflow/helpers/branch_commands/review.rb

Class Method Summary collapse

Class Method Details

.included(thor) ⇒ Object



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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/zenflow/helpers/branch_commands/review.rb', line 5

def self.included(thor)
  thor.class_eval do

    desc "review", "Start a code review."
    def review
      branch_name
      create_pull_request
    end

    no_commands do
      def create_pull_request
        already_created?(Zenflow::PullRequest.find_by_ref("#{flow}/#{branch_name}"))

        pull = Zenflow::PullRequest.create(
          base:  branch(:destination) || branch(:source),
          head:  "#{flow}/#{branch_name}",
          title: "#{flow}: #{branch_name}",
          body:  Zenflow::Ask("Describe this #{flow}:", required: true)
        )

        return handle_invalid_pull_request(pull) unless pull.valid?

        Zenflow::Log("Pull request was created!")
        Zenflow::Log(pull["html_url"], indent: true, color: false)
        Zenflow::Shell["open #{pull['html_url']}"]
      end

      def already_created?(pull)
        return unless pull
        Zenflow::Log("A pull request for #{flow}/#{branch_name} already exists", color: :red)
        Zenflow::Log(pull[:html_url], indent: true, color: false)
        exit(1)
      end

      def handle_invalid_pull_request(pull)
        Zenflow::Log("There was a problem creating the pull request:", color: :red)
        if pull["errors"]
          pull["errors"].each do |error|
            Zenflow::Log("* #{error['message'].gsub(/^base\s*/,'')}", indent: true, color: :red)
          end
        elsif pull["message"]
          Zenflow::Log("* #{pull['message']}", indent: true, color: :red)
        else
          Zenflow::Log(" * unexpected failure, both 'errors' and 'message' were empty in the response")
        end
      end
    end

  end
end