Skip to content
This repository has been archived by the owner on Jul 25, 2024. It is now read-only.

Commit

Permalink
Opensource hook (#3)
Browse files Browse the repository at this point in the history
Co-authored-by: Joe Kaufeld <opensource@joekaufeld.com>
  • Loading branch information
MaxymVlasov and itsthejoker committed Feb 28, 2023
1 parent 2955390 commit f62451b
Show file tree
Hide file tree
Showing 18 changed files with 1,707 additions and 2 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
root = true

[*]
indent_style = space
indent_size = 2
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true


[{*.{py,md},Dockerfile}]
indent_size = 4

[*.md]
trim_trailing_whitespace = false
4 changes: 4 additions & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Use this file to define individuals or teams that are responsible for code in a repository.
# Read more: <https://help.github.com/articles/about-codeowners/>

* @MaxymVlasov @SpotOnInc/open-source
67 changes: 67 additions & 0 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Notes for contributors

* [Run and debug hooks locally](#run-and-debug-hooks-locally)
* [Add new hook](#add-new-hook)
* [Before writing code](#before-writing-code)
* [Prepare basic documentation](#prepare-basic-documentation)
* [Add code](#add-code)
* [Finish with the documentation](#finish-with-the-documentation)

## Run and debug hooks locally

```bash
pre-commit try-repo {-a} /path/to/local/pre-commit-terraform/repo {hook_name}
```

I.e.

```bash
pre-commit try-repo /mnt/c/Users/tf/pre-commit-terraform terraform_fmt # Run only `terraform_fmt` check
pre-commit try-repo -a ~/pre-commit-terraform # run all existing checks from repo
```

Running `pre-commit` with `try-repo` ignores all arguments specified in `.pre-commit-config.yaml`.

If you need to test hook with arguments, follow [pre-commit doc](https://pre-commit.com/#arguments-pattern-in-hooks) to test hooks.

For example, to test that the [`terraform_fmt`](../README.md#terraform_fmt) hook works fine with arguments:

```bash
/tmp/pre-commit-yq/hooks/yq_yaml_prettier.sh --args="-r -i '( ... |select(type == \"!!seq\")) |= sort_by( select(tag == \"!!str\") // (keys | .[0]) )'" test-dir/foo.yaml test-dir/bar.yaml
```


## Add new hook

### Before writing code

1. Try to figure out future hook usage.
2. Confirm the concept with SpotOn OSS team.

### Prepare basic documentation

1. Identify and describe dependencies in [Install dependencies](../README.md#1-install-dependencies)

### Add code

1. Add new hook to [`.pre-commit-hooks.yaml`](../.pre-commit-hooks.yaml)
2. Create hook file. Don't forget to make it executable via `chmod +x /path/to/hook/file`.
3. Test hook. How to do it is described in [Run and debug hooks locally](#run-and-debug-hooks-locally) section.
4. Test hook one more time.
1. Push commit with hook file to GitHub
2. Grab SHA hash of the commit
3. Test hook using `.pre-commit-config.yaml`:

```yaml
repos:
- repo: https://github.com/SpotOnInc/pre-commit-yq # Your repo
rev: 3d76da3885e6a33d59527eff3a57d246dfb66620 # Your commit SHA
hooks:
- id: new_hook # New hook name
args:
- --args=--config=.docs.yml # Some args that you'd like to test
```
### Finish with the documentation
Create and populate a new hook section in [README](../README.md) which will include the hook description and usage examples.
58 changes: 58 additions & 0 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
---
name: Bug report
about: Create a bug report
labels:
- bug
---

<!--
Thank you for helping to improve pre-commit-yq!
Please be sure to search for open issues before raising a new one. We use issues
for bug reports and feature requests. Please note, this template is for bugs
report, not feature requests.
-->

### Describe the bug

<!--
Please let us know what behavior you expected and how pre-commit-yq diverged
from that behavior.
-->


### How can we reproduce it?

<!--
Help us to reproduce your bug as succinctly and precisely as possible. Any and
all steps or script that triggers the issue are highly appreciated!
Do you have long logs to share? Please use collapsible sections, that can be created via:
<details><summary>SECTION_NAME</summary>
```bash
YOUR_LOG_HERE
```
</details>
-->


### Environment information


```bash
Insert here output of next command:

echo -e "
bash:
$(bash --version)
\n\npatch:
$(patch --version)
\n\nyq:
$(yq --version)
\n\nyq docker:
$(docker images mikefarah/yq)
"
```
29 changes: 29 additions & 0 deletions .github/ISSUE_TEMPLATE/feature_request.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
name: Feature request
about: Suggest an idea for this project
labels:
- feature
---

<!--
Thank you for helping to improve pre-commit-yq!
Please be sure to search for open issues before raising a new one. We use issues
for bug reports and feature requests. Please note, this template is for feature
requests, not bugs report.
-->

### What problem are you facing?

<!--
Please tell us a little about your use case - it's okay if it's hypothetical!
Leading with this context helps frame the feature request so we can ensure we
implement it sensibly.
--->


### How could pre-commit-yq help solve your problem?

<!--
Let us know how you think pre-commit-yq could help with your use case.
-->
41 changes: 41 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<!--
Thank you for helping to improve pre-commit-yq!
-->

Put an `x` into the box if that applies:

- [ ] This PR introduces breaking change.
- [ ] This PR fixes a bug.
- [ ] This PR adds new functionality.
- [ ] This PR enhances existing functionality.

### Description of your changes

<!--
Briefly describe what this pull request does. Be sure to direct your reviewers'
attention to anything that needs special consideration.
We love Pull Requests that resolve an open pre-commit-yq issue. If yours does, you
can uncomment the below line to indicate which issue/Jira ticket your PR fixes, for example
"Fixes #123456":
-->

<!-- Fixes # -->
<!-- Ticket: -->

### How was it tested

<!--
Before reviewers can be confident in the correctness of this pull request,
it needs to be tested and shown to be correct. Briefly describe the testing
that has already been done or which is planned for this change.
-->



<!--
Last minute questions to consider, please make sure to note the below:
- Does this change require an update to any other applications or third-party libraries?
- Is this change blocked by anything else?
- Does this change require actions outside this PR? For example, updating secrets or keys?
-->
55 changes: 55 additions & 0 deletions .github/workflows/pr-title-validation.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: "Validate PR title"

on:
pull_request_target:
types:
- opened
- edited
- synchronize

jobs:
main:
name: Validate PR title
runs-on: ubuntu-latest
steps:
# Please look up the latest version from
# https://github.com/amannn/action-semantic-pull-request/releases
- uses: amannn/action-semantic-pull-request@b6bca70dcd3e56e896605356ce09b76f7e1e0d39 # v5.1.0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
# Configure which types are allowed.
# Default: https://github.com/commitizen/conventional-commit-types
types: |
fix
feat
docs
ci
chore
# Configure that a scope must always be provided.
requireScope: false
# Configure additional validation for the subject based on a regex.
# This example ensures the subject starts with an uppercase character.
subjectPattern: ^[A-Z].+$
# Ignore Renovatebot PRs
ignoreLabels: |
auto-update
# If `subjectPattern` is configured, you can use this property to override
# the default error message that is shown when the pattern doesn't match.
# The variables `subject` and `title` can be used within the message.
subjectPatternError: |
The subject "{subject}" found in the pull request title "{title}"
didn't match the configured pattern. Please ensure that the subject
starts with an uppercase character.
# For work-in-progress PRs you can typically use draft pull requests
# from Github. However, private repositories on the free plan don't have
# this option and therefore this action allows you to opt-in to using the
# special "[WIP]" prefix to indicate this state. This will avoid the
# validation of the PR title and the pull request checks remain pending.
# Note that a second check will be reported if this is enabled.
wip: true
# When using "Squash and merge" on a PR with only one commit, GitHub
# will suggest using that commit message instead of the PR title for the
# merge commit, and it's easy to commit this by mistake. Enable this option
# to also validate the commit message for one commit PRs.
validateSingleCommit: false
60 changes: 60 additions & 0 deletions .github/workflows/pre-commit.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: Common issues check

on: [pull_request]

jobs:
pre-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@ac593985615ec2ede58e132d2e21d2b1cbd6127c # v3.3.0
- run: |
git fetch --no-tags --prune --depth=1 origin +refs/heads/*:refs/remotes/origin/*
- name: Get changed files
id: file_changes
run: |
export DIFF=$(git diff --name-only origin/${{ github.base_ref }} ${{ github.sha }})
echo "Diff between ${{ github.base_ref }} and ${{ github.sha }}"
echo "files=$( echo "$DIFF" | xargs echo )" >> $GITHUB_OUTPUT
- name: Install shfmt
run: |
curl -L "$(curl -s https://github.com/gitapi/repos/mvdan/sh/releases/latest | grep -o -E -m 1 "https://.+?linux_amd64")" > shfmt \
&& chmod +x shfmt && sudo mv shfmt /usr/bin/
- name: Install shellcheck
run: |
sudo apt update && sudo apt install shellcheck
- uses: actions/setup-python@d27e3f3d7c64b4bbf8e4abfb9b63b83e846e0435 # v4.5.0
with:
python-version: '3.9'
# Need to success pre-commit fix push
- uses: actions/checkout@ac593985615ec2ede58e132d2e21d2b1cbd6127c # v3.3.0
with:
fetch-depth: 0
ref: ${{ github.event.pull_request.head.ref }}
# Need to trigger pre-commit workflow on autofix commit
# Guide: https://web.archive.org/web/20210731173012/https://github.community/t/required-check-is-expected-after-automated-push/187545/
ssh-key: "${{ secrets.GHA_AUTOFIX_COMMIT_KEY }}"

- name: Execute pre-commit
uses: pre-commit/action@646c83fcd040023954eafda54b4db0192ce70507 # v3.0.0
env:
SKIP: no-commit-to-branch
with:
extra_args: --color=always --show-diff-on-failure --files ${{ steps.file_changes.outputs.files }}
# Need to trigger pre-commit workflow on autofix commit.
# Native pre-commit action fix by `token` not works in this case.
- name: Push fixes
if: failure()
uses: EndBug/add-and-commit@61a88be553afe4206585b31aa72387c64295d08b # v9.1.1
with:
# Determines the way the action fills missing author name and email. Three options are available:
# - github_actor -> UserName <UserName@users.noreply.github.com>
# - user_info -> Your Display Name <your-actual@email.com>
# - github_actions -> github-actions <email associated with the github logo>
# Default: github_actor
default_author: github_actor
# The message for the commit.
# Default: 'Commit from GitHub Actions (name of the workflow)'
message: '[pre-commit] Autofix violations'
27 changes: 27 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Release

on:
workflow_dispatch:
repository_dispatch:
types: [trigger-release]

jobs:
release:
name: Release
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@ac593985615ec2ede58e132d2e21d2b1cbd6127c # v3.3.0
with:
persist-credentials: false
fetch-depth: 0

- name: Release
uses: cycjimmy/semantic-release-action@8f6ceb9d5aae5578b1dcda6af00008235204e7fa # v3.2.0
with:
semantic_version: 18.0.0
extra_plugins: |
@semantic-release/changelog@6.0.0
@semantic-release/git@10.0.0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
31 changes: 31 additions & 0 deletions .github/workflows/trigger-release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Trigger Release if more than 30 days no releases

on:
schedule:
# Every Monday at 04:00
- cron: '0 4 * * 1'

jobs:
trigger-check:
name: Check is a new Release needed
runs-on: ubuntu-latest
steps:
- name: Gets latest created release info
id: latest_release_info
uses: jossef/action-latest-release-info@v1.2.1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- id: calculation
run: |
RELEASE_TIME="$(date +%s -u -d ${{ steps.latest_release_info.outputs.created_at }})"
TIME_NOW="$(date +%s -u)"
DAYS_AFTER_RELEASE="$(date +%-j -d @$(( $TIME_NOW - $RELEASE_TIME )))"
echo "days_after_release=$DAYS_AFTER_RELEASE" >> $GITHUB_OUTPUT
- name: Repository Dispatch
if: ${{ steps.calculation.outputs.days_after_release > 30 }}
uses: peter-evans/repository-dispatch@26b39ed245ab8f31526069329e112ab2fb224588 # v2.1.1
with:
event-type: trigger-release
Loading

0 comments on commit f62451b

Please sign in to comment.