From 7f7aa09c9ad3821d1638a5cf0c0c8dc56599ebb9 Mon Sep 17 00:00:00 2001 From: Paolo Tranquilli Date: Thu, 26 Oct 2023 10:58:39 +0200 Subject: [PATCH] Add pre-commit configuration [pre-commit](https://pre-commit.com/) is a framework for easy set-up of pre-commit hooks. This adds configuration for it to run: * ts compilation * ts linting * pr-checks synchronization The latter required the `sync.py` to be callable from the project root. `pre-commit` can be enabled with ``` python3 -m pip install pre-commit pre-commit install ``` --- .pre-commit-config.yaml | 20 ++++++++++++++++++++ pr-checks/sync.py | 14 +++++++++----- 2 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 .pre-commit-config.yaml mode change 100644 => 100755 pr-checks/sync.py diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000000..421720e468 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,20 @@ +repos: + - repo: local + hooks: + - id: compile-ts + name: Compile typescript + files: \.[tj]s$ + language: system + entry: npm run build + pass_filenames: false + - id: lint-ts + name: Lint typescript code + files: \.ts$ + language: system + entry: npm run lint -- --fix + - id: pr-checks-sync + name: Synchronize PR check workflows + files: ^.github/workflows/__.*\.yml$|^pr-checks + language: system + entry: python3 pr-checks/sync.py + pass_filenames: false diff --git a/pr-checks/sync.py b/pr-checks/sync.py old mode 100644 new mode 100755 index 995705d2a8..652cf0c37f --- a/pr-checks/sync.py +++ b/pr-checks/sync.py @@ -1,6 +1,8 @@ +#!/usr/bin/env python + import ruamel.yaml from ruamel.yaml.scalarstring import FoldedScalarString -import os +import pathlib import textwrap # The default set of CodeQL Bundle versions to use for the PR checks. @@ -47,9 +49,11 @@ def writeHeader(checkStream): yaml = ruamel.yaml.YAML() yaml.Representer = NonAliasingRTRepresenter +this_dir = pathlib.Path(__file__).resolve().parent + allJobs = {} -for file in os.listdir('checks'): - with open(f"checks/{file}", 'r') as checkStream: +for file in (this_dir / 'checks').glob('*.yml'): + with open(file, 'r') as checkStream: checkSpecification = yaml.load(checkStream) matrix = [] @@ -126,9 +130,9 @@ def writeHeader(checkStream): checkJob['env'] = checkJob.get('env', {}) if 'CODEQL_ACTION_TEST_MODE' not in checkJob['env']: checkJob['env']['CODEQL_ACTION_TEST_MODE'] = True - checkName = file[:len(file) - 4] + checkName = file.stem - with open(f"../.github/workflows/__{checkName}.yml", 'w') as output_stream: + with open(this_dir.parent / ".github" / "workflows" / f"__{checkName}.yml", 'w') as output_stream: writeHeader(output_stream) yaml.dump({ 'name': f"PR Check - {checkSpecification['name']}",