summaryrefslogtreecommitdiff
path: root/lib/api/builds.rb
blob: 863be0d5e4079b2da570ab3adb9a22175a2e3868 (plain)
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
module API
  # Projects builds API
  class Builds < Grape::API
    before { authenticate! }

    resource :projects do
      # Get a project repository commits
      #
      # Parameters:
      #   id (required) - The ID of a project
      #   scope (optional) - The scope of builds to show (one of: all, finished, running)
      #   page (optional) - The page number for pagination (default: 1)
      #   per_page (ooptional) - The value of items per page to show (default 30)
      # Example Request:
      #   GET /projects/:id/builds
      get ':id/builds' do
        all_builds = user_project.builds
        builds = all_builds.order('id DESC')
        builds =
          case params[:scope]
          when 'finished'
            builds.finished
          when 'running'
            builds.running
          else
            builds
          end

        page = (params[:page] || 1).to_i
        per_page = (params[:per_page] || 30).to_i

        present builds.page(page).per(per_page), with: Entities::Build
      end

      # Get a specific build of a project
      #
      # Parameters:
      #   id (required) - The ID of a project
      #   build_id (required) - The ID of a build
      # Example Request:
      #   GET /projects/:id/builds/:build_id
      get ':id/builds/:build_id' do
        present get_build(params[:build_id]), with: Entities::Build
      end

      # Get a trace of a specific build of a project
      #
      # Parameters:
      #   id (required) - The ID of a project
      #   build_id (required) - The ID of a build
      # Example Request:
      #   GET /projects/:id/build/:build_id/trace
      get ':id/builds/:build_id/trace' do
        build = get_build(params[:build_id])

        header 'Content-Disposition', "infile; filename=\"#{build.id}.log\""
        content_type 'text/plain'
        env['api.format'] = :binary

        trace = build.trace
        body trace
      end
    end

    helpers do
      def get_build(id)
        user_project.builds.where(id: id).first
      end
    end
  end
end