Skip to content

Logic to schedule upload job #131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ group :development, :test do
end

group :test do
gem 'climate_control'
gem 'shoulda-matchers', '~> 5.0'
gem 'webmock'
end
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -307,6 +308,7 @@ DEPENDENCIES
aws-sdk-s3
bootsnap
cancancan (~> 3.3)
climate_control
dotenv-rails
factory_bot_rails
faker
Expand Down
9 changes: 7 additions & 2 deletions app/controllers/github_webhooks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ class GithubWebhooksController < ActionController::API
include GithubWebhook::Processor

def github_push(payload)
UploadJob.perform_later if payload['ref'] == ENV.fetch('GITHUB_WEBHOOK_REF')
head :ok
UploadJob.perform_later if payload[:ref] == ENV.fetch('GITHUB_WEBHOOK_REF') && edited_code?(payload)
end

private

def webhook_secret(_payload)
ENV.fetch('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
Original file line number Diff line number Diff line change
@@ -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('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