Class: CypressViewportUpdater::GithubService

Inherits:
Object
  • Object
show all
Includes:
SentryLogging
Defined in:
app/sidekiq/cypress_viewport_updater/github_service.rb

Constant Summary collapse

REPO =
'department-of-veterans-affairs/vets-website'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SentryLogging

#log_exception_to_sentry, #log_message_to_sentry, #non_nil_hash?, #normalize_level, #rails_logger, #set_sentry_metadata

Constructor Details

#initializeGithubService

Returns a new instance of GithubService.



11
12
13
14
15
16
17
18
19
# File 'app/sidekiq/cypress_viewport_updater/github_service.rb', line 11

def initialize
  @private_key = OpenSSL::PKey::RSA.new(Settings.github_cvu.private_pem&.gsub('\n', "\n"))
  @installation_id = Settings.github_cvu.installation_id
  @integration_id = Settings.github_cvu.integration_id
  @client = Octokit::Client.new(bearer_token: new_jwt_token)
  response = @client.create_installation_access_token(@installation_id,
                                                      accept: 'application/vnd.github.v3+json')
  @client.bearer_token = response.to_h[:token]
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



9
10
11
# File 'app/sidekiq/cypress_viewport_updater/github_service.rb', line 9

def client
  @client
end

#feature_branch_nameObject

Returns the value of attribute feature_branch_name.



9
10
11
# File 'app/sidekiq/cypress_viewport_updater/github_service.rb', line 9

def feature_branch_name
  @feature_branch_name
end

Instance Method Details

#create_branchObject



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/sidekiq/cypress_viewport_updater/github_service.rb', line 34

def create_branch
  set_feature_branch_name
  ref = "heads/#{feature_branch_name}"

  begin
    sha = @client.ref(REPO, 'heads/main').object.sha
    @client.create_ref(REPO, ref, sha)
  rescue Octokit::ClientError, Octokit::UnprocessableEntity => e
    # :nocov:
    log_exception_to_sentry(e)
    # :nocov:
  end
end

#get_content(file:) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/sidekiq/cypress_viewport_updater/github_service.rb', line 21

def get_content(file:)
  begin
    file.sha = @client.content(REPO, path: file.github_path).sha
    file.raw_content = @client.content(REPO, path: file.github_path, accept: 'application/vnd.github.V3.raw')
  rescue Octokit::ClientError, Octokit::UnprocessableEntity => e
    # :nocov:
    log_exception_to_sentry(e)
    # :nocov:
  end

  self
end

#new_jwt_tokenObject (private)



77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/sidekiq/cypress_viewport_updater/github_service.rb', line 77

def new_jwt_token
  payload = {
    # issued at time
    iat: Time.now.to_i,
    # JWT expiration time (10 minute maximum)
    exp: Time.now.to_i + (10 * 60),
    # GitHub App's identifier
    iss: @integration_id
  }

  JWT.encode(payload, @private_key, 'RS256')
end

#pr_bodyObject (private)



100
101
102
103
104
105
106
107
108
109
# File 'app/sidekiq/cypress_viewport_updater/github_service.rb', line 100

def pr_body
  last_month = Time.zone.today.prev_month.strftime('%m/%Y')

  'Updates `config/cypress.config.js` and ' \
    '`src/platform/testing/e2e/cypress/support/commands/viewportPreset.js` ' \
    "with Google Analytics viewport data from last month (#{last_month}).\n\n" \
    'These files are updated automatically via a Sidekiq job in `vets-api` ' \
    'that runs at noon on the 2nd day of each month to get the analytics data ' \
    'for the previous month. (Google Analytics updates every 24 hours.)'
end

#pr_titleObject (private)



96
97
98
# File 'app/sidekiq/cypress_viewport_updater/github_service.rb', line 96

def pr_title
  'Update Cypress Viewport Data (Automatic Update)'
end

#set_feature_branch_nameObject (private)



90
91
92
93
94
# File 'app/sidekiq/cypress_viewport_updater/github_service.rb', line 90

def set_feature_branch_name
  prefix = DateTime.now.strftime('%m%d%Y%H%M%S%L')
  name = 'update_cypress_viewport_data'
  self.feature_branch_name = "#{prefix}_#{name}"
end

#submit_prObject



61
62
63
64
65
66
67
68
69
70
71
# File 'app/sidekiq/cypress_viewport_updater/github_service.rb', line 61

def submit_pr
  @client.create_pull_request(REPO,
                              'main',
                              feature_branch_name,
                              pr_title,
                              pr_body)
rescue Octokit::ClientError, Octokit::UnprocessableEntity => e
  # :nocov:
  log_exception_to_sentry(e)
  # :nocov:
end

#update_content(file:) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/sidekiq/cypress_viewport_updater/github_service.rb', line 48

def update_content(file:)
  @client.update_content(REPO,
                         file.github_path,
                         "update #{file.name}",
                         file.sha,
                         file.updated_content,
                         branch: feature_branch_name)
rescue Octokit::ClientError, Octokit::UnprocessableEntity => e
  # :nocov:
  log_exception_to_sentry(e)
  # :nocov:
end