summaryrefslogtreecommitdiff
path: root/qa/qa/resource/package.rb
blob: 1009353a296ebac25a6f89d0f28441d791efe636 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# frozen_string_literal: true

require 'securerandom'

module QA
  module Resource
    class Package < Base
      attr_accessor :name

      attribute :project do
        Project.fabricate_via_api! do |resource|
          resource.name = 'project-with-package'
          resource.description = 'Project with Package'
        end
      end

      attribute :id do
        packages = project.packages

        return unless (this_package = packages&.find { |package| package[:name] == "#{project.path_with_namespace}/#{name}" }) # rubocop:disable Cop/AvoidReturnFromBlocks

        this_package[:id]
      end

      def fabricate!
      end

      def fabricate_via_api!
        resource_web_url(api_get)
      rescue ResourceNotFoundError
        super
      end

      def remove_via_api!
        packages = project.packages

        if packages && !packages.empty?
          QA::Runtime::Logger.debug("Deleting package '#{name}' from '#{project.path_with_namespace}' via API")
          super
        end
      end

      def api_delete_path
        "/projects/#{project.id}/packages/#{id}"
      end

      def api_get_path
        "/projects/#{project.id}/packages"
      end
    end
  end
end