Class: PullRequestAi::PullRequestAiController

Inherits:
ApplicationController show all
Defined in:
app/controllers/pull_request_ai/pull_request_ai_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



47
48
49
50
51
52
53
54
# File 'app/controllers/pull_request_ai/pull_request_ai_controller.rb', line 47

def create
  result = client.open_pull_request(
    pr_params[:branch],
    pr_params[:title],
    pr_params[:description]
  )
  proccess_result(result)
end

#newObject



7
8
9
10
11
12
13
14
15
16
17
18
# File 'app/controllers/pull_request_ai/pull_request_ai_controller.rb', line 7

def new
  @types = [['Feature', :feature], ['Release', :release], ['HotFix', :hotfix]]

  @misconfigured = PullRequestAi.openai_api_key.nil? || PullRequestAi.openai_api_key.empty?
  @error_message = 'Missing the OpenAI API Key.' if @misconfigured

  client.destination_branches.fmap do |branches|
    @branches = branches  
  end.or do |error|
    @error_message ||= error.description
  end
end

#prepareObject



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
# File 'app/controllers/pull_request_ai/pull_request_ai_controller.rb', line 20

def prepare
  client.flatten_current_changes(prepare_params[:branch]).fmap do |changes|
    if changes.empty?
      render(
        json: { notice: SYMBOL_DETAILS[:no_changes_btween_branches] },
        status: :unprocessable_entity
      )
    else
      client.suggested_description(prepare_params[:type], prepare_params[:summary], changes).fmap do |description|
        response = { description: description }
        client.current_opened_pull_requests(prepare_params[:branch]).fmap do |open_prs|
          response[:remote_enabled] = true
          response[:open_pr] = open_prs.first unless open_prs.empty?
          render(json: response)
        end.or do |_|
          response[:remote_enabled] = false
          render(json: response)
        end
      end.or do |error|
        render(json: { errors: error.description }, status: :unprocessable_entity)
      end
    end
  end.or do |error|
    render(json: { errors: error.description }, status: :unprocessable_entity)
  end
end

#updateObject



56
57
58
59
60
61
62
63
64
# File 'app/controllers/pull_request_ai/pull_request_ai_controller.rb', line 56

def update
  result = client.update_pull_request(
    pr_params[:number],
    pr_params[:branch],
    pr_params[:title],
    pr_params[:description]
  )
  proccess_result(result)
end