Skip to content

Commit

Permalink
Feature/progress on app (#3)
Browse files Browse the repository at this point in the history
* Add new endpoints and improve tests

* remove char

* Fixes

* fix name
  • Loading branch information
jameswburke authored Aug 8, 2024
1 parent 52f0b13 commit 1a47dce
Show file tree
Hide file tree
Showing 14 changed files with 12,472 additions and 11,799 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,6 @@ The application logs all errors and job states (success or failure) using the Mo
## License

This project is under proprietary license.

## Credits
Bits and pieces plucked from https://github.com/tlinhart/s3-browser
39 changes: 27 additions & 12 deletions __checks__/helpers/capture-and-upload-screenshots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ export async function captureAndUploadScreenshots ({
url = null,
}) {

// Parse the url right away.
const pathParts = getPathPartsByUrl(url);
if ( ! pathParts ) {
return false;
}

const { hostname, pathname } = pathParts;

// Hide ads.
const urlObj = new URL( url );
urlObj.searchParams.set( 'skconfig', 's::true' );
Expand All @@ -41,7 +49,8 @@ export async function captureAndUploadScreenshots ({
await processScreenshot({
screenshot: desktopScreenshot,
name: 'desktop',
url,
hostname,
pathname,
});

await page.setViewportSize({ height: 1200, width: 800 });
Expand All @@ -50,7 +59,8 @@ export async function captureAndUploadScreenshots ({
processScreenshot({
screenshot: tabletScreenshot,
name: 'tablet',
url,
hostname,
pathname,
});

await page.setViewportSize({ height: 1200, width: 400 });
Expand All @@ -59,7 +69,19 @@ export async function captureAndUploadScreenshots ({
processScreenshot({
screenshot: mobileScreenshot,
name: 'mobile',
url,
hostname,
pathname,
});

// Trigger a Slack notification endpoint with a link to these new
// screenshots.
// @todo Add a header auth key.
const prefix = `screenshots/${hostname}/${pathname}/${timestamp}/`;
await request({
method: 'POST',
host: 'pmc-wayback-machine.vercel.app',
path: '/api/v1/slack/notification-for-capture',
body: JSON.stringify({ prefix }),
});

// @todo Include the screenshot urls in this response?
Expand All @@ -76,17 +98,11 @@ export async function captureAndUploadScreenshots ({
* @param {string} url URL for the screenshot.
*/
const processScreenshot = async ({
hostname,
name,
pathname,
screenshot,
url,
}) => {
const pathParts = getPathPartsByUrl(url);
if ( ! pathParts ) {
return false;
}

const { hostname, pathname } = pathParts;

const requestOptions = {
host: `${S3_BUCKET_NAME}.s3.${AWS_REGION}.amazonaws.com`,
method: 'PUT',
Expand Down Expand Up @@ -125,7 +141,6 @@ async function request(opts) {
data: opts.body || '',
});
} catch (error) {
console.log('Error', error);
return false;
}
}
Expand Down
3 changes: 1 addition & 2 deletions __checks__/screenshots-group.check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ export const pmcWaybackMachineGroup = new CheckGroup( 'groups-pmc-wayback-machin
concurrency: 100,
} );


/**
* Create a "catch-all" check that can be used via CLI to test any URL.
* @type {[type]}
*/
new BrowserCheck(`screenshot-generic`, {
new BrowserCheck('screenshot-generic', {
name: `Catch-All: Screenshot any URL via CLI`,
locations: ['us-east-1', 'eu-west-1'],
group: pmcWaybackMachineGroup,
Expand Down
47 changes: 47 additions & 0 deletions app/api/v1/github/capture-url-workflow/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import githubOctokitApi from '../github-octokit-api';

export const dynamic = 'force-dynamic';

/**
* Trigger the GitHub Workflow responsible for capturing screenshots with
* Checkly.
*
* @param {Request} request Request object.
*/
export async function POST(request) {
const url = request.nextUrl.searchParams.get('url');
try {
new URL(url); // Throws an error if not a url.
} catch (error) {
return Response.json(
{ error },
{ status: 403 }
);
}

// @todo Abstract this for open sourcing.
try {
const response = await githubOctokitApi.request(
'POST /repos/{owner}/{repo}/actions/workflows/{workflow_id}/dispatches',
{
owner: 'penske-media-corp',
repo: 'pmc-wayback-machine',
workflow_id: 'capture-url.yml',
ref: 'main',
inputs: {
url,
},
headers: {
'X-GitHub-Api-Version': '2022-11-28'
}
}
);

return Response.json({response});
} catch (error) {
return Response.json(
{error},
{ status: 403 }
);
}
}
8 changes: 8 additions & 0 deletions app/api/v1/github/github-octokit-api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Octokit } from '@octokit/core';

// https://github.com/octokit/core.js#readme
const octokit = new Octokit({
auth: process.env.GITHUB_TOKEN ?? '',
});

export default octokit;
16 changes: 2 additions & 14 deletions app/list-objects/route.js → app/api/v1/s3/list-objects/route.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@
import { S3Client, ListObjectsV2Command } from "@aws-sdk/client-s3";
import s3Client from '../s3-client';
import { ListObjectsV2Command } from "@aws-sdk/client-s3";

export const dynamic = 'force-dynamic';

/**
* S3 client.
*
* @type {S3Client}
*/
const s3Client = new S3Client({
region: process.env.AWS_REGION,
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
},
});

/**
* Proxy the S3 request to our server to avoid exposing secrets.
*
Expand Down
16 changes: 16 additions & 0 deletions app/api/v1/s3/s3-client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { S3Client } from "@aws-sdk/client-s3";

/**
* S3 client.
*
* @type {S3Client}
*/
const s3Client = new S3Client({
region: process.env.AWS_REGION,
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
},
});

export default s3Client;
36 changes: 36 additions & 0 deletions app/api/v1/slack/notification-for-capture/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import slackWebApi from '../slack-web-api';

/**
* Channel id for `#brand-performance-dashboard-notifications`.
*
* @type {String}
*/
const SLACK_CHANNEL_ID = process.env.SLACK_CHANNEL_ID;

export const dynamic = 'force-dynamic';

/**
* Trigger the GitHub Workflow responsible for capturing screenshots with
* Checkly.
*
* @param {Request} request Request object.
*/
export async function POST(request) {
try {
const prefix = request.nextUrl.searchParams.get('prefix');
const link = `https://pmc-wayback-machine.vercel.app/#${prefix}`;
const message = `<${link}|New Screenshots!>`;

const success = await slackWebApi.chat.postMessage({
channel: SLACK_CHANNEL_ID,
text: message,
});

return Response.json({ success });
} catch(error) {
return Response.json(
{ error: 'Something went wrong! Check the logs.' },
{ status: 403 }
);
}
}
5 changes: 5 additions & 0 deletions app/api/v1/slack/slack-web-api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { WebClient } from '@slack/web-api';

const web = new WebClient(process.env.SLACK_TOKEN ?? '');

export default web;
Loading

0 comments on commit 1a47dce

Please sign in to comment.