Skip to content

Commit 6abe5fe

Browse files
authored
Merge pull request #24 from CreatekIO/master
Master into rails-6-1-compability
2 parents fa5f552 + 37ba706 commit 6abe5fe

18 files changed

Lines changed: 292 additions & 93 deletions

File tree

.buildkite/pipeline.yml

Lines changed: 0 additions & 32 deletions
This file was deleted.

.buildkite/test.sh

Lines changed: 0 additions & 26 deletions
This file was deleted.

.github/workflows/ci.yml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
name: Test Suite
2+
3+
on:
4+
push:
5+
workflow_dispatch:
6+
7+
jobs:
8+
spec:
9+
name: RSpec
10+
runs-on: ubuntu-latest
11+
12+
container:
13+
image: ruby:2.7.8
14+
credentials:
15+
username: ${{ secrets.ORG_DOCKERHUB_USERNAME }}
16+
password: ${{ secrets.ORG_DOCKERHUB_TOKEN }}
17+
env:
18+
DB_HOST: db
19+
DB_USERNAME: root
20+
21+
services: # versions here should match those used in docker-compose.yml
22+
db:
23+
image: mysql:8.0.34
24+
env:
25+
MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
26+
options: >-
27+
--health-cmd="mysqladmin ping"
28+
--health-interval=10s
29+
--health-timeout=5s
30+
--health-retries=3
31+
32+
steps:
33+
- uses: actions/checkout@v4
34+
35+
- name: Run RSpec
36+
shell: script -q -e -c "bash {0}" # force colour output - see https://github.com/actions/runner/issues/241
37+
run: |
38+
set -euo pipefail
39+
40+
./docker-entrypoint.sh
41+
42+
mkdir -p tmp
43+
mkdir -p log
44+
45+
bin/rspec
46+
47+
- uses: actions/upload-artifact@v4
48+
if: ${{ !cancelled() }}
49+
with:
50+
name: "test.log"
51+
path: log/test.log

.github/workflows/codeql.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ jobs:
4242

4343
steps:
4444
- name: Checkout repository
45-
uses: actions/checkout@v3
45+
uses: actions/checkout@v4
4646

4747
# Initializes the CodeQL tools for scanning.
4848
- name: Initialize CodeQL
49-
uses: github/codeql-action/init@v2
49+
uses: github/codeql-action/init@v3
5050
with:
5151
languages: ${{ matrix.language }}
5252
# If you wish to specify custom queries, you can do so here or in a config file.
@@ -60,7 +60,7 @@ jobs:
6060
# Autobuild attempts to build any compiled languages (C/C++, C#, Go, Java, or Swift).
6161
# If this step fails, then you should remove it and run the build manually (see below)
6262
- name: Autobuild
63-
uses: github/codeql-action/autobuild@v2
63+
uses: github/codeql-action/autobuild@v3
6464

6565
# ℹ️ Command-line programs to run using the OS shell.
6666
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun
@@ -73,6 +73,6 @@ jobs:
7373
# ./location_of_script_within_repo/buildscript.sh
7474

7575
- name: Perform CodeQL Analysis
76-
uses: github/codeql-action/analyze@v2
76+
uses: github/codeql-action/analyze@v3
7777
with:
7878
category: "/language:${{matrix.language}}"
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: GitHub Security Alerts for Jira
2+
3+
on:
4+
schedule:
5+
- cron: '0 4 * * *'
6+
workflow_dispatch:
7+
8+
jobs:
9+
syncSecurityAlerts:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: "Sync security alerts to Jira issues"
13+
uses: reload/github-security-jira@v1.5.0
14+
env:
15+
GH_SECURITY_TOKEN: ${{ secrets.ORG_GITHUBSECURITYTOKEN }}
16+
JIRA_TOKEN: ${{ secrets.ORG_JIRA_TOKEN }}
17+
JIRA_HOST: https://360insights.atlassian.net/
18+
JIRA_USER: ${{secrets.ORG_JIRA_USERNAME}}
19+
JIRA_PROJECT: SB
20+
JIRA_ISSUE_TYPE: Security Code Analysis
21+
JIRA_ISSUE_LABELS: MYREWARDS_SECURITY_ALERT
22+
JIRA_RESTRICTED_COMMENT_ROLE: Developer

app/controllers/concerns/csv2db/controller_helpers.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def enqueue_csv_import_and_redirect(klass, options = {}, &block)
3636
end
3737

