summaryrefslogtreecommitdiff
path: root/lib/api/builds.rb
blob: 67b73db767ece347ae8a3a9642fc8bf77eda5b1f (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
module API
  # Builds API
  class Builds < Grape::API
    resource :builds do
      # Runs oldest pending build by runner - Runners only
      #
      # Parameters:
      #   token (required) - The uniq token of runner
      #
      # Example Request:
      #   POST /builds/register
      post "register" do
        authenticate_runner!
        update_runner_last_contact
        required_attributes! [:token]
        not_found! unless current_runner.active?

        build = RegisterBuildService.new.execute(current_runner)

        if build
          update_runner_info
          present build, with: Entities::Build
        else
          not_found!
        end
      end

      # Update an existing build - Runners only
      #
      # Parameters:
      #   id (required) - The ID of a project
      #   state (optional) - The state of a build
      #   trace (optional) - The trace of a build
      # Example Request:
      #   PUT /builds/:id
      put ":id" do
        authenticate_runner!
        update_runner_last_contact
        build = Build.where(runner_id: current_runner.id).running.find(params[:id])
        build.update_attributes(trace: params[:trace]) if params[:trace]

        case params[:state].to_s
        when 'success'
          build.success
        when 'failed'
          build.drop
        end
      end
    end
  end
end