Skip to content

Add pre-commit hook to verify required apache headers - #91

Open
mschaefer28 wants to merge 1 commit into
vinyldns:masterfrom
mschaefer28:issue-7-license-header-check
Open

Add pre-commit hook to verify required apache headers#91
mschaefer28 wants to merge 1 commit into
vinyldns:masterfrom
mschaefer28:issue-7-license-header-check

Conversation

@mschaefer28

Copy link
Copy Markdown

Closes issue #7

Problem: Needed a way to automatically check that the apache license header exists in all relevant files to ensure it does not accidentally get left out from future commits to any relevant source files.

Approach: I implemented a pre-commit hook (.pre-commit-config.yaml) that calls a small python script (scripts/apache_header_check.py). The script checks all ‘.py’ files in the repo to see if the license boiler text exists in the comments of each file. If it does not, it fails and lets you know which file is missing it. I did not check the full copyright text as I noticed the copyright year isn’t always 2018 , so I only checked ‘http://www.apache.org/licenses/LICENSE-2.0', but can rework to include full copyright text check if needed.

Validation: You can test this by choosing a current ‘.py’ file that has the boiler plate text and removing it. When you go to commit the file, it will error out and let you know that the file is missing the boiler plate text. If you re-add the license information and re-commit it will pass. You can also manually run the command in the terminal with: pre-commit run apache-license-header-check --all-files

@CLAassistant

CLAassistant commented Aug 11, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (master@6038702). Learn more about missing BASE report.

Additional details and impacted files
@@            Coverage Diff            @@
##             master      #91   +/-   ##
=========================================
  Coverage          ?   93.84%           
=========================================
  Files             ?        9           
  Lines             ?     1138           
  Branches          ?        0           
=========================================
  Hits              ?     1068           
  Misses            ?       70           
  Partials          ?        0           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@JoshSEdwards JoshSEdwards left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for adding this, and I'm glad it did catch a few files that were missing headers! Just a few minor comments below.

I think it would be helpful to document how to install and enable the hook in the contributor setup docs. Could we also add pre-commit run --all-files to CI, after
installing pre-commit there? That would make the local workflow clear and ensure the check still runs when someone hasn't enabled the local hook.

for token in tokenize.generate_tokens(file.readline):
# Check if the current token is a comment
if token.type == tokenize.COMMENT:
# Check if apache link string is inside that comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This looks pretty good, but it currently allows for the Apache link string to be in a comment anywhere in the file. For example, a Python file with no header but a trailing comment such as # http://www.apache.org/licenses/LICENSE-2.0 passes. I think we should restrict the check to the initial header region, allowing only a shebang/encoding declaration before it, or if it's easier we can just validate the full expected header.

Comment thread .pre-commit-config.yaml
@@ -0,0 +1,11 @@

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is nitpicky, but can we remove the blank lines at the beginning and end of this file?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants