Skip to content

Commit

Permalink
Implement API endpoint to link a package to a repo
Browse files Browse the repository at this point in the history
  • Loading branch information
sclu1034 committed Jan 16, 2024
1 parent b827024 commit 339af37
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 0 deletions.
1 change: 1 addition & 0 deletions routers/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1412,6 +1412,7 @@ func Routes() *web.Route {
m.Delete("", reqToken(), reqPackageAccess(perm.AccessModeWrite), packages.DeletePackage)
m.Get("/files", reqToken(), packages.ListPackageFiles)
})
m.Post("/{type}/{name}/link", reqPackageAccess(perm.AccessModeWrite), packages.LinkPackage)
m.Get("/", reqToken(), packages.ListPackages)
}, tokenRequiresScopes(auth_model.AccessTokenScopeCategoryPackage), context_service.UserAssignmentAPI(), context.PackageAssignmentAPI(), reqPackageAccess(perm.AccessModeRead))

Expand Down
49 changes: 49 additions & 0 deletions routers/api/v1/packages/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,52 @@ 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
// responses:
// "201":
// "$ref": "#/responses/empty"
// "404":
// "$ref": "#/responses/notFound"

repoID := ctx.FormInt64("repo")
packageType := ctx.Params("type")
name := ctx.Params("name")
pkg, err := packages.GetPackageByName(ctx, ctx.ContextUser.ID, packages.Type(packageType), name)
if err != nil {
ctx.Error(http.StatusInternalServerError, "LinkPackage", err)
return
}

err = packages_service.LinkPackage(ctx, ctx.Doer, pkg, repoID)
if err != nil {
ctx.Error(http.StatusInternalServerError, "LinkPackage", err)
return
}
ctx.Status(http.StatusNoContent)
}
34 changes: 34 additions & 0 deletions services/packages/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -657,3 +659,35 @@ func RemoveAllPackages(ctx context.Context, userID int64) (int, error) {
}
return count, nil
}

func LinkPackage(ctx context.Context, doer *user_model.User, p *packages_model.Package, repoID int64) error {
if repoID != 0 {
repo, err := repo_model.GetRepositoryByID(ctx, repoID)
if err != nil {
return fmt.Errorf("Error getting repository %d: %w", repoID, err)
}

canWrite := repo.OwnerID == doer.ID

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")
}

repoID = repo.ID
}

if err := packages_model.SetRepositoryLink(ctx, p.ID, repoID); err != nil {
return fmt.Errorf("Error updating package: %w", err)
}

return nil
}
47 changes: 47 additions & 0 deletions templates/swagger/v1_json.tmpl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 339af37

Please sign in to comment.