Skip to content

Commit

Permalink
Merge Develop to Main (#52)
Browse files Browse the repository at this point in the history
* Update README.md (#45)

* Move github release info to helper

* Added new placeholder translation

* Removed view typo

* Update Octokit credentials

* Improve/octokit caching (#51)

* Improve Octokit error handling (#50)

* Update README.md (#45)

* Move github release info to helper

* Added new placeholder translation

* Removed view typo

* Update Octokit credentials

* Octokit caching
  • Loading branch information
rossme authored Oct 25, 2023
1 parent 82874cb commit c74d82b
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 14 deletions.
31 changes: 17 additions & 14 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,28 @@ def flash_class(level)
end

def github_release_details
# Check if the personal access token has expired, Octokit then raises an exception
return I18n.t('application.release_details.error') unless latest_release && latest_deploy
return I18n.t('application.release_details.error') unless octokit_request

I18n.t('application.release_details.info', latest_release: latest_release, latest_deploy: latest_deploy)
I18n.t('application.release_details.info',
latest_release: octokit_request[:latest_release],
latest_deploy: octokit_request[:latest_deploy]
)
end

def latest_release
Octokit.latest_release('rossme/formless')&.tag_name
rescue Octokit::Unauthorized
nil # this error is handled in github_release_details
def remote_ip_address
request.remote_ip
end

def latest_deploy
Octokit.deployments('rossme/formless').first[:updated_at]
rescue Octokit::Unauthorized
# `rails dev:cache` toggles caching in development environment
def octokit_request
@octokit_request ||= Rails.cache.fetch('octokit_request', expires_in: 12.hours) do
Rails.logger.info(I18n.t('application.release_details.caching'))
{
latest_release: Octokit.latest_release('rossme/formless')&.tag_name,
latest_deploy: Octokit.deployments('rossme/formless').first[:updated_at]
}
end
rescue Octokit::Unauthorized, Octokit::TooManyRequests
nil # this error is handled in github_release_details
end

def remote_ip_address
request.remote_ip
end
end
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ en:
release_details:
info: "Latest release: %{latest_release} deployed %{latest_deploy}"
error: "Unable to retrieve GitHub release details with Octokit. Your access token may have expired."
caching: "Cache miss: Executing code block for octokit_request."
views:
prompt:
form:
Expand Down
16 changes: 16 additions & 0 deletions spec/config/initializers/octokit_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

require 'rails_helper'
require 'support/devise'

RSpec.describe Octokit, type: :model do
describe 'configuration' do
it 'should have a login' do
expect(Octokit.login).to eq(ENV['GITHUB_USERNAME'])
end

it 'should have an access token' do
expect(Octokit.access_token).to eq(ENV['GITHUB_PERSONAL_ACCESS_TOKEN'])
end
end
end
37 changes: 37 additions & 0 deletions spec/helpers/application_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

require 'rails_helper'
require 'support/devise'

RSpec.describe ApplicationHelper, type: :helper do
describe '#octokit_request' do
it 'should be a hash' do
expect(helper.octokit_request).to be_a(Hash)
end

it 'should have a latest_release key' do
expect(helper.octokit_request).to have_key(:latest_release)
end

it 'should have a latest_deploy key' do
expect(helper.octokit_request).to have_key(:latest_deploy)
end

it 'should have a latest_release value' do
expect(helper.octokit_request[:latest_release]).to be_present
end

it 'should store the request in cache' do
expect(Rails.cache).to receive(:fetch).with('octokit_request', expires_in: 12.hours)
helper.octokit_request
end

it 'should raise an error if the latest release request fails' do
allow(Octokit).to receive(:latest_release).and_raise(Octokit::Unauthorized)
end

it 'should raise an error if the deployments request fails' do
allow(Octokit).to receive(:deployments).and_raise(Octokit::Unauthorized)
end
end
end

0 comments on commit c74d82b

Please sign in to comment.