Skip to content

Commit

Permalink
3.0 updates (#23) (#24)
Browse files Browse the repository at this point in the history
* Updating precommit hook to not require an api key

* updating reference and readme for 3.0
  • Loading branch information
koryf committed Mar 29, 2023
1 parent 352398f commit 67567f5
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 16 deletions.
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ This integration requires an endpoint to make requests against. For instructions
``` yaml
repos:
- repo: https://github.com/privateai/pai-pre-commit-hook.git
rev: v1.2.1-beta
rev: v1.3.0
hooks:
- id: pii-check
args:
Expand All @@ -35,9 +35,7 @@ This integration requires an endpoint to make requests against. For instructions

1. Replace `<URL>` with the url of where your container is hosted.\
eg. [http://localhost:8080/v3/process_text](http://localhost:8080/v3/process_text) for a container running locally.
1. Create a .env file and add your API_KEY like so:\
API_KEY=`<put your API KEY here>`
1. Replace `<ENV_FILE_PATH>` with the path to your .env file.

1. Run 'pre-commit install' from inside the git repo where you want to use this hook.

After the above steps, your project structure should look somewhat like:
Expand Down Expand Up @@ -65,7 +63,7 @@ Here is an example of what your pre-commit-config.yaml may look like:
``` yaml
repos:
- repo: https://github.com/privateai/pai-pre-commit-hook.git
rev: v1.2.1-beta
rev: v1.3.0
hooks:
- id: pii-check
args:
Expand Down
15 changes: 5 additions & 10 deletions pii_check/pii_check_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ def get_ignored_lines(filename: str) -> Set[int]:
return ignored_lines


def get_response_from_api(content, url, api_key, enabled_entity_list, blocked_list):
def get_response_from_api(content, url, enabled_entity_list, blocked_list):
payload = get_payload(content, enabled_entity_list, blocked_list)
headers = {"Content-Type": "application/json", "X-API-KEY": api_key}
headers = {"Content-Type": "application/json"}

response = requests.post(url, json=payload, headers=headers)
response.raise_for_status()
Expand Down Expand Up @@ -106,7 +106,7 @@ class PiiResult:
entity_type: str


def check_for_pii(filename: str, url: str, api_key: str, enabled_entity_list: List[str], blocked_list: List[str]) -> List[PiiResult]:
def check_for_pii(filename: str, url: str, enabled_entity_list: List[str], blocked_list: List[str]) -> List[PiiResult]:

if not os.path.getsize(filename):
# dont't expect PII in empty files
Expand All @@ -121,7 +121,7 @@ def check_for_pii(filename: str, url: str, api_key: str, enabled_entity_list: Li
# this contains also context around the added lines
added_text = ["".join(hunk.target) for hunk in hunks]

api_pii_results = get_response_from_api(added_text, url, api_key, enabled_entity_list, blocked_list)
api_pii_results = get_response_from_api(added_text, url, enabled_entity_list, blocked_list)
ignored_lines = get_ignored_lines(filename)

pii_results: List[PiiResult] = []
Expand Down Expand Up @@ -158,11 +158,6 @@ def main():
dotenv_path = Path(os.environ["PWD"], args.env_file_path)
load_dotenv(dotenv_path=dotenv_path)

if "API_KEY" in os.environ:
API_KEY = os.environ["API_KEY"]
else:
sys.exit("Your .env file is missing from the provided path or does not contain API_KEY")

enabled_entity_list = [item.upper() for item in args.enabled_entities]

blocked_list = [blocked for blocked in args.blocked_list] if args.blocked_list else []
Expand All @@ -171,7 +166,7 @@ def main():
pii_results = [
result
for filename in args.filenames
for result in check_for_pii(os.path.abspath(filename), args.url, API_KEY, enabled_entity_list, blocked_list)
for result in check_for_pii(os.path.abspath(filename), args.url, enabled_entity_list, blocked_list)
]
except RuntimeError as e:
print(e)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from setuptools import find_packages, setup

VERSION = "1.2.1"
VERSION = "1.3.0"
DESCRIPTION = "Pre-commit hook to check if modified files have PII and marks it."


Expand Down

0 comments on commit 67567f5

Please sign in to comment.