-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Implement API endpoint to link a package to a repo #23851
base: main
Are you sure you want to change the base?
Changes from all commits
339af37
84a2bc3
10df3c4
6e2661b
f07cfa9
9fb1f1c
221dcdc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,9 @@ import ( | |
|
||
"code.gitea.io/gitea/models/db" | ||
packages_model "code.gitea.io/gitea/models/packages" | ||
access_model "code.gitea.io/gitea/models/perm/access" | ||
repo_model "code.gitea.io/gitea/models/repo" | ||
"code.gitea.io/gitea/models/unit" | ||
user_model "code.gitea.io/gitea/models/user" | ||
"code.gitea.io/gitea/modules/json" | ||
"code.gitea.io/gitea/modules/log" | ||
|
@@ -657,3 +659,29 @@ func RemoveAllPackages(ctx context.Context, userID int64) (int, error) { | |
} | ||
return count, nil | ||
} | ||
|
||
func LinkPackageToRepository(ctx context.Context, doer *user_model.User, p *packages_model.Package, repoID int64) error { | ||
if repoID != 0 { | ||
repo, err := repo_model.GetRepositoryByID(ctx, repoID) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This MUST check if the repo belongs to the package owner. And there should be a test for linking a repo of another user. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To avoid further back and forth on permissions, and me doing best-effort guesses, can you give me a detailed run down on who should be allowed to do what? |
||
if err != nil { | ||
return fmt.Errorf("Error getting repository %d: %w", repoID, err) | ||
} | ||
|
||
perms, err := access_model.GetUserRepoPermission(ctx, repo, doer) | ||
if err != nil { | ||
return fmt.Errorf("Error getting permissions for user %d on repository %d: %w", doer.ID, repo.ID, err) | ||
} | ||
|
||
canWrite := perms.CanWrite(unit.TypePackages) | ||
|
||
if !canWrite { | ||
return fmt.Errorf("No permission to link this package and repository, or packages are disabled") | ||
} | ||
} | ||
|
||
if err := packages_model.SetRepositoryLink(ctx, p.ID, repoID); err != nil { | ||
return fmt.Errorf("Error updating package: %w", err) | ||
} | ||
|
||
return nil | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we really want to expose the repository's id? I don't think we should do that. Using
OwnerName
andRepoName
should be better.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure what you mean by "exposing" in this context. The existing
GET /repos/{owner}/{repo}
endpoint makes theid
value available in its response, so that information is already "exposed" to every consumer of the public API.What's the harm in giving the user a choice which identifier they can run this new endpoint against?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sclu1034 I think last comments refer to the fact that most, if not all the API, refer to orgs and repos using their name, and not their id. At least I already played with the API and I don't recall I ever sent requests using the repo ids. It just feels more natural to use the repo name instead.
Moreover, the web frontend only allow linking to a repository of the same owner. If that is by design, you would have to drop the owner part in the repo name.
EDIT: from @KN4CK3R's previous comment, it is indeed by design.
Just my last two cents on the endpoint: there could only be zero or one link to a repository, so I'd rather use PUT and DELETE.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is by design. It's weird to link a package to another owner's repository.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lunny well I'm still wondering. I develop some Unity (game engine) packages. Distribution relies on NPM protocol, although packages are obviously not meant for Nodejs (it's C#). And I also develop some Nodejs packages. But Gitea only provides a single NPM registry per owner. So it seems to me the only way to cleanly separate both usages is to make a dedicated "owner", so to have two registries. But this is only for distribution. Development still occurs in the same org.