Skip to content
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
35 changes: 35 additions & 0 deletions .github/workflows/black-autocommit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Black Auto-Commit

on:
pull_request: # only run for PRs (e.g., branch "test" -> "main")

permissions:
contents: write # allow pushing commits to the branch that opened the PR

jobs:
black:
runs-on: ubuntu-latest
steps:
- name: Check out PR branch
uses: actions/checkout@v4
with:
# When triggered by pull_request, checkout@v4 checks out the PR HEAD.
fetch-depth: 0 # needed to push
persist-credentials: true # lets the action use GITHUB_TOKEN to push

- name: Run Black (format in-place)
id: black
uses: rickstaa/action-black@v1
with:
# no --check: we WANT it to rewrite files
black_args: "."

- name: Commit & push if files were reformatted
if: steps.black.outputs.is_formatted == 'true'
uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: ":art: Apply Black formatting"
# By default it commits to the currently checked out branch (the PR branch).
# Optionally restrict to Python paths:
# file_pattern: |
# **/*.py
11 changes: 11 additions & 0 deletions api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,43 @@

app = FastAPI(title="Example Items API", version="1.0.0")


class ItemBase(BaseModel):
name: str = Field(..., min_length=1, max_length=100)
description: Optional[str] = None
price: float = Field(..., gt=0)
in_stock: bool = True


class ItemCreate(ItemBase):
pass


class Item(ItemBase):
id: str


items_db: dict[str, Item] = {}


@app.get("/")
def root():
return {"message": "Welcome to the Items API"}


@app.get("/health")
def health_check():
return {"status": "ok"}


@app.post("/items", response_model=Item, status_code=201)
def create_item(item: ItemCreate):
item_id = str(uuid4())
new_item = Item(id=item_id, **item.dict())
items_db[item_id] = new_item
return new_item


@app.get("/items", response_model=List[Item])
def list_items(
min_price: Optional[float] = Query(None, gt=0),
Expand All @@ -46,12 +54,14 @@ def list_items(
results = [i for i in results if i.in_stock == in_stock]
return results


@app.get("/items/{item_id}", response_model=Item)
def get_item(item_id: str):
if item_id not in items_db:
raise HTTPException(status_code=404, detail="Item not found")
return items_db[item_id]


@app.put("/items/{item_id}", response_model=Item)
def update_item(item_id: str, updated_item: ItemCreate):
if item_id not in items_db:
Expand All @@ -61,6 +71,7 @@ def update_item(item_id: str, updated_item: ItemCreate):
items_db[item_id] = item
return item


@app.delete("/items/{item_id}", status_code=204)
def delete_item(item_id: str):
if item_id not in items_db:
Expand Down