builds.rb 7.23 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
        commit = user_project.pipelines.find_by_sha(params[:sha])
37 38 39
        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
      end

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
      # Download the artifacts file from build
      #
      # Parameters:
      #   id (required) - The ID of a build
      #   token (required) - The build authorization token
      # Example Request:
      #   GET /projects/:id/builds/:build_id/artifacts
      get ':id/builds/:build_id/artifacts' do
        authorize_read_builds!

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

        artifacts_file = build.artifacts_file

        unless artifacts_file.file_storage?
          return redirect_to build.artifacts_file.url
        end

Kamil Trzcinski's avatar
Kamil Trzcinski committed
82
        return not_found! unless artifacts_file.exists?
83 84 85 86

        present_file!(artifacts_file.path, artifacts_file.filename)
      end

87 88 89 90 91 92 93
      # 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
94 95 96 97
      #
      # 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.
98
      get ':id/builds/:build_id/trace' do
99 100
        authorize_read_builds!

101
        build = get_build(params[:build_id])
102
        return not_found!(build) unless build
103 104 105 106

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

108 109
        trace = build.trace
        body trace
110
      end
111

112
      # Cancel a specific build of a project
113 114 115 116 117 118 119
      #
      # 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
120
        authorize_update_builds!
121 122 123 124 125 126

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

        build.cancel

127
        present build, with: Entities::Build,
128
                       user_can_download_artifacts: can?(current_user, :read_build, user_project)
129 130
      end

131
      # Retry a specific build of a project
132 133 134 135 136 137 138
      #
      # 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
139
        authorize_update_builds!
140 141

        build = get_build(params[:build_id])
142 143
        return not_found!(build) unless build
        return forbidden!('Build is not retryable') unless build.retryable?
144 145 146

        build = Ci::Build.retry(build)

147
        present build, with: Entities::Build,
148
                       user_can_download_artifacts: can?(current_user, :read_build, user_project)
149
      end
150 151 152 153 154 155 156

      # 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:
157 158
      #  post  /projects/:id/build/:build_id/erase
      post ':id/builds/:build_id/erase' do
159
        authorize_update_builds!
160 161 162

        build = get_build(params[:build_id])
        return not_found!(build) unless build
163
        return forbidden!('Build is not erasable!') unless build.erasable?
164

165
        build.erase(erased_by: current_user)
166 167 168
        present build, with: Entities::Build,
                       user_can_download_artifacts: can?(current_user, :download_build_artifacts, user_project)
      end
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187

      # Keep the artifacts to prevent them to be deleted
      #
      # Parameters:
      #   id (required) - The ID of a build
      # Example Request:
      #   POST /projects/:id/builds/:build_id/artifacts/keep
      post ':id/builds/:build_id/artifacts/keep' do
        authorize_update_builds!

        build = get_build(params[:build_id])
        return not_found!(build) unless build && build.artifacts?

        build.keep_artifacts!

        status 200
        present build, with: Entities::Build,
                user_can_download_artifacts: can?(current_user, :read_build, user_project)
      end
188 189 190 191
    end

    helpers do
      def get_build(id)
192
        user_project.builds.find_by(id: id.to_i)
193
      end
194 195

      def filter_builds(builds, scope)
196 197
        return builds if scope.nil? || scope.empty?

198
        available_statuses = ::CommitStatus::AVAILABLE_STATUSES
199
        scope =
200 201 202 203
          if scope.is_a?(String)
            [scope]
          elsif scope.is_a?(Hashie::Mash)
            scope.values
204
          else
205
            ['unknown']
206 207
          end

208 209
        unknown = scope - available_statuses
        render_api_error!('Scope contains invalid value(s)', 400) unless unknown.empty?
210

211
        builds.where(status: available_statuses && scope)
212
      end
213

214 215 216 217 218 219
      def authorize_read_builds!
        authorize! :read_build, user_project
      end

      def authorize_update_builds!
        authorize! :update_build, user_project
220
      end
221 222 223
    end
  end
end