Skip to content
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
44 changes: 44 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Run linter and tests

on:
push:
branches:
- master
pull_request:

permissions:
contents: read

env:
GIT_AUTHOR_NAME: ${{ vars.GIT_AUTHOR_NAME }}
GIT_AUTHOR_EMAIL: ${{ vars.GIT_AUTHOR_EMAIL }}

jobs:
test:
name: Run linter and tests
runs-on: ubuntu-latest
services:
postgres:
image: postgres:16
env:
POSTGRES_DB: metadataregistry_test
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432
env:
DATABASE_URL: postgresql://postgres:postgres@localhost:5432/metadataregistry_test
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
- uses: ruby/setup-ruby@28c4deda893d5a96a6b2d958c5b47fc18d65c9d3
with:
bundler-cache: true
- run: RACK_ENV=test bundle exec rake db:migrate
- run: bundle exec overcommit --sign
- run: bundle exec overcommit --run
- run: bundle exec rspec
37 changes: 26 additions & 11 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
require: rubocop-performance
require:
- rubocop-performance
- rubocop-factory_bot
- rubocop-rake
- rubocop-rspec

AllCops:
TargetRubyVersion: 2.7

NewCops: enable
TargetRubyVersion: 3.3
ActiveSupportExtensionsEnabled: true
DisplayCopNames: true

DisplayStyleGuide: true
Exclude:
- 'bin/**/*'
- 'db/migrate/**/*'
- "bin/**/*"
- "db/migrate/**/*"

Bundler/OrderedGems:
Enabled: false
Expand All @@ -22,6 +26,9 @@ Style/FrozenStringLiteralComment:
Style/RaiseArgs:
Enabled: false

Style/OpenStructUse:
Enabled: false

Metrics/ClassLength:
Exclude:
- lib/swagger_docs.rb
Expand All @@ -32,16 +39,24 @@ Metrics/MethodLength:
Exclude:
- lib/swagger_helpers.rb

Metrics/LineLength:
Layout/LineLength:
Max: 100

Metrics/BlockLength:
Exclude:
- 'spec/**/*'
ExcludedMethods: [
'resource', 'namespace', 'swagger_schema', 'swagger_path', 'included'
]
- "spec/**/*"
AllowedMethods:
["resource", "namespace", "swagger_schema", "swagger_path", "included"]

# scope triggers a false positive, this will be fixed in rubocop 0.48.2
Lint/AmbiguousBlockAssociation:
Enabled: false

RSpec/ExampleLength:
Max: 10

RSpec/MultipleExpectations:
Max: 3

RSpec/NoExpectationExample:
Enabled: false
24 changes: 12 additions & 12 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@ FROM ruby:3.2.2
ARG ENCRYPTED_PRIVATE_KEY_SECRET

ENV APP_PATH /app/
ENV LANGUAGE en_US:en
ENV LANG C.UTF-8
ENV LANGUAGE en_US:en
ENV LANG C.UTF-8
ENV LC_ALL C.UTF-8
ENV BUNDLE_PATH=/app/vendor/bundle
ENV ENCRYPTED_PRIVATE_KEY_SECRET=$ENCRYPTED_PRIVATE_KEY_SECRET

WORKDIR $APP_PATH

