File: google_play.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (126 lines) | stat: -rw-r--r-- 4,127 bytes parent folder | download
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# frozen_string_literal: true

module Integrations
  class GooglePlay < Integration
    PACKAGE_NAME_REGEX = /\A[A-Za-z][A-Za-z0-9_]*(\.[A-Za-z][A-Za-z0-9_]*){1,20}\z/

    SECTION_TYPE_GOOGLE_PLAY = 'google_play'

    with_options if: :activated? do
      validates :service_account_key, presence: true, json_schema: {
        filename: "google_service_account_key", parse_json: true
      }
      validates :service_account_key_file_name, presence: true
      validates :package_name, presence: true, format: { with: PACKAGE_NAME_REGEX }
      validates :google_play_protected_refs, inclusion: [true, false]
    end

    field :package_name,
      section: SECTION_TYPE_CONNECTION,
      placeholder: 'com.example.myapp',
      description: -> { _('Package name of the app in Google Play.') },
      required: true

    field :service_account_key_file_name,
      section: SECTION_TYPE_CONNECTION,
      required: true,
      description: -> { _('File name of the Google Play service account key.') }

    field :service_account_key,
      required: true,
      description: -> { _('Google Play service account key.') },
      api_only: true

    field :google_play_protected_refs,
      type: :checkbox,
      section: SECTION_TYPE_CONFIGURATION,
      title: -> { s_('GooglePlayStore|Protected branches and tags only') },
      description: -> { _('Set variables on protected branches and tags only.') },
      checkbox_label: -> { s_('GooglePlayStore|Set variables on protected branches and tags only') }

    def self.title
      s_('GooglePlay|Google Play')
    end

    def self.description
      s_('GooglePlay|Use GitLab to build and release an app in Google Play.')
    end

    def self.help
      variable_list = [
        ActionController::Base.helpers.content_tag(:code, "SUPPLY_PACKAGE_NAME"),
        ActionController::Base.helpers.content_tag(:code, "SUPPLY_JSON_KEY_DATA")
      ]

      docs_link = ActionController::Base.helpers.link_to(
        '',
        Rails.application.routes.url_helpers.help_page_url('user/project/integrations/google_play.md'),
        target: '_blank',
        rel: 'noopener noreferrer'
      )
      tag_pair_docs_link = tag_pair(docs_link, :link_start, :link_end)

      texts = [
        s_("Use this integration to connect to Google Play with fastlane in CI/CD pipelines."),
        s_("After you enable the integration, the following protected variables are created for CI/CD use:"),
        ActionController::Base.helpers.safe_join(variable_list, ActionController::Base.helpers.tag(:br)),
        safe_format(s_("For more information, see the %{link_start}documentation%{link_end}."), tag_pair_docs_link)
      ]

      ActionController::Base.helpers.safe_join(texts, ActionController::Base.helpers.tag(:br) * 2)
    end

    def self.to_param
      'google_play'
    end

    def self.supported_events
      []
    end

    def sections
      [
        {
          type: SECTION_TYPE_GOOGLE_PLAY,
          title: s_('Integrations|Integration details'),
          description: help
        }
      ]
    end

    def test(*_args)
      client.list_reviews(package_name)
      { success: true }
    rescue Google::Apis::ClientError => error
      { success: false, message: error }
    end

    def ci_variables(protected_ref:)
      return [] unless activated?
      return [] if google_play_protected_refs && !protected_ref

      [
        { key: 'SUPPLY_JSON_KEY_DATA', value: service_account_key, masked: true, public: false },
        { key: 'SUPPLY_PACKAGE_NAME', value: package_name, masked: false, public: false }
      ]
    end

    def initialize_properties
      super
      self.google_play_protected_refs = true if google_play_protected_refs.nil?
    end

    private

    def client
      service = Google::Apis::AndroidpublisherV3::AndroidPublisherService.new # rubocop: disable CodeReuse/ServiceClass

      service.authorization = Google::Auth::ServiceAccountCredentials.make_creds(
        json_key_io: StringIO.new(service_account_key),
        scope: [Google::Apis::AndroidpublisherV3::AUTH_ANDROIDPUBLISHER]
      )

      service
    end
  end
end