-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Implement API endpoint to link a package to a repo #23851
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
Changes from 5 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 |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
package packages | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
|
||
"code.gitea.io/gitea/models/packages" | ||
|
@@ -213,3 +214,54 @@ func ListPackageFiles(ctx *context.APIContext) { | |
|
||
ctx.JSON(http.StatusOK, apiPackageFiles) | ||
} | ||
|
||
// LinkPackage sets a repository link for a package | ||
func LinkPackage(ctx *context.APIContext) { | ||
// swagger:operation POST /packages/{owner}/{type}/{name}/link package linkPackage | ||
// --- | ||
// summary: Link a package to a repository | ||
// parameters: | ||
// - name: owner | ||
// in: path | ||
// description: owner of the package | ||
// type: string | ||
// required: true | ||
// - name: type | ||
// in: path | ||
// description: type of the package | ||
// type: string | ||
// required: true | ||
// - name: name | ||
// in: path | ||
// description: name of the package | ||
// type: string | ||
// required: true | ||
// - name: repo | ||
// in: query | ||
// description: ID of the repository to link | ||
// type: integer | ||
// required: true | ||
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. Would be nice if you can't just pass the repository id (which you don't have usually) but the repo name. 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. Not particularly proud of how my code parsing those two parameters turned out, but I didn't find any useful utilities. |
||
// responses: | ||
// "201": | ||
// "$ref": "#/responses/empty" | ||
// "404": | ||
// "$ref": "#/responses/notFound" | ||
|
||
pkg, err := packages.GetPackageByName(ctx, ctx.ContextUser.ID, packages.Type(ctx.Params("type")), ctx.Params("name")) | ||
if err != nil { | ||
if errors.Is(err, util.ErrNotExist) { | ||
ctx.Error(http.StatusNotFound, "GetPackageByName", err) | ||
} else { | ||
ctx.Error(http.StatusInternalServerError, "GetPackageByName", err) | ||
} | ||
return | ||
} | ||
|
||
err = packages_service.LinkPackageToRepository(ctx, ctx.Doer, pkg, ctx.FormInt64("repo")) | ||
if err != nil { | ||
ctx.Error(http.StatusInternalServerError, "LinkPackageToRepository", err) | ||
return | ||
} | ||
|
||
ctx.Status(http.StatusNoContent) | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.