From 24d7739541e26b7264daed241f3032b3adbade07 Mon Sep 17 00:00:00 2001 From: loiswells97 Date: Thu, 2 Feb 2023 16:51:56 +0000 Subject: [PATCH 1/6] Create draft PR for #115 From 4c3339455ba181daeb60116c1250658bd0dac2ca Mon Sep 17 00:00:00 2001 From: Lois Wells Date: Fri, 3 Feb 2023 09:34:12 +0000 Subject: [PATCH 2/6] first attempt at getting webhook talking to api --- app/controllers/api/projects_controller.rb | 5 +++++ app/models/ability.rb | 1 + config/environments/development.rb | 3 +++ config/routes.rb | 3 +++ 4 files changed, 12 insertions(+) diff --git a/app/controllers/api/projects_controller.rb b/app/controllers/api/projects_controller.rb index 4ab555d32..d81c09c8d 100644 --- a/app/controllers/api/projects_controller.rb +++ b/app/controllers/api/projects_controller.rb @@ -44,6 +44,11 @@ def destroy head :ok end + def upload + puts 'hello there' + head :ok + end + private def load_project diff --git a/app/models/ability.rb b/app/models/ability.rb index fdac1845a..6f587dba7 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -5,6 +5,7 @@ class Ability def initialize(user) can :show, Project, user_id: nil + can :upload, Project, user_id: nil return if user.blank? diff --git a/config/environments/development.rb b/config/environments/development.rb index 874d92fad..d0e74e562 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -66,4 +66,7 @@ # Uncomment if you wish to allow Action Cable access from any origin. # config.action_cable.disable_request_forgery_protection = true + + # Allow smee requests + config.hosts << 'smee.io' end diff --git a/config/routes.rb b/config/routes.rb index 6dad38ebe..1ed2d0ec0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -8,6 +8,9 @@ end resources :projects, only: %i[index show update destroy create] do + collection do + post :upload + end resource :remix, only: %i[create], controller: 'projects/remixes' resource :images, only: %i[create], controller: 'projects/images' end From 0c86ad903076836a4da93df885d5546e76aafdfc Mon Sep 17 00:00:00 2001 From: Lois Wells Date: Fri, 3 Feb 2023 11:31:22 +0000 Subject: [PATCH 3/6] refactoring to use github_webhooks gem --- .env.example | 2 ++ Gemfile | 1 + Gemfile.lock | 5 +++++ app/controllers/api/projects_controller.rb | 5 ----- app/controllers/github_webhooks_controller.rb | 14 ++++++++++++++ config/routes.rb | 7 +++---- 6 files changed, 25 insertions(+), 9 deletions(-) create mode 100644 app/controllers/github_webhooks_controller.rb diff --git a/.env.example b/.env.example index 7bafedc37..c1b186564 100644 --- a/.env.example +++ b/.env.example @@ -5,6 +5,8 @@ AWS_S3_ACTIVE_STORAGE_BUCKET=changeme AWS_S3_REGION=changeme AWS_SECRET_ACCESS_KEY=changeme +GITHUB_WEBHOOK_SECRET=test_token + POSTGRES_HOST=changeme POSTGRES_USER=changeme POSTGRES_PASSWORD=changeme diff --git a/Gemfile b/Gemfile index e51ba026e..6cb64894b 100644 --- a/Gemfile +++ b/Gemfile @@ -9,6 +9,7 @@ gem 'aws-sdk-s3', require: false gem 'bootsnap', require: false gem 'cancancan', '~> 3.3' gem 'faraday' +gem 'github_webhook', '~> 1.4' gem 'importmap-rails' gem 'jbuilder' gem 'pg', '~> 1.1' diff --git a/Gemfile.lock b/Gemfile.lock index 740404e5c..b6f15aec0 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -113,6 +113,10 @@ GEM faraday-net_http (>= 2.0, < 3.1) ruby2_keywords (>= 0.0.4) faraday-net_http (3.0.2) + github_webhook (1.4.2) + activesupport (>= 4) + rack (>= 1.3) + railties (>= 4) globalid (1.0.0) activesupport (>= 5.0) hashdiff (1.0.1) @@ -280,6 +284,7 @@ DEPENDENCIES factory_bot_rails faker faraday + github_webhook (~> 1.4) importmap-rails jbuilder pg (~> 1.1) diff --git a/app/controllers/api/projects_controller.rb b/app/controllers/api/projects_controller.rb index d81c09c8d..4ab555d32 100644 --- a/app/controllers/api/projects_controller.rb +++ b/app/controllers/api/projects_controller.rb @@ -44,11 +44,6 @@ def destroy head :ok end - def upload - puts 'hello there' - head :ok - end - private def load_project diff --git a/app/controllers/github_webhooks_controller.rb b/app/controllers/github_webhooks_controller.rb new file mode 100644 index 000000000..2661e41d0 --- /dev/null +++ b/app/controllers/github_webhooks_controller.rb @@ -0,0 +1,14 @@ +class GithubWebhooksController < ActionController::API + include GithubWebhook::Processor + + def github_push(payload) + # TODO: handle push webhook + head :ok + end + + private + + def webhook_secret(payload) + ENV['GITHUB_WEBHOOK_SECRET'] + end +end diff --git a/config/routes.rb b/config/routes.rb index 1ed2d0ec0..f48e084c7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -8,11 +8,10 @@ end resources :projects, only: %i[index show update destroy create] do - collection do - post :upload - end resource :remix, only: %i[create], controller: 'projects/remixes' resource :images, only: %i[create], controller: 'projects/images' - end + end end + + resource :github_webhooks, only: :create, defaults: { formats: :json } end From b180d62a7b51855a1f27a0803aad657009ecb93c Mon Sep 17 00:00:00 2001 From: Lois Wells Date: Fri, 3 Feb 2023 11:42:48 +0000 Subject: [PATCH 4/6] Putting smee in docker --- .env.example | 2 ++ docker-compose.yml | 3 +++ 2 files changed, 5 insertions(+) diff --git a/.env.example b/.env.example index c1b186564..f54e52754 100644 --- a/.env.example +++ b/.env.example @@ -14,6 +14,8 @@ POSTGRES_PASSWORD=changeme HYDRA_ADMIN_URL=http://host.docker.internal:9002 HYDRA_SECRET= +SMEE_TUNNEL=https://smee.io/MLq0n9kvAes2vydX + # Add the below to bypass token authentication with hyrdra # BYPASS_AUTH=true # AUTH_USER_ID=<> diff --git a/docker-compose.yml b/docker-compose.yml index 145d883b2..66a9925e6 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -35,3 +35,6 @@ services: - POSTGRES_DB - POSTGRES_PASSWORD - POSTGRES_USER + smee: + image: deltaprojects/smee-client + command: -u $SMEE_TUNNEL -t http://api:3009/github_webhooks From c806b2504067fd05b463bd385117a0a7e2e24830 Mon Sep 17 00:00:00 2001 From: Lois Wells Date: Fri, 3 Feb 2023 11:52:49 +0000 Subject: [PATCH 5/6] fixing linting errors --- app/controllers/github_webhooks_controller.rb | 8 +++++--- config/routes.rb | 4 ++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/app/controllers/github_webhooks_controller.rb b/app/controllers/github_webhooks_controller.rb index 2661e41d0..5d5422623 100644 --- a/app/controllers/github_webhooks_controller.rb +++ b/app/controllers/github_webhooks_controller.rb @@ -1,14 +1,16 @@ +# frozen_string_literal: true + class GithubWebhooksController < ActionController::API include GithubWebhook::Processor - def github_push(payload) + def github_push(_payload) # TODO: handle push webhook head :ok end private - def webhook_secret(payload) - ENV['GITHUB_WEBHOOK_SECRET'] + def webhook_secret(_payload) + ENV.fetch('GITHUB_WEBHOOK_SECRET') end end diff --git a/config/routes.rb b/config/routes.rb index f48e084c7..c53576b12 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -10,8 +10,8 @@ resources :projects, only: %i[index show update destroy create] do resource :remix, only: %i[create], controller: 'projects/remixes' resource :images, only: %i[create], controller: 'projects/images' - end + end end - + resource :github_webhooks, only: :create, defaults: { formats: :json } end From 1807fa557abb0cc78e72b5f4f5066b749cead7fb Mon Sep 17 00:00:00 2001 From: Lois Wells Date: Thu, 9 Feb 2023 15:06:18 +0000 Subject: [PATCH 6/6] Adding webhooks section to README --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index b978f5c30..d2f17aa15 100644 --- a/README.md +++ b/README.md @@ -39,3 +39,8 @@ Add a comma separated list to the relevant enviroment settings. E.g for developm ``` ALLOWED_ORIGINS=localhost:3002,localhost:3000 +``` + +# Webhooks + +This API receives push event data from the [Raspberry Pi Learning](https://github.com/raspberrypilearning) organisation via webhooks. These webhooks are mediated locally through `smee`, which runs in a Docker container. The webhook data is processed using the `github_webhooks` gem in the `github_webhooks_controller`.