Skip to content

refactor: use named Permission field in Repository struct instead of anonymous embedding#37441

Merged
bircni merged 5 commits intomainfrom
copilot/refactor-repository-permission-field
Apr 26, 2026
Merged

refactor: use named Permission field in Repository struct instead of anonymous embedding#37441
bircni merged 5 commits intomainfrom
copilot/refactor-repository-permission-field

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 26, 2026

The Repository struct in services/context/repo.go embedded access_model.Permission anonymously, causing all permission methods to be promoted directly onto Repository. This made it unclear at call sites whether a method belonged to Repository itself or to its embedded Permission.

Changes

  • services/context/repo.go: Replace anonymous access_model.Permission with named field Permission access_model.Permission
  • 49 files updated to route permission method calls through the named field:
// Before
ctx.Repo.IsAdmin()
ctx.Repo.CanWrite(unit.TypeCode)
ctx.Repo.CanReadIssuesOrPulls(isPull)
slices.ContainsFunc(unitTypes, ctx.Repo.CanWrite)

// After
ctx.Repo.Permission.IsAdmin()
ctx.Repo.Permission.CanWrite(unit.TypeCode)
ctx.Repo.Permission.CanReadIssuesOrPulls(isPull)
slices.ContainsFunc(unitTypes, ctx.Repo.Permission.CanWrite)

Methods defined directly on *Repository (CanWriteToBranch, CanCreateBranch, etc.) are unchanged.

…anonymous embedding

Change the anonymous `access_model.Permission` field to a named
`Permission access_model.Permission` field in the `Repository` struct
in services/context/repo.go.

Update all callers that previously accessed the promoted methods directly
(e.g. `ctx.Repo.IsAdmin()`) to go through the named field
(e.g. `ctx.Repo.Permission.IsAdmin()`).

Also fix `CanWriteToBranch` which is a method on `*Repository` (not on
`Permission`) and must not be prefixed.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>

Agent-Logs-Url: https://github.com/go-gitea/gitea/sessions/204bbaf4-671f-4d10-adee-5ad1d4170a6d

Co-authored-by: wxiaoguang <2114189+wxiaoguang@users.noreply.github.com>
@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Apr 26, 2026
@wxiaoguang
Copy link
Copy Markdown
Contributor

wxiaoguang commented Apr 26, 2026

"Restrict permission check on repositories and fix some problems (#5314)" made the Permission field anonymous, then the code became difficult to understand or maintain.

This PR introduces a proper name for the field.

ctx.Data["Repo"] = ctx.Repo is also not right, it is useless after #13246, just copy&paste result of PRs like "Correctly handle failed migrations #17575"

@wxiaoguang wxiaoguang marked this pull request as ready for review April 26, 2026 17:46
@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Apr 26, 2026
@GiteaBot GiteaBot added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Apr 26, 2026
@lunny lunny added the type/refactoring Existing code has been cleaned up. There should be no new functionality. label Apr 26, 2026
@bircni bircni enabled auto-merge (squash) April 26, 2026 19:15
@lunny lunny added this to the 1.27.0 milestone Apr 26, 2026
@wxiaoguang wxiaoguang added the reviewed/wait-merge This pull request is part of the merge queue. It will be merged soon. label Apr 26, 2026
@bircni bircni merged commit 45b4fff into main Apr 26, 2026
30 checks passed
@bircni bircni deleted the copilot/refactor-repository-permission-field branch April 26, 2026 20:18
@GiteaBot GiteaBot removed the reviewed/wait-merge This pull request is part of the merge queue. It will be merged soon. label Apr 26, 2026
zjjhot added a commit to zjjhot/gitea that referenced this pull request Apr 27, 2026
* main: (33 commits)
  refactor: use named `Permission` field in `Repository` struct instead of anonymous embedding (go-gitea#37441)
  Refactor pull request view (3) (go-gitea#37439)
  Update 1.26.1 changelog in main (go-gitea#37442)
  Make GetPossibleUserByID can handle deleted user (go-gitea#37430)
  Fix fetch action redirect (go-gitea#37437)
  Refactor integration test DecodeJSON calls to use generic return value (go-gitea#37432)
  Integrate renovate bot for all dependency updates (go-gitea#37050)
  Refactor pull request view (2) (go-gitea#37428)
  Use MarkLongPolling instead of hard-coded route path (go-gitea#37427)
  Optimize CI caches (go-gitea#37387)
  Update AGENTS.md (go-gitea#37420)
  Update Nix flake (go-gitea#37425)
  [skip ci] Updated translations via Crowdin
  remove excessive quote from terraform instructions (go-gitea#37424)
  Improve testing init, clean up webhook tests (go-gitea#37412)
  Fix color regressions, add `priority` color (go-gitea#37417)
  [skip ci] Updated translations via Crowdin
  Stabilize e2e logout propagation test (go-gitea#37403)
  refactor: serve site manifest via `/assets/site-manifest.json` endpoint (go-gitea#37405)
  feat(security): set X-Content-Type-Options: nosniff by default (go-gitea#37354)
  ...
silverwind added a commit to hanism01/gitea that referenced this pull request Apr 27, 2026
…-review-feedback

* origin/main: (144 commits)
  Add API endpoint to reply to pull request review comments (go-gitea#36683)
  Add CurrentURL template variable back (go-gitea#37444)
  refactor: use named `Permission` field in `Repository` struct instead of anonymous embedding (go-gitea#37441)
  Refactor pull request view (3) (go-gitea#37439)
  Update 1.26.1 changelog in main (go-gitea#37442)
  Make GetPossibleUserByID can handle deleted user (go-gitea#37430)
  Fix fetch action redirect (go-gitea#37437)
  Refactor integration test DecodeJSON calls to use generic return value (go-gitea#37432)
  Integrate renovate bot for all dependency updates (go-gitea#37050)
  Refactor pull request view (2) (go-gitea#37428)
  Use MarkLongPolling instead of hard-coded route path (go-gitea#37427)
  Optimize CI caches (go-gitea#37387)
  Update AGENTS.md (go-gitea#37420)
  Update Nix flake (go-gitea#37425)
  [skip ci] Updated translations via Crowdin
  remove excessive quote from terraform instructions (go-gitea#37424)
  Improve testing init, clean up webhook tests (go-gitea#37412)
  Fix color regressions, add `priority` color (go-gitea#37417)
  [skip ci] Updated translations via Crowdin
  Stabilize e2e logout propagation test (go-gitea#37403)
  ...

# Conflicts:
#	models/project/column.go
#	routers/web/repo/issue_page_meta.go
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. type/refactoring Existing code has been cleaned up. There should be no new functionality.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants