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

Automatically connect source code repository to package on upload operation #27318

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion routers/api/packages/alpine/alpine.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func UploadPackageFile(ctx *context.Context) {
return
}

_, _, err = packages_service.CreatePackageOrAddFileToExisting(
pv, _, err := packages_service.CreatePackageOrAddFileToExisting(
ctx,
&packages_service.PackageCreationInfo{
PackageInfo: packages_service.PackageInfo{
Expand Down Expand Up @@ -178,6 +178,20 @@ func UploadPackageFile(ctx *context.Context) {
return
}

if err = helper.TryConnectRepository(ctx, pv.PackageID); err != nil {
switch {
case errors.Is(err, util.ErrPermissionDenied):
apiError(ctx, http.StatusForbidden, err)
case errors.Is(err, util.ErrNotExist):
apiError(ctx, http.StatusNotFound, err)
case errors.Is(err, util.ErrInvalidArgument):
apiError(ctx, http.StatusBadRequest, err)
default:
apiError(ctx, http.StatusInternalServerError, err)
}
return
}

ctx.Status(http.StatusCreated)
}

Expand Down
14 changes: 14 additions & 0 deletions routers/api/packages/cargo/cargo.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,20 @@ func UploadPackage(ctx *context.Context) {
return
}

if err = helper.TryConnectRepository(ctx, pv.PackageID); err != nil {
switch {
case errors.Is(err, util.ErrPermissionDenied):
apiError(ctx, http.StatusForbidden, err)
case errors.Is(err, util.ErrNotExist):
apiError(ctx, http.StatusNotFound, err)
case errors.Is(err, util.ErrInvalidArgument):
apiError(ctx, http.StatusBadRequest, err)
default:
apiError(ctx, http.StatusInternalServerError, err)
}
return
}

ctx.JSON(http.StatusOK, StatusResponse{OK: true})
}

Expand Down
16 changes: 15 additions & 1 deletion routers/api/packages/chef/chef.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ func UploadPackage(ctx *context.Context) {
return
}

_, _, err = packages_service.CreatePackageAndAddFile(
pv, _, err := packages_service.CreatePackageAndAddFile(
ctx,
&packages_service.PackageCreationInfo{
PackageInfo: packages_service.PackageInfo{
Expand Down Expand Up @@ -319,6 +319,20 @@ func UploadPackage(ctx *context.Context) {
return
}

if err = helper.TryConnectRepository(ctx, pv.PackageID); err != nil {
switch {
case errors.Is(err, util.ErrPermissionDenied):
apiError(ctx, http.StatusForbidden, err)
case errors.Is(err, util.ErrNotExist):
apiError(ctx, http.StatusNotFound, err)
case errors.Is(err, util.ErrInvalidArgument):
apiError(ctx, http.StatusBadRequest, err)
default:
apiError(ctx, http.StatusInternalServerError, err)
}
return
}

ctx.JSON(http.StatusCreated, make(map[any]any))
}

Expand Down
16 changes: 15 additions & 1 deletion routers/api/packages/composer/composer.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func UploadPackage(ctx *context.Context) {
cp.Version = v.String()
}

_, _, err = packages_service.CreatePackageAndAddFile(
pv, _, err := packages_service.CreatePackageAndAddFile(
ctx,
&packages_service.PackageCreationInfo{
PackageInfo: packages_service.PackageInfo{
Expand Down Expand Up @@ -256,5 +256,19 @@ func UploadPackage(ctx *context.Context) {
return
}

if err = helper.TryConnectRepository(ctx, pv.PackageID); err != nil {
switch {
case errors.Is(err, util.ErrPermissionDenied):
apiError(ctx, http.StatusForbidden, err)
case errors.Is(err, util.ErrNotExist):
apiError(ctx, http.StatusNotFound, err)
case errors.Is(err, util.ErrInvalidArgument):
apiError(ctx, http.StatusBadRequest, err)
default:
apiError(ctx, http.StatusInternalServerError, err)
}
return
}

ctx.Status(http.StatusCreated)
}
19 changes: 18 additions & 1 deletion routers/api/packages/conan/conan.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package conan

import (
std_ctx "context"
"errors"
"fmt"
"io"
"net/http"
Expand All @@ -21,6 +22,7 @@ import (
packages_module "code.gitea.io/gitea/modules/packages"
conan_module "code.gitea.io/gitea/modules/packages/conan"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/routers/api/packages/helper"
notify_service "code.gitea.io/gitea/services/notify"
packages_service "code.gitea.io/gitea/services/packages"
Expand Down Expand Up @@ -367,6 +369,7 @@ func uploadFile(ctx *context.Context, fileFilter container.Set[string], fileKey
apiError(ctx, http.StatusInternalServerError, err)
return
}

pv, err := packages_model.GetVersionByNameAndVersion(ctx, pci.Owner.ID, pci.PackageType, pci.Name, pci.Version)
if err != nil && err != packages_model.ErrPackageNotExist {
apiError(ctx, http.StatusInternalServerError, err)
Expand Down Expand Up @@ -407,7 +410,7 @@ func uploadFile(ctx *context.Context, fileFilter container.Set[string], fileKey
}
}

_, _, err = packages_service.CreatePackageOrAddFileToExisting(
pv, _, err := packages_service.CreatePackageOrAddFileToExisting(
ctx,
pci,
pfci,
Expand All @@ -424,6 +427,20 @@ func uploadFile(ctx *context.Context, fileFilter container.Set[string], fileKey
return
}

if err = helper.TryConnectRepository(ctx, pv.PackageID); err != nil {
switch {
case errors.Is(err, util.ErrPermissionDenied):
apiError(ctx, http.StatusForbidden, err)
case errors.Is(err, util.ErrNotExist):
apiError(ctx, http.StatusNotFound, err)
case errors.Is(err, util.ErrInvalidArgument):
apiError(ctx, http.StatusBadRequest, err)
default:
apiError(ctx, http.StatusInternalServerError, err)
}
return
}

ctx.Status(http.StatusCreated)
}

Expand Down
16 changes: 15 additions & 1 deletion routers/api/packages/conda/conda.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func UploadPackageFile(ctx *context.Context) {
return
}

_, _, err = packages_service.CreatePackageOrAddFileToExisting(
pv, _, err := packages_service.CreatePackageOrAddFileToExisting(
ctx,
&packages_service.PackageCreationInfo{
PackageInfo: packages_service.PackageInfo{
Expand Down Expand Up @@ -271,6 +271,20 @@ func UploadPackageFile(ctx *context.Context) {
return
}

if err = helper.TryConnectRepository(ctx, pv.PackageID); err != nil {
switch {
case errors.Is(err, util.ErrPermissionDenied):
apiError(ctx, http.StatusForbidden, err)
case errors.Is(err, util.ErrNotExist):
apiError(ctx, http.StatusNotFound, err)
case errors.Is(err, util.ErrInvalidArgument):
apiError(ctx, http.StatusBadRequest, err)
default:
apiError(ctx, http.StatusInternalServerError, err)
}
return
}

ctx.Status(http.StatusCreated)
}

Expand Down
16 changes: 15 additions & 1 deletion routers/api/packages/cran/cran.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func uploadPackageFile(ctx *context.Context, compositeKey string, properties map
return
}

_, _, err = packages_service.CreatePackageOrAddFileToExisting(
pv, _, err := packages_service.CreatePackageOrAddFileToExisting(
ctx,
&packages_service.PackageCreationInfo{
PackageInfo: packages_service.PackageInfo{
Expand Down Expand Up @@ -218,6 +218,20 @@ func uploadPackageFile(ctx *context.Context, compositeKey string, properties map
return
}

if err = helper.TryConnectRepository(ctx, pv.PackageID); err != nil {
switch {
case errors.Is(err, util.ErrPermissionDenied):
apiError(ctx, http.StatusForbidden, err)
case errors.Is(err, util.ErrNotExist):
apiError(ctx, http.StatusNotFound, err)
case errors.Is(err, util.ErrInvalidArgument):
apiError(ctx, http.StatusBadRequest, err)
default:
apiError(ctx, http.StatusInternalServerError, err)
}
return
}

ctx.Status(http.StatusCreated)
}

Expand Down
16 changes: 15 additions & 1 deletion routers/api/packages/debian/debian.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func UploadPackageFile(ctx *context.Context) {
return
}

_, _, err = packages_service.CreatePackageOrAddFileToExisting(
pv, _, err := packages_service.CreatePackageOrAddFileToExisting(
ctx,
&packages_service.PackageCreationInfo{
PackageInfo: packages_service.PackageInfo{
Expand Down Expand Up @@ -203,6 +203,20 @@ func UploadPackageFile(ctx *context.Context) {
return
}

if err = helper.TryConnectRepository(ctx, pv.PackageID); err != nil {
switch {
case errors.Is(err, util.ErrPermissionDenied):
apiError(ctx, http.StatusForbidden, err)
case errors.Is(err, util.ErrNotExist):
apiError(ctx, http.StatusNotFound, err)
case errors.Is(err, util.ErrInvalidArgument):
apiError(ctx, http.StatusBadRequest, err)
default:
apiError(ctx, http.StatusInternalServerError, err)
}
return
}

ctx.Status(http.StatusCreated)
}

Expand Down
17 changes: 16 additions & 1 deletion routers/api/packages/generic/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/log"
packages_module "code.gitea.io/gitea/modules/packages"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/routers/api/packages/helper"
packages_service "code.gitea.io/gitea/services/packages"
)
Expand Down Expand Up @@ -88,7 +89,7 @@ func UploadPackage(ctx *context.Context) {
}
defer buf.Close()

_, _, err = packages_service.CreatePackageOrAddFileToExisting(
pv, _, err := packages_service.CreatePackageOrAddFileToExisting(
ctx,
&packages_service.PackageCreationInfo{
PackageInfo: packages_service.PackageInfo{
Expand Down Expand Up @@ -120,6 +121,20 @@ func UploadPackage(ctx *context.Context) {
return
}

if err = helper.TryConnectRepository(ctx, pv.PackageID); err != nil {
switch {
case errors.Is(err, util.ErrPermissionDenied):
apiError(ctx, http.StatusForbidden, err)
case errors.Is(err, util.ErrNotExist):
apiError(ctx, http.StatusNotFound, err)
case errors.Is(err, util.ErrInvalidArgument):
apiError(ctx, http.StatusBadRequest, err)
default:
apiError(ctx, http.StatusInternalServerError, err)
}
return
}

ctx.Status(http.StatusCreated)
}

Expand Down
32 changes: 32 additions & 0 deletions routers/api/packages/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ import (
"net/url"

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"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
)

// LogAndProcessError logs an error and calls a custom callback with the processed error message.
Expand Down Expand Up @@ -61,3 +65,31 @@ func ServePackageFile(ctx *context.Context, s io.ReadSeekCloser, u *url.URL, pf

ctx.ServeContent(s, opts)
}

func TryConnectRepository(ctx *context.Context, packageID int64) error {
headers := ctx.Req.Header["Package-Connection-Repository"]
This conversation was marked as resolved.
Show resolved Hide resolved

if len(headers) == 0 {
return nil
}

if len(headers) != 1 {
return util.NewInvalidArgumentErrorf("too many package repository connection headers")
}

repo, err := repo_model.GetRepositoryByName(ctx, ctx.Package.Owner.ID, headers[0])
if err != nil {
return err
}

perms, err := access_model.GetUserRepoPermission(ctx, repo, ctx.Doer)
if err != nil {
return err
}

if !perms.CanWrite(unit.TypePackages) {
return util.NewPermissionDeniedErrorf("no permission to link package to repository: %s, or packages are disabled", repo.Name)
}

return packages_model.SetRepositoryLink(ctx, packageID, repo.ID)
}
17 changes: 16 additions & 1 deletion routers/api/packages/maven/maven.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"code.gitea.io/gitea/modules/log"
packages_module "code.gitea.io/gitea/modules/packages"
maven_module "code.gitea.io/gitea/modules/packages/maven"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/routers/api/packages/helper"
packages_service "code.gitea.io/gitea/services/packages"

Expand Down Expand Up @@ -355,7 +356,7 @@ func UploadPackageFile(ctx *context.Context) {
}
}

_, _, err = packages_service.CreatePackageOrAddFileToExisting(
pv, _, err := packages_service.CreatePackageOrAddFileToExisting(
ctx,
pvci,
pfci,
Expand All @@ -372,6 +373,20 @@ func UploadPackageFile(ctx *context.Context) {
return
}

if err = helper.TryConnectRepository(ctx, pv.PackageID); err != nil {
switch {
case errors.Is(err, util.ErrPermissionDenied):
apiError(ctx, http.StatusForbidden, err)
case errors.Is(err, util.ErrNotExist):
apiError(ctx, http.StatusNotFound, err)
case errors.Is(err, util.ErrInvalidArgument):
apiError(ctx, http.StatusBadRequest, err)
default:
apiError(ctx, http.StatusInternalServerError, err)
}
return
}

ctx.Status(http.StatusCreated)
}

Expand Down
Loading