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",
"scripts": {
"start": "bun src/index.ts"
}
"name": "validate_tags",
"scripts": {
"start": "bun src/index.ts"
},
"dependencies": {
"glob": "^10.3.3"
}
}

View file

@ -1,36 +1,48 @@
interface Tag {
keywords: string[];
content: string;
}
import type { Tag } from "../../../src/structs/Tag.ts";
import { globSync as glob } from "glob";
import * as matter from "gray-matter";
import { join, dirname } from "node:path";
import { readFileSync } from "node:fs";
const githubToken = process.env['github-token'];
const commitSha = process.env['commit-sha'];
const pullRequestNumber = process.env['pr-number'];
const codeBlockRegex = /(`{1,3}).+?\1/gs;
const urlRegex =
/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
const files = await Bun.file('./files.json').text();
if (!files.includes('files/tags.toml')) process.exit(0);
const files = await Bun.file('./files.json').json() as string[];
if (!files.some(f => f.includes("tags"))) process.exit(0);
const errors = [];
let tags;
try {
tags = (await import('../../../files/tags.toml')).default;
const tags: Tag[] = [];
const tagPaths = glob(join(__dirname, "..", "..", "..", "data", "tags", "*.md"));
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`, {
labels: ['tags'],
});
} catch (e) {
tags = [];
errors.push(e.message);
}
for (const [key, value] of Object.entries(tags)) {
const tag = value as Tag;
for (const tag of tags) {
const key = tag.name;
if (!tag?.keywords || tag.keywords.length === 0)
errors.push(`**[${key}]:** Tag must have keywords`);
@ -38,15 +50,12 @@ for (const [key, value] of Object.entries(tags)) {
errors.push(
`**[${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) {
const cleanedContent = tag.content
.replaceAll('+++', '```')
.replace(codeBlockRegex, '');
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 (tag.answer) {
for (const url of tag.answer.match(urlRegex) || []) {
const firstChar = tag.answer.split(url)[0].slice(-1);
const lastChar = tag.answer.split(url)[1].slice(0, 1);
if (firstChar !== '<' || lastChar !== '>')
errors.push(`**[${key}]:** Link must be wrapped in <>`);
}

View file

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