Skip to content

feat(terrascan): Add support for __GIT_WORKING_DIR__ in hooks arguments#943

Merged
MaxymVlasov merged 2 commits intoantonbabenko:masterfrom
GSokol:feat/support_GIT_WORKING_DIR_for_terrasan
Oct 14, 2025
Merged

feat(terrascan): Add support for __GIT_WORKING_DIR__ in hooks arguments#943
MaxymVlasov merged 2 commits intoantonbabenko:masterfrom
GSokol:feat/support_GIT_WORKING_DIR_for_terrasan

Conversation

@GSokol
Copy link
Copy Markdown
Contributor

@GSokol GSokol commented Oct 10, 2025

Put an x into the box if that apply:

  • This PR introduces breaking change.
  • This PR fixes a bug.
  • This PR adds new functionality.
  • This PR enhances existing functionality.

Description of your changes

Add __GIT_WORKING_DIR__ placeholder support for terrascan.

How can we test changes

In some repository:

cat <<EOF | .terrascan.toml
[severity]
level = "high"
EOF

Then, add it ti .pre-commit-config.yaml

- id: terrascan
  args:
    - --args=--config-path=__GIT_WORKING_DIR__/.terrascan.toml

Copilot AI review requested due to automatic review settings October 10, 2025 14:00
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds support for the __GIT_WORKING_DIR__ placeholder in the terrascan hook, allowing users to reference the repository root directory in terrascan configuration paths.

  • Implements string replacement for __GIT_WORKING_DIR__ placeholder in terrascan arguments
  • Documents the new functionality with an example usage for config file paths

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
hooks/terrascan.sh Adds loop to replace __GIT_WORKING_DIR__ placeholder with current working directory
README.md Documents the new placeholder functionality with example usage

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment thread hooks/terrascan.sh
Comment thread README.md Outdated
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Oct 10, 2025

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

📝 Walkthrough

Summary by CodeRabbit

  • New Features
    • Added support for the GIT_WORKING_DIR placeholder in terrascan hook arguments, enabling paths to resolve relative to the repository root.
  • Documentation
    • Updated README with examples showing how to pass a config file path using GIT_WORKING_DIR, including a YAML snippet demonstrating --config-path=GIT_WORKING_DIR/.terrascan.toml.

Walkthrough

Adds placeholder substitution for __GIT_WORKING_DIR__ in the Terrascan hook arguments so repo-root paths are expanded before execution, and updates README.md with an example demonstrating --config-path=__GIT_WORKING_DIR__/....

Changes

Cohort / File(s) Summary of Changes
Documentation
README.md
Inserted example(s) showing how to pass a Terrascan config path using the --config-path=__GIT_WORKING_DIR__/.terrascan.toml placeholder.
TerraScan Hook Logic
hooks/terrascan.sh
Added loop in main to replace __GIT_WORKING_DIR__ occurrences in the ARGS array with the current working directory before invoking per_dir_hook; no other behavioral changes.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor Developer as Developer
  participant Hook as hooks/terrascan.sh
  participant Subst as Placeholder Substitution
  participant PerDir as per_dir_hook
  participant Terra as terrascan

  Developer->>Hook: Run hook with ARGS (may include __GIT_WORKING_DIR__)
  Hook->>Subst: Iterate ARGS and replace __GIT_WORKING_DIR__ with CWD
  Subst-->>Hook: Return updated ARGS
  Hook->>PerDir: Invoke per_dir_hook using updated ARGS
  PerDir->>Terra: Execute terrascan with resolved config/path
  Terra-->>PerDir: Return results
  PerDir-->>Hook: Aggregate results
  Hook-->>Developer: Emit output/exit status
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed The pull request description clearly relates to the changeset by outlining the enhancement of supporting the GIT_WORKING_DIR placeholder in terrascan. It includes relevant information on testing procedures and specifies what to expect. The level of detail is appropriate for understanding the purpose of the changes.
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.
Title Check ✅ Passed The pull request title succinctly follows conventional commit style by indicating a new feature for the terrascan hook and clearly states the addition of support for the __GIT_WORKING_DIR__ placeholder in hook arguments, directly reflecting the main enhancement implemented.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Comment thread hooks/terrascan.sh
Comment on lines +15 to +18
# Support for setting PATH to repo root.
for i in "${!ARGS[@]}"; do
ARGS[i]=${ARGS[i]/__GIT_WORKING_DIR__/$(pwd)\/}
done
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@MaxymVlasov Granted this is going to be 5th hook (out of 16) that has this feature implemented locally inside hook, would it make sense to look into moving this feature over right into common::parse_cmdline function so that it is available to all hooks that utilize common::parse_cmdline func?
Are there any obstacles or issues with this that I can't see off hands?
If not, then I can work on this task. Please let me know.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Yes, should be generally supported by most of hooks.
As far as I remember, it just not make much sense in some hooks, but that's a nit a big deal

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ok, would move it there.

