Skip to content

Commit

Permalink
[TT-13669] Add pre-commit, pre-push hooks (#6733)
Browse files Browse the repository at this point in the history
<details open>
<summary><a href="https://tyktech.atlassian.net/browse/TT-13669"
title="TT-13669" target="_blank">TT-13669</a></summary>
  <br />
  <table>
    <tr>
      <th>Summary</th>
      <td>Add pre commit hooks to run task lint</td>
    </tr>
    <tr>
      <th>Type</th>
      <td>
<img alt="Task"
src="https://tyktech.atlassian.net/rest/api/2/universal_avatar/view/type/issuetype/avatar/10318?size=medium"
/>
        Task
      </td>
    </tr>
    <tr>
      <th>Status</th>
      <td>In Dev</td>
    </tr>
    <tr>
      <th>Points</th>
      <td>N/A</td>
    </tr>
    <tr>
      <th>Labels</th>
      <td>-</td>
    </tr>
  </table>
</details>
<!--
  do not remove this marker as it will break jira-lint's functionality.
  added_by_jira_lint
-->

---

### **User description**
<!-- Provide a general summary of your changes in the Title above -->

## Description
This PR adds pre commit hook scripts to the project.
A task command `setup` is also added, so that a new contributor can have
the dependencies installed and pre commit hook setup locally.

## Related Issue
https://tyktech.atlassian.net/browse/TT-13669

## Motivation and Context

<!-- Why is this change required? What problem does it solve? -->

## How This Has Been Tested

<!-- Please describe in detail how you tested your changes -->
<!-- Include details of your testing environment, and the tests -->
<!-- you ran to see how your change affects other areas of the code,
etc. -->
<!-- This information is helpful for reviewers and QA. -->

## Screenshots (if appropriate)

## Types of changes

<!-- What types of changes does your code introduce? Put an `x` in all
the boxes that apply: -->

- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Refactoring or add test (improvements in base code or adds test
coverage to functionality)

## Checklist

<!-- Go over all the following points, and put an `x` in all the boxes
that apply -->
<!-- If there are no documentation updates required, mark the item as
checked. -->
<!-- Raise up any additional concerns not covered by the checklist. -->

- [ ] I ensured that the documentation is up to date
- [ ] I explained why this PR updates go.mod in detail with reasoning
why it's required
- [ ] I would like a code coverage CI quality gate exception and have
explained why


___

### **PR Type**
Enhancement, Documentation


___

### **Description**
- Introduced a pre-commit hook script (`scripts/pre-commit.sh`) to
enforce linting before commits.
- Added a `setup_hooks` task in `.taskfiles/deps/Taskfile.yml` for
setting up pre-commit hooks.
- Updated `Taskfile.yml` with a new `setup` task to streamline project
initialization, including dependencies and hooks.
- Enhanced `CONTRIBUTING.md` with instructions on using Task commands
for setup and testing, emphasizing the new pre-commit hook setup.



___



### **Changes walkthrough** 📝
<table><thead><tr><th></th><th align="left">Relevant
files</th></tr></thead><tbody><tr><td><strong>Enhancement</strong></td><td><table>
<tr>
  <td>
    <details>
<summary><strong>pre-commit.sh</strong><dd><code>Add pre-commit script
for linting enforcement</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></summary>
<hr>

scripts/pre-commit.sh

<li>Added a pre-commit script to run linting tasks before commits.<br>
<li> Ensures commits are aborted if linting fails.<br> <li> Provides
feedback on linting success or failure.<br>


</details>


  </td>
<td><a
href="https://github.com/TykTechnologies/tyk/pull/6733/files#diff-daf6c6cbb9aa58db0628121a2e695ff3ec13927c9b3641aebc8f2be89d231552">+14/-0</a>&nbsp;
&nbsp; </td>

</tr>

<tr>
  <td>
    <details>
<summary><strong>Taskfile.yml</strong><dd><code>Add task for pre-commit
hook setup</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></summary>
<hr>

.taskfiles/deps/Taskfile.yml

<li>Added a <code>setup_hooks</code> task to set up pre-commit
hooks.<br> <li> Updated the <code>default</code> task to include
<code>setup_hooks</code>.<br> <li> Defined commands for copying and
enabling the pre-commit script.<br>


</details>


  </td>
<td><a
href="https://github.com/TykTechnologies/tyk/pull/6733/files#diff-bd2d25e8a19db2d54ea34295acdc50b5402b7469eaf3e16b083f3e66dbe4fd9d">+9/-0</a>&nbsp;
&nbsp; &nbsp; </td>

</tr>

<tr>
  <td>
    <details>
<summary><strong>Taskfile.yml</strong><dd><code>Add setup task for
project initialization</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
</dd></summary>
<hr>

Taskfile.yml

<li>Added a <code>setup</code> task to install dependencies and
pre-commit hooks.<br> <li> Linked <code>setup</code> task to
<code>deps:setup_hooks</code> for hook setup.<br>


</details>


  </td>
<td><a
href="https://github.com/TykTechnologies/tyk/pull/6733/files#diff-cd2d359855d0301ce190f1ec3b4c572ea690c83747f6df61c9340720e3d2425e">+6/-0</a>&nbsp;
&nbsp; &nbsp; </td>

</tr>
</table></td></tr><tr><td><strong>Documentation</strong></td><td><table>
<tr>
  <td>
    <details>
<summary><strong>CONTRIBUTING.md</strong><dd><code>Update contributing
guide with Task commands</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></summary>
<hr>

CONTRIBUTING.md

<li>Updated documentation to include Task commands for setup and
testing.<br> <li> Added instructions for setting up pre-commit hooks
using <code>task setup</code>.<br> <li> Improved clarity on using Task
for local development.<br>


</details>


  </td>
<td><a
href="https://github.com/TykTechnologies/tyk/pull/6733/files#diff-eca12c0a30e25b4b46522ebf89465a03ba72a03f540796c979137931d8f92055">+10/-2</a>&nbsp;
&nbsp; </td>

</tr>
</table></td></tr></tr></tbody></table>

___

> 💡 **PR-Agent usage**: Comment `/help "your question"` on any pull
request to receive relevant information
  • Loading branch information
jeffy-mathew authored and sredny buitrago committed Dec 4, 2024
1 parent 1046152 commit 17b3910
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 3 deletions.
9 changes: 9 additions & 0 deletions .taskfiles/deps/Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ tasks:
- golangci-lint
- faillint
- mockgen
- lefthook
cmds:
- for: ['go-fsck', 'schema-gen', 'summary']
task: exp
Expand Down Expand Up @@ -47,6 +48,14 @@ tasks:
cmds:
- go install github.com/fatih/faillint@latest

lefthook:
internal: true
desc: 'Install lefthook'
status:
- type lefthook
cmds:
- go install github.com/evilmartians/lefthook@latest

exp:
desc: 'Install exp/cmd'
requires:
Expand Down
23 changes: 23 additions & 0 deletions .taskfiles/hooks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
version: "3"

includes:
lint_dep: lint.yml

tasks:
pre-commit:
desc: "Run pre-commit hooks"
cmds:
- task: :lint:tidy
- task: lint_dep:compile-tests

pre-push:
desc: "Run pre-push hooks"
cmds:
- task: :lint:tidy
- task: lint_dep:vet
- task: lint_dep:golangci-lint
vars:
args: "{{.golangci}} --new-from-rev=origin/master"
- task: :lint:x-tyk-gateway
- task: lint_dep:compile-tests
- task: lint_dep:simple-build
10 changes: 10 additions & 0 deletions .taskfiles/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,13 @@ tasks:
dir: '{{.root}}'
cmds:
- gofmt -w .

simple-build:
desc: "Run go build for sanity check"
cmds:
- go build .

compile-tests:
desc: "Compile go tests for sanity check"
cmds:
- cd gateway && go test -c .
12 changes: 10 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,17 @@ To build and test Tyk use built-in `go` commands: `go build` and `go test -v`. I

Currently, in order for tests to pass, a **Redis host is required**. We know, this is terrible and should be handled with an interface, and it is, however in the current version there is a hard requirement for the application to have its default memory setup to use Redis as part of a deployment, this is to make it easier to install the application for the end-user. Future versions will work around this, or we may drop the memory requirement. The simplest way to run Redis is to use official Docker image [https://hub.docker.com/_/redis/](https://hub.docker.com/_/redis/)

### Adding dependencies
#### Using Task Commands

If your patch depends on new packages, ensure that they will be put in `/vendor` folder. Here at Tyk we use `govendor` for managing our dependencies. Adding new dependencies can be done using following command: `govendor fetch github.com/alicebob/miniredis`.
In addition to the standard Go commands, we also provide [Task](https://taskfile.dev/) commands to simplify the setup and local dev process.
If you haven't installed Task yet, please follow the [installation instructions](https://taskfile.dev/installation).

Once Task is installed, you can use the following commands:

```shell
task setup # Sets up the project depdendencies, including pre-commit hooks
task test:integration # Runs the tests
```

### Geo IP features
This product utilises GeoLite2 data created by MaxMind, available from [http://www.maxmind.com](http://www.maxmind.com).
8 changes: 7 additions & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version: "3"
includes:
test: .taskfiles/test.yml
deps: .taskfiles/deps/Taskfile.yml
hooks: .taskfiles/hooks.yml
opentelemetry:
taskfile: ci/tests/tracing/Taskfile.yml
dir: ci/tests/tracing/
Expand Down Expand Up @@ -73,7 +74,6 @@ tasks:
- go mod tidy

lint:all:
internal: true
deps:
- lint:config
- lint:imports
Expand All @@ -95,3 +95,9 @@ tasks:
internal: true
cmds:
- go test -count 1 -v -run=TestXTykGateway_Lint ./apidef/oas/

setup:
desc: "Setup the project including dependencies and git hooks using lefthooks"
cmds:
- task: deps:default
- lefthook install
8 changes: 8 additions & 0 deletions lefthook.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
pre-commit:
commands:
task:
run: task hooks:pre-commit
pre-push:
commands:
task:
run: task hooks:pre-push

0 comments on commit 17b3910

Please sign in to comment.