Class: Readiness::SupportSuperFormProcessor::CreateArticle

Inherits:
Client
  • Object
show all
Defined in:
lib/support_readiness/support_super_form_processor/create_article.rb

Overview

Defines the class CreateArticle within the module Zendesk.

Author:

  • Jason Colyer

Since:

  • 1.0.42

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

.article_comment_message(articles) ⇒ Object

Returns string for comment body

Author:

  • Jason Colyer

Since:

  • 1.0.42



335
336
337
338
339
340
341
342
343
344
345
# File 'lib/support_readiness/support_super_form_processor/create_article.rb', line 335

def self.article_comment_message(articles)
  <<~STRING
    We have added the article to the requested Zendesk instance(s). You can see the article at the following link(s) once the sync completes (in the next 5-10 minutes):

    #{article_links_comment_string(articles)}

    If you see any issues, please reopen this issue and post in the support operations channel in Slack.

    /close
  STRING
end

.article_links_comment_string(articles) ⇒ Object

Returns string for article links

Author:

  • Jason Colyer

Since:

  • 1.0.42



352
353
354
355
356
357
# File 'lib/support_readiness/support_super_form_processor/create_article.rb', line 352

def self.article_links_comment_string(articles)
  out = []
    out.push("- https://support.gitlab.com/hc/en-us/articles/#{articles[:global].id}") unless articles[:global].nil?
    out.push("- https://federal-support.gitlab.com/hc/en-us/articles/#{articles[:usgov].id}") unless articles[:usgov].nil?
    out.join("\n")
end

.article_titleObject

Sets the global variable article_title

Author:

  • Jason Colyer

Since:

  • 1.0.42



265
266
267
# File 'lib/support_readiness/support_super_form_processor/create_article.rb', line 265

def self.article_title
  @article_title ||= determine_article_title
end

.create_article_commits(articles) ⇒ Object

Commit article files to the repos

Author:

  • Jason Colyer

Since:

  • 1.0.42



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
# File 'lib/support_readiness/support_super_form_processor/create_article.rb', line 47

def self.create_article_commits(articles)
  unless articles[:global].nil?
    repo_project = Readiness::GitLab::Projects.find!(@gitlab_client, 52796747)
    commit_params = {
      branch: 'master',
      commit_message: 'Creating new article file',
      actions: [
        {
          file_path: "data/articles/active/#{create_article_link.split('/').last.gsub('.md', '.yaml')}",
          action: 'create',
          content: global_file_content(articles[:global])
        }
      ]
    }
    Readiness::GitLab::Repositories.create_commit!(@gitlab_client, repo_project, commit_params)
  end
  unless articles[:usgov].nil?
    repo_project = Readiness::GitLab::Projects.find!(@gitlab_client, 53653025)
    commit_params = {
      branch: 'master',
      commit_message: 'Creating new article file',
      actions: [
        {
          file_path: "data/articles/active/#{create_article_link.split('/').last.gsub('.md', '.yaml')}",
          action: 'create',
          content: usgov_file_content(articles[:usgov])
        }
      ]
    }
    Readiness::GitLab::Repositories.create_commit!(@gitlab_client, repo_project, commit_params)
  end
end

.create_article_linkObject

Sets the global variable create_article_link

Author:

  • Jason Colyer

Since:

  • 1.0.42



256
257
258
# File 'lib/support_readiness/support_super_form_processor/create_article.rb', line 256

def self.create_article_link
  @create_article_link ||= ENV.fetch('CREATE_ARTICLE_LINK')
end

.create_articles_in_zendeskObject

Return a Hash of Article objects

Author:

  • Jason Colyer

Since:

  • 1.0.42



152
153
154
155
156
157
# File 'lib/support_readiness/support_super_form_processor/create_article.rb', line 152

def self.create_articles_in_zendesk
  {
    global: create_global_article,
    usgov: create_usgov_article
  }
end

.create_global_articleObject

Create an article in Zendesk Global

Author:

  • Jason Colyer

Since:

  • 1.0.42



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/support_readiness/support_super_form_processor/create_article.rb', line 164

