Skip to content

Commit

Permalink
1st round of md validation (#1542)
Browse files Browse the repository at this point in the history
* adds new actions, one is for checking all shows, one is for checking only the merged shows

* updates all shows validate

* adds pnpm setup

* adds checkout v4

* adds basic show md validation(#1543)

* adds 3 fake md show

* adds validate to all prs

* removes all shows and just ahs the merging show

* fixes path

* removes conditions from pullrequest

* changes branch to current base branch isntead of the main

* changes the way we're accessing changed files

* adds fetch to action

* adds checks to makes sure all links are working and all files have .md

* adds path and tweaks code adds .md to one of the should fail files

* tweaks the validation script to only look at new files in shows

* further tweaks to get action working

* adds setup

* adds this back to give access to all branches

* deletes 3 failing test shows, should pass now
  • Loading branch information
stolinski authored Feb 12, 2024
1 parent b68f1b6 commit c41ff84
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 2 deletions.
10 changes: 10 additions & 0 deletions .github/testing.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"pull_request": {
"base": {
"ref": "show-ci"
},
"head": {
"ref": "pr-actions-tester"
}
}
}
39 changes: 39 additions & 0 deletions .github/workflows/merging-show-validation.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Check Links in New Markdown Files

on:
pull_request:
paths:
- '**/*.md'

jobs:
check-new-links:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0 # Ensure the entire history is fetched

- name: Fetch base branch
run: git fetch origin ${GITHUB_BASE_REF}:${GITHUB_BASE_REF}
env:
GITHUB_BASE_REF: ${{ github.base_ref }}

- name: pnpm-setup
uses: pnpm/action-setup@v2
with:
version: 8

- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: 18.18.2
cache: 'pnpm'

- name: Install dependencies
run: pnpm install

- name: Check for broken links in new MD files
run: node ./scripts/merging-show-validation.js
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_BASE_REF: ${{ github.base_ref }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ test-results
# core.wasm

start_script/target
bin
80 changes: 80 additions & 0 deletions scripts/merging-show-validation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { exec } from 'child_process';
import fs from 'fs/promises';
import { promisify } from 'util';
// import path from 'path';
const execAsync = promisify(exec);

// Function to check URL availability
async function isUrlValid(url) {
try {
const response = await fetch(url, {
headers: {
'User-Agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
}
});
// Consider valid if the status code is in the range 200-399, covering success and redirection
return response.status !== 404;
} catch (error) {
console.error(`Error checking URL: ${url}`, error);
return false; // Treat any error as an invalid URL
}
}

// Function to extract URLs from markdown content
const extractUrls = (content) => {
const urlRegex = /https?:\/\/[^\s\)]+/g;
return content.match(urlRegex) || [];
};

// Function to process a single markdown file for broken links
const processFile = async (filePath) => {
const content = await fs.readFile(filePath, 'utf8');
const urls = extractUrls(content);
const checkPromises = urls.map(isUrlValid);
const results = await Promise.all(checkPromises);
return urls.filter((_, index) => !results[index]);
};
// Function to get new files added in the PR within ./shows directory
const getNewFilesInShows = async () => {
const baseBranch = process.env.GITHUB_BASE_REF; // Use the base branch of the PR
const { stdout } = await execAsync(
`git diff --diff-filter=A --name-only ${baseBranch} HEAD -- 'shows/'`
);
return stdout.split('\n').filter((line) => line.startsWith('shows/'));
};

// Main function modified to check for non-.md files in ./shows
const main = async () => {
const newFiles = await getNewFilesInShows();
const nonMdFiles = newFiles.filter((file) => !file.endsWith('.md'));

if (nonMdFiles.length > 0) {
console.error('Error: Non-markdown files found in ./shows:', nonMdFiles);
process.exit(1); // Fail if there are non-markdown files
}

// Filter out .md files for further processing
const mdFiles = newFiles.filter((file) => file.endsWith('.md'));

if (mdFiles.length === 0) {
console.log('No new markdown files to check in ./shows.');
return;
}

let hasBrokenLinks = false;
for (const file of mdFiles) {
const brokenLinks = await processFile(file);
if (brokenLinks.length > 0) {
hasBrokenLinks = true;
console.log(`Broken links found in ${file}:`);
brokenLinks.forEach((link) => console.log(`- ${link}`));
}
}

if (hasBrokenLinks) {
process.exit(1);
}
};

await main();
4 changes: 2 additions & 2 deletions src/lib/AdminMenu.svelte
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<script lang="ts">
import { SideMenu } from '@leveluptuts/svelte-side-menu';
import ThemeToggle from './theme/ThemeToggle.svelte';
import { enhance } from '$app/forms';
import { SideMenu } from '@leveluptuts/svelte-side-menu';
import { form_action } from './form_action';
import ThemeToggle from './theme/ThemeToggle.svelte';
</script>

<SideMenu
Expand Down

0 comments on commit c41ff84

Please sign in to comment.