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

View file

@ -32,27 +32,10 @@ class Repo(BaseModel):
class Commit(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")) id: str = Field(validation_alias=AliasChoices("sha", "id"))
message: str = Field( message: str
validation_alias=AliasChoices("message", AliasPath("commit", "message")) url: str
) created_at: datetime = Field(validation_alias="timestamp")
url: str = Field(validation_alias=AliasChoices("html_url", "url"))
created_at: datetime = Field(
validation_alias=AliasChoices(
"timestamp",
"created_at",
"created",
AliasPath("commit", "committer", "date"),
)
)
class Branch(BaseModel): class Branch(BaseModel):
@ -81,8 +64,8 @@ async def get_branches(
owner: str, owner: str,
repo: str, repo: str,
*, *,
limit: int = 10, limit=10,
page: int = 1, page=1,
) -> list[Branch]: ) -> list[Branch]:
"""Get the branches of a repository from the Forgejo API. """Get the branches of a repository from the Forgejo API.
Args: Args:
@ -111,7 +94,7 @@ async def get_commits(
owner: str, owner: str,
repo: str, repo: str,
*, *,
ref: str, branch: str,
limit: int = 10, limit: int = 10,
page: int = 1, page: int = 1,
) -> list[Commit]: ) -> list[Commit]:
@ -121,7 +104,7 @@ async def get_commits(
access_token: The Forgejo API token. access_token: The Forgejo API token.
owner: The owner of the repository. owner: The owner of the repository.
repo: The name 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. limit: The maximum number of commits to return.
page: The page number to return. page: The page number to return.
@ -130,12 +113,13 @@ async def get_commits(
""" """
commits = await get( commits = await get(
app_settings, 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, access_token,
) )
if not commits: if not commits:
return [] 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: async def post(app_settings: AppSettings, path: str, data: dict) -> dict:

View file

@ -1,7 +1,7 @@
from datetime import datetime from datetime import datetime
from typing import Annotated, Literal 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 pydantic import BaseModel, EmailStr
from sqlalchemy import select from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession 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 app_settings, access_token, owner, repo, limit=limit, page=page
) )
return branches 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, "reconfiguring": existing is not None and has_users,
"forgejo_base_url": existing.forgejo_base_url if existing else "", "forgejo_base_url": existing.forgejo_base_url if existing else "",
"forgejo_oauth_client_id": existing.forgejo_oauth_client_id 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 "firebreak_public_base_url": existing.firebreak_public_base_url
# accidentally left blank: an empty value only works while the caller if existing
# is browsing the same origin that Forgejo redirects back to, and any else "",
# generated share link needs an externally resolvable address.
"firebreak_public_base_url": public_base,
"oauth_callback_url": f"{public_base}/auth/callback/forgejo", "oauth_callback_url": f"{public_base}/auth/callback/forgejo",
} }
return templates.TemplateResponse(request, "setup.html", ctx) return templates.TemplateResponse(request, "setup.html", ctx)

View file

@ -112,17 +112,16 @@
<label for="firebreak_public_base_url" <label for="firebreak_public_base_url"
class="block text-xs font-semibold uppercase tracking-[0.05em] text-[#f7f2e8]/50 mb-1.5"> class="block text-xs font-semibold uppercase tracking-[0.05em] text-[#f7f2e8]/50 mb-1.5">
Public Base URL Public Base URL
<span class="ml-1 normal-case tracking-normal font-normal text-[#f7f2e8]/30">(optional)</span>
</label> </label>
<input type="url" <input type="url"
id="firebreak_public_base_url" id="firebreak_public_base_url"
name="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" 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 }}" /> value="{{ firebreak_public_base_url }}" />
<p class="mt-1.5 text-xs text-[#f7f2e8]/30"> <p class="mt-1.5 text-xs text-[#f7f2e8]/30">
The address Forgejo redirects back to after sign-in, and the origin 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.
share links point at. Pre-filled from how you reached this page
&mdash; change it to your real domain once Firebreak is deployed.
</p> </p>
</div> </div>
<button type="submit" class="btn btn-primary w-full mt-2"> <button type="submit" class="btn btn-primary w-full mt-2">