Skip to content

Commit

Permalink
First version PR file validator
Browse files Browse the repository at this point in the history
  • Loading branch information
kmturley committed Sep 18, 2024
1 parent e016542 commit b8f8dbc
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 20 deletions.
47 changes: 47 additions & 0 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Validate

on:
pull_request:

jobs:
validate:
name: Validate code
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Install
run: |
npm ci
- name: Build
run: |
npm run build
- name: Changed files
id: changed-files
uses: tj-actions/changed-files@v45

- name: List changed files
env:
CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }}
run: |
for file in ${CHANGED_FILES}; do
node build/validate.js $file
done
- name: Set up Go
uses: actions/setup-go@v4

- name: Build
run: |
GOOS=windows GOARCH=386 go build -o ./ghaction-virustotal-win32.exe -v -ldflags "-s -w"
GOOS=windows GOARCH=amd64 go build -o ./ghaction-virustotal-win64.exe -v -ldflags "-s -w"
- name: VirusTotal Scan
uses: crazy-max/ghaction-virustotal@v4
with:
vt_api_key: ${{ secrets.VT_API_KEY }}
request_rate: 4
files: cat downloads/log.txt
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ coverage

# Transpiled files
build/
downloads/
out/

# VS Code
Expand All @@ -27,4 +28,4 @@ out/
.eslintcache

# Misc
.DS_Store
.DS_Store
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"prebuild": "npm run lint",
"build": "npm run clean && tsc -p tsconfig.json && npm run copy",
"dev": "tsx ./src/main.ts",
"dev:validate": "tsx ./src/validate.ts src/plugins/airwindows/airwindows/1.0.0.yaml",
"build:release": "npm run clean && tsc -p tsconfig.release.json && npm run copy",
"lint": "eslint ./**/*.ts",
"test": "vitest run ./tests",
Expand Down
48 changes: 29 additions & 19 deletions src/sources/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,7 @@ export function localGetPack() {
const pack: PluginPack = {};
const filepaths: string[] = dirRead(LOCAL_REG);
filepaths.forEach((filepath: string) => {
// TODO update studiorack/core to handle these strings
const parts: string[] = filepath.replace(LOCAL_DIR, '').replace(LOCAL_EXT, '').substring(1).split(path.sep);
const pluginId: string = safeSlug(`${parts[0]}/${parts[1]}`);
const pluginVersion: string = parts[2];

// Get plugin from yaml files.
const plugin: PluginVersion = localGetFile(filepath);
if (typeof plugin.date === 'object') plugin.date = (plugin.date as Date).toISOString();

// Ensure plugin has valid fields.
const errors: string | boolean = pluginValidateSchema(plugin as PluginVersionLocal);
const compatibility: string | boolean = pluginCompatibility(plugin);
if (errors) {
console.log(chalk.red(`X ${pluginId} | ${pluginVersion} | ${filepath}`));
console.log(chalk.yellow(compatibility) ? chalk.red(errors) + chalk.yellow(compatibility) : chalk.red(errors));
} else {
console.log(chalk.green(`✓ ${pluginId} | ${pluginVersion} | ${filepath}`));
if (compatibility) console.log(chalk.yellow(compatibility));
}
const { plugin, pluginId, pluginVersion } = validatePluginYaml(filepath);

// Add plugin to the plugin pack.
if (!pack[pluginId]) {
Expand All @@ -60,6 +42,34 @@ export function localGetPack() {
return pack;
}

export function validatePluginYaml(filepath: string) {
// TODO update studiorack/core to handle these strings
const parts: string[] = filepath.replace(LOCAL_DIR, '').replace(LOCAL_EXT, '').substring(1).split(path.sep);
const pluginId: string = safeSlug(`${parts[0]}/${parts[1]}`);
const pluginVersion: string = parts[2];

// Get plugin from yaml files.
const plugin: PluginVersion = localGetFile(filepath);
if (typeof plugin.date === 'object') plugin.date = (plugin.date as Date).toISOString();

// Ensure plugin has valid fields.
const errors: string | boolean = pluginValidateSchema(plugin as PluginVersionLocal);
const compatibility: string | boolean = pluginCompatibility(plugin);
if (errors) {
console.log(chalk.red(`X ${pluginId} | ${pluginVersion} | ${filepath}`));
console.log(compatibility ? chalk.red(errors) + chalk.yellow(compatibility) : chalk.red(errors));
} else {
console.log(chalk.green(`✓ ${pluginId} | ${pluginVersion} | ${filepath}`));
if (compatibility) console.log(chalk.yellow(compatibility));
}

return {
plugin,
pluginId,
pluginVersion,
};
}

export function localGetFile(path: string) {
const file: string = fileReadString(path);
return yaml.load(file) as PluginVersion;
Expand Down
47 changes: 47 additions & 0 deletions src/validate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import {
apiBuffer,
dirCreate,
dirExists,
fileCreate,
fileExists,
fileReadString,
pathGetExt,
PluginFile,
PluginFiles,
} from '@studiorack/core';
import { validatePluginYaml } from './sources/local.js';
import chalk from 'chalk';
import path from 'path';

const DIR_DOWNLOADS = 'downloads';
const DIR_LOG = path.join(DIR_DOWNLOADS, 'log.txt');

const filepath: string = process.argv[2];
const ext: string = pathGetExt(filepath);

if (ext === 'yaml') {
// Ensure directory and log file exist
if (!dirExists(DIR_DOWNLOADS)) dirCreate(DIR_DOWNLOADS);
if (!fileExists(DIR_LOG)) fileCreate(DIR_LOG, '');

// Validate the schema and fields
const { plugin } = validatePluginYaml(filepath);
let log: string = fileReadString(DIR_LOG);

// Download files and compare
for (const type in plugin.files) {
const pluginFile: PluginFile = plugin.files[type as keyof PluginFiles];
const pluginFileName: string = path.basename(pluginFile.url);
const pluginFileBuffer: Buffer | void = await apiBuffer(pluginFile.url);
const pluginFileLocalPath: string = path.join(DIR_DOWNLOADS, pluginFileName);
if (pluginFile.size === pluginFileBuffer.length) {
console.log(chalk.green(`✓ ${pluginFile.url}`));
} else {
console.log(chalk.red(`X ${pluginFile.url} size needs updating to ${pluginFileBuffer.length}`));
}
// Add file to log for virus scanning
fileCreate(pluginFileLocalPath, pluginFileBuffer);
log += pluginFileLocalPath + '\n';
}
fileCreate(DIR_LOG, log);
}

0 comments on commit b8f8dbc

Please sign in to comment.