BigW Consortium Gitlab

builds.rb 8.49 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:
14
      #   GET /projects/:id/builds
15
      get ':id/builds' do
16 17
        builds = user_project.builds.order('id DESC')
        builds = filter_builds(builds, params[:scope])
18 19

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

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

35 36
        return not_found! unless user_project.commit(params[:sha])

37 38
        pipelines = user_project.pipelines.where(sha: params[:sha])
        builds = user_project.builds.where(pipeline: pipelines).order('id DESC')
39
        builds = filter_builds(builds, params[:scope])
40

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

55
        build = get_build!(params[:build_id])
56

57
        present build, with: Entities::Build,
58
                       user_can_download_artifacts: can?(current_user, :read_build, user_project)
59 60
      end

61 62 63 64 65 66 67 68 69 70
      # 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!

71
        build = get_build!(params[:build_id])
72

73
        present_artifacts!(build.artifacts_file)
74
      end
75

76 77 78 79 80 81 82
      # Download the artifacts file from ref_name and job
      #
      # Parameters:
      #   id (required) - The ID of a project
      #   ref_name (required) - The ref from repository
      #   job (required) - The name for the build
      # Example Request:
83
      #   GET /projects/:id/builds/artifacts/:ref_name/download?job=name
84 85
      get ':id/builds/artifacts/:ref_name/download',
        requirements: { ref_name: /.+/ } do
86 87
        authorize_read_builds!

88 89
        builds = user_project.latest_successful_builds_for(params[:ref_name])
        latest_build = builds.find_by!(name: params[:job])
90

91
        present_artifacts!(latest_build.artifacts_file)
92 93
      end

94 95 96 97 98 99 100
      # 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
101 102 103 104
      #
      # 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.
105
      get ':id/builds/:build_id/trace' do
106 107
        authorize_read_builds!

108
        build = get_build!(params[:build_id])
109 110 111 112

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

114 115
        trace = build.trace
        body trace
116
      end
117

118
      # Cancel a specific build of a project
119 120 121 122 123 124 125
      #
      # 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
126
        authorize_update_builds!
127

128
        build = get_build!(params[:build_id])
129 130 131

        build.cancel

132
        present build, with: Entities::Build,
133
                       user_can_download_artifacts: can?(current_user, :read_build, user_project)
134 135
      end

136
      # Retry a specific build of a project
137 138 139 140 141 142 143
      #
      # 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
144
        authorize_update_builds!
145

146
        build = get_build!(params[:build_id])
147
        return forbidden!('Build is not retryable') unless build.retryable?
148

149
        build = Ci::Build.retry(build, current_user)
150

151
        present build, with: Entities::Build,
152
                       user_can_download_artifacts: can?(current_user, :read_build, user_project)
153
      end
154 155 156 157 158 159 160

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

165
        build = get_build!(params[:build_id])
166
        return forbidden!('Build is not erasable!') unless build.erasable?
167

168
        build.erase(erased_by: current_user)
169 170 171
        present build, with: Entities::Build,
                       user_can_download_artifacts: can?(current_user, :download_build_artifacts, user_project)
      end
172

173
      # Keep the artifacts to prevent them from being deleted
174 175
      #
      # Parameters:
176 177
      #   id (required) - the id of a project
      #   build_id (required) - The ID of a build
178 179 180 181 182
      # Example Request:
      #   POST /projects/:id/builds/:build_id/artifacts/keep
      post ':id/builds/:build_id/artifacts/keep' do
        authorize_update_builds!

183 184
        build = get_build!(params[:build_id])
        return not_found!(build) unless build.artifacts?
185 186 187 188 189

        build.keep_artifacts!

        status 200
        present build, with: Entities::Build,
Kamil Trzcinski committed
190
                       user_can_download_artifacts: can?(current_user, :read_build, user_project)
191
      end
192 193 194 195 196 197 198 199 200 201 202 203 204

      desc 'Trigger a manual build' do
        success Entities::Build
        detail 'This feature was added in GitLab 8.11'
      end
      params do
        requires :build_id, type: Integer, desc: 'The ID of a Build'
      end
      post ":id/builds/:build_id/play" do
        authorize_read_builds!

        build = get_build!(params[:build_id])

Z.J. van de Weg committed
205 206 207 208 209 210 211
        bad_request!("Unplayable Build") unless build.playable?

        build.play(current_user)

        status 200
        present build, with: Entities::Build,
                       user_can_download_artifacts: can?(current_user, :read_build, user_project)
212
      end
213 214 215 216
    end

    helpers do
      def get_build(id)
217
        user_project.builds.find_by(id: id.to_i)
218
      end
219

220 221 222 223
      def get_build!(id)
        get_build(id) || not_found!
      end

224
      def present_artifacts!(artifacts_file)
225 226 227 228 229 230 231 232 233
        if !artifacts_file.file_storage?
          redirect_to(build.artifacts_file.url)
        elsif artifacts_file.exists?
          present_file!(artifacts_file.path, artifacts_file.filename)
        else
          not_found!
        end
      end

234
      def filter_builds(builds, scope)
235 236
        return builds if scope.nil? || scope.empty?

237
        available_statuses = ::CommitStatus::AVAILABLE_STATUSES
238
        scope =
239 240 241 242
          if scope.is_a?(String)
            [scope]
          elsif scope.is_a?(Hashie::Mash)
            scope.values
243
          else
244
            ['unknown']
245 246
          end

247 248
        unknown = scope - available_statuses
        render_api_error!('Scope contains invalid value(s)', 400) unless unknown.empty?
249

250
        builds.where(status: available_statuses && scope)
251
      end
252

253 254 255 256 257 258
      def authorize_read_builds!
        authorize! :read_build, user_project
      end

      def authorize_update_builds!
        authorize! :update_build, user_project
259
      end
260 261 262
    end
  end
end