fix: update GitHub versions link and version fetching (#105)

* fix: update GitHub versions link and version fetching

* test: specs

* chore: use timeout and log error

* refactor: redundant helper method
This commit is contained in:
Gabriel Jablonski 2025-08-28 10:50:03 -03:00 committed by GitHub
parent 9f5c62c724
commit dbb41df67e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 104 additions and 12 deletions

View File

@ -73,7 +73,7 @@ export default {
v-if="shouldShowBanner"
color-scheme="primary"
:banner-message="bannerMessage"
href-link="https://github.com/chatwoot/chatwoot/releases"
href-link="https://github.com/fazer-ai/chatwoot/releases"
:href-link-text="$t('GENERAL_SETTINGS.LEARN_MORE')"
has-close-button
@close="dismissUpdateBanner"

View File

@ -4,16 +4,23 @@ class Internal::CheckNewVersionsJob < ApplicationJob
def perform
return unless Rails.env.production?
@instance_info = ChatwootHub.sync_with_hub
update_version_info
latest_version = fetch_latest_github_release
::Redis::Alfred.set(::Redis::Alfred::LATEST_CHATWOOT_VERSION, latest_version) if latest_version.present?
end
private
def update_version_info
return if @instance_info['version'].blank?
def fetch_latest_github_release
response = HTTParty.get('https://api.github.com/repos/fazer-ai/chatwoot/releases/latest', timeout: 5)
unless response.success?
Rails.logger.error "Failed to fetch latest GitHub release: HTTP #{response.code} - #{response.body}"
return nil
end
::Redis::Alfred.set(::Redis::Alfred::LATEST_CHATWOOT_VERSION, @instance_info['version'])
response['tag_name']&.sub(/^v/, '')
rescue StandardError => e
Rails.logger.error "Failed to fetch latest GitHub release: #{e.message}"
nil
end
end

View File

@ -3,12 +3,97 @@ require 'rails_helper'
RSpec.describe Internal::CheckNewVersionsJob do
subject(:job) { described_class.perform_now }
it 'updates the latest chatwoot version in redis' do
data = { 'version' => '1.2.3' }
before do
allow(Rails.env).to receive(:production?).and_return(true)
allow(ChatwootHub).to receive(:sync_with_hub).and_return(data)
job
expect(ChatwootHub).to have_received(:sync_with_hub)
expect(Redis::Alfred.get(Redis::Alfred::LATEST_CHATWOOT_VERSION)).to eq data['version']
Redis::Alfred.delete(Redis::Alfred::LATEST_CHATWOOT_VERSION)
end
describe '#perform' do
context 'when GitHub API returns successful response' do
before do
stub_request(:get, 'https://api.github.com/repos/fazer-ai/chatwoot/releases/latest')
.to_return(
status: 200,
body: { tag_name: 'v1.2.3' }.to_json,
headers: { 'Content-Type' => 'application/json' }
)
end
it 'updates the latest chatwoot version in redis with v prefix removed' do
job
expect(Redis::Alfred.get(Redis::Alfred::LATEST_CHATWOOT_VERSION)).to eq '1.2.3'
end
end
context 'when tag name has no v prefix' do
before do
stub_request(:get, 'https://api.github.com/repos/fazer-ai/chatwoot/releases/latest')
.to_return(
status: 200,
body: { tag_name: '1.2.3' }.to_json,
headers: { 'Content-Type' => 'application/json' }
)
end
it 'handles tag names without v prefix' do
job
expect(Redis::Alfred.get(Redis::Alfred::LATEST_CHATWOOT_VERSION)).to eq '1.2.3'
end
end
context 'when GitHub API returns failed response' do
before do
stub_request(:get, 'https://api.github.com/repos/fazer-ai/chatwoot/releases/latest')
.to_return(status: 404)
allow(Rails.logger).to receive(:error)
end
it 'does not update redis when response is not successful' do
expect { job }.not_to change { Redis::Alfred.get(Redis::Alfred::LATEST_CHATWOOT_VERSION) }.from(nil)
expect(Rails.logger).to have_received(:error).with(/Failed to fetch latest GitHub release: HTTP 404 - /)
end
end
context 'when GitHub API raises an error' do
let(:error_message) { 'Network timeout' }
before do
stub_request(:get, 'https://api.github.com/repos/fazer-ai/chatwoot/releases/latest')
.to_raise(StandardError.new(error_message))
allow(Rails.logger).to receive(:error)
end
it 'logs the error and does not update redis' do
expect { job }.not_to change { Redis::Alfred.get(Redis::Alfred::LATEST_CHATWOOT_VERSION) }.from(nil)
expect(Rails.logger).to have_received(:error).with("Failed to fetch latest GitHub release: #{error_message}")
end
end
context 'when not in production environment' do
before do
allow(Rails.env).to receive(:production?).and_return(false)
end
it 'does not make any API calls or update redis' do
expect { job }.not_to change { Redis::Alfred.get(Redis::Alfred::LATEST_CHATWOOT_VERSION) }.from(nil)
expect(a_request(:get, 'https://api.github.com/repos/fazer-ai/chatwoot/releases/latest')).not_to have_been_made
end
end
context 'when tag_name is nil' do
before do
stub_request(:get, 'https://api.github.com/repos/fazer-ai/chatwoot/releases/latest')
.to_return(
status: 200,
body: { tag_name: nil }.to_json,
headers: { 'Content-Type' => 'application/json' }
)
end
it 'does not update redis when tag_name is nil' do
expect { job }.not_to change { Redis::Alfred.get(Redis::Alfred::LATEST_CHATWOOT_VERSION) }.from(nil)
end
end
end
end