Read about our upcoming Code of Conduct on this issue

builds_controller.rb 2.27 KB
Newer Older
1
class Projects::BuildsController < Projects::ApplicationController
Kamil Trzcinski's avatar
Kamil Trzcinski committed
2
  before_action :build, except: [:index, :cancel_all]
3

4
  before_action :authorize_manage_builds!, except: [:index, :show, :status]
Kamil Trzcinski's avatar
Kamil Trzcinski committed
5
  before_action :authorize_download_build_artifacts!, only: [:download]
6

7
8
  layout "project"

Kamil Trzcinski's avatar
Kamil Trzcinski committed
9
10
  def index
    @scope = params[:scope]
11
    @all_builds = project.builds
Kamil Trzcinski's avatar
Kamil Trzcinski committed
12
    @builds = @all_builds.order('created_at DESC')
Kamil Trzcinski's avatar
Kamil Trzcinski committed
13
14
    @builds =
      case @scope
Kamil Trzcinski's avatar
Kamil Trzcinski committed
15
      when 'all'
Kamil Trzcinski's avatar
Kamil Trzcinski committed
16
        @builds
Kamil Trzcinski's avatar
Kamil Trzcinski committed
17
      when 'finished'
Kamil Trzcinski's avatar
Kamil Trzcinski committed
18
        @builds.finished
Kamil Trzcinski's avatar
Kamil Trzcinski committed
19
      else
Kamil Trzcinski's avatar
Kamil Trzcinski committed
20
        @builds.running_or_pending.reverse_order
Kamil Trzcinski's avatar
Kamil Trzcinski committed
21
      end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
22
    @builds = @builds.page(params[:page]).per(30)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
23
24
25
  end

  def cancel_all
26
    @project.builds.running_or_pending.each(&:cancel)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
27
28
29
30

    redirect_to namespace_project_builds_path(project.namespace, project)
  end

31
  def show
32
    @builds = @project.ci_commits.find_by_sha(@build.sha).builds.order('id DESC')
Kamil Trzcinski's avatar
Kamil Trzcinski committed
33
    @builds = @builds.where("id not in (?)", @build.id)
34
35
36
37
38
39
40
41
42
43
    @commit = @build.commit

    respond_to do |format|
      format.html
      format.json do
        render json: @build.to_json(methods: :trace_html)
      end
    end
  end

44
  def retry
Kamil Trzcinski's avatar
Kamil Trzcinski committed
45
    unless @build.retryable?
46
47
48
49
50
      return page_404
    end

    build = Ci::Build.retry(@build)

Kamil Trzcinski's avatar
Kamil Trzcinski committed
51
    redirect_to build_path(build)
52
53
  end

Kamil Trzcinski's avatar
Kamil Trzcinski committed
54
55
56
57
58
59
60
61
62
63
64
65
  def download
    unless artifacts_file.file_storage?
      return redirect_to artifacts_file.url
    end

    unless artifacts_file.exists?
      return not_found!
    end

    send_file artifacts_file.path, disposition: 'attachment'
  end

66
67
68
69
70
71
72
73
74
75
  def status
    render json: @build.to_json(only: [:status, :id, :sha, :coverage], methods: :sha)
  end

  def cancel
    @build.cancel

    redirect_to build_path(@build)
  end

76
77
78
  private

  def build
79
    @build ||= project.builds.unscoped.find_by!(id: params[:id])
80
  end
81

Kamil Trzcinski's avatar
Kamil Trzcinski committed
82
83
84
85
  def artifacts_file
    build.artifacts_file
  end

86
  def build_path(build)
87
    namespace_project_build_path(build.project.namespace, build.project, build)
88
  end
89
90
91
92
93
94

  def authorize_manage_builds!
    unless can?(current_user, :manage_builds, project)
      return page_404
    end
  end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
95
96
97
98
99
100
101
102
103
104

  def authorize_download_build_artifacts!
    unless can?(current_user, :download_build_artifacts, @project)
      if current_user.nil?
        return authenticate_user!
      else
        return render_404
      end
    end
  end
105
end