Skip to content

Commit

Permalink
add ConvertJsonToCsv service
Browse files Browse the repository at this point in the history
  • Loading branch information
NVMakarenko committed Dec 1, 2024
1 parent 642f370 commit 51a5f49
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 55 deletions.
18 changes: 0 additions & 18 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,12 @@ jobs:
ruby-version: 3.3.5
bundler-cache: true

- name: Set up Unlighthouse Scan
run: npm install -g @unlighthouse/cli puppeteer

- uses: miloserdow/capistrano-deploy@v3
with:
target: staging
deploy_key: ${{ secrets.STAGING_KEY_PASSWORD }}
enc_rsa_key_pth: config/credentials/staging_deploy_id_ed25519_enc

- name: Unlighthouse Assertions and Client
run: unlighthouse-ci --site http://51.44.25.104/en --build-static --output-path .unlighthouse/dist

- name: Make convert_json_to_csv executable
run: chmod +x bin/convert_json_to_csv.rb

- name: Convert JSON to CSV
run: bin/convert_json_to_csv.rb .unlighthouse/dist/ci-result.json .unlighthouse/dist/ci-result.csv

- name: Upload Unlighthouse Report
uses: actions/upload-artifact@v4
with:
name: unlighthouse-report
path: .unlighthouse/dist/**

deploy-to-production:
runs-on: ubuntu-latest
if: github.event_name == 'release' && github.event.action == 'published'
Expand Down
39 changes: 39 additions & 0 deletions .github/workflows/scan_unlighthouse.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Unlighthouse reports

on:
workflow_dispatch:

jobs:
generate-unlighthouse-report:
runs-on: ubuntu-latest
if: github.event_name == 'pull_request' && github.event.action == 'closed' && github.event.pull_request.merged == true
steps:
- uses: actions/checkout@v2

- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: 3.3.5
bundler-cache: true

- name: Set up Unlighthouse Scan
run: npm install -g @unlighthouse/cli puppeteer

- name: Unlighthouse Assertions and Client
run: unlighthouse-ci --site http://51.44.25.104/en --build-static --output-path .unlighthouse/dist

- name: Convert JSON to CSV
run: |
ruby -e "require './lib/convert_json_to_csv_service'; ConvertJsonToCsvService.call('.unlighthouse/dist/ci-result.json', '.unlighthouse/dist/ci-result.csv')"
- name: Upload Unlighthouse Report
uses: actions/upload-artifact@v4
with:
name: unlighthouse-report
path: .unlighthouse/dist/**

- name: Upload СCSV Report
uses: actions/upload-artifact@v4
with:
name: csv-report
path: .unlighthouse/dist/ci-result.csv
11 changes: 0 additions & 11 deletions app/lib/convert_json_to_csv.rb

This file was deleted.

19 changes: 19 additions & 0 deletions app/services/convert_json_to_csv_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class ConvertJsonToCsvService
def self.call(input_file, output_file)
new(input_file, output_file).call
end

def initialize(input_file, output_file)
@input_file = input_file
@output_file = output_file
end

def call
json_data = JSON.parse(File.read(@input_file))

CSV.open(@output_file, "w", write_headers: true, headers: json_data.first.keys) do |csv|
json_data.each { |row| csv << row.values }
end
@output_file
end
end
3 changes: 3 additions & 0 deletions spec/fixtures/files/output.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
path,score,performance,accessibility,best-practices,seo
/en,0.78,0.64,0.86,0.79,0.83
/en/about-us,0.78,0.63,0.77,0.79,0.92
26 changes: 0 additions & 26 deletions spec/lib/convert_json_to_csv_spec.rb

This file was deleted.

42 changes: 42 additions & 0 deletions spec/services/convert_json_to_csv_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require "rails_helper"

RSpec.describe ConvertJsonToCsvService do
let(:input_file) { "spec/fixtures/files/unlighthouse_report.json" }
let(:output_file) { "spec/fixtures/files/output.csv" }
let(:csv_content) { CSV.read(output_file, headers: true) }

describe ".call" do
context "when initializing and calling an instance" do
let(:service_instance) { instance_double(ConvertJsonToCsvService) }

before do
allow(ConvertJsonToCsvService).to receive(:new).with(input_file, output_file).and_return(service_instance)
allow(service_instance).to receive(:call).and_return(output_file)
end

it "initializes and calls the instance with the provided arguments" do
result = ConvertJsonToCsvService.call(input_file, output_file)

expect(ConvertJsonToCsvService).to have_received(:new).with(input_file, output_file)
expect(service_instance).to have_received(:call)
expect(result).to eq(output_file)
end
end

context "when creating a CSV file" do
it "creates a csv file with correct data" do
result = ConvertJsonToCsvService.call(input_file, output_file)

expect(result).to eq(output_file)
expect(File.exist?(output_file)).to be_truthy
expect(csv_content.headers).to eq(["path", "score", "performance", "accessibility", "best-practices", "seo"])
expect(csv_content[0]["path"]).to eq("/en")
expect(csv_content[0]["score"]).to eq("0.78")
expect(csv_content[0]["performance"]).to eq("0.64")
expect(csv_content[0]["accessibility"]).to eq("0.86")
expect(csv_content[0]["best-practices"]).to eq("0.79")
expect(csv_content[0]["seo"]).to eq("0.83")
end
end
end
end

0 comments on commit 51a5f49

Please sign in to comment.