Skip to content

Commit 8cf73b3

Browse files
committed
Make this into a github action
1 parent 94aad69 commit 8cf73b3

File tree

7 files changed

+327
-126
lines changed

7 files changed

+327
-126
lines changed

.github/workflows/precommit.yml

Lines changed: 0 additions & 11 deletions
This file was deleted.

.github/workflows/status_check.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: pre-commit-check
2+
on: [push, pull_request]
3+
jobs:
4+
Pull-request-check:
5+
runs-on: ubuntu-latest
6+
steps:
7+
- uses: actions/checkout@v2
8+
with:
9+
ref: ${{ github.head_ref }}
10+
fetch-depth: 0
11+
- uses: actions/setup-python@v2
12+
with:
13+
python-version: "3.7"
14+
- name: Install dependencies
15+
run: |
16+
python -m pip install --upgrade pip
17+
pip install flake8 pytest
18+
- name: Lint with flake8
19+
run: |
20+
# stop the build if there are Python syntax errors or undefined names
21+
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
22+
# exit-zero treats all errors as warnings.
23+
flake8 . --count --exit-zero --max-complexity=10 --statistics
24+
- name: Pytest
25+
run: |
26+
pytest main/githooks.py
27+
- name: Check files with commit hook action
28+
uses: ccdc-opensource/commit-hooks@precommit_action

Dockerfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FROM python:3
2+
3+
COPY . .
4+
5+
CMD [ "python", "/main.py" ]
6+

README.md

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,45 @@
1+
This repository contains files that can be used as a github action and local
2+
git hooks.
3+
4+
It does a few checks on source codes to ensure compliance with some general
5+
CCDC coding standard.
6+
7+
The commit will be flagged if it includes text files with:
8+
9+
* File name that does meet Windows filename requirement
10+
* CRLF line endings
11+
* NO NOT MERGE or DO NOT COMMIT
12+
* Trailing whitespaces
13+
* Tabs
14+
* Missing terminating newline for certain files
15+
* Certain C++ #include patterns and std::exception
16+
17+
18+
# Github action
19+
20+
## Usage
21+
```yaml
22+
- uses: ccdc-opensource/commit-hooks@v1
23+
```
24+
25+
## Scenarios
26+
### Check files
27+
```yaml
28+
- uses: actions/checkout@v2
29+
with:
30+
ref: ${{ github.head_ref }}
31+
fetch-depth: 0
32+
- id: check_files
33+
uses: ccdc-opensource/commit-hooks@v1
34+
```
35+
136
# commit-hooks
2-
A set of commit hooks that repositories can use to automate code checks
37+
You can use this as git hooks for local repositories.
338
4-
It is recommended at all developers set up the hooks.
39+
A set of hooks include:
40+
* commit-msg
41+
* pre-commit
42+
* pre-merge-commit
543
644
## Setting up
745
1. Clone this repo

action.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name: CCDC File Checks
2+
author: CCDC
3+
description: Check changed files for compliance
4+
runs:
5+
using: "docker"
6+
image: "Dockerfile"
7+
branding:
8+
icon: 'check-square'
9+
color: 'green'

main.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env python3
2+
'''
3+
This is a github action entry point.
4+
5+
This github action does some checks on changed files.
6+
7+
'''
8+
9+
from pathlib import Path
10+
import sys
11+
12+
sys.path.insert(0, str(Path(__file__).resolve().parent / 'main'))
13+
import githooks
14+
15+
if __name__ == '__main__':
16+
17+
print(f'Checking commit {githooks.get_sha()} by {githooks.get_user()} in {githooks.get_branch()}')
18+
19+
files = githooks.get_commit_files()
20+
print(f'Checking {githooks.get_event()} modified files:')
21+
print(' ' + '\n '.join(files['M']))
22+
print(f'Checking {githooks.get_event()} new files:')
23+
print(' ' + '\n '.join(files['A']))
24+
25+
retval = 0
26+
27+
if githooks._is_pull_request():
28+
retval += githooks.check_do_not_merge(files['M'])
29+
retval += githooks.check_do_not_merge(files['A'], new_files=True)
30+
31+
retval += githooks.remove_trailing_white_space(files['M'], in_place=False)
32+
retval += githooks.remove_trailing_white_space(files['A'], new_files=True,
33+
in_place=False)
34+
retval += githooks.check_filenames(files['M'] + files['A'])
35+
retval += githooks.check_eol(files['M'] + files['A'])
36+
retval += githooks.check_content(files['M'] + files['A'])
37+
38+
sys.exit(retval)

0 commit comments

Comments
 (0)