Skip to content

Commit

Permalink
Update mistake checker action
Browse files Browse the repository at this point in the history
  • Loading branch information
SKPG-Tech committed Sep 30, 2023
1 parent cf11395 commit e96455b
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 63 deletions.
85 changes: 40 additions & 45 deletions .github/workflows/mistake_check.py
Original file line number Diff line number Diff line change
@@ -1,57 +1,52 @@
import re
import sys
import urllib.request as r
import json

number = sys.argv[1]
page = r.urlopen(f'https://github.com/pewpewlive/ppl-i18n/pull/{number}.diff').read().decode("utf8").split('\n')
page = r.urlopen(
f'https://github.com/pewpewlive/ppl-i18n/pull/{number}.diff').read().decode("utf8").split('\n')
filename = page[0].split(' ')[2][2:]
page = page[4:]

def parse(page):
id_text = ""
current_line_number = 0
parsed_strings = {}
for line in page:
if line.startswith("@@"):
current_line_number = int(line.split(' ')[1][1:].split(',')[0]) - 1
continue

current_line_number += 1

if line.startswith(" msgid"):
id_text = line.rstrip()[8:-1]
elif line.startswith("-msgstr"):
current_line_number -= 1
elif line.startswith("+msgstr"):
str_text = line.rstrip()[9:-1]

if str_text == '':
continue

id_color_count = len(re.findall(
r'(?:#(?:[0-9a-fA-F]{2}){4})', id_text))
str_color_count = len(re.findall(
r'(?:#(?:[0-9a-fA-F]{2}){4})', str_text))
id_s_count = id_text.count("%s")
str_s_count = str_text.count("%s")

parsed_strings[id_text] = {"condition": "colors_invalid" if id_color_count != str_color_count else
"s_invalid" if id_s_count != str_s_count else "0",
"line": current_line_number,
"id": id_text,
"str": str_text}


return parsed_strings
id_text = ""
current_line_number = 0
parsed_strings = []
for line in page:
if line.startswith("@@"):
current_line_number = int(line.split(' ')[1][1:].split(',')[0]) - 1
continue

current_line_number += 1
if line.startswith(" msgid"):
id_text = line.rstrip()[8:-1]
elif line.startswith("-msgstr"):
current_line_number -= 1
elif line.startswith("+msgstr"):
str_text = line.rstrip()[9:-1]

if str_text == '':
continue

id_color_count = len(re.findall(
r'(?:#(?:[0-9a-fA-F]{2}){4})', id_text))
str_color_count = len(re.findall(
r'(?:#(?:[0-9a-fA-F]{2}){4})', str_text))
id_s_count = id_text.count("%s")
str_s_count = str_text.count("%s")

if id_color_count != str_color_count or id_s_count != str_s_count:
parsed_strings.append({"condition": "invalid_colors" if id_color_count != str_color_count else
"invalid_s" if id_s_count != str_s_count else "other",
"line": current_line_number,
"id": id_text,
"str": str_text})

return parsed_strings

strings = parse(page)
final_output = ""

for key in strings:
if strings[key]["condition"] == "colors_invalid":
final_output += f'w // col // {strings[key]["line"]} // {strings[key]["id"]} // {strings[key]["str"]} /// '
elif strings[key]["condition"] == "s_invalid":
final_output += f'e // s // {strings[key]["line"]} // {strings[key]["id"]} // {strings[key]["str"]} /// '
strings = parse(page)
final_output = json.dumps(strings, ensure_ascii=False)

final_output += "0"
print(final_output)
print(final_output)
6 changes: 3 additions & 3 deletions .github/workflows/mistake_check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ jobs:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
let fs = require('fs');
const mistakes = fs.readFileSync('./mistakes.txt').toString().split(" /// ");
const mistakes = JSON.parse(fs.readFileSync('./mistakes.txt').toString());
if (mistakes[0] !== "0")
if (mistakes.map(mistake => mistake.split(" // ")[0]).includes("e"))
if (mistakes.length !== 0)
if (mistakes.map(mistake => mistake.condition).includes("invalid_s"))
core.setFailed("Mistakes found! Check comment for more details");
27 changes: 12 additions & 15 deletions .github/workflows/mistake_notifier.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,40 +49,37 @@ jobs:
script: |
let fs = require('fs');
const pr_number = Number(fs.readFileSync('./number.txt'));
const mistakes = fs.readFileSync('./mistakes.txt').toString().split(" /// ");
const mistakes = JSON.parse(fs.readFileSync('./mistakes.txt').toString());
let message = '';
if (mistakes[0] !== '0' && mistakes[0] !== '0\n') {
if (mistakes.length !== 0) {
message += 'Thank you for contributing!\n\n';
message += 'To keep translations error-free, we check them for mistakes. The files you changed seem to contain some, no worries though, they can be fixed! ';
message += `See helpful info below:\n\`\`\`diff\n`;
message += '===================================================================\n';
for (mistake of mistakes) {
mistake_info = mistake.split(' // ');
if (mistake_info[0] === '0' || mistake_info[0] === '\n0') break;
message += `@@ Line: ${mistake.line} @@\n`;
message += `@@ Line: ${mistake_info[2]} @@\n`;
switch (mistake_info[0]) {
case 'e':
switch (mistake.condition) {
case 'invalid_s':
message += '--- Non-Matching use of %s\n';
message += `+ msgid "${mistake_info[3]}"\n`;
message += `- msgstr "${mistake_info[4]}"\n`;
message += `+ msgid "${mistake.id}"\n`;
message += `- msgstr "${mistake.str}"\n`;
break;
case 'w':
case 'invalid_color':
message += '!!! Possible misuse of colors\n';
message += `+ msgid "${mistake_info[3]}"\n`;
message += `- msgstr "${mistake_info[4]}"\n`;
message += `+ msgid "${mistake.id}"\n`;
message += `- msgstr "${mistake.str}"\n`;
break;
}
if (mistakes.indexOf(mistake) !== mistakes.length - 2) {
if (mistakes.indexOf(mistake) !== mistakes.length - 1) {
message += '***************\n';
}
}
message += '===================================================================';
message += `===================================================================\n\`\`\``;
}
if (message !== "") {
Expand Down

0 comments on commit e96455b

Please sign in to comment.