Skip to content
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

add-run-rubocop #10

Merged
merged 4 commits into from
Dec 29, 2022
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
58 changes: 0 additions & 58 deletions .github/workflows/brakeman.yml

This file was deleted.

33 changes: 32 additions & 1 deletion .github/workflows/main_flow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,35 @@ jobs:
run: bin/rails db:schema:load
# Add or replace test runners here
- name: Run tests
run: bin/rake test
run: bin/rake
- name: SonarCloud Scan
uses: SonarSource/sonarcloud-github-action@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
with:
args:
-Dsonar.projectKey=lytrungtin_url_shortener
-Dsonar.organization=lytrungtin
-Dsonar.exclusions=/vendor/**
-Dsonar.java.file.suffixes=-
-Dsonar.c.file.suffixes=-
-Dsonar.cpp.file.suffixes=-
-Dsonar.objc.file.suffixes=-
-Dsonar.ruby.coverage.reportPaths=/home/runner/work/url_shortener/url_shortener/coverage/.resultset.json
lint:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Install Ruby and gems
uses: ruby/setup-ruby@ee2113536afb7f793eed4ce60e8d3b26db912da4 # v1.127.0
with:
bundler-cache: true
# Add or replace any other lints here
- name: Security audit dependencies
run: bundle exec bundler-audit update
- name: Security audit application code
run: bundle exe brakeman -q -w2
- name: Lint Ruby files
run: bundle exec rubocop -A --parallel --require rubocop-rails
11 changes: 5 additions & 6 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,12 @@ gem 'rack-cors'

group :development, :test do
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
gem 'debug', platforms: %i[mri mingw x64_mingw]
gem 'dotenv-rails'
gem 'simplecov', require: false, group: :test
end

group :development do
gem 'brakeman'
gem 'bundle-audit'
gem 'debug', platforms: %i[mri mingw x64_mingw]
gem 'dotenv-rails'
gem 'rubocop'
gem 'rubocop-rails'
gem 'rubocop-rake'
gem 'simplecov', group: :test
end
8 changes: 8 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,12 @@ GEM
unicode-display_width (>= 1.4.0, < 3.0)
rubocop-ast (1.24.0)
parser (>= 3.1.1.0)
rubocop-rails (2.17.4)
activesupport (>= 4.2.0)
rack (>= 1.1)
rubocop (>= 1.33.0, < 2.0)
rubocop-rake (0.6.0)
rubocop (~> 1.0)
ruby-progressbar (1.11.0)
simplecov (0.22.0)
docile (~> 1.1)
Expand Down Expand Up @@ -211,6 +217,8 @@ DEPENDENCIES
rack-cors
rails (~> 7.0.4)
rubocop
rubocop-rails
rubocop-rake
simplecov
tzinfo-data

Expand Down
1 change: 1 addition & 0 deletions app/controllers/api/v1/url_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

module Api
module V1
# /api/v1/url
class UrlController < ApplicationController
before_action :decode_params, only: [:decode]
before_action :encode_params, only: [:encode]
Expand Down
5 changes: 3 additions & 2 deletions app/controllers/redirection_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

class RedirectionController < ActionController::Base
# Handles redirections for All urls already shortened with :slug param
class RedirectionController < ApplicationController
before_action :validate_slug
def redirect
result = Rails.cache.fetch("decode_shortened_url:#{request.original_url}") do
Expand All @@ -14,7 +15,7 @@ def redirect
private

def not_found
render file: "#{Rails.root}/public/404.html", layout: false, status: :not_found
render file: Rails.root.join('/public/404.html'), layout: false, status: :not_found
end

def validate_slug
Expand Down
1 change: 1 addition & 0 deletions app/mailers/application_mailer.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

# app/mailers/application_mailer.rb
class ApplicationMailer < ActionMailer::Base
default from: 'from@example.com'
layout 'mailer'
Expand Down
1 change: 1 addition & 0 deletions app/models/application_record.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

# app/models/application_record.rb
class ApplicationRecord < ActiveRecord::Base
primary_abstract_class
end
13 changes: 7 additions & 6 deletions app/models/url.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# frozen_string_literal: true

# Storing and validating URLs input before generate shorten slug
class Url < ApplicationRecord
validates_presence_of :original_url, :slug
validates :original_url, :slug, presence: true
validates :original_url, url: true
validates_uniqueness_of :slug
validates_length_of :original_url, within: 10..2048
validates_length_of :slug, within: 4..4
validates :slug, uniqueness: true
validates :original_url, length: { within: 10..2048 }
validates :slug, length: { within: 4..4 }

before_validation :generate_slug
before_save do
Expand All @@ -18,7 +19,7 @@ class Url < ApplicationRecord
end

def generate_slug
return if !slug.nil? && !slug.empty?
return if !slug.nil? && !slug.strip.nil?

slug = SecureRandom.alphanumeric(4)
return generate_slug if Url.exists?(slug:)
Expand All @@ -43,7 +44,7 @@ def self.decode(shortened_url)
return ['Shorten URL is not valid']
end

url = Url.find_by_slug(shortened_uri.path.split('/').last)
url = Url.find_by(slug: shortened_uri.path.split('/').last)
return url.original_url if url

['Shorten URL is not existed']
Expand Down
1 change: 1 addition & 0 deletions app/validators/url_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require 'net/http'

# URL validator for urls from Model should valid, be able to redirect or response success
class UrlValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
return if value.blank?
Expand Down
1 change: 1 addition & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
Dotenv::Railtie.load if %w[development test].include? ENV['RAILS_ENV']

module Shortening
# config/application.rb
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 7.0
Expand Down
86 changes: 0 additions & 86 deletions config/database.yml

This file was deleted.

1 change: 1 addition & 0 deletions db/migrate/20221224115757_create_urls.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

# db/migrate/20221224115757_create_urls.rb
class CreateUrls < ActiveRecord::Migration[7.0]
def change
create_table :urls do |t|
Expand Down
1 change: 1 addition & 0 deletions db/migrate/20221227171400_remove_index_to_urls.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

# db/migrate/20221227171400_remove_index_to_urls.rb
class RemoveIndexToUrls < ActiveRecord::Migration[7.0]
def change
remove_index :urls, :original_url, unique: true
Expand Down
5 changes: 5 additions & 0 deletions test/controllers/redirection_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ class RedirectionControllerTest < ActionDispatch::IntegrationTest
assert_response :not_found
end

test 'access with invalid slug format should render to not found' do
get shortened_url(slug: 'te@@st')
assert_response :not_found
end

test 'access with not existed slug should render to not found' do
get shortened_url(slug: '1a2b3c')
assert_response :not_found
Expand Down
13 changes: 10 additions & 3 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@

ENV['RAILS_ENV'] ||= 'test'
require 'simplecov'
require "simplecov_json_formatter"
SimpleCov.formatter = SimpleCov::Formatter::JSONFormatter
SimpleCov.start
require 'simplecov_json_formatter'

# Generate HTML and JSON reports
SimpleCov.formatters = SimpleCov::Formatter::MultiFormatter.new([
SimpleCov::Formatter::HTMLFormatter,
SimpleCov::Formatter::JSONFormatter
])
SimpleCov.start do
enable_coverage :branch
end
require_relative '../config/environment'
require 'rails/test_help'

Expand Down