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 unlighthouse scan after staging #996

Open
wants to merge 15 commits into
base: develop
Choose a base branch
from
18 changes: 18 additions & 0 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,30 @@ jobs:
ruby-version: 3.3.5
bundler-cache: true

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this report generation should be outside deploy action, with the same conditions, but that's another thing tather deploy


- 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
11 changes: 11 additions & 0 deletions app/lib/convert_json_to_csv.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require "json"
require "csv"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that should already present


class ConvertJsonToCsv
def self.perform(input_file, output_file)
json_data = JSON.parse(File.read(input_file))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
json_data = JSON.parse(File.read(input_file))
json_data = JSON.parse(File.read(input_file))

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

write it as service object

CSV.open(output_file, "w", write_headers: true, headers: json_data.first.keys) do |csv|
json_data.each { |row| csv << row.values }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that would be fine to make link available to open

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if it's possible

end
end
end
6 changes: 6 additions & 0 deletions bin/convert_json_to_csv.rb
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use rake task for this

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env ruby
require_relative "../lib/convert_json_to_csv"

input_file = ARGV[0]
output_file = ARGV[1]
ConvertJsonToCsv.perform(input_file, output_file)
18 changes: 18 additions & 0 deletions spec/fixtures/files/unlighthouse_report.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[
{
"path": "/en",
"score": 0.78,
"performance": 0.64,
"accessibility": 0.86,
"best-practices": 0.79,
"seo": 0.83
},
{
"path": "/en/about-us",
"score": 0.78,
"performance": 0.63,
"accessibility": 0.77,
"best-practices": 0.79,
"seo": 0.92
}
]
26 changes: 26 additions & 0 deletions spec/lib/convert_json_to_csv_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require "rails_helper"

RSpec.describe ConvertJsonToCsv do
let(:input_file) { "spec/fixtures/files/unlighthouse_report.json" }
let(:output_file) { "spec/fixtures/files/output.csv" }

after do
File.delete(output_file) if File.exist?(output_file)
end

describe ".perform" do
it "converts JSON to CSV" do
ConvertJsonToCsv.perform(input_file, output_file)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

before hook

csv_content = CSV.read(output_file, headers: true)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use let


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