Skip to content

Commit

Permalink
## 🚀 Pythonプロジェクトのセットアップとコード品質向上の改善 (#39)
Browse files Browse the repository at this point in the history
* Refactor workflow and scripts, improve configuration (#37)

* Refactor workflow and scripts, improve configuration

* Refactor pyproject.toml and scripts, improve configuration

* Add installation of "ruff" package using Homebrew

* Refactor README.ja.md and improve setup instructions

* Refactor README.ja.md and add "ruff" package to dependencies

* Add "ripgrep" to cspell.json

* Add ruff pre-commit hooks for Python linting and formatting

* Add Python version setup to pre-commit workflow
  • Loading branch information
tqer39 authored Oct 27, 2024
1 parent 056962d commit 764f467
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 43 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/pre-commit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ jobs:
with:
ref: ${{ github.head_ref }}

- name: Read Python version from .python-version
id: read_python_version
run: |
PYTHON_VERSION=$(cat .python-version)
echo "PYTHON_VERSION=$PYTHON_VERSION" >> $GITHUB_ENV
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: ${{ env.PYTHON_VERSION }}

- name: pre-commit
uses: pre-commit/[email protected]
with:
Expand Down
29 changes: 29 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,32 @@ repos:
hooks:
- id: renovate-config-validator
files: renovate\.json

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.6
hooks:
# Run the linter.
- id: ruff
name: ruff
description: "Run 'ruff' for extremely fast Python linting"
entry: ruff check --force-exclude
language: python
language_version: 3.12.1
types_or: [python, pyi]
# --fix: enable lint fixes
args: [--fix]
require_serial: true
additional_dependencies: []
minimum_pre_commit_version: "2.9.2"
# Run the formatter.
- id: ruff-format
name: ruff-format
description: "Run 'ruff' for extremely fast Python formatting"
entry: ruff format --force-exclude
language: python
language_version: 3.12.1
types_or: [python, pyi]
args: []
require_serial: true
additional_dependencies: []
minimum_pre_commit_version: "2.9.2"
32 changes: 14 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,20 @@ curl -sL https://setup.tqer39.dev | bash

```mermaid
sequenceDiagram
participant User
participant Cloudflare
participant CloudDomains
participant GitHub
participant GCP
participant setup.tqer39.dev
participant bash
participant User
participant Cloudflare
participant CloudDomains
participant GitHub
participant setup.tqer39.dev
User->>Cloudflare: https://setup.tqer39.dev
Cloudflare->>Cloudflare: Add rules
Cloudflare->>setup.tqer39.dev: Redirect
setup.tqer39.dev->>GitHub: setup.sh
GitHub->>setup.tqer39.dev: setup.sh
setup.tqer39.dev->>bash: setup.sh
bash->>CloudDomains: Acquire
CloudDomains->>Cloudflare: Define
Cloudflare->>Cloudflare: SSL/TLS
Cloudflare->>Cloudflare: NS record
Cloudflare->>User: Complete
User->>Cloudflare: https://setup.tqer39.dev
Cloudflare->>Cloudflare: Add rule
Cloudflare->>setup.tqer39.dev: Redirect
setup.tqer39.dev->>GitHub: setup.sh
GitHub->>setup.tqer39.dev: setup.sh
setup.tqer39.dev->>User: setup.sh
User->>User: Install software
User->>User: Configure dotfiles
```

## Setup Contents
Expand All @@ -58,6 +53,7 @@ sequenceDiagram
- [jq](https://stedolan.github.io/jq/)
- [Rancher Desktop](https://rancherdesktop.io/)
- [ripgrep](https://github.com/BurntSushi/ripgrep)
- [ruff](https://github.com/astral-sh/ruff)
- [Starship](https://starship.rs/)
- [Visual Studio Code](https://code.visualstudio.com/)
- [zsh](https://www.zsh.org/)
Expand Down
1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"OPENAI",
"pcra",
"pyenv",
"ripgrep",
"rxvt",
"SAVEHIST",
"schelp",
Expand Down
33 changes: 14 additions & 19 deletions docs/README.ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,22 @@ curl -sL https://setup.tqer39.dev | bash

```mermaid
sequenceDiagram
participant User
participant Cloudflare
participant CloudDomains
participant GitHub
participant GCP
participant setup.tqer39.dev
participant bash
participant User
participant Cloudflare
participant CloudDomains
participant GitHub
participant setup.tqer39.dev
User->>Cloudflare: https://setup.tqer39.dev
Cloudflare->>Cloudflare: ルール追加
Cloudflare->>setup.tqer39.dev: リダイレクト
setup.tqer39.dev->>GitHub: setup.sh
GitHub->>setup.tqer39.dev: setup.sh
setup.tqer39.dev->>bash: setup.sh
bash->>CloudDomains: 取得
CloudDomains->>Cloudflare: 定義
Cloudflare->>Cloudflare: SSL/TLS
Cloudflare->>Cloudflare: NS レコード
Cloudflare->>User: 完了
User->>Cloudflare: https://setup.tqer39.dev
Cloudflare->>Cloudflare: ルール追加
Cloudflare->>setup.tqer39.dev: リダイレクト
setup.tqer39.dev->>GitHub: setup.sh
GitHub->>setup.tqer39.dev: setup.sh
setup.tqer39.dev->>User: setup.sh
User->>User: ソフトウェアのインストール
User->>User: dotfiles の設定
```


## セットアップ内容

### インストールされるソフトウェア
Expand All @@ -59,6 +53,7 @@ sequenceDiagram
- [jq](https://stedolan.github.io/jq/)
- [Rancher Desktop](https://rancherdesktop.io/)
- [ripgrep](https://github.com/BurntSushi/ripgrep)
- [ruff](https://github.com/astral-sh/ruff)
- [Starship](https://starship.rs/)
- [Visual Studio Code](https://code.visualstudio.com/)
- [zsh](https://www.zsh.org/)
Expand Down
5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ requires-python = ">=3.12"
dependencies = [
"openai==1.52.2",
]

[tool.ruff]
line-length = 88
select = ["E", "F", "W", "C"]
ignore = ["E501"] # 長い行を無視する場合
24 changes: 18 additions & 6 deletions scripts/generate_pr_description.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@

client = OpenAI(
# This is the default and can be omitted
api_key = os.getenv("OPENAI_API_KEY")
api_key=os.getenv("OPENAI_API_KEY")
)


# プロンプトの準備
def create_prompt(commit_logs):
return f"""
Expand Down Expand Up @@ -61,6 +62,7 @@ def create_prompt(commit_logs):
- 詳細な説明B
"""


# OpenAI API でのリクエスト
def generate_pr_description(commit_logs):
prompt = create_prompt(commit_logs)
Expand All @@ -77,26 +79,36 @@ def generate_pr_description(commit_logs):

return response.choices[0].message.content.strip()


# Git コミットログとファイルの差分の取得
def get_commit_logs_and_diffs():
# リモートの変更を取得
subprocess.run(['git', 'fetch', 'origin'], check=True)
subprocess.run(["git", "fetch", "origin"], check=True)

result = subprocess.run(['git', 'log', '--pretty=format:%H %s', 'origin/main..HEAD', '-n', '70'], capture_output=True, text=True) # コミットログの数を制限
commit_logs = result.stdout.strip().split('\n')
result = subprocess.run(
["git", "log", "--pretty=format:%H %s", "origin/main..HEAD", "-n", "70"],
capture_output=True,
text=True,
) # コミットログの数を制限
commit_logs = result.stdout.strip().split("\n")

if not commit_logs or commit_logs == ['']:
if not commit_logs or commit_logs == [""]:
return ""

logs_and_diffs = []
for commit in commit_logs:
commit_hash = commit.split()[0]
if commit_hash:
diff_result = subprocess.run(['git', 'diff', commit_hash + '^!', '--'], capture_output=True, text=True)
diff_result = subprocess.run(
["git", "diff", commit_hash + "^!", "--"],
capture_output=True,
text=True,
)
logs_and_diffs.append(f"Commit: {commit}\nDiff:\n{diff_result.stdout}")

return "\n\n".join(logs_and_diffs)


# メインロジック
if __name__ == "__main__":
commit_logs_and_diffs = get_commit_logs_and_diffs()
Expand Down
3 changes: 3 additions & 0 deletions setup
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,9 @@ install_if_missing "eza" "brew install eza"
# fd
install_if_missing "fd" "brew install fd"

# ruff
install_if_missing "ruff" "brew install ruff"

# ripgrep
if ! command -v rp &> /dev/null; then
echo "ripgrep がインストールされていません。インストールを試みます。"
Expand Down

0 comments on commit 764f467

Please sign in to comment.