Add support for pgindent to GitHub Actions and pre-commit.py

Gate PRs on whether or not pgindent passes for C files in pgxn.
This commit is contained in:
Tristan Partin
2023-12-08 14:49:53 -06:00
committed by Tristan Partin
parent 8bb45fd5da
commit 0f5e118789
6 changed files with 97 additions and 3783 deletions

52
.github/workflows/pgindent.yml vendored Normal file
View File

@@ -0,0 +1,52 @@
name: pgindent Neon
on:
push:
branches:
- main
- release
paths:
- 'pgxn/**.[ch]'
- '.github/workflows/pgindent.yml'
pull_request:
paths:
- 'pgxn/**.[ch]'
- '.github/workflows/pgindent.yml'
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
jobs:
pgindent:
runs-on: ubuntu-24.04
container:
image: 369495373322.dkr.ecr.eu-central-1.amazonaws.com/rust:pinned
options: --init
steps:
- name: Checkout
uses: actions/checkout@v3
with:
submodules: true
fetch-depth: 1
- name: Set pg 17 revision for caching
id: pg_v17_rev
run: echo pg_rev=$(git rev-parse HEAD:vendor/postgres-v17) >> $GITHUB_OUTPUT
- name: Cache postgres v17 build
id: cache_pg_17
uses: actions/cache@v3
with:
path: pg_install/v17
key: v1-${{ runner.os }}-release-pg-${{ steps.pg_v17_rev.outputs.pg_rev }}-${{ hashFiles('Makefile') }}
- name: Run pgindent
run: |
make -s -j neon-pgindent-check
- name: How to fix
if: ${{ failure() }}
run: |
echo Run \"make neon-pgindent\" in the event of a failure

View File

@@ -19,6 +19,7 @@ ln -s ../../pre-commit.py .git/hooks/pre-commit
```
This will run following checks on staged files before each commit:
- `pgindent` over any Neon Postgres extension files
- `rustfmt`
- checks for Python files, see [obligatory checks](/docs/sourcetree.md#obligatory-checks).

View File

@@ -215,6 +215,10 @@ neon-pgindent: postgres-v17-pg-bsd-indent neon-pg-ext-v17
-C $(BUILD_DIR)/pgxn-v17/neon \
-f $(ROOT_PROJECT_DIR)/pgxn/neon/Makefile pgindent
# Check whether pxgn/neon code is compliant with pgindent.
.PHONY: pgindent
neon-pgindent-check:
$(MAKE) PGINDENT_FLAGS=--silent-diff neon-pgindent
.PHONY: setup-pre-commit-hook
setup-pre-commit-hook:

View File

@@ -87,11 +87,14 @@ libwalproposer.a: $(WALPROP_OBJS)
# INDENT pointing to pg_bsd_indent
# PGINDENT_SCRIPT pointing to pgindent (be careful with PGINDENT var name:
# pgindent will pick it up as pg_bsd_indent path).
#
# optional vars:
# PGINDENT_FLAGS additional flags to pass to pgindent
.PHONY: pgindent
pgindent:
+@ echo top_srcdir=$(top_srcdir) top_builddir=$(top_builddir) srcdir=$(srcdir)
$(FIND_TYPEDEF) . > neon.typedefs
INDENT=$(INDENT) $(PGINDENT_SCRIPT) --typedefs neon.typedefs $(srcdir)/*.c $(srcdir)/*.h
INDENT=$(INDENT) $(PGINDENT_SCRIPT) $(PGINDENT_FLAGS) --typedefs neon.typedefs $(srcdir)/*.c $(srcdir)/*.h
PG_CONFIG = pg_config
PGXS := $(shell $(PG_CONFIG) --pgxs)

File diff suppressed because it is too large Load Diff

View File

@@ -10,7 +10,7 @@ import sys
@enum.unique
class Color(enum.Enum):
class Color(enum.StrEnum):
RED = "\033[0;31m"
GREEN = "\033[0;33m"
CYAN = "\033[0;36m"
@@ -61,16 +61,35 @@ def get_commit_files() -> list[str]:
return files.decode().splitlines()
def pgindent(fix_inplace: bool) -> str:
if fix_inplace:
return "make neon-pgindent"
return "make -s -j neon-pgindent-check"
def is_applicable(fname: str, suffix: str | set[str]) -> bool:
fname = fname.strip()
if isinstance(suffix, str):
suffix = {suffix}
for s in suffix:
if fname.endswith(s):
return True
return False
def check(
name: str,
suffix: str,
suffix: str | set[str],
cmd: str,
changed_files: list[str],
no_color: bool = False,
append_files_to_cmd: bool = True,
):
print(f"Checking: {name} ", end="")
applicable_files = list(filter(lambda fname: fname.strip().endswith(suffix), changed_files))
applicable_files = list(filter(lambda fname: is_applicable(fname, suffix), changed_files))
if not applicable_files:
print(colorify("[NOT APPLICABLE]", Color.CYAN, no_color))
return
@@ -83,15 +102,20 @@ def check(
print(colorify("[FAILED]", Color.RED, no_color))
if name == "mypy":
print("Please inspect the output below and fix type mismatches.")
elif name == "pgindent":
print("pgindent does not print output.")
else:
print("Please inspect the output below and run make fmt to fix automatically.")
if suffix == ".py":
print(
"If the output is empty, ensure that you've installed Python tooling by\n"
"running './scripts/pysync' in the current directory (no root needed)"
+ "running './scripts/pysync' in the current directory (no root needed)"
)
print()
print(res.stdout.decode())
output = res.stdout.decode()
if len(output) > 0:
print()
print(res.stdout.decode())
sys.exit(1)
print(colorify("[OK]", Color.GREEN, no_color))
@@ -138,3 +162,9 @@ if __name__ == "__main__":
changed_files=files,
no_color=args.no_color,
)
check(
name="pgindent",
suffix={"c", "h"},
cmd=pgindent(fix_inplace=args.fix_inplace),
changed_files=files,
)