diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d88de92..0e07ea5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -4,14 +4,11 @@ on: branches: - main -# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages permissions: contents: read pages: write id-token: write -# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued. -# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete. concurrency: group: "pages" cancel-in-progress: false @@ -41,6 +38,7 @@ jobs: path: 'site' deploy: + if: github.event.repository.fork == false environment: name: github-pages url: ${{ steps.deployment.outputs.page_url }} diff --git a/jsweb/middleware.py b/jsweb/middleware.py index ed91bbe..7aa14c8 100644 --- a/jsweb/middleware.py +++ b/jsweb/middleware.py @@ -175,8 +175,24 @@ async def __call__(self, scope, receive, send): from .database import db_session try: - await self.app(scope, receive, send) - db_session.commit() + status_code = None + + async def send_wrapper(message): + nonlocal status_code + if message["type"] == "http.response.start": + status_code = message["status"] + await send(message ) + + await self.app(scope, receive, send_wrapper) + + # Commit only if the response status code is a success (2xx) + # If status_code is None, it means no response was sent, which is an error state + # or a successful response that didn't send headers yet (unlikely in a standard flow). + # It's safer to rollback if status_code is not set or is not 2xx. + if status_code is not None and 200 <= status_code < 300: + db_session.commit() + else: + db_session.rollback() except Exception: db_session.rollback() raise