def self.create_global_article
  return nil unless create_article_link =~ /all_instances/ || create_article_link =~ /global_only/

  article = Readiness::Zendesk::Articles.new
  article.author_id = 365068329233
  article.body = "Placeholder for #{create_article_link}"
  article.comments_disabled = true
  article.draft = true
  article.locale = 'en-us'
  article.permission_group_id = 113047
  article.title = article_title
  article.user_segment_id = 1536307
  article.section_id = section_id('Global')
  Readiness::Zendesk::Articles.create!(@zendesk_global_client, article)
end

.create_usgov_articleObject

Create an article in Zendesk US Government

Author:

  • Jason Colyer

Since:

  • 1.0.42



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/support_readiness/support_super_form_processor/create_article.rb', line 185

def self.create_usgov_article
  return nil unless create_article_link =~ /all_instances/ || create_article_link =~ /us_government_only/

  article = Readiness::Zendesk::Articles.new
  article.author_id = 381755018552
  article.body = "Placeholder for #{create_article_link}"
  article.comments_disabled = true
  article.draft = true
  article.locale = 'en-us'
  article.permission_group_id = 2721132
  article.title = article_title
  article.user_segment_id = 360001187072
  article.section_id = section_id('US Government')
  Readiness::Zendesk::Articles.create!(@zendesk_usgov_client, article)
end

.determine_article_titleObject

Determines the article title

Author:

  • Jason Colyer

Since:

  • 1.0.42



274
275
276
277
278
279
# File 'lib/support_readiness/support_super_form_processor/create_article.rb', line 274

