chore(validateTags): finish

This commit is contained in:
xHyroM 2022-07-16 15:40:06 +02:00
parent 9d3dda4b6b
commit de8a1e27f1
8 changed files with 91 additions and 16 deletions

View file

@ -28,6 +28,6 @@ jobs:
- name: Validate tag - name: Validate tag
run: bun run validate run: bun run validate
env: with:
PR_ID: ${{ github.event.pull_request.number }} github-token: ${{ secrets.GITHUB_TOKEN }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} commit-sha: ${{ github.event.pull_request.head.sha }}

View file

@ -15,6 +15,6 @@ I love bun ❤️
Informations: Informations:
- The bot uses interactions so you can use emoji from all discord servers. However, please only use ours as we can't control the others. - The bot uses interactions so you can use emoji from all discord servers. However, please only use ours as we can't control the others.
- You can use hyperlink `[bun.sh](https://bun.sh)` - You can use hyperlink `[bun.sh](<https://bun.sh>)`
- Keywords need to include the tag name - Keywords need to include the tag name
- Use `+++` for codeblock (https://canary.discord.com/channels/876711213126520882/887787428973281300/997045766411530281) - Use `+++` for codeblock (https://canary.discord.com/channels/876711213126520882/887787428973281300/997045766411530281)

View file

@ -3,7 +3,7 @@
"name": "bun-discord-bot", "name": "bun-discord-bot",
"scripts": { "scripts": {
"start": "bun src/index.ts", "start": "bun src/index.ts",
"validate": "cd scripts/validateTags && bun start", "validate": "cd scripts/validateTags && bun install && bun start",
"cloudflare:tunnel": "sudo cloudflared tunnel run" "cloudflare:tunnel": "sudo cloudflared tunnel run"
}, },
"devDependencies": { "devDependencies": {

BIN
scripts/validateTags/bun.lockb Executable file

Binary file not shown.

View file

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

View file

@ -2,5 +2,9 @@
"name": "validate-tags", "name": "validate-tags",
"scripts": { "scripts": {
"start": "bun src/index.ts" "start": "bun src/index.ts"
},
"dependencies": {
"@actions/core": "^1.9.0",
"@actions/github": "^5.0.3"
} }
} }

View file

@ -1,8 +1,80 @@
import { context } from '@actions/github';
import { getInput } from '@actions/core';
interface Tag {
keywords: string[];
content: string;
}
const githubToken = getInput('github-token');
const commitSha = getInput('commit-sha');
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;
const files = await Bun.file('./files.json').text(); const files = await Bun.file('./files.json').text();
if (!files.includes('files/tags.toml')) process.exit(0); if (!files.includes('files/tags.toml')) process.exit(0);
const errors = [];
let tags;
try {
// @ts-expect-error types // @ts-expect-error types
const tags = await import('../../../files/tags.toml'); tags = (await import('../../../files/tags.toml')).default;
console.log(tags); } catch(e) {
tags = [];
errors.push(e.message);
}
for (const [key, value] of Object.entries(tags)) {
const tag = value as Tag;
if (!tag?.keywords || tag.keywords.length === 0) errors.push(`**[${key}]:** Tag must have keywords`);
if (tag?.keywords?.[0] !== key) 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.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 (
firstChar !== '<' ||
lastChar !== '>'
) errors.push(`**[${key}]:** Link must be wrapped in <>`);
}
}
if (tag.keywords) {
const keywords = [...new Set(tag.keywords)];
if (keywords.length !== tag.keywords.length) errors.push(`**[${key}]:** Keywords must be unique`);
}
}
if (errors.length === 0) {
fetch(`https://api.github.com/repos/${context.repo.owner}/${context.repo.repo}/pulls/${context.payload.pull_request.number}/reviews`, {
method: 'POST',
headers: {
'Accept': 'application/vnd.github+json',
'Authorization': `token ${githubToken}`
},
body: JSON.stringify({
commit_id: commitSha,
event: 'APPROVE',
})
})
} else {
fetch(`https://api.github.com/repos/${context.repo.owner}/${context.repo.repo}/pulls/${context.payload.pull_request.number}/reviews`, {
method: 'POST',
headers: {
'Accept': 'application/vnd.github+json',
'Authorization': `token ${githubToken}`
},
body: JSON.stringify({
commit_id: commitSha,
body: 'Please fix the following problems:\n' + errors.join('\n'),
event: 'REQUEST_CHANGES',
})
})
}
export { }; export { };

View file

@ -8,10 +8,6 @@ export interface Tag {
content: string; content: string;
} }
export interface ExtendedTag extends Tag {
match: '✅' | '🔑' | '📄'
}
const tagCache: Collection<string, Tag> = new Collection(); const tagCache: Collection<string, Tag> = new Collection();
for (const [key, value] of Object.entries(tags)) { for (const [key, value] of Object.entries(tags)) {