Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: decouple context from migration structs #33399

Merged
merged 8 commits into from
Feb 7, 2025

Conversation

TheFox0x7
Copy link
Contributor


I've started working on pulling out contexts from structs per golang conventions and I took migrations on first.
It should be fine apart from gogs which I have no idea how to decouple - safe for updating/forking gogs client.

Ideas and initial feedback are very much welcome.

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Jan 25, 2025
@pull-request-size pull-request-size bot added the size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. label Jan 25, 2025
@github-actions github-actions bot added the modifies/go Pull requests that update Go code label Jan 25, 2025
@wxiaoguang
Copy link
Contributor

I've started working on pulling out contexts from structs per golang conventions

I think it depends on whether it really brings enough benefits. Golang itself (official packages, for example: http) also store the ctx into struct field in many cases .........

@TheFox0x7
Copy link
Contributor Author

According to the go's blog post the only reason why net/http has context in its struct is backwards compatibility:
https://go.dev/blog/context-and-structs
And it's not recommended to store it there.

@wxiaoguang
Copy link
Contributor

According to the go's blog post the only reason why net/http has context in its struct is backwards compatibility: https://go.dev/blog/context-and-structs And it's not recommended to store it there.

Yup, that's same to Gitea's code base. New code seldom stores context into a struct.

Some old code already stores the context into the struct, which is not good, while not too bad if it hasn't caused any problem (for example: markup render also does so). That's what I meant "I think it depends on whether the refactoring really brings enough benefits."


For "gogs downloader", the NewClient is quite lightweight, if you'd like to make the downloader support context, maybe it could re-create the client in every function.

@silverwind silverwind added the type/refactoring Existing code has been cleaned up. There should be no new functionality. label Jan 28, 2025
@wxiaoguang
Copy link
Contributor

As a refactoring PR, I think we'd better to make it move on fast (to avoid unnecessary conflicts)

If "gogs client ctx" is the last blocker, maybe I could help to try to figure out how to make it work (edit this PR) if you don't mind .......

@TheFox0x7
Copy link
Contributor Author

Sorry for the delay, I started jumping between various topics semi-related (like what kind of magic makes gitea's context for request handlers) and unrelated (checking if migrating swagger to oapi-codegen is doable) to this.

I figured that probably the best solution is to make the gogs library have context support so I went ahead and added it in my fork.

@TheFox0x7 TheFox0x7 marked this pull request as ready for review February 5, 2025 21:08
@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 Feb 5, 2025
@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 Feb 5, 2025
@wxiaoguang
Copy link
Contributor

I figured that probably the best solution is to make the gogs library have context support so I went ahead and added it in my fork.

Hmm, I prefer to keep using the official one (although it isn't active), in case the official one gets updated we might miss it.

The forked dependencies are difficult to maintain TBH. I think the gogs client ctx could be fixed by #33399 (comment)

@TheFox0x7
Copy link
Contributor Author

The change isn't that big that a rebase once a while can't deal with. Though I see your point.
Can you share a snippet of code with your idea for a fix then? I figured you meant just creating new client per function but I don't see how context comes into play there...

@wxiaoguang
Copy link
Contributor

wxiaoguang commented Feb 6, 2025

-> Make gogs client work with context TheFox0x7#1


The reason why I don't want to introduce more forked packages:

  • It takes extra work to maintain.
  • It's difficult to follow upstream changes.
  • If there is a dependency security vulnerability detected (by some bots/tools), our fork will miss it.

ps: see the comment for another fork (just below): // TODO: the only difference is in `PutObject`: the fork doesn't use `NewVerifyingReader(r, sha256.New(), oid, expectedSize)`, need to figure out why .........

image

Make gogs client work with context
@wxiaoguang wxiaoguang enabled auto-merge (squash) February 7, 2025 02:50
@wxiaoguang wxiaoguang merged commit 1ec8d80 into go-gitea:main Feb 7, 2025
26 checks passed
@GiteaBot GiteaBot added this to the 1.24.0 milestone Feb 7, 2025
zjjhot added a commit to zjjhot/gitea that referenced this pull request Feb 7, 2025
* giteaofficial/main:
  refactor: decouple context from migration structs (go-gitea#33399)
  Move gitgraph from modules to services layer (go-gitea#33527)
  Add go wrapper around git diff-tree --raw -r -M (go-gitea#33369)
  [skip ci] Updated translations via Crowdin
  Update MAINTAINERS (go-gitea#33529)
  Add cropping support when modifying the user/org/repo avatar (go-gitea#33498)
  [skip ci] Updated translations via Crowdin
  Add alphabetical project sorting (go-gitea#33504)
  Refactor gitdiff test (go-gitea#33507)
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. modifies/go Pull requests that update Go code size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. 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.

6 participants