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

Implement API endpoint to link a package to a repo #23851

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

sclu1034
Copy link

Closes #21062.

@silverwind silverwind added type/feature Completely new functionality. Can only be merged if feature freeze is not active. modifies/api This PR adds API routes or modifies them labels Mar 31, 2023
@silverwind silverwind added this to the 1.20.0 milestone Mar 31, 2023
services/packages/packages.go Outdated Show resolved Hide resolved
routers/api/v1/packages/package.go Outdated Show resolved Hide resolved
@GiteaBot GiteaBot added lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Apr 2, 2023
@pull-request-size pull-request-size bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label May 2, 2023
@delvh delvh removed this from the 1.20.0 milestone Jun 4, 2023
@denyskon
Copy link
Member

@sclu1034 Cloud you update your branch and resolve merge conflicts?

sclu1034 and others added 4 commits January 16, 2024 16:57
To satisfy the API's restrictions on write access when linking packages,
the test needs to be run with a different user.
Co-authored-by: KN4CK3R <admin@oldschoolhack.me>
Co-authored-by: KN4CK3R <admin@oldschoolhack.me>
@sclu1034
Copy link
Author

Rebased on current main

Comment on lines 672 to 683
if !canWrite {
perms, err := access_model.GetUserRepoPermission(ctx, repo, doer)
if err != nil {
return fmt.Errorf("Error getting repository permissions for %d on %d: %w", doer.ID, repo.ID, err)
}

canWrite = perms.CanWrite(unit.TypePackages)
}

if !canWrite {
return fmt.Errorf("No permissions to link this package and repository")
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if !canWrite {
perms, err := access_model.GetUserRepoPermission(ctx, repo, doer)
if err != nil {
return fmt.Errorf("Error getting repository permissions for %d on %d: %w", doer.ID, repo.ID, err)
}
canWrite = perms.CanWrite(unit.TypePackages)
}
if !canWrite {
return fmt.Errorf("No permissions to link this package and repository")
}
perms, err := access_model.GetUserRepoPermission(ctx, repo, doer)
if err != nil {
return fmt.Errorf("Error getting repository permissions for %d on %d: %w", doer.ID, repo.ID, err)
}
if !perms.CanWrite(unit.TypePackages) {
return fmt.Errorf("No permissions to link this package and repository")
}

It is also possible that packages are disabled for this repo, so I think we should check for all users, not just if user isn't the owner.

services/packages/packages.go Outdated Show resolved Hide resolved
@denyskon denyskon self-requested a review January 16, 2024 20:50
@VAllens
Copy link

VAllens commented Jan 17, 2024

Goooooooooooooooooooooooooooooooooooooooooood !!!

Co-authored-by: Denys Konovalov <kontakt@denyskon.de>

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)
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Author

Choose a reason for hiding this comment

The 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?

routers/api/v1/api.go Outdated Show resolved Hide resolved
Comment on lines 239 to 243
// - name: repo
// in: query
// description: ID of the repository to link
// type: integer
// required: true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice if you can't just pass the repository id (which you don't have usually) but the repo name.
Could have two parameters repo_id=... and repo_name=.... The endpoint should choose the matching repo then. Could prefer the id over the name.

Copy link
Author

@sclu1034 sclu1034 Jan 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not particularly proud of how my code parsing those two parameters turned out, but I didn't find any useful utilities.
Any ideas to improve 221dcdc?

@GiteaBot GiteaBot added lgtm/blocked A maintainer has reservations with the PR and thus it cannot be merged and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Jan 18, 2024
sclu1034 and others added 2 commits January 22, 2024 09:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lgtm/blocked A maintainer has reservations with the PR and thus it cannot be merged modifies/api This PR adds API routes or modifies them size/L Denotes a PR that changes 100-499 lines, ignoring generated files. topic/packages type/feature Completely new functionality. Can only be merged if feature freeze is not active.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Ability to link package to a repo from api
8 participants