Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a GitHub CI to check sorting HttpsExclusions #596

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
33 changes: 33 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Lint HttpsExclusions

env:
NODE_VERSION: 20

on:
push:
branches:
- master
paths:
- "**/*.txt"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both "**/*.txt" can be replaced by a relative path of banks.txt?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, But, it can be extended in the future.

pull_request:
branches:
- master
paths:
- "**/*.txt"

jobs:
bank:
name: Check that banks.txt is sorted
runs-on: ubuntu-latest
steps:
- name: Set up Node.js LTS
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
cache: npm
- name: Install dependencies
run: npm ci
- name: Check out to repository
uses: actions/checkout@v4
- name: Run lint
run: npm run lint
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
dist
.DS_Store
.DS_Store
node_modules
140 changes: 140 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,13 @@
"main": "index.js",
"repository": "[email protected]:AdguardTeam/HttpsExclusions.git",
"author": "AdGuard",
"license": "MIT"
"license": "MIT",
"scripts": {
"lint": "tsx tools/check.ts exclusions/banks.txt"
},
"devDependencies": {
"@types/node": "^20.14.2",
"tsx": "^4.15.4",
"typescript": "^5.4.5"
}
}
65 changes: 65 additions & 0 deletions tools/check.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const { readFile, stat } = require('fs').promises;

const RE_NL = /\r?\n/;
const COMMENT_MARKER = '//';

const isFileExists = async (filePath: string): Promise<boolean> => {
return stat(filePath)
.then((stats) => stats.isFile())
.catch(() => false);
};

const readRules = async (filePath: string): Promise<string[]> => {
const fileContent = await readFile(filePath, { encoding: 'utf-8' });

return fileContent
.split(RE_NL)
.map((line) => line.trim())
.filter((line) => line.length > 0 && !line.startsWith(COMMENT_MARKER));
};

const isSortedAlphabetically = (arr: string[]): boolean => {
for (let i = 0; i < arr.length - 1; i += 1) {
if (arr[i] > arr[i + 1]) {
return false;
}
}
return true;
};

const getErrorMessage = (err: unknown): string => {
if (err instanceof Error) {
return err.message;
}

return 'Unknown error';
};

const isNonEmptyString = (value: unknown): value is string => {
return typeof value === 'string' && value.length > 0;
};

const main = async () => {
const filePath = process.argv[2];

try {
if (!isNonEmptyString(filePath)) {
throw new Error('File path is not provided');
}

if (!(await isFileExists(filePath))) {
throw new Error(`File ${filePath} does not exist`);
}

const rules = await readRules(filePath);

if (!isSortedAlphabetically(rules)) {
throw new Error(`Rules in ${filePath} is not sorted alphabetically`);
}
} catch (err) {
console.error(getErrorMessage(err));
process.exit(1);
}
};

main();
13 changes: 13 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"target": "es2016",
"module": "commonjs",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
},
"include": [
"tools/**/*.ts"
]
}