Copy link
Copy Markdown
Collaborator

@yermulnik yermulnik Oct 10, 2025

Choose a reason for hiding this comment

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

@GSokol Oh, I didn't mean for you to work on it (I thought I'd pick this up in a separate PR), although if you're up to — that's marvelous.
Once moved, these hooks need to be updated by removing the for loop (+ probably README update to provide more clear info):

hooks/terraform_tflint.sh:18:    ARGS[i]=${ARGS[i]/__GIT_WORKING_DIR__/$(pwd)\/}
hooks/terraform_trivy.sh:17:    ARGS[i]=${ARGS[i]/__GIT_WORKING_DIR__/$(pwd)\/}
hooks/terraform_checkov.sh:17:    ARGS[i]=${ARGS[i]/__GIT_WORKING_DIR__/$(pwd)\/}
hooks/terraform_tfsec.sh:17:    ARGS[i]=${ARGS[i]/__GIT_WORKING_DIR__/$(pwd)\/}

Let me know if you'd prefer not to pick this up (it is all fine!) and I'll work on this bit in a separate PR. Thanks.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I'm approving this PR as it is at the moment though.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Let me know if you'd prefer not to pick this up (it is all fine!) and I'll work on this bit in a separate PR. Thanks.

@GSokol Could you please let me know whether you're ok to pick this up or would prefer me to take on this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I would pick this up into a separate PR (as I need the changes ASAP).

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@MaxymVlasov I've created an issue for this: #944

@GSokol Feel free to pick that issue up if you're up to 👍🏻

Copy link
Copy Markdown
Collaborator

@yermulnik yermulnik Oct 16, 2025

Choose a reason for hiding this comment

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

@GSokol Would you have a chance to check whether #945 works as expected for your use case please?

Comment thread README.md Outdated
Co-authored-by: George Yermulnik (Georgii Iermulnik) <yz@yz.kiev.ua>
Comment thread hooks/terrascan.sh
Comment on lines +15 to +18
# Support for setting PATH to repo root.
for i in "${!ARGS[@]}"; do
ARGS[i]=${ARGS[i]/__GIT_WORKING_DIR__/$(pwd)\/}
done
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I'm approving this PR as it is at the moment though.

Copy link
Copy Markdown
Collaborator

@MaxymVlasov MaxymVlasov left a comment

Choose a reason for hiding this comment

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

Let's merge it at the EOW as is, if @GSokol will not respond

@GSokol
Copy link
Copy Markdown
Contributor Author

GSokol commented Oct 14, 2025

I can't merge it. And as I understand, we agreed, that I would later create a separate PR for moving __GIT_WORKING_DIR__ processing to the shared scope.

@MaxymVlasov MaxymVlasov changed the title feat: Support __GIT_WORKING_DIR__ for terrascan feat(terrascan): Add support for __GIT_WORKING_DIR__ in hooks arguments Oct 14, 2025
@MaxymVlasov MaxymVlasov merged commit c4ef91e into antonbabenko:master Oct 14, 2025
45 checks passed
antonbabenko pushed a commit that referenced this pull request Oct 14, 2025
# [1.102.0](v1.101.1...v1.102.0) (2025-10-14)

### Features

* **terrascan:** Add support for `__GIT_WORKING_DIR__` in hooks arguments ([#943](#943)) ([c4ef91e](c4ef91e))
@antonbabenko
Copy link
Copy Markdown
Owner

This PR is included in version 1.102.0 🎉

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

Labels

feature New feature or request hook/terrascan Bash hook

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants