BigW Consortium Gitlab

builds.rb 7.37 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 56 57
        build = get_build(params[:build_id])
        return not_found!(build) unless build

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

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
      # 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

81
        return not_found! unless artifacts_file.exists?
82 83 84 85

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

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

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

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

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

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

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

        build.cancel

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

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

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

144
        build = Ci::Build.retry(build, current_user)
145

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

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

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

164
        build.erase(erased_by: current_user)
165 166 167
        present build, with: Entities::Build,
                       user_can_download_artifacts: can?(current_user, :download_build_artifacts, user_project)
      end
168

169
      # Keep the artifacts to prevent them from being deleted
170 171
      #
      # Parameters:
172 173
      #   id (required) - the id of a project
      #   build_id (required) - The ID of a build
174 175 176 177 178 179 180 181 182 183 184 185
      # 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,
Kamil Trzcinski committed
186
                       user_can_download_artifacts: can?(current_user, :read_build, user_project)
187
      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