def self.determine_article_title
  path = create_article_link.split('master/').last.split('?').first
  pages_project = Readiness::GitLab::Projects.find!(@gitlab_client, 52066066)
  file_contents = Readiness::GitLab::Repositories.raw_file(@gitlab_client, pages_project, path, 'master')
  file_contents.split("\n").detect { |f| f =~ /^\#\ / }.split('# ').last.gsub('`', '').gsub("'", '"')
end

.errorsObject

Sets the global variable errors

Author:

  • Jason Colyer

Since:

  • 1.0.42



220
221
222
# File 'lib/support_readiness/support_super_form_processor/create_article.rb', line 220

def self.errors
  @errors ||= []
end

.global_file_content(article) ⇒ Object

Returns a string for the Global article sync repo

Author:

  • Jason Colyer

Since:

  • 1.0.42



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/support_readiness/support_super_form_processor/create_article.rb', line 85

def self.global_file_content(article)
  <<~STRING
    ---
    id: #{article.id}
    author_id: 365068329233
    comments_disabled: true
    draft: false
    promoted: false
    position: 0
    name: '#{article_title}'
    title: '#{article_title}'
    locale: 'en-us'
    user_segment_id: null
    permission_group_id: 113047
    label_names: []
    body: null
    section_id: #{section_id('Global')}
    content_path: '#{create_article_link.split(%r{master/(knowledge-base|support-pages)/}).last}'
  STRING
end

.instance_blockObject

Returns string for displaying what instances this applies to

Author:

  • Jason Colyer

Since:

  • 1.0.42



286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/support_readiness/support_super_form_processor/create_article.rb', line 286

def self.instance_block
  output = []
  if create_article_link =~ /all_instances/ || create_article_link =~ /global_only/
    output.push('  - [x] Global')
  else
    output.push('  - [~] Global')
  end
  if create_article_link =~ /all_instances/ || create_article_link =~ /us_government_only/
    output.push('  - [x] US Government')
  else
    output.push('  - [~] US Government')
  end
  output.join("\n")
end

.issue_messageObject

Returns string for issue body

Author:

  • Jason Colyer

Since:

  • 1.0.42



306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/support_readiness/support_super_form_processor/create_article.rb', line 306

def self.issue_message
  <<~STRING
    ## Create a new article

    Greetings @#{requester.username} !

    This is a request to create a new article within Zendesk. The details are as follows:

    - Link to article file: #{create_article_link}
    - Zendesk Instance(s):
    #{instance_block}

    ---

    ### Support Readiness Process

    This process should be completely automated. If you see any issues, please rectify them and notify Jason so the processor can be looked into.

    /label ~"administrative" ~"Stage::Completed" ~zendesk ~"Customer::Support" ~"p4"

    /assign @jcolyer @dtragjasi
  STRING
end

.process!(gitlab_client, gitlab_admin_client, zendesk_global_client, zendesk_usgov_client) ⇒ Object

Process a Create Article request

Author:

  • Jason Colyer

Since:

  • 1.0.42



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/support_readiness/support_super_form_processor/create_article.rb', line 18

def self.process!(gitlab_client, gitlab_admin_client, zendesk_global_client, zendesk_usgov_client)
  @gitlab_client = gitlab_client
  @gitlab_admin_client = gitlab_admin_client
  @zendesk_global_client = zendesk_global_client
  @zendesk_usgov_client = zendesk_usgov_client
  requester
  request_checks
  unless errors.count.zero?
    Readiness::SupportSuperFormProcessor::Shared.invalid_article_creation(requester, errors)
  end
  issue = Readiness::GitLab::Issues.new
  issue.title = 'Create a new article'
  issue.description = issue_message
  create = Readiness::GitLab::Issues.create!(@gitlab_client, project, issue)
  puts "Issue created: #{create.web_url}"
  articles = create_articles_in_zendesk
  create_article_commits(articles)
  comment_params = {
    body: article_comment_message(articles)
  }
  comment = Readiness::GitLab::Issues.create_comment!(@gitlab_client, project, create, comment_params)
  exit 0
end

.projectObject

Sets the global variable project

Author:

  • Jason Colyer

Since:

  • 1.0.42



247
248
249
# File 'lib/support_readiness/support_super_form_processor/create_article.rb', line 247

def self.project
  @project ||= Readiness::GitLab::Projects.find!(@gitlab_client, 68534119)
end

.request_checksObject

Perform the needed checks for article creation

Author:

  • Jason Colyer

Since:

  • 1.0.42



206
207
208
209
210
211
212
213
# File 'lib/support_readiness/support_super_form_processor/create_article.rb', line 206

def self.request_checks
  pages_project = Readiness::GitLab::Projects.find!(@gitlab_client, 52066066)
  path = create_article_link.split('master/').last.split('?').first
  file = Readiness::GitLab::Repositories.file(@gitlab_client, pages_project, path, 'master')
  unless file.key? 'file_name'
    errors.push("No such article file: #{path}")
  end
end

.requesterObject

Sets the global variable requester

Author:

  • Jason Colyer

Since:

  • 1.0.42



229
230
231
# File 'lib/support_readiness/support_super_form_processor/create_article.rb', line 229

def self.requester
  @requester ||= Readiness::SupportSuperFormProcessor::Shared.gitlab_user_check(@gitlab_admin_client, requester_email)
end

.requester_emailObject

Sets the global variable requester_email

Author:

  • Jason Colyer

Since:

  • 1.0.42



238
239
240
# File 'lib/support_readiness/support_super_form_processor/create_article.rb', line 238

def self.requester_email
  @requester_email ||= ENV.fetch('REQUESTER_EMAIL')
end

.section_id(instance) ⇒ Object

Retunrs the correct section ID to use

Author:

  • Jason Colyer

Since:

  • 1.0.42



137
138
139
140
141
142
143
144
145
# File 'lib/support_readiness/support_super_form_processor/create_article.rb', line 137

def self.section_id(instance)
  path = create_article_link.split('master/').last.split('/').first
  return 360004459140 if instance == 'Global' && path == 'support-pages'
  return 15215649512604 if instance == 'Global' && path == 'knowledge-base'
  return 10593044624020 if instance == 'US Government' && path == 'support-pages'
  return 29015014994068 if instance == 'US Government' && path == 'knowledge-base'

  nil
end

.usgov_file_content(article) ⇒ Object

Returns a string for the US Gov article sync repo

Author:

  • Jason Colyer

Since:

  • 1.0.42



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/support_readiness/support_super_form_processor/create_article.rb', line 111

def self.usgov_file_content(article)
  <<~STRING
    ---
    id: #{article.id}
    author_id: 381755018552
    comments_disabled: true
    draft: false
    promoted: false
    position: 0
    name: '#{article_title}'
    title: '#{article_title}'
    locale: 'en-us'
    user_segment_id: null
    permission_group_id: 2721132
    label_names: []
    body: null
    section_id: #{section_id('US Government')}
    content_path: '#{create_article_link.split(%r{master/(knowledge-base|support-pages)/}).last}'
  STRING
end