Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add CI and pre-commit check for formatting and linting #116

Merged
merged 5 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions .github/actions/setup-poetry-env/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: "setup-poetry-env"
description: "Composite action to setup the Python and poetry environment."

inputs:
python-version:
required: false
description: "The python version to use"
default: "3.11"

runs:
using: "composite"
steps:
- name: Set up python
uses: actions/setup-python@v4
with:
python-version: ${{ inputs.python-version }}

- name: Install Poetry
uses: snok/install-poetry@v1
with:
virtualenvs-in-project: true

- name: Load cached venv
id: cached-poetry-dependencies
uses: actions/cache@v3
with:
path: .venv
key: venv-${{ runner.os }}-${{ inputs.python-version }}-${{ hashFiles('poetry.lock') }}

- name: Install dependencies
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
run: poetry install --no-interaction
shell: bash
26 changes: 26 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Main

on:
push:
branches:
- "*"
pull_request:
types: [opened, synchronize, reopened, ready_for_review]

jobs:
quality:
runs-on: ubuntu-latest
steps:
- name: Check out
uses: actions/checkout@v3

- uses: actions/cache@v3
with:
path: ~/.cache/pre-commit
key: pre-commit-${{ hashFiles('.pre-commit-config.yaml') }}

- name: Set up the environment
uses: ./.github/actions/setup-poetry-env

- name: Run checks
run: make check
23 changes: 23 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
exclude: ^gen/
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: "v4.4.0"
hooks:
- id: check-case-conflict
- id: check-merge-conflict
- id: check-toml
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: "v0.1.6"
hooks:
- id: ruff
args: [--exit-non-zero-on-fix]
- id: ruff-format

- repo: https://github.com/pre-commit/mirrors-prettier
rev: "v3.0.3"
hooks:
- id: prettier
18 changes: 13 additions & 5 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ cd Amibot-tg

### Installing dependencies

1. Install Python dependencies:
1. Install Python dependencies, pre-commit hooks and setup the virtual environment:

```shell
poetry install
make install
```

2. Install Nodemon
Expand Down Expand Up @@ -99,6 +99,16 @@ If you want to automatically restart the bot whenever you make changes to the co
make dev
```

## Linting/Formatting

Always make sure to check the quality of code before committing/pushing i.e making sure that the codebase is properly formatted and linted.

Use the following command to Lint and format.

```shell
make check
```

## Go Amizone

This project uses [go-amizone](https://github.com/ditsuke/go-amizone) via gRPC to get its data. While knowledge of gRPC isn't a requirement, you can test how we get data through this [Postman collection](postman.com/ditsuke/workspace/ditsuke/)
Expand All @@ -120,11 +130,9 @@ For testing CI/CD, we use [act](https://github.com/nektos/act). Act is a tool th
```shell
brew install act
```

Further, you can run the following command to test the CI/CD pipeline:

```shell
act -W .github/workflows/workflow-file-name.yml
```



44 changes: 24 additions & 20 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
gen:
rm -f gen/*.py; cd proto; buf generate; cd ...

mongo:
docker run -d --name py-mongo -p 27017:27017 mongo

dev:
poetry run nodemon --no-colors --exec python main.py
install: ## Install dependencies and pre-commit hooks + setup virtual environment.
@echo "🚀 Creating virtual environment using pyenv and poetry"
@poetry install
@poetry run pre-commit install
@poetry shell

env:
poetry shell

docker:
docker build -t py-amibot .
gen: ## Generate proto buffs.
rm -f gen/*.py; cd proto; buf generate; cd ...

dockerRun:
docker run -d --name py-amibot -p 3333:3333 py-amibot
mongo: ## Run mongodb server in docker.
docker run -d --name py-mongo -p 27017:27017 mongo

lint:
poetry run ruff check **/*.py
dev: ## Run amibot server
poetry run nodemon --no-colors --exec python main.py

format:
poetry run ruff format **/*.py
docker: ## Builds the docker image for amibot server.
@docker build -t py-amibot .

.PHONY: gen, mongo, dev, env, docker, dockerRun
dockerRun: ## Runs the amibot server in docker.
@docker run -d --name py-amibot -p 3333:3333 py-amibot

check: ## Run code quality tools.
@echo "🚀 Checking Poetry lock file consistency with 'pyproject.toml': Running poetry lock --check"
@poetry check --lock
@echo "🚀 Linting code: Running pre-commit"
@poetry run pre-commit run -a

help: ## List available targets with respective description.
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}'

.PHONY: gen, mongo, dev, env, docker, dockerRun, install, check, help
.DEFAULT_GOAL := help
Loading
Loading