RUN apt-get update && \
apt-get install -y \
lsb-release \
curl && \
rm -rf /var/lib/apt/lists/* /var/cache/apt/archives/*
apt-get install -y \
lsb-release \
curl && \
rm -rf /var/lib/apt/lists/* /var/cache/apt/archives/*
RUN curl -Ss https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - \
&& echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" | tee /etc/apt/sources.list.d/pgdg.list \
&& apt-get update -qqy \
&& apt-get install -y \
--no-install-recommends \
postgresql-client-16 && \
rm -rf /var/lib/apt/lists/* /var/cache/apt/archives/*
&& echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" | tee /etc/apt/sources.list.d/pgdg.list \
&& apt-get update -qqy \
&& apt-get install -y \
--no-install-recommends \
postgresql-client-16 && \
rm -rf /var/lib/apt/lists/* /var/cache/apt/archives/*
COPY Gemfile Gemfile.lock ./

RUN gem install bundler && bundle config set deployment true && DOCKER_ENV=true RACK_ENV=production bundle install
Expand Down
7 changes: 5 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
source 'https://rubygems.org'

docker_group = ENV['DOCKER_ENV'] == 'true'
ENV.fetch('DOCKER_ENV', nil)

# API
gem 'api-pagination', '~> 6.0'
Expand Down Expand Up @@ -81,14 +81,17 @@ group :development do
# Code quality tools
gem 'overcommit', '~> 0.64'
gem 'rubocop', '~> 1.70', require: false
gem 'rubocop-factory_bot', '~> 2.26', require: false
gem 'rubocop-faker', '~> 1.2', require: false
gem 'rubocop-performance', '~> 1.23'
gem 'rubocop-rake', '~> 0.6', require: false
gem 'rubocop-rspec', '~> 3.3', require: false
end

group :test do
gem 'coveralls_reborn', '~> 0.28', require: false
gem 'database_rewinder', github: 'kucho/database_rewinder', branch: 'fix/rails-7-2-connection-warning'
gem 'database_rewinder', github: 'kucho/database_rewinder',
branch: 'fix/rails-7-2-connection-warning'
gem 'factory_bot', '~> 6.5'
gem 'faker', '~> 3.5'
gem 'rspec', '~> 3.13'
Expand Down
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -310,12 +310,16 @@ GEM
unicode-display_width (>= 2.4.0, < 4.0)
rubocop-ast (1.37.0)
parser (>= 3.3.1.0)
rubocop-factory_bot (2.26.1)
rubocop (~> 1.61)
rubocop-faker (1.2.0)
faker (>= 2.12.0)
rubocop (>= 1.13.0)
rubocop-performance (1.23.1)
rubocop (>= 1.48.1, < 2.0)
rubocop-ast (>= 1.31.1, < 2.0)
rubocop-rake (0.6.0)
rubocop (~> 1.0)
rubocop-rspec (3.3.0)
rubocop (~> 1.61)
ruby-progressbar (1.13.0)
Expand Down Expand Up @@ -428,8 +432,10 @@ DEPENDENCIES
rest-client (~> 2.1)
rspec (~> 3.13)
rubocop (~> 1.70)
rubocop-factory_bot (~> 2.26)
rubocop-faker (~> 1.2)
rubocop-performance (~> 1.23)
rubocop-rake (~> 0.6)
rubocop-rspec (~> 3.3)
ruby-progressbar (~> 1.13)
rubyzip (~> 2.4)
Expand Down
8 changes: 4 additions & 4 deletions app/api/entities/envelope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Envelope < Grape::Entity

expose :resource,
documentation: { type: 'string',
desc: 'Learning resource in its original '\
desc: 'Learning resource in its original ' \
'encoded format' },
unless: { type: :metadata_only }

Expand Down Expand Up @@ -70,9 +70,9 @@ class Envelope < Grape::Entity
desc: 'Envelope secondary publisher id' }

expose :resource_publish_type,
documentation: { type: 'string',
desc: 'Resource publish type',
values: ['primary', 'secondary'] }
documentation: { type: 'string',
desc: 'Resource publish type',
values: %w[primary secondary] }

expose :decoded_node_headers,
as: :node_headers,
Expand Down
8 changes: 4 additions & 4 deletions app/api/entities/envelope_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
module API
module Entities
# Presenter for EnvelopeResource
class EnvelopeResource < Grape::Entity
class EnvelopeResource < Grape::Entity # rubocop:todo Metrics/ClassLength
include PayloadFormatter

expose :envelope_community,
Expand Down Expand Up @@ -37,7 +37,7 @@ class EnvelopeResource < Grape::Entity

expose :resource,
documentation: { type: 'string',
desc: 'Learning resource in its original '\
desc: 'Learning resource in its original ' \
'encoded format' },
unless: { type: :metadata_only }

Expand All @@ -47,8 +47,8 @@ class EnvelopeResource < Grape::Entity
unless: { type: :metadata_only }

expose :resource_publish_type,
documentation: { type: 'string',
desc: 'Resource publish type' }
documentation: { type: 'string',
desc: 'Resource publish type' }

expose :resource_format,
documentation: { type: 'string',
Expand Down
4 changes: 4 additions & 0 deletions app/api/entities/publish_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@ class PublishRequest < Grape::Entity

expose :envelope_id,
documentation: { type: 'string',
# rubocop:todo Layout/LineLength
desc: 'Unique identifier (in UUID format) for created or updated envelope' }
# rubocop:enable Layout/LineLength

expose :envelope_ceterms_ctid,
documentation: { type: 'string',
# rubocop:todo Layout/LineLength
desc: 'Unique identifier (ceterms:ctid) for created or updated envelope' }
# rubocop:enable Layout/LineLength

expose :error,
documentation: { type: 'string',
Expand Down
4 changes: 2 additions & 2 deletions app/api/helpers/community_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ def select_community
end

def default_community
@default ||= EnvelopeCommunity.host_mapped(@env['HTTP_HOST']) ||
EnvelopeCommunity.default.name
@default_community ||= EnvelopeCommunity.host_mapped(@env['HTTP_HOST']) ||
EnvelopeCommunity.default.name
end

def community_error(msg)
Expand Down
6 changes: 3 additions & 3 deletions app/api/helpers/shared_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def json_error!(errs, schemas = nil, status = :unprocessable_entity)
error! resp, status
end

def log_backtrace(e)
def log_backtrace(e) # rubocop:todo Naming/MethodParameterName
MR.logger.error("\n#{e.backtrace.join("\n")}\n")
end

Expand Down Expand Up @@ -121,7 +121,7 @@ def authenticate_community!(flag = :secured?)
return unless community&.send(flag)

auth_header = request.headers['Authorization']
api_key = auth_header.split(' ').last if auth_header.present?
api_key = auth_header.split.last if auth_header.present?
return if api_key.present? && ValidateApiKey.call(api_key, community)

json_error!(['401 Unauthorized'], nil, 401)
Expand All @@ -130,7 +130,7 @@ def authenticate_community!(flag = :secured?)
def current_user
@current_user ||= begin
auth_header = request.headers['Authorization']
token = auth_header.split(' ').last if auth_header.present?
token = auth_header.split.last if auth_header.present?
auth_token = AuthToken.find_by(value: token) if token.present?
auth_token&.user
end
Expand Down
12 changes: 4 additions & 8 deletions app/api/v1/bulk_purge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module API
module V1
# Purges a given publisher's envelopes
class BulkPurge < MountableAPI
mounted do
mounted do # rubocop:todo Metrics/BlockLength
helpers CommunityHelpers

resource :envelopes do
Expand All @@ -19,7 +19,7 @@ class BulkPurge < MountableAPI
optional :until, type: DateTime
at_least_one_of :owned_by, :published_by
end
delete do
delete do # rubocop:todo Metrics/BlockLength
owner =
if (owned_by = params[:owned_by])
Organization.find_by!(_ctid: owned_by)
Expand All @@ -32,13 +32,9 @@ class BulkPurge < MountableAPI

envelopes = Envelope.in_community(select_community)

if owner
envelopes = envelopes.where(organization: owner)
end
envelopes = envelopes.where(organization: owner) if owner

if publisher
envelopes = envelopes.where(publishing_organization: publisher)
end
envelopes = envelopes.where(publishing_organization: publisher) if publisher

if params[:resource_type]
envelopes = envelopes.where(resource_type: params[:resource_type])
Expand Down
2 changes: 1 addition & 1 deletion app/api/v1/ce_registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class CERegistry < Grape::API

route_param :envelope_community do
before_validation do
unless params[:envelope_community].underscore =~ /ce_registry/
unless params[:envelope_community].underscore.include?('ce_registry')
msg = 'envelope_community does not have a valid value'
error!({ errors: msg }, 400)
end
Expand Down
4 changes: 2 additions & 2 deletions app/api/v1/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Config < Grape::API
authenticate!
end

route_param :community_name do
route_param :community_name do # rubocop:todo Metrics/BlockLength
before do
@envelope_community = EnvelopeCommunity.find_by!(
name: select_community
Expand All @@ -23,7 +23,7 @@ class Config < Grape::API
)
end

resources :config do
resources :config do # rubocop:todo Metrics/BlockLength
desc "Returns the community's config"
get do
@envelope_community.config
Expand Down
2 changes: 1 addition & 1 deletion app/api/v1/ctdl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module API
module V1
# CTDL endpoint
class Ctdl < MountableAPI
mounted do
mounted do # rubocop:todo Metrics/BlockLength
helpers CommunityHelpers
helpers SharedHelpers

Expand Down
Loading