Compare commits

..

No commits in common. "e64315f68d5d4ad8040643035068dd494340655f" and "c34e533c81e8b669bffadf4041dee6155b266809" have entirely different histories.

5 changed files with 22 additions and 69 deletions

View file

@ -71,10 +71,11 @@ async def lookup_session(
sess = result.scalar_one_or_none()
if sess is None:
return None
# sess.user is eagerly loaded via selectinload and the sessionmaker uses
# expire_on_commit=False, so it stays accessible without a refresh. last_seen_at
# is intentionally not updated here to keep auth reads free of writes; it is
# reserved for a future sliding-window expiry feature.
# Track activity whenever a valid cookie is used, then refresh the related
# user so route handlers can safely access sess.user.
sess.last_seen_at = now
await db.commit()
await db.refresh(sess, attribute_names=["user"])
return sess

View file

@ -32,27 +32,10 @@ class Repo(BaseModel):
class Commit(BaseModel):
"""A commit, normalized across Forgejo's two payload shapes.
``/branches`` nests a trimmed commit keyed ``id``/``timestamp``/``url``,
while ``/commits`` returns a flat GitHub-style object keyed
``sha``/``commit.message``/``html_url``/``created``. Each field therefore
accepts either shape.
"""
id: str = Field(validation_alias=AliasChoices("sha", "id"))
message: str = Field(
validation_alias=AliasChoices("message", AliasPath("commit", "message"))
)
url: str = Field(validation_alias=AliasChoices("html_url", "url"))
created_at: datetime = Field(
validation_alias=AliasChoices(
"timestamp",
"created_at",
"created",
AliasPath("commit", "committer", "date"),
)
)
message: str
url: str
created_at: datetime = Field(validation_alias="timestamp")
class Branch(BaseModel):
@ -81,8 +64,8 @@ async def get_branches(
owner: str,
repo: str,
*,
limit: int = 10,
page: int = 1,
limit=10,
page=1,
) -> list[Branch]:
"""Get the branches of a repository from the Forgejo API.
Args:
@ -111,7 +94,7 @@ async def get_commits(
owner: str,
repo: str,
*,
ref: str,
branch: str,
limit: int = 10,
page: int = 1,
) -> list[Commit]:
@ -121,7 +104,7 @@ async def get_commits(
access_token: The Forgejo API token.
owner: The owner of the repository.
repo: The name of the repository.
ref: The branch name, commit sha, or tag to list commits for.
branch: The branch to list commits for.
limit: The maximum number of commits to return.
page: The page number to return.
@ -130,12 +113,13 @@ async def get_commits(
"""
commits = await get(
app_settings,
f"/api/v1/repos/{owner}/{repo}/commits?sha={ref}&limit={limit}&page={page}",
f"/api/v1/repos/{owner}/{repo}/commits?branch={branch}&limit={limit}&page={page}",
access_token,
)
if not commits:
return []
return [Commit.model_validate(c) for c in commits]
return commits
# return [Commit.model_validate(c) for c in commits]
async def post(app_settings: AppSettings, path: str, data: dict) -> dict:

View file

@ -1,7 +1,7 @@
from datetime import datetime
from typing import Annotated, Literal
from fastapi import APIRouter, Depends, HTTPException, Path, Query, status
from fastapi import APIRouter, Depends, HTTPException, Path, status
from pydantic import BaseModel, EmailStr
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
@ -94,32 +94,3 @@ async def get_repository_branches(
app_settings, access_token, owner, repo, limit=limit, page=page
)
return branches
@router.get("/repositories/{repo}/commits")
async def get_repository_commits(
app_settings: Annotated[AppSettings, Depends(get_app_settings)],
sess: Annotated[UserBrowserSession, Depends(get_current_session)],
access_token: Annotated[str, Depends(get_forgejo_token)],
db: Annotated[AsyncSession, Depends(get_db)],
repo: Annotated[str, Path(min_length=1, description="The name of the repository")],
ref: Annotated[str, Query(description="Branch name, commit sha, or tag")] = "main",
limit: int = 10,
page: int = 1,
):
# Derive {owner} path param from users linked identity provider server-side
stmt = select(LinkedIdentity).where(
LinkedIdentity.user_id == sess.user_id, LinkedIdentity.provider == "forgejo"
)
result = await db.execute(stmt)
identity = result.scalar_one_or_none()
if identity is None:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="No linked identity for forgejo was found. Please sign-in and try again.",
)
owner = identity.provider_username
commits = await forgejo.get_commits(
app_settings, access_token, owner, repo, ref=ref, limit=limit, page=page
)
return commits

View file

@ -61,11 +61,9 @@ async def setup_page(
"reconfiguring": existing is not None and has_users,
"forgejo_base_url": existing.forgejo_base_url if existing else "",
"forgejo_oauth_client_id": existing.forgejo_oauth_client_id if existing else "",
# Pre-fill with the request's own base URL so the field is never
# accidentally left blank: an empty value only works while the caller
# is browsing the same origin that Forgejo redirects back to, and any
# generated share link needs an externally resolvable address.
"firebreak_public_base_url": public_base,
"firebreak_public_base_url": existing.firebreak_public_base_url
if existing
else "",
"oauth_callback_url": f"{public_base}/auth/callback/forgejo",
}
return templates.TemplateResponse(request, "setup.html", ctx)

View file

@ -112,17 +112,16 @@
<label for="firebreak_public_base_url"
class="block text-xs font-semibold uppercase tracking-[0.05em] text-[#f7f2e8]/50 mb-1.5">
Public Base URL
<span class="ml-1 normal-case tracking-normal font-normal text-[#f7f2e8]/30">(optional)</span>
</label>
<input type="url"
id="firebreak_public_base_url"
name="firebreak_public_base_url"
class="block w-full rounded-md border border-[#f7f2e8]/14 bg-[#1f2226] px-3 py-2.5 text-sm text-[#f7f2e8] placeholder:text-[#f7f2e8]/30 focus:border-[#f7f2e8]/30 focus:outline-none focus:ring-2 focus:ring-[#f7f2e8]/10"
required
placeholder="https://share.example.com"
value="{{ firebreak_public_base_url }}" />
<p class="mt-1.5 text-xs text-[#f7f2e8]/30">
The address Forgejo redirects back to after sign-in, and the origin
share links point at. Pre-filled from how you reached this page
&mdash; change it to your real domain once Firebreak is deployed.
Your production domain for OAuth redirects. Leave blank on localhost &mdash; Firebreak will use <code class="font-mono text-[#f7f2e8]/40">http://localhost:8000</code> automatically.
</p>
</div>
<button type="submit" class="btn btn-primary w-full mt-2">