File: web_ide_terminals_controller.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (101 lines) | stat: -rw-r--r-- 2,417 bytes parent folder | download
1
2
3
4
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
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
# frozen_string_literal: true

class Projects::WebIdeTerminalsController < Projects::ApplicationController
  before_action :authenticate_user!

  before_action :build, except: [:check_config, :create]
  before_action :authorize_create_web_ide_terminal!
  before_action :authorize_read_web_ide_terminal!, except: [:check_config, :create]
  before_action :authorize_update_web_ide_terminal!, only: [:cancel, :retry]

  feature_category :web_ide

  urgency :low, [:check_config]

  def check_config
    return respond_422 unless branch_sha

    result = ::Ide::TerminalConfigService.new(project, current_user, sha: branch_sha).execute

    if result[:status] == :success
      head :ok
    else
      respond_422
    end
  end

  def show
    render_terminal(build)
  end

  def create
    result = ::Ci::CreateWebIdeTerminalService.new(project, current_user, ref: params[:branch]).execute

    if result[:status] == :error
      render status: :bad_request, json: result[:message]
    else
      pipeline = result[:pipeline]
      current_build = pipeline.builds.last

      if current_build
        Gitlab::UsageDataCounters::WebIdeCounter.increment_terminals_count

        render_terminal(current_build)
      else
        render status: :bad_request, json: pipeline.errors.full_messages
      end
    end
  end

  def cancel
    return respond_422 unless build.cancelable?

    build.cancel

    head :ok
  end

  def retry
    response = Ci::RetryJobService.new(build.project, current_user).execute(build)

    if response.success?
      render_terminal(response[:job])
    else
      respond_422
    end
  end

  private

  def authorize_create_web_ide_terminal!
    access_denied! unless can?(current_user, :create_web_ide_terminal, project)
  end

  def authorize_read_web_ide_terminal!
    authorize_build_ability!(:read_web_ide_terminal)
  end

  def authorize_update_web_ide_terminal!
    authorize_build_ability!(:update_web_ide_terminal)
  end

  def authorize_build_ability!(ability)
    access_denied! unless can?(current_user, ability, build)
  end

  def build
    @build ||= project.builds.find(params[:id])
  end

  def branch_sha
    return unless params[:branch].present?

    project.commit(params[:branch])&.id
  end

  def render_terminal(current_build)
    render json: WebIdeTerminalSerializer
      .new(project: project, current_user: current_user)
      .represent(current_build)
  end
end