builds.rb 5.91 KB
Newer Older
1 2 3 4 5 6
module API
  # Projects builds API
  class Builds < Grape::API
    before { authenticate! }

    resource :projects do
7
      # Get a project builds
8 9 10
      #
      # Parameters:
      #   id (required) - The ID of a project
11 12
      #   scope (optional) - The scope of builds to show (one or array of: pending, running, failed, success, canceled;
      #                      if none provided showing all builds)
13
      # Example Request:
Tomasz Maczukin's avatar
Tomasz Maczukin committed
14
      #   GET /projects/:id/builds
15
      get ':id/builds' do
16

17 18
        builds = user_project.builds.order('id DESC')
        builds = filter_builds(builds, params[:scope])
19 20

        present paginate(builds), with: Entities::Build,
21
                                  user_can_download_artifacts: can?(current_user, :read_build, user_project)
22
      end
23

24
      # Get builds for a specific commit of a project
25 26 27 28
      #
      # Parameters:
      #   id (required) - The ID of a project
      #   sha (required) - The SHA id of a commit
29 30
      #   scope (optional) - The scope of builds to show (one or array of: pending, running, failed, success, canceled;
      #                      if none provided showing all builds)
31
      # Example Request:
32 33
      #   GET /projects/:id/repository/commits/:sha/builds
      get ':id/repository/commits/:sha/builds' do
34 35
        authorize_read_builds!

36 37 38 39
        commit = user_project.ci_commits.find_by_sha(params[:sha])
        return not_found! unless commit

        builds = commit.builds.order('id DESC')
40
        builds = filter_builds(builds, params[:scope])
41

42
        present paginate(builds), with: Entities::Build,
43
                                  user_can_download_artifacts: can?(current_user, :read_build, user_project)
44 45 46 47 48 49 50 51 52 53
      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
54 55
        authorize_read_builds!

56 57 58
        build = get_build(params[:build_id])
        return not_found!(build) unless build

59
        present build, with: Entities::Build,
60
                       user_can_download_artifacts: can?(current_user, :read_build, user_project)
61 62 63 64 65 66 67 68 69
      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
70 71 72 73
      #
      # TODO: We should use `present_file!` and leave this implementation for backward compatibility (when build trace
      #       is saved in the DB instead of file). But before that, we need to consider how to replace the value of
      #       `runners_token` with some mask (like `xxxxxx`) when sending trace file directly by workhorse.
74
      get ':id/builds/:build_id/trace' do
75 76
        authorize_read_builds!

77
        build = get_build(params[:build_id])
78
        return not_found!(build) unless build
79 80 81 82

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

84 85
        trace = build.trace
        body trace
86
      end
87

88
      # Cancel a specific build of a project
89 90 91 92 93 94 95
      #
      # parameters:
      #   id (required) - the id of a project
      #   build_id (required) - the id of a build
      # example request:
      #   post /projects/:id/build/:build_id/cancel
      post ':id/builds/:build_id/cancel' do
96
        authorize_update_builds!
97 98 99 100 101 102

        build = get_build(params[:build_id])
        return not_found!(build) unless build

        build.cancel

103
        present build, with: Entities::Build,
104
                       user_can_download_artifacts: can?(current_user, :read_build, user_project)
105 106
      end

107
      # Retry a specific build of a project
108 109 110 111 112 113 114
      #
      # parameters:
      #   id (required) - the id of a project
      #   build_id (required) - the id of a build
      # example request:
      #   post /projects/:id/build/:build_id/retry
      post ':id/builds/:build_id/retry' do
115
        authorize_update_builds!
116 117

        build = get_build(params[:build_id])
118 119
        return not_found!(build) unless build
        return forbidden!('Build is not retryable') unless build.retryable?
120 121 122

        build = Ci::Build.retry(build)

123
        present build, with: Entities::Build,
124
                       user_can_download_artifacts: can?(current_user, :read_build, user_project)
125
      end
126 127 128 129 130 131 132 133 134 135 136 137 138

      # Erase build (remove artifacts and build trace)
      #
      # Parameters:
      #   id (required) - the id of a project
      #   build_id (required) - the id of a build
      # example Request:
      #  delete  /projects/:id/build/:build_id/content
      delete ':id/builds/:build_id/content' do
        authorize_manage_builds!

        build = get_build(params[:build_id])
        return not_found!(build) unless build
139
        return forbidden!('Build is not erasable!') unless build.erasable?
140 141 142 143 144

        build.erase!
        present build, with: Entities::Build,
                       user_can_download_artifacts: can?(current_user, :download_build_artifacts, user_project)
      end
145 146 147 148
    end

    helpers do
      def get_build(id)
149
        user_project.builds.find_by(id: id.to_i)
150
      end
151 152

      def filter_builds(builds, scope)
153 154
        return builds if scope.nil? || scope.empty?

155
        available_statuses = ::CommitStatus::AVAILABLE_STATUSES
156
        scope =
157 158 159 160
          if scope.is_a?(String)
            [scope]
          elsif scope.is_a?(Hashie::Mash)
            scope.values
161
          else
162
            ['unknown']
163 164
          end

165 166
        unknown = scope - available_statuses
        render_api_error!('Scope contains invalid value(s)', 400) unless unknown.empty?
167

168
        builds.where(status: available_statuses && scope)
169
      end
170

171 172 173 174 175 176
      def authorize_read_builds!
        authorize! :read_build, user_project
      end

      def authorize_update_builds!
        authorize! :update_build, user_project
177
      end
178 179 180
    end
  end
end