Skip to content

Commit

Permalink
review and add action to gen report and send email
Browse files Browse the repository at this point in the history
  • Loading branch information
claromes committed Nov 10, 2024
1 parent f29fea2 commit a8f7b7a
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 44 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/reports.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: LH Reports via Resend

on:
push:
branches: ["main"]
pull_request:
branches: ["main"]

permissions:
contents: read

jobs:
reports:
runs-on: ubuntu-latest

steps:
- name: Setup repo
uses: actions/checkout@v4

- uses: denoland/setup-deno@v2
with:
deno-version: v2.x

- name: Install Deno dependencies
run: deno install

- name: Deno build
run: deno task build
env:
RESEND_API_KEY: ${{ secrets.RESEND_API_KEY }}
RESEND_EMAIL_FROM: ${{ secrets.RESEND_EMAIL_FROM }}
RESEND_EMAIL_TO: ${{ secrets.RESEND_EMAIL_TO }}
URLS: ${{ secrets.URLS }}
14 changes: 12 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,22 @@ import "@std/dotenv/load";
import { logging } from "./utils/utils.ts";
import { lighthouseReport } from "./modules/lighthouseReport.ts";
import { celereReport } from "./modules/celereReport.ts";
import { sendReport } from "./modules/sendReport.ts";

async function buildReport(url: string): Promise<void> {
const report = await lighthouseReport(url);

let result;

if (report) {
celereReport(report);
result = celereReport(report);
}

if (result) {
const [subject, body] = result;
if (subject && body) {
await sendReport(subject, body);
}
}
}

Expand All @@ -21,7 +31,7 @@ if (URL) {
.then(() => {
logging("Done.");

// Deno.exit(0); // Fix ./modules/getLighthouseReport.js:29
Deno.exit(0); // Fix ./modules/getLighthouseReport.js:29
})
.catch((error) => {
console.error(error);
Expand Down
9 changes: 4 additions & 5 deletions src/modules/celereReport.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { format } from "@std/datetime";

import { logging } from "./../utils/utils.ts";
import { sendReport } from "./sendReport.ts";
import { webVitalsLabels } from "./../utils/webVitalsLabels.ts";

export interface CelereReport {
Expand Down Expand Up @@ -40,7 +39,7 @@ export interface CelereReport {
};
}

export function celereReport(report: string): void {
export function celereReport(report: string): string[] | undefined {
const content: CelereReport = JSON.parse(report);

// Info
Expand Down Expand Up @@ -75,8 +74,8 @@ export function celereReport(report: string): void {
const seo = content.categories.seo.score * 100;

// Email Content
const emailSubject = `${hostname} - Core Web Vitals: ${labels.cwv} - Desempenho: ${performance}`;
const emailBody = `
const subject = `${hostname} - Core Web Vitals: ${labels.cwv} - Desempenho: ${performance}`;
const body = `
URL: ${content.finalDisplayedUrl}<br>
Criação do relatório: ${formattedDate}<br><br>
Expand All @@ -100,6 +99,6 @@ export function celereReport(report: string): void {
if (content) {
logging("Text report generated.");

sendReport(emailSubject, emailBody);
return [subject, body];
}
}
57 changes: 20 additions & 37 deletions src/modules/sendReport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,29 @@ import "@std/dotenv/load";

import { logging } from "./../utils/utils.ts";

export function sendReport(subject: string, body: string): void {
export async function sendReport(subject: string, body: string): Promise<void> {
const RESEND_API_KEY = Deno.env.get("RESEND_API_KEY");
const RESEND_EMAIL_FROM = Deno.env.get("RESEND_EMAIL_FROM");
const RESEND_EMAIL_TO = Deno.env.get("RESEND_EMAIL_TO");

const handler = async (_request: Request): Promise<Response> => {
const res = await fetch("https://api.resend.com/emails", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${RESEND_API_KEY}`,
},
body: JSON.stringify({
from: RESEND_EMAIL_FROM,
to: RESEND_EMAIL_TO,
subject: subject,
html: body,
}),
});
const res = await fetch("https://api.resend.com/emails", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${RESEND_API_KEY}`,
},
body: JSON.stringify({
from: RESEND_EMAIL_FROM,
to: RESEND_EMAIL_TO,
subject: subject,
html: body,
}),
});

if (res.ok) {
logging("E-mail sent.");

return new Response(res.body, {
status: 200,
headers: {
"Content-Type": "application/json",
},
});
} else {
return new Response(res.body, {
status: res.status,
headers: {
"Content-Type": "application/json",
},
});
}
};

Deno.serve(handler);
// console.log(subject);
// console.log(body);
if (res.ok) {
logging("E-mail sent.");
} else {
const error = await res.text();
logging(`Failed to send email: ${error}`);
}
}

0 comments on commit a8f7b7a

Please sign in to comment.