3838
def enqueue_csv_import(klass, options = {})
39-
permitted_params = options.fetch(:params) do
39+
permitted_params = options.fetch(:params) do
4040
params.require(klass.model_name.param_key).permit(
4141
:file,
4242
*options[:extra_params]
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
module Csv2db::ActiveStorageAdapter
2+
require 'active_support/all'
3+
4+
extend ActiveSupport::Concern
5+
FILE_TYPE = 'text/csv'.freeze
6+
LINK_MAX_EXPIRY = 7.days.to_s.freeze
7+
8+
included do
9+
has_one_attached Csv2db.config.file_attachment_name
10+
11+
validate :check_file_extension
12+
13+
alias_method :file_attachment, Csv2db.config.file_attachment_name
14+
end
15+
16+
def file=(file)
17+
# Override Dragonfly setter method
18+
return unless file.present?
19+
20+
filename = file.original_filename
21+
22+
file_attachment.attach(
23+
io: File.open(file),
24+
filename: filename,
25+
content_type: file.content_type
26+
)
27+
28+
self.file_name = filename
29+
end
30+
31+
def download_link
32+
return unless file_attachment.present? && file_attachment.attached?
33+
34+
set_current_host
35+
36+
file_attachment.service_url(
37+
expires_in: LINK_MAX_EXPIRY.to_i,
38+
disposition: 'attachment'
39+
)
40+
end
41+
42+
private
43+
44+
def set_current_host
45+
return unless %i[test local].include?(Rails.application.config.active_storage.service)
46+
47+
ActiveStorage::Current.host = Csv2db.config.local_storage_host
48+
end
49+
50+
def check_file_extension
51+
# very basic check of file extension
52+
errors.add(:file, I18n.t('shared.file_processor.incorrect_file_type')) unless file_attachment.blob.content_type == FILE_TYPE
53+
end
54+
55+
def file_data
56+
return @file_data if @file_data.present?
57+
58+
file_attachment.blob.open do |blob|
59+
@file_data = str_to_utf8(blob.read)
60+
end
61+
62+
byte_order_mark = Csv2db::Import::BYTE_ORDER_MARK
63+
@file_data.sub!(byte_order_mark, '') if @file_data.starts_with?(byte_order_mark)
64+
65+
@file_data
66+
end
67+
end
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
module Csv2db::DragonflyAdapter
2+
extend ActiveSupport::Concern
3+
require 'dragonfly'
4+
5+
included do
6+
extend Dragonfly::Model
7+
8+
dragonfly_accessor :file
9+
10+
validates :file, presence: true
11+
validate :check_file_extension
12+
end
13+
14+
def download_link
15+
file.url
16+
end
17+
18+
private
19+
20+
def check_file_extension
21+
# very basic check of file extension
22+
errors.add(:file, I18n.t('shared.file_processor.incorrect_file_type')) unless file.ext == 'csv'
23+
end
24+
25+
def file_data
26+
file_data = str_to_utf8(file.data)
27+
byte_order_mark = Csv2db::Import::BYTE_ORDER_MARK
28+
file_data.sub!(byte_order_mark, '') if file_data.starts_with?(byte_order_mark)
29+
file_data
30+
end
31+
end

app/models/concerns/csv2db/import.rb

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,9 @@ def around_process(*args, &block)
2727
end
2828

2929
included do
30-
extend Dragonfly::Model
30+
include Module.const_get("Csv2db::#{Csv2db.config.storage_adapter.camelize.constantize}Adapter")
3131

32-
validates :file, presence: true
3332
validate :required_params_are_present
34-
validate :check_file_extension
35-
36-
dragonfly_accessor :file
3733

3834
after_initialize :set_default_values, :set_required_params
3935

@@ -128,10 +124,14 @@ def method_missing(method, *args, &block)
128124
end
129125
end
130126

127+
def respond_to_missing?(method, include_private = false)
128+
method.to_s.start_with?('param_') || super
129+
end
130+
131131
private
132132

133133
def check_file_contains_data
134-
error(I18n.t('shared.file_processor.insufficient_rows')) unless file.data.present? && csv.count > 0
134+
error(I18n.t('shared.file_processor.insufficient_rows')) unless csv.headers.present? && csv.count.positive?
135135
stop if errors?
136136
end
137137

@@ -165,12 +165,6 @@ def csv
165165
@csv ||= CSV.parse(file_data, headers: true)
166166
end
167167

168-
def file_data
169-
file_data = str_to_utf_8(file.data)
170-
file_data.sub!(BYTE_ORDER_MARK, '') if file_data.starts_with?(BYTE_ORDER_MARK)
171-
file_data
172-
end
173-
174168
def required_params_are_present
175169
return if @required_params.empty?
176170

@@ -183,7 +177,7 @@ def required_params_are_present
183177
end
184178

185179
def log(message, level = :info)
186-
log_messages << { message: str_to_utf_8(message), level: level, time: Time.now }
180+
log_messages << { message: str_to_utf8(message), level: level, time: Time.now }
187181
end
188182

189183
def error(message)
@@ -203,16 +197,16 @@ def set_default_values
203197
self.status ||= Status::PENDING
204198
end
205199

206-
def str_to_utf_8(str)
207-
CharlockHolmes::Converter.convert(str, str.detect_encoding[:encoding], 'UTF-8')
200+
def str_to_utf8(str)
201+
CharlockHolmes::Converter.convert(str, str_encoding(str), 'UTF-8')
208202
end
209203

210-
def set_required_params
211-
@required_params = []
204+
def str_encoding(str)
205+
str.detect_encoding[:encoding]
212206
end
213207

214-
def check_file_extension
215-
errors.add(:file, I18n.t('shared.file_processor.incorrect_file_type')) unless file.ext == 'csv'
208+
def set_required_params
209+
@required_params = []
216210
end
217211
end
218212
end

app/views/csv2db/_csv_import_table.html.haml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
%tr
1616
%td= import.id
1717
%td= import.created_at
18-
%td= link_to import.file.name, import.file.url, target: '_blank'
18+
%td= link_to(import.file_name, import.download_link)
1919
%td
2020
.badge{ class: "badge-upload-#{import.status}" }= import.status
2121
%td

0 commit comments

Comments
 (0)