BigW Consortium Gitlab

builds.rb 5.08 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 20

        present paginate(builds), with: Entities::Build,
                                  user_can_download_artifacts: can?(current_user, :download_build_artifacts, 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 35 36
        commit = user_project.ci_commits.find_by_sha(params[:sha])
        return not_found! unless commit

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

39 40
        present paginate(builds), with: Entities::Build,
                                  user_can_download_artifacts: can?(current_user, :download_build_artifacts, user_project)
41 42 43 44 45 46 47 48 49 50
      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
51 52 53
        build = get_build(params[:build_id])
        return not_found!(build) unless build

54 55
        present build, with: Entities::Build,
                       user_can_download_artifacts: can?(current_user, :download_build_artifacts, user_project)
56 57 58 59 60 61 62 63 64
      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
65 66 67 68
      #
      # 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.
69
      get ':id/builds/:build_id/trace' do
70
        build = get_build(params[:build_id])
71
        return not_found!(build) unless build
72 73 74 75

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

77 78
        trace = build.trace
        body trace
79
      end
80

81
      # Cancel a specific build of a project
82 83 84 85 86 87 88 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
        authorize_manage_builds!

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

        build.cancel

96 97
        present build, with: Entities::Build,
                       user_can_download_artifacts: can?(current_user, :download_build_artifacts, user_project)
98 99
      end

100
      # Retry a specific build of a project
101 102 103 104 105 106 107 108 109 110
      #
      # 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
        authorize_manage_builds!

        build = get_build(params[:build_id])
111
        return forbidden!('Build is not retryable') unless build && build.retryable?
112 113 114

        build = Ci::Build.retry(build)

115 116
        present build, with: Entities::Build,
                       user_can_download_artifacts: can?(current_user, :download_build_artifacts, user_project)
117
      end
118 119 120 121
    end

    helpers do
      def get_build(id)
122
        user_project.builds.find_by(id: id.to_i)
123
      end
124 125

      def filter_builds(builds, scope)
126 127
        return builds if scope.nil? || scope.empty?

128
        available_statuses = ::CommitStatus::AVAILABLE_STATUSES
129
        scope =
130 131 132 133
          if scope.is_a?(String)
            [scope]
          elsif scope.is_a?(Hashie::Mash)
            scope.values
134
          else
135
            ['unknown']
136 137
          end

138 139
        unknown = scope - available_statuses
        render_api_error!('Scope contains invalid value(s)', 400) unless unknown.empty?
140

141
        builds.where(status: available_statuses && scope)
142
      end
143 144 145 146

      def authorize_manage_builds!
        authorize! :manage_builds, user_project
      end
147 148 149
    end
  end
end