summaryrefslogtreecommitdiff
path: root/qa/qa/resource/project_web_hook.rb
blob: 86e662932e1a7934de249aae10228f6235856d13 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# frozen_string_literal: true

module QA
  module Resource
    class ProjectWebHook < Base
      EVENT_TRIGGERS = %i[
        issues
        job
        merge_requests
        note
        pipeline
        push
        tag_push
        wiki_page
        confidential_issues
        confidential_note
      ].freeze

      attr_accessor :url, :enable_ssl

      attribute :disabled_until
      attribute :id
      attribute :alert_status

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

      EVENT_TRIGGERS.each do |trigger|
        attribute "#{trigger}_events".to_sym do
          false
        end
      end

      def initialize
        @id = nil
        @enable_ssl = false
        @alert_status = nil
        @url = nil
      end

      def fabricate_via_api!
        resource_web_url = super

        @id = api_response[:id]

        resource_web_url
      end

      def resource_web_url(resource)
        "/project/#{project.name}/~/hooks/##{resource[:id]}/edit"
      end

      def api_get_path
        "#{api_post_path}/#{api_response[:id]}"
      end

      def api_post_path
        "/projects/#{project.id}/hooks"
      end

      def api_post_body
        body = {
          id: project.id,
          url: url,
          enable_ssl_verification: enable_ssl
        }
        EVENT_TRIGGERS.each_with_object(body) do |trigger, memo|
          attr = "#{trigger}_events"
          memo[attr.to_sym] = send(attr)
          memo
        end
      end
    end
  end
end