Add lint checker

This commit is contained in:
Muntashir Al-Islam 2023-07-14 20:21:00 +06:00
parent e25b427972
commit d02d384662
9 changed files with 254 additions and 19 deletions

27
.github/workflows/lint.yml vendored Normal file
View File

@ -0,0 +1,27 @@
name: Lint
on:
push:
paths-ignore:
- '*.md'
pull_request:
paths-ignore:
- '*.md'
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Clone the repository
uses: actions/checkout@v3
with:
submodules: 'recursive'
- name: Setup PHP
uses: shivammathur/setup-php@v2
- name: Run lint
run: php ./scripts/lint.php
- name: Upload lint results
if: ${{ always() }}
uses: actions/upload-artifact@v3
with:
path: ./build/lint-results.txt

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
.idea/
.DS_Store
build/

View File

@ -28,7 +28,7 @@
{
"id": "com.android.soundpicker",
"description": "Google Sounds. Needed to pick up a phone ringtone. No weird permissions.",
"label": [
"web": [
"https://beta.pithus.org/report/f5f7c265c6d98666c78267b91643bbfb635021d5d4f85c93407079ba4aad88ee"
],
"removal": "caution"
@ -736,7 +736,7 @@
"https://www.androidcentral.com/amber-alerts-and-android-what-you-need-know",
"https://android.googlesource.com/platform/packages/apps/CellBroadcastReceiver/+/refs/heads/master/src/com/android/cellbroadcastreceiver"
],
"removal": "caution",
"removal": "caution"
},
{
"id": "com.google.android.cellbroadcastservice",
@ -748,7 +748,7 @@
"https://en.wikipedia.org/wiki/Cell_Broadcast",
"https://www.androidcentral.com/amber-alerts-and-android-what-you-need-know"
],
"removal": "caution",
"removal": "caution"
},
{
"id": "com.google.android.configupdater",

View File

@ -80,7 +80,7 @@
},
{
"id": "com.amazon.kindle",
"label": "Amazon Kindle"
"label": "Amazon Kindle",
"description": "Kindle eBook reader app",
"web": [
"https://play.google.com/store/apps/details?id=com.amazon.kindle"
@ -105,7 +105,7 @@
},
{
"id": "com.amazon.mp3",
"label": "Amazon Music"
"label": "Amazon Music",
"description": "Amazon Music streaming app",
"web": [
"https://play.google.com/store/apps/details?id=com.amazon.mp3"
@ -126,7 +126,7 @@
},
{
"id": "com.aspiro.tidal",
"label": "TIDAL Music"
"label": "TIDAL Music",
"description": "Tidal Music streaming app",
"web": [
"https://play.google.com/store/apps/details?id=com.aspiro.tidal"
@ -172,7 +172,7 @@
},
{
"id": "com.cnn.mobile.android.phone",
"label": "CNN Breaking US & World News"
"label": "CNN Breaking US & World News",
"description": "News app from CNN",
"web": [
"https://play.google.com/store/apps/details?id=com.cnn.mobile.android.phone"
@ -354,7 +354,7 @@
},
{
"id": "com.galaxyfirsatlari",
"label": "Galaxy Fırsatları"
"label": "Galaxy Fırsatları",
"description": "Samsung-only app for Turkish people\nRecommands you stuff to buy. You are supposed to save money but we all know this kind of apps\nEncourages consumption.\nExodus found 10 trackers and 17 permissions : https://reports.exodus-privacy.eu.org/fr/reports/143830/",
"web": [
"https://play.google.com/store/apps/details?id=com.galaxyfirsatlari"
@ -612,7 +612,7 @@
"web": [
"https://play.google.com/store/apps/details?id=com.microsoft.office.onenote"
],
"removal": "replace"
"removal": "replace",
"suggestions": "note_taking_apps"
},
{
@ -1179,7 +1179,7 @@
"web": [
"https://play.google.com/store/apps/details?id=com.skype.raider"
],
"removal": "replace"
"removal": "replace",
"suggestions": "meeting_apps"
},
{

View File

@ -1971,8 +1971,8 @@
"web": [
"https://beta.pithus.org/report/3cdd48fb6a9c94435fb1b46b41a19c553b96ae889aaa2d7285f28cd64d12363e"
],
"removal": "replace"
"warning": "Make sure have installed another launcher before debloating this app"
"removal": "replace",
"warning": "Make sure have installed another launcher before debloating this app",
"suggestions": "launchers"
},
{
@ -2901,7 +2901,7 @@
{
"id": "com.miui.screenrecorder",
"description": "Mi Screen Recorder\n",
"removal": "replace"
"removal": "replace",
"suggestions": "screen_recorders"
},
{
@ -4039,7 +4039,7 @@
"web": [
"https://beta.pithus.org/report/f55e935357865f4647e59c98afb5a3a46aba22a48844d80d2819d122781e3fde"
],
"removal": "delete"
"removal": "delete",
"warning": "Removing this package prevents the OnePlus Game Center to detect games."
},
{
@ -4924,7 +4924,7 @@
{
"id": "com.samsung.android.messaging",
"description": "Samsung Messaging app\n",
"removal": "replace"
"removal": "replace",
"suggestions": "sms"
},
{
@ -6980,7 +6980,7 @@
"id": "com.tblenovo.whatsnewhost",
"label": "Lenovo Feature Update Host",
"description": "Exactly not sure what it does.\n",
"web" [
"web": [
"https://beta.pithus.org/report/e08b8712d07899653631f3a9ac12c0ed48ff7bdb699651724eba26871ac0ca2b"
],
"removal": "caution"

207
scripts/lint.php Normal file
View File

@ -0,0 +1,207 @@
<?php
/* SPDX-License-Identifier: AGPL-3.0-or-later */
const SUPPORTED_REMOVAL_TYPES = ['delete', 'replace', 'caution', 'unsafe'];
const SUPPORTED_TAGS = [];
const REPO_DIR = __DIR__ . "/..";
const SUGGESTIONS_DIR = REPO_DIR . '/suggestions';
const LINT_DIR = __DIR__ . "/../build";
if (!file_exists(LINT_DIR)) {
mkdir(LINT_DIR, 0777, true);
}
$lint_writer = fopen(LINT_DIR . "/lint-results.txt", "w");
// START MAIN
$error_count = 0;
foreach (scandir(REPO_DIR) as $filename) {
if (!str_ends_with($filename, ".json")) {
continue;
}
$file = REPO_DIR . '/' . $filename;
$type = substr($filename, 0, -5);
$list = json_decode(file_get_contents($file), true);
if ($list === null) {
fprintf($lint_writer, "Malformed file: $file\n");
++$error_count;
continue;
} else fprintf($lint_writer, "Adding $filename\n");
foreach ($list as $item) {
$error_count += validate_bloatware_item($item);
}
}
foreach (scandir(SUGGESTIONS_DIR) as $filename) {
if (!str_ends_with($filename, ".json")) {
continue;
}
$suggestion_file = SUGGESTIONS_DIR . '/' . $filename;
$suggestion_id = substr($filename, 0, -5);
$single_suggestion_list = json_decode(file_get_contents($suggestion_file), true);
if ($single_suggestion_list === null) {
fprintf($lint_writer, "Malformed file: $suggestion_file\n");
++$error_count;
continue;
} else fprintf($lint_writer, "Adding $filename\n");
foreach ($single_suggestion_list as $suggestion) {
$error_count += validate_suggestion_item($suggestion);
}
}
if ($error_count != 0) {
$msg = "\n$error_count ERRORS.\n";
fprintf($lint_writer, $msg);
fprintf(STDERR, $msg);
exit(1);
} else {
$msg = "No errors.\n";
fprintf($lint_writer, $msg);
fprintf(STDERR, $msg);
exit(0);
}
// END MAIN
function validate_bloatware_item(array $item): int {
global $lint_writer;
$error_count = 0;
// `id` is a string
if (gettype($item['id']) != 'string') {
fprintf($lint_writer, "Expected `id` field to be a string, found: " . gettype($item['id']) . "\n");
++$error_count;
}
// `label` is an optional string
if (isset($item['label']) && gettype($item['label']) != 'string') {
fprintf($lint_writer, "{$item['id']}: Expected `label` field to be a string, found: " . gettype($item['label']) . "\n");
++$error_count;
}
// `dependencies` is an optional string[]
if (isset($item['dependencies'])) {
if (gettype($item['dependencies']) != 'array') {
fprintf($lint_writer, "{$item['id']}: Expected `dependencies` field to be an array, found: " . gettype($item['dependencies']) . "\n");
++$error_count;
} else {
foreach ($item['dependencies'] as $dependency) {
if (gettype($dependency) != 'string') {
fprintf($lint_writer, "{$item['id']}: Expected `dependencies` items to be a string, found: " . gettype($dependency) . "\n");
++$error_count;
}
}
}
}
// `required_by` is an optional string[]
if (isset($item['required_by'])) {
if (gettype($item['required_by']) != 'array') {
fprintf($lint_writer, "{$item['id']}: Expected `required_by` field to be an array, found: " . gettype($item['required_by']) . "\n");
++$error_count;
} else {
foreach ($item['required_by'] as $required_by) {
if (gettype($required_by) != 'string') {
fprintf($lint_writer, "{$item['id']}: Expected `required_by` items to be a string, found: " . gettype($required_by) . "\n");
++$error_count;
}
}
}
}
// `tags` is an optional string[]
if (isset($item['tags'])) {
if (gettype($item['tags']) != 'array') {
fprintf($lint_writer, "{$item['id']}: Expected `tags` field to be an array, found: " . gettype($item['tags']) . "\n");
++$error_count;
} else {
foreach ($item['tags'] as $tag) {
if (gettype($tag) != 'string') {
fprintf($lint_writer, "{$item['id']}: Expected `tags` items to be a string, found: " . gettype($tag) . "\n");
++$error_count;
} else if (!in_array($tag, SUPPORTED_TAGS)) {
fprintf($lint_writer, "{$item['id']}: Invalid `tag`: $tag\n");
++$error_count;
}
}
}
}
// `description` is a string
if (gettype($item['description']) != 'string') {
fprintf($lint_writer, "{$item['id']}: Expected `description` field to be a string, found: " . gettype($item['description']) . "\n");
++$error_count;
}
// `web` is an optional string[]
if (isset($item['web'])) {
if (gettype($item['web']) != 'array') {
fprintf($lint_writer, "{$item['id']}: Expected `web` field to be an array, found: " . gettype($item['web']) . "\n");
++$error_count;
} else {
foreach ($item['web'] as $site) {
if (gettype($site) != 'string') {
fprintf($lint_writer, "{$item['id']}: Expected `web` items to be a string, found: " . gettype($site) . "\n");
++$error_count;
}
}
}
}
// `removal` is a string
if (gettype($item['removal']) != 'string') {
fprintf($lint_writer, "{$item['id']}: Expected `removal` field to be a string, found: " . gettype($item['removal']) . "\n");
++$error_count;
} else if (!in_array($item['removal'], SUPPORTED_REMOVAL_TYPES)) {
fprintf($lint_writer, "{$item['id']}: Invalid `removal` type: {$item['removal']}\n");
++$error_count;
}
// `warning` is an optional string
if (isset($item['warning']) && gettype($item['warning']) != 'string') {
fprintf($lint_writer, "{$item['id']}: Expected `warning` field to be a string, found: " . gettype($item['warning']) . "\n");
++$error_count;
}
// `warning` is an optional string
if (isset($item['suggestions'])) {
if (gettype($item['suggestions']) != 'string') {
fprintf($lint_writer, "{$item['id']}: Expected `suggestions` field to be a string, found: " . gettype($item['suggestions']) . "\n");
++$error_count;
} else {
$suggestion_file = SUGGESTIONS_DIR . '/' . $item['suggestions'] . '.json';
if (!file_exists($suggestion_file)) {
fprintf($lint_writer, "{$item['id']}: Suggestion ID ({$item['suggestions']}) does not exist.\n");
++$error_count;
}
}
}
return $error_count;
}
function validate_suggestion_item(array $item): int {
global $lint_writer;
$error_count = 0;
// `id` is a string
if (gettype($item['id']) != 'string') {
fprintf($lint_writer, "Expected `id` field to be a string, found: " . gettype($item['id']) . "\n");
++$error_count;
}
// `label` is a string
if (gettype($item['label']) != 'string') {
fprintf($lint_writer, "{$item['id']}: Expected `label` field to be a string, found: " . gettype($item['label']) . "\n");
++$error_count;
}
// `reason` is an optional string
if (isset($item['reason']) && gettype($item['reason']) != 'string') {
fprintf($lint_writer, "{$item['id']}: Expected `reason` field to be a string, found: " . gettype($item['reason']) . "\n");
++$error_count;
}
// `source` is an optional string
if (isset($item['source'])) {
if (gettype($item['source']) != 'string') {
fprintf($lint_writer, "{$item['id']}: Expected `source` field to be a string, found: " . gettype($item['source']) . "\n");
++$error_count;
} else if (!preg_match("/^[fgas]+$/", $item['source'])) {
fprintf($lint_writer, "{$item['id']}: Expected `source` field to contain one or more from `fgas`, found: {$item['source']}\n");
++$error_count;
}
}
// `repo` is a string
if (gettype($item['repo']) != 'string') {
fprintf($lint_writer, "{$item['id']}: Expected `repo` field to be a string, found: " . gettype($item['repo']) . "\n");
++$error_count;
}
return $error_count;
}

View File

@ -5,9 +5,9 @@ $json_files = array_filter(scandir(__DIR__), function ($item) {
});
foreach ($json_files as $json_file) {
$list = json_decode(file_get_contents(__DIR__ . '/' . $json_file), true);
$list = json_decode(file_get_contents(__DIR__ . 'test.php/' . $json_file), true);
usort($list, function ($o1, $o2) {
return $o1['id'] <=> $o2['id'];
});
file_put_contents(__DIR__ . '/' . $json_file, json_encode($list, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
file_put_contents(__DIR__ . 'test.php/' . $json_file, json_encode($list, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
}

View File

@ -4,5 +4,5 @@
"label": "Binary Eye",
"source": "fg",
"repo": "https://github.com/markusfisch/BinaryEye"
},
}
]