summaryrefslogtreecommitdiff
path: root/qa/qa/resource/project_web_hook.rb
blob: 8b806c420307d37b2725c38be8b8b43ebe1b54c5 (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
# 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, :id

      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
        @url = nil
      end

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

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

      def api_post_path
        api_get_path
      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