builds.rb 4.67 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
        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 31 32
      # Example Request:
      #   GET /projects/:id/builds/commit/:sha
      get ':id/builds/commit/:sha' 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
        present paginate(builds), with: Entities::Build,
                                  user_can_download_artifacts: can?(current_user, :download_build_artifacts, user_project)
40 41 42 43 44 45 46 47 48 49
      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
50 51 52
        build = get_build(params[:build_id])
        return not_found!(build) unless build

53 54
        present build, with: Entities::Build,
                       user_can_download_artifacts: can?(current_user, :download_build_artifacts, user_project)
55 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
      get ':id/builds/:build_id/trace' do
65
        build = get_build(params[:build_id])
66
        return not_found!(build) unless build
67 68 69 70

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

72 73
        trace = build.trace
        body trace
74
      end
75

76
      # Cancel a specific build of a project
77 78 79 80 81 82 83 84 85 86 87 88 89 90
      #
      # 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

91 92
        present build, with: Entities::Build,
                       user_can_download_artifacts: can?(current_user, :download_build_artifacts, user_project)
93 94
      end

95
      # Retry a specific build of a project
96 97 98 99 100 101 102 103 104 105
      #
      # 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])
106
        return forbidden!('Build is not retryable') unless build && build.retryable?
107 108 109

        build = Ci::Build.retry(build)

110 111
        present build, with: Entities::Build,
                       user_can_download_artifacts: can?(current_user, :download_build_artifacts, user_project)
112
      end
113 114 115 116 117 118
    end

    helpers do
      def get_build(id)
        user_project.builds.where(id: id).first
      end
119 120

      def filter_builds(builds, scope)
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
        available_scopes = Ci::Build.available_statuses
        scope =
          if scope.is_a?(String) || scope.is_a?(Symbol)
            available_scopes & [scope.to_s]
          elsif scope.is_a?(Array)
            available_scopes & scope
          elsif scope.respond_to?(:to_h)
            available_scopes & scope.to_h.values
          else
            []
          end

        return builds if scope.empty?

        builds.where(status: scope)
136
      end
137 138 139 140

      def authorize_manage_builds!
        authorize! :manage_builds, user_project
      end
141 142 143
    end
  end
end