diff --git a/Gemfile b/Gemfile index adb5f433d..24e93f47c 100644 --- a/Gemfile +++ b/Gemfile @@ -34,6 +34,7 @@ group :development, :test do end group :test do + gem 'climate_control' gem 'shoulda-matchers', '~> 5.0' gem 'webmock' end diff --git a/Gemfile.lock b/Gemfile.lock index e755a3307..c5cca8ce3 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -90,6 +90,7 @@ GEM builder (3.2.4) byebug (11.1.3) cancancan (3.4.0) + climate_control (1.2.0) coderay (1.1.3) concurrent-ruby (1.2.0) crack (0.4.5) @@ -307,6 +308,7 @@ DEPENDENCIES aws-sdk-s3 bootsnap cancancan (~> 3.3) + climate_control dotenv-rails factory_bot_rails faker diff --git a/app/controllers/github_webhooks_controller.rb b/app/controllers/github_webhooks_controller.rb index 0a71e0b7d..164078a71 100644 --- a/app/controllers/github_webhooks_controller.rb +++ b/app/controllers/github_webhooks_controller.rb @@ -4,8 +4,7 @@ class GithubWebhooksController < ActionController::API include GithubWebhook::Processor def github_push(payload) - UploadJob.perform_later if payload['ref'] == ENV.fetch('https://github.com/GITHUB_WEBHOOK_REF') - head :ok + UploadJob.perform_later if payload[:ref] == ENV.fetch('https://github.com/GITHUB_WEBHOOK_REF') && edited_code?(payload) end private @@ -13,4 +12,10 @@ def github_push(payload) def webhook_secret(_payload) ENV.fetch('https://github.com/GITHUB_WEBHOOK_SECRET') end + + def edited_code?(payload) + commits = payload[:commits] + modified_paths = commits.map { |commit| commit[:added] | commit[:modified] | commit[:removed] }.flatten + modified_paths.count { |path| path.start_with?('en/code') }.positive? + end end diff --git a/spec/requests/github_webhooks/github_webhooks_controller_push_spec.rb b/spec/requests/github_webhooks/github_webhooks_controller_push_spec.rb new file mode 100644 index 000000000..3f48f496b --- /dev/null +++ b/spec/requests/github_webhooks/github_webhooks_controller_push_spec.rb @@ -0,0 +1,76 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe GithubWebhooksController do + around do |example| + ClimateControl.modify GITHUB_WEBHOOK_SECRET: 'secret', GITHUB_WEBHOOK_REF: 'branches/whatever' do + example.run + end + end + + let(:params) do + { + ref:, + commits: + } + end + + let(:headers) do + { + 'X-Hub-Signature-256': "sha256=#{OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), ENV.fetch('https://github.com/GITHUB_WEBHOOK_SECRET'), params.to_json)}", + 'X-GitHub-Event': 'push', + 'Content-Type': 'application/json' + } + end + + before do + allow(UploadJob).to receive(:perform_later) + post '/github_webhooks', env: { RAW_POST_DATA: params.to_json }, headers: + end + + shared_examples 'upload job' do + it 'schedules the job' do + expect(UploadJob).to have_received(:perform_later) + end + end + + describe 'when webhook ref matches branch of interest on github push' do + let(:ref) { 'branches/whatever' } + + context 'when code has been added' do + let(:commits) { [{ added: ['en/code/project1/main.py'], modified: [], removed: [] }] } + + it_behaves_like 'upload job' + end + + context 'when code has been modified' do + let(:commits) { [{ added: [], modified: ['en/code/project1/main.py'], removed: [] }] } + + it_behaves_like 'upload job' + end + + context 'when code has been removed' do + let(:commits) { [{ added: [], modified: [], removed: ['en/code/project1/main.py'] }] } + + it_behaves_like 'upload job' + end + + context 'when code has not been changed' do + let(:commits) { [{ added: ['en/step2.md'], modified: ['en/step1.md'], removed: ['en/step0.md'] }] } + + it 'does not schedule the upload job' do + expect(UploadJob).not_to have_received(:perform_later) + end + end + end + + describe 'when webhook ref does not match branch of interest on github push' do + let(:ref) { 'branches/master' } + let(:commits) { [{ added: [], modified: ['en/code/project1/main.py'], removed: [] }] } + + it 'does not schedule the upload job' do + expect(UploadJob).not_to have_received(:perform_later) + end + end +end