Class: Readiness::TicketProcessor::CMPCreation

Inherits:
Client
  • Object
show all
Defined in:
lib/support_readiness/ticket_processor/cmp_creation.rb

Overview

Defines the class CMPCreation within the module Zendesk.

Author:

  • Jason Colyer

Since:

  • 1.0.124

Class Method Summary collapse

Methods inherited from Client

auth_error, bad_request_error, convert_actions, convert_conditions, convert_standard_names_to_ids, convert_ticket_form_agent_conditions, convert_ticket_form_brands, convert_ticket_form_end_user_conditions, convert_ticket_form_names_to_ids, convert_view_names_to_ids, convert_view_restrictions, covert_ticket_form_field_ids, create_package!, erb_renderer, handle_request_error, not_found_error, not_processible_error, put_into_archive, recursively_deflate_directory, timestamp_filename, to_clean_json, to_clean_json_with_key, to_hash, to_nearly_clean_json, to_nearly_clean_json_with_key, to_param_string, write_entries

Class Method Details

.create_projectObject

Creates the actual CMP project

Author:

  • Jason Colyer

Since:

  • 1.0.124



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
127
128
# File 'lib/support_readiness/ticket_processor/cmp_creation.rb', line 63

def self.create_project
  new_project = Readiness::GitLab::Projects.new
  new_project.name = "Organization #{organization.id}"
  new_project.path = "organization-#{organization.id}"
  new_project.namespace_id = ENV.fetch('GROUP_ID')
  new_project.analytics_access_level = 'disabled'
  new_project.approvals_before_merge = 0
  new_project.auto_cancel_pending_pipelines = 'enabled'
  new_project.auto_devops_deploy_strategy = 'continuous'
  new_project.auto_devops_enabled = false
  new_project.autoclose_referenced_issues = true
  new_project.build_timeout = 3600
  new_project.ci_config_path = ''
  new_project.ci_forward_deployment_enabled = false
  new_project.container_expiration_policy = {
    "cadence" => 'id',
    "enabled" => false,
    "keep_n" => 10,
    "older_than" => '90d',
    "name_regex" => '.*',
    "name_regex_keep" => nil
  }
  new_project.container_registry_access_level = 'disabled'
  new_project.container_registry_enabled = false
  new_project.emails_disabled = false
  new_project.emails_enabled = true
  new_project.environments_access_level = 'disabled'
  new_project.external_authorization_classification_label = ''
  new_project.feature_flags_access_level = 'disabled'
  new_project.forking_access_level = 'disabled'
  new_project.group_runners_enabled = false
  new_project.infrastructure_access_level = 'disabled'
  new_project.issues_enabled = false
  new_project.jobs_enabled = false
  new_project.lfs_enabled = false
  new_project.merge_method = 'merge'
  new_project.merge_pipelines_enabled = false
  new_project.merge_requests_enabled = true
  new_project.merge_trains_enabled = false
  new_project.merge_trains_skip_train_allowed = false
  new_project.mirror = false
  new_project.model_experiments_access_level = 'disabled'
  new_project.model_registry_access_level = 'disabled'
  new_project.monitor_access_level = 'disabled'
  new_project.only_allow_merge_if_all_discussions_are_resolved = false
  new_project.only_allow_merge_if_pipeline_succeeds = false
  new_project.packages_enabled = false
  new_project.pages_access_level = 'disabled'
  new_project.printing_merge_request_link_enabled = true
  new_project.public_jobs = true
  new_project.releases_access_level = 'disabled'
  new_project.remove_source_branch_after_merge = true
  new_project.request_access_enabled = false
  new_project.requirements_access_level = 'disabled'
  new_project.requirements_enabled = false
  new_project.resolve_outdated_diff_discussions = false
  new_project.security_and_compliance_access_level = 'disabled'
  new_project.service_desk_enabled = false
  new_project.shared_runners_enabled = false
  new_project.snippets_enabled = false
  new_project.squash_option = 'default_off'
  new_project.visibility = private
  new_project.warn_about_potentially_unwanted_characters = true
  new_project.wiki_enabled = false
  Readiness::GitLab::Projects.create!(@gitlab_client, new_project)
end

.create_readme_fileObject

Makes a commit to populate README.md

