ci: adapt validate tags

This commit is contained in:
Jozef Steinhübl 2023-08-25 22:17:08 +02:00
parent c7cffecfab
commit 4ed957a650
5 changed files with 39 additions and 35 deletions

BIN
scripts/validate_tags/bun.lockb Executable file

Binary file not shown.

View file

@ -1 +0,0 @@
["files/tags.toml"]

View file

@ -1,6 +1,9 @@
{ {
"name": "validate_tags", "name": "validate_tags",
"scripts": { "scripts": {
"start": "bun src/index.ts" "start": "bun src/index.ts"
} },
"dependencies": {
"glob": "^10.3.3"
}
} }

View file

@ -1,36 +1,48 @@
interface Tag { import type { Tag } from "../../../src/structs/Tag.ts";
keywords: string[]; import { globSync as glob } from "glob";
content: string; import * as matter from "gray-matter";
} import { join, dirname } from "node:path";
import { readFileSync } from "node:fs";
const githubToken = process.env['github-token']; const githubToken = process.env['github-token'];
const commitSha = process.env['commit-sha']; const commitSha = process.env['commit-sha'];
const pullRequestNumber = process.env['pr-number']; const pullRequestNumber = process.env['pr-number'];
const codeBlockRegex = /(`{1,3}).+?\1/gs;
const urlRegex = const urlRegex =
/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&\/\/=]*)/gi; /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&\/\/=]*)/gi;
// Check if files/tags.toml was changed // Check if files/tags.toml was changed
const files = await Bun.file('./files.json').text(); const files = await Bun.file('./files.json').json() as string[];
if (!files.includes('files/tags.toml')) process.exit(0); if (!files.some(f => f.includes("tags"))) process.exit(0);
const errors = []; const errors = [];
let tags; const tags: Tag[] = [];
try { const tagPaths = glob(join(__dirname, "..", "..", "..", "data", "tags", "*.md"));
tags = (await import('../../../files/tags.toml')).default; for (const tagPath of tagPaths) {
const content = readFileSync(tagPath);
const frontMatter = matter(content);
tags.push({
name: dirname(tagPath),
question: frontMatter.data.question,
keywords: frontMatter.data.keywords,
answer: frontMatter.content,
category_ids: frontMatter.data.category_ids ?? null,
channel_ids: frontMatter.data.channel_ids ?? null,
});
}
try {
await requestGithub(`issues/${pullRequestNumber}/labels`, { await requestGithub(`issues/${pullRequestNumber}/labels`, {
labels: ['tags'], labels: ['tags'],
}); });
} catch (e) { } catch (e) {
tags = [];
errors.push(e.message); errors.push(e.message);
} }
for (const [key, value] of Object.entries(tags)) { for (const tag of tags) {
const tag = value as Tag; const key = tag.name;
if (!tag?.keywords || tag.keywords.length === 0) if (!tag?.keywords || tag.keywords.length === 0)
errors.push(`**[${key}]:** Tag must have keywords`); errors.push(`**[${key}]:** Tag must have keywords`);
@ -38,15 +50,12 @@ for (const [key, value] of Object.entries(tags)) {
errors.push( errors.push(
`**[${key}]:** First keyword of tag is not the same as the tag name` `**[${key}]:** First keyword of tag is not the same as the tag name`
); );
if (!tag.content) errors.push(`**[${key}]:** Tag must have content`); if (!tag.answer) errors.push(`**[${key}]:** Tag must have content`);
if (tag.content) { if (tag.answer) {
const cleanedContent = tag.content for (const url of tag.answer.match(urlRegex) || []) {
.replaceAll('+++', '```') const firstChar = tag.answer.split(url)[0].slice(-1);
.replace(codeBlockRegex, ''); const lastChar = tag.answer.split(url)[1].slice(0, 1);
for (const url of cleanedContent.match(urlRegex) || []) {
const firstChar = tag.content.split(url)[0].slice(-1);
const lastChar = tag.content.split(url)[1].slice(0, 1);
if (firstChar !== '<' || lastChar !== '>') if (firstChar !== '<' || lastChar !== '>')
errors.push(`**[${key}]:** Link must be wrapped in <>`); errors.push(`**[${key}]:** Link must be wrapped in <>`);
} }

View file

@ -1,10 +1,3 @@
{ {
"compilerOptions": { "extends": "../../tsconfig.json"
"lib": ["ESNext"],
"module": "esnext",
"target": "esnext",
"moduleResolution": "Node",
// "bun-types" is the important part
"types": ["bun-types"]
}
} }