Author:

  • Jason Colyer

Since:

  • 1.0.124



172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/support_readiness/ticket_processor/cmp_creation.rb', line 172

def self.create_readme_file
  commit_params = {
    branch: 'master',
    commit_message: 'Creating CONTACTS.md file',
    actions: [
      {
        file_path: 'CONTACTS.md',
        content: readme_contents,
        action: 'create'
      }
    ]
  }
  Readiness::GitLab::Repositories.create_commit!(@gitlab_client, @project, commit_params)
end

.create_webhookObject

Creates the CMP project’s webhook

Author:

  • Jason Colyer

Since:

  • 1.0.124



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/support_readiness/ticket_processor/cmp_creation.rb', line 135

def self.create_webhook
  token = ENV.fetch('CMP_TOKEN')
  new_hook = Readiness::GitLab::ProjectWebhooks.new
  new_hook.url = "https://gitlab.com/api/v4/projects/39250741/ref/master/trigger/pipeline?token=#{token}&variables[PROJECT_ID]=#{@project.id}"
  new_hook.push_events = true
  new_hook.alert_status = 'executable'
  new_hook.push_events_branch_filter = 'master'
  new_hook.branch_filter_strategy = 'wildcard'
  new_hook.project_id = @project.id
  Readiness::GitLab::ProjectWebhooks.create!(@gitlab_client, new_hook)
end

.organizationObject

Fetches the organization

Author:

  • Jason Colyer

Since:

  • 1.0.124



45
46
47
# File 'lib/support_readiness/ticket_processor/cmp_creation.rb', line 45

def self.organization
  @organization ||= Readiness::Zendesk::Organizations.find!(@zendesk_client, ticket.organization_id)
end

.organization_usersObject

Fetches the organization users

Author:

  • Jason Colyer

Since:

  • 1.0.124



54
55
56
# File 'lib/support_readiness/ticket_processor/cmp_creation.rb', line 54

def self.organization_users
  @organization_users ||= Readiness::Zendesk::Organizations.users(@zendesk_client, organization)
end

.populate_contacts_fileObject

Makes a commit to populate contacts.yaml

Author:

  • Jason Colyer

Since:

  • 1.0.124



152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/support_readiness/ticket_processor/cmp_creation.rb', line 152

def self.populate_contacts_file
  commit_params = {
    branch: 'master',
    commit_message: 'Populating initial contacts',
    actions: [
      {
        file_path: 'contacts.yaml',
        content: yaml_contents,
        action: 'create'
      }
    ]
  }
  Readiness::GitLab::Repositories.create_commit!(@gitlab_client, @project, commit_params)
end

.process!(zendesk_client, gitlab_client, email_list) ⇒ Object

Process a CMP Creation request

Author:

  • Jason Colyer

Since:

  • 1.0.124



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/support_readiness/ticket_processor/cmp_creation.rb', line 18

def self.process!(zendesk_client, gitlab_client, email_list)
  @zendesk_client = zendesk_client
  @gitlab_client = gitlab_client
  @email_list = email_list
  @project = create_project
  create_readme_file
  create_webhook
  populate_contacts_file
  send_invitations
  update_organization
  update_ticket
end

.readme_contentsObject

Returns the contents for the README.md file

Author:

  • Jason Colyer

Since:

  • 1.0.124



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/support_readiness/ticket_processor/cmp_creation.rb', line 204

def self.readme_contents
  <<~STRING
    # Zendesk Global Organization Contact Syncing

    ## NOTE

    This is a beta feature that is very likely to be deprecated in the future. Should it be deprecated, any existing contact management projects would be deactivated. If you are over 30 support contacts at that time, you would be unable to add more until the contacts were removed to get back to the 30 maximum limit.

    ## How it works

    You edit the contacts.yaml file. The changes trigger a pipeline on our Ops instance. This syncs the changes over to the global support portal. Your organization's contacts then match the contacts.yaml file exactly.

    ## Adding a contact

    To add a contact, add a new line at the bottom of the contact.yaml file in the following format:

    ```
    - name: NAME_OF_CONTACT
      email: EMAIL_OF_CONTACT
    ```

    Replacing `NAME_OF_CONTACT` with the contact's name and `EMAIL_OF_CONTACT` with the email of the contact.

    Commit the changes and the sync will take care of the rest!

    ## Removing a contact
    
    To remove a contact, just delete the name and email rows from the file of the contact.

    Commit the changes and the sync will take care of the rest!

    ## Maximum number of contacts
    
    This will only manage the first 50 contacts in a file. Any beyond that are simply ignored by the pipelines.

    ## How can I change who can see this project?

    To get others added or removed, please file a ticket with Support Operations using:
    
    https://support.gitlab.com/hc/en-us/requests/new?ticket_form_id=360001801419
    
    ## Can I use this for important files for the support team?
    
    As the access to this project is very restricted, the greater GitLab teams cannot access this. As such, you should not use this project for the purpose of storing files meant to seen by others.

    ## How do I know if it worked?
    
    Upon success or failure, a badge is added to the project. This can be used to determine the status of the sync.
    
    If you see the badge say the sync has failed, please open a ticket with ops:
    
    https://support.gitlab.com/hc/en-us/requests/new?ticket_form_id=360001801419

    ## What should I do if it didn’t work?

    Please open a ticket with ops:

    https://support.gitlab.com/hc/en-us/requests/new?ticket_form_id=360001801419        
  STRING
end

.send_invitationsObject

Creates project invitations

Author:

  • Jason Colyer

Since:

  • 1.0.124



270
271
272
273
274
275
276
277
278
# File 'lib/support_readiness/ticket_processor/cmp_creation.rb', line 270

def self.send_invitations
  @email_list.each do |e|
    data = {
      email: e,
      access_level: 30
    }
    Readiness::GitLab::Projects.create_invitation!(@gitlab_client, @project, data)
  end
end

.ticketObject

Fetches the ticket

Author:

  • Jason Colyer

Since:

  • 1.0.124



36
37
38
# File 'lib/support_readiness/ticket_processor/cmp_creation.rb', line 36

def self.ticket
  @ticket ||= Readiness::Zendesk::Tickets.find!(@zendesk_client, ENV.fetch('TICKET_ID'))
end

.ticket_commentObject

Returns the contents for the ticket comment

Author:

  • Jason Colyer

Since:

  • 1.0.124



314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/support_readiness/ticket_processor/cmp_creation.rb', line 314

def self.ticket_comment
  <<~STRING
    Greetings,

    We have setup your contact management project at this time. You can find it at #{@project.web_url}

    Any developer without a gitlab.com account should receive an email for the invitation to the project. Any developer with a gitlab.com should be auto-added.

    We have seeded the contacts.yaml file with your organization's current contact list to help get you started.

    Please let us know if you encounter any issues in the use of the contacts management project.
  STRING
end

.update_organizationObject

Adds the CMP ID to the organization

Author:

  • Jason Colyer

Since:

  • 1.0.124



285
286
287
288
289
290
291
292
# File 'lib/support_readiness/ticket_processor/cmp_creation.rb', line 285

def self.update_organization
  new_org = Readiness::Zendesk::Organizations.new
  new_org.id = organization.id
  new_org.organization_fields = {
    cmp_id: @project.id
  }
  Readiness::Zendesk::Organizations.update!(@zendesk_client, new_org)
end

.update_ticketObject

Updates the ticket

Author:

  • Jason Colyer

Since:

  • 1.0.124



299
300
301
302
303
304
305
306
307
# File 'lib/support_readiness/ticket_processor/cmp_creation.rb', line 299

def self.update_ticket
  new_ticket = Readiness::Zendesk::Tickets.new
  new_ticket.id = ticket.id
  new_ticket.status = 'pending'
  new_ticket.comment = {
    body: ticket_comment
  }
  Readiness::Zendesk::Tickets.update!(@zendesk_client, new_ticket)
end

.yaml_contentsObject

Returns the contents for the contacts.yaml file

Author:

  • Jason Colyer

Since:

  • 1.0.124



192
193
194
195
196
197
# File 'lib/support_readiness/ticket_processor/cmp_creation.rb', line 192

def self.yaml_contents
  {
    'org_id' => organization.id,
    'contacts' => organization_users.map { |a| { "name" => a.name, "email" => a.email } }
  }.to_yaml
end