2019-09-15 23:28:25 +08:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-28 02:20:29 +08:00
|
|
|
// SPDX-License-Identifier: MIT
|
2019-09-15 23:28:25 +08:00
|
|
|
|
|
|
|
package release
|
|
|
|
|
|
|
|
import (
|
2022-01-20 07:26:57 +08:00
|
|
|
"context"
|
2021-03-23 00:09:51 +08:00
|
|
|
"errors"
|
2019-09-15 23:28:25 +08:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
2021-09-19 19:49:59 +08:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-06-12 23:51:54 +08:00
|
|
|
git_model "code.gitea.io/gitea/models/git"
|
2021-11-19 21:39:57 +08:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-24 17:49:20 +08:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2022-10-12 13:18:26 +08:00
|
|
|
"code.gitea.io/gitea/modules/container"
|
2019-09-15 23:28:25 +08:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
Simplify how git repositories are opened (#28937)
## Purpose
This is a refactor toward building an abstraction over managing git
repositories.
Afterwards, it does not matter anymore if they are stored on the local
disk or somewhere remote.
## What this PR changes
We used `git.OpenRepository` everywhere previously.
Now, we should split them into two distinct functions:
Firstly, there are temporary repositories which do not change:
```go
git.OpenRepository(ctx, diskPath)
```
Gitea managed repositories having a record in the database in the
`repository` table are moved into the new package `gitrepo`:
```go
gitrepo.OpenRepository(ctx, repo_model.Repo)
```
Why is `repo_model.Repository` the second parameter instead of file
path?
Because then we can easily adapt our repository storage strategy.
The repositories can be stored locally, however, they could just as well
be stored on a remote server.
## Further changes in other PRs
- A Git Command wrapper on package `gitrepo` could be created. i.e.
`NewCommand(ctx, repo_model.Repository, commands...)`. `git.RunOpts{Dir:
repo.RepoPath()}`, the directory should be empty before invoking this
method and it can be filled in the function only. #28940
- Remove the `RepoPath()`/`WikiPath()` functions to reduce the
possibility of mistakes.
---------
Co-authored-by: delvh <dev.lh@web.de>
2024-01-28 04:09:51 +08:00
|
|
|
"code.gitea.io/gitea/modules/gitrepo"
|
2024-01-24 11:02:04 +08:00
|
|
|
"code.gitea.io/gitea/modules/graceful"
|
2019-09-15 23:28:25 +08:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2020-01-10 17:34:21 +08:00
|
|
|
"code.gitea.io/gitea/modules/repository"
|
2020-08-18 12:23:45 +08:00
|
|
|
"code.gitea.io/gitea/modules/storage"
|
2019-09-15 23:28:25 +08:00
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
2022-12-09 14:35:56 +08:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2023-09-06 02:37:47 +08:00
|
|
|
notify_service "code.gitea.io/gitea/services/notify"
|
2019-09-15 23:28:25 +08:00
|
|
|
)
|
|
|
|
|
2022-11-19 16:12:33 +08:00
|
|
|
func createTag(ctx context.Context, gitRepo *git.Repository, rel *repo_model.Release, msg string) (bool, error) {
|
2023-09-22 07:43:29 +08:00
|
|
|
err := rel.LoadAttributes(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = rel.Repo.MustNotBeArchived()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2021-03-23 00:09:51 +08:00
|
|
|
var created bool
|
2019-09-15 23:28:25 +08:00
|
|
|
// Only actual create when publish.
|
|
|
|
if !rel.IsDraft {
|
|
|
|
if !gitRepo.IsTagExist(rel.TagName) {
|
2022-11-19 16:12:33 +08:00
|
|
|
if err := rel.LoadAttributes(ctx); err != nil {
|
2021-06-25 22:28:55 +08:00
|
|
|
log.Error("LoadAttributes: %v", err)
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2022-11-19 16:12:33 +08:00
|
|
|
protectedTags, err := git_model.GetProtectedTags(ctx, rel.Repo.ID)
|
2021-06-25 22:28:55 +08:00
|
|
|
if err != nil {
|
2022-10-25 03:29:17 +08:00
|
|
|
return false, fmt.Errorf("GetProtectedTags: %w", err)
|
2021-06-25 22:28:55 +08:00
|
|
|
}
|
2022-09-04 18:47:56 +08:00
|
|
|
|
|
|
|
// Trim '--' prefix to prevent command line argument vulnerability.
|
|
|
|
rel.TagName = strings.TrimPrefix(rel.TagName, "--")
|
2022-11-19 16:12:33 +08:00
|
|
|
isAllowed, err := git_model.IsUserAllowedToControlTag(ctx, protectedTags, rel.TagName, rel.PublisherID)
|
2021-06-25 22:28:55 +08:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if !isAllowed {
|
|
|
|
return false, models.ErrProtectedTagName{
|
|
|
|
TagName: rel.TagName,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-15 23:28:25 +08:00
|
|
|
commit, err := gitRepo.GetCommit(rel.Target)
|
|
|
|
if err != nil {
|
2024-09-17 10:23:40 +08:00
|
|
|
return false, err
|
2019-09-15 23:28:25 +08:00
|
|
|
}
|
|
|
|
|
2021-03-01 03:57:45 +08:00
|
|
|
if len(msg) > 0 {
|
|
|
|
if err = gitRepo.CreateAnnotatedTag(rel.TagName, msg, commit.ID.String()); err != nil {
|
|
|
|
if strings.Contains(err.Error(), "is not a valid tag name") {
|
2021-03-23 00:09:51 +08:00
|
|
|
return false, models.ErrInvalidTagName{
|
2021-03-01 03:57:45 +08:00
|
|
|
TagName: rel.TagName,
|
|
|
|
}
|
|
|
|
}
|
2021-03-23 00:09:51 +08:00
|
|
|
return false, err
|
2021-03-01 03:57:45 +08:00
|
|
|
}
|
|
|
|
} else if err = gitRepo.CreateTag(rel.TagName, commit.ID.String()); err != nil {
|
2019-09-15 23:28:25 +08:00
|
|
|
if strings.Contains(err.Error(), "is not a valid tag name") {
|
2021-03-23 00:09:51 +08:00
|
|
|
return false, models.ErrInvalidTagName{
|
2019-09-15 23:28:25 +08:00
|
|
|
TagName: rel.TagName,
|
|
|
|
}
|
|
|
|
}
|
2021-03-23 00:09:51 +08:00
|
|
|
return false, err
|
2019-09-15 23:28:25 +08:00
|
|
|
}
|
2021-03-23 00:09:51 +08:00
|
|
|
created = true
|
2019-09-15 23:28:25 +08:00
|
|
|
rel.LowerTagName = strings.ToLower(rel.TagName)
|
2021-06-25 22:28:55 +08:00
|
|
|
|
2024-02-24 14:55:19 +08:00
|
|
|
objectFormat := git.ObjectFormatFromName(rel.Repo.ObjectFormatName)
|
2021-12-29 19:40:57 +08:00
|
|
|
commits := repository.NewPushCommits()
|
|
|
|
commits.HeadCommit = repository.CommitToPushCommit(commit)
|
2023-12-17 19:56:08 +08:00
|
|
|
commits.CompareURL = rel.Repo.ComposeCompareURL(objectFormat.EmptyObjectID().String(), commit.ID.String())
|
2021-12-29 19:40:57 +08:00
|
|
|
|
2023-05-26 09:04:48 +08:00
|
|
|
refFullName := git.RefNameFromTag(rel.TagName)
|
2023-09-06 02:37:47 +08:00
|
|
|
notify_service.PushCommits(
|
2022-11-19 16:12:33 +08:00
|
|
|
ctx, rel.Publisher, rel.Repo,
|
2020-10-31 05:59:02 +08:00
|
|
|
&repository.PushUpdateOptions{
|
2023-05-26 09:04:48 +08:00
|
|
|
RefFullName: refFullName,
|
2023-12-17 19:56:08 +08:00
|
|
|
OldCommitID: objectFormat.EmptyObjectID().String(),
|
2020-10-31 05:59:02 +08:00
|
|
|
NewCommitID: commit.ID.String(),
|
2021-12-29 19:40:57 +08:00
|
|
|
}, commits)
|
2023-09-06 02:37:47 +08:00
|
|
|
notify_service.CreateRef(ctx, rel.Publisher, rel.Repo, refFullName, commit.ID.String())
|
2020-07-29 02:08:45 +08:00
|
|
|
rel.CreatedUnix = timeutil.TimeStampNow()
|
2019-09-15 23:28:25 +08:00
|
|
|
}
|
|
|
|
commit, err := gitRepo.GetTagCommit(rel.TagName)
|
|
|
|
if err != nil {
|
2022-10-25 03:29:17 +08:00
|
|
|
return false, fmt.Errorf("GetTagCommit: %w", err)
|
2019-09-15 23:28:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
rel.Sha1 = commit.ID.String()
|
|
|
|
rel.NumCommits, err = commit.CommitsCount()
|
|
|
|
if err != nil {
|
2022-10-25 03:29:17 +08:00
|
|
|
return false, fmt.Errorf("CommitsCount: %w", err)
|
2019-09-15 23:28:25 +08:00
|
|
|
}
|
2020-10-22 08:55:25 +08:00
|
|
|
|
2021-01-02 05:03:31 +08:00
|
|
|
if rel.PublisherID <= 0 {
|
Add context cache as a request level cache (#22294)
To avoid duplicated load of the same data in an HTTP request, we can set
a context cache to do that. i.e. Some pages may load a user from a
database with the same id in different areas on the same page. But the
code is hidden in two different deep logic. How should we share the
user? As a result of this PR, now if both entry functions accept
`context.Context` as the first parameter and we just need to refactor
`GetUserByID` to reuse the user from the context cache. Then it will not
be loaded twice on an HTTP request.
But of course, sometimes we would like to reload an object from the
database, that's why `RemoveContextData` is also exposed.
The core context cache is here. It defines a new context
```go
type cacheContext struct {
ctx context.Context
data map[any]map[any]any
lock sync.RWMutex
}
var cacheContextKey = struct{}{}
func WithCacheContext(ctx context.Context) context.Context {
return context.WithValue(ctx, cacheContextKey, &cacheContext{
ctx: ctx,
data: make(map[any]map[any]any),
})
}
```
Then you can use the below 4 methods to read/write/del the data within
the same context.
```go
func GetContextData(ctx context.Context, tp, key any) any
func SetContextData(ctx context.Context, tp, key, value any)
func RemoveContextData(ctx context.Context, tp, key any)
func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error)
```
Then let's take a look at how `system.GetString` implement it.
```go
func GetSetting(ctx context.Context, key string) (string, error) {
return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) {
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
res, err := GetSettingNoCache(ctx, key)
if err != nil {
return "", err
}
return res.SettingValue, nil
})
})
}
```
First, it will check if context data include the setting object with the
key. If not, it will query from the global cache which may be memory or
a Redis cache. If not, it will get the object from the database. In the
end, if the object gets from the global cache or database, it will be
set into the context cache.
An object stored in the context cache will only be destroyed after the
context disappeared.
2023-02-15 21:37:34 +08:00
|
|
|
u, err := user_model.GetUserByEmail(ctx, commit.Author.Email)
|
2021-01-02 05:03:31 +08:00
|
|
|
if err == nil {
|
|
|
|
rel.PublisherID = u.ID
|
|
|
|
}
|
2020-10-22 08:55:25 +08:00
|
|
|
}
|
2019-09-15 23:28:25 +08:00
|
|
|
} else {
|
|
|
|
rel.CreatedUnix = timeutil.TimeStampNow()
|
|
|
|
}
|
2021-03-23 00:09:51 +08:00
|
|
|
return created, nil
|
2019-09-15 23:28:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateRelease creates a new release of repository.
|
2022-08-25 10:31:57 +08:00
|
|
|
func CreateRelease(gitRepo *git.Repository, rel *repo_model.Release, attachmentUUIDs []string, msg string) error {
|
|
|
|
has, err := repo_model.IsReleaseExist(gitRepo.Ctx, rel.RepoID, rel.TagName)
|
2019-09-15 23:28:25 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-06-03 14:13:58 +08:00
|
|
|
} else if has {
|
2022-08-25 10:31:57 +08:00
|
|
|
return repo_model.ErrReleaseAlreadyExist{
|
2019-09-15 23:28:25 +08:00
|
|
|
TagName: rel.TagName,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-19 16:12:33 +08:00
|
|
|
if _, err = createTag(gitRepo.Ctx, gitRepo, rel, msg); err != nil {
|
2019-09-15 23:28:25 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-11-15 10:06:31 +08:00
|
|
|
rel.Title, _ = util.SplitStringAtByteN(rel.Title, 255)
|
2019-09-15 23:28:25 +08:00
|
|
|
rel.LowerTagName = strings.ToLower(rel.TagName)
|
2022-06-03 14:13:58 +08:00
|
|
|
if err = db.Insert(gitRepo.Ctx, rel); err != nil {
|
2019-09-15 23:28:25 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-25 10:31:57 +08:00
|
|
|
if err = repo_model.AddReleaseAttachments(gitRepo.Ctx, rel.ID, attachmentUUIDs); err != nil {
|
2019-09-15 23:28:25 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !rel.IsDraft {
|
2023-09-06 02:37:47 +08:00
|
|
|
notify_service.NewRelease(gitRepo.Ctx, rel)
|
2019-09-15 23:28:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-01 03:57:45 +08:00
|
|
|
// CreateNewTag creates a new repository tag
|
2022-01-20 07:26:57 +08:00
|
|
|
func CreateNewTag(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, commit, tagName, msg string) error {
|
2022-08-25 10:31:57 +08:00
|
|
|
has, err := repo_model.IsReleaseExist(ctx, repo.ID, tagName)
|
2021-03-01 03:57:45 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-06-03 14:13:58 +08:00
|
|
|
} else if has {
|
2021-03-01 03:57:45 +08:00
|
|
|
return models.ErrTagAlreadyExists{
|
|
|
|
TagName: tagName,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Simplify how git repositories are opened (#28937)
## Purpose
This is a refactor toward building an abstraction over managing git
repositories.
Afterwards, it does not matter anymore if they are stored on the local
disk or somewhere remote.
## What this PR changes
We used `git.OpenRepository` everywhere previously.
Now, we should split them into two distinct functions:
Firstly, there are temporary repositories which do not change:
```go
git.OpenRepository(ctx, diskPath)
```
Gitea managed repositories having a record in the database in the
`repository` table are moved into the new package `gitrepo`:
```go
gitrepo.OpenRepository(ctx, repo_model.Repo)
```
Why is `repo_model.Repository` the second parameter instead of file
path?
Because then we can easily adapt our repository storage strategy.
The repositories can be stored locally, however, they could just as well
be stored on a remote server.
## Further changes in other PRs
- A Git Command wrapper on package `gitrepo` could be created. i.e.
`NewCommand(ctx, repo_model.Repository, commands...)`. `git.RunOpts{Dir:
repo.RepoPath()}`, the directory should be empty before invoking this
method and it can be filled in the function only. #28940
- Remove the `RepoPath()`/`WikiPath()` functions to reduce the
possibility of mistakes.
---------
Co-authored-by: delvh <dev.lh@web.de>
2024-01-28 04:09:51 +08:00
|
|
|
gitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, repo)
|
2021-03-01 03:57:45 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-20 07:26:57 +08:00
|
|
|
defer closer.Close()
|
2021-03-01 03:57:45 +08:00
|
|
|
|
2022-08-25 10:31:57 +08:00
|
|
|
rel := &repo_model.Release{
|
2021-03-01 03:57:45 +08:00
|
|
|
RepoID: repo.ID,
|
2021-06-25 22:28:55 +08:00
|
|
|
Repo: repo,
|
2021-03-01 03:57:45 +08:00
|
|
|
PublisherID: doer.ID,
|
2021-06-25 22:28:55 +08:00
|
|
|
Publisher: doer,
|
2021-03-01 03:57:45 +08:00
|
|
|
TagName: tagName,
|
|
|
|
Target: commit,
|
|
|
|
IsDraft: false,
|
|
|
|
IsPrerelease: false,
|
|
|
|
IsTag: true,
|
|
|
|
}
|
|
|
|
|
2022-11-19 16:12:33 +08:00
|
|
|
if _, err = createTag(ctx, gitRepo, rel, msg); err != nil {
|
2021-03-01 03:57:45 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-06-03 14:13:58 +08:00
|
|
|
return db.Insert(ctx, rel)
|
2021-03-01 03:57:45 +08:00
|
|
|
}
|
|
|
|
|
2021-03-23 00:09:51 +08:00
|
|
|
// UpdateRelease updates information, attachments of a release and will create tag if it's not a draft and tag not exist.
|
|
|
|
// addAttachmentUUIDs accept a slice of new created attachments' uuids which will be reassigned release_id as the created release
|
|
|
|
// delAttachmentUUIDs accept a slice of attachments' uuids which will be deleted from the release
|
|
|
|
// editAttachments accept a map of attachment uuid to new attachment name which will be updated with attachments.
|
2023-09-25 21:17:37 +08:00
|
|
|
func UpdateRelease(ctx context.Context, doer *user_model.User, gitRepo *git.Repository, rel *repo_model.Release,
|
2022-02-24 04:16:07 +08:00
|
|
|
addAttachmentUUIDs, delAttachmentUUIDs []string, editAttachments map[string]string,
|
2023-07-07 13:31:56 +08:00
|
|
|
) error {
|
2021-03-23 00:09:51 +08:00
|
|
|
if rel.ID == 0 {
|
|
|
|
return errors.New("UpdateRelease only accepts an exist release")
|
|
|
|
}
|
2024-05-16 05:34:38 +08:00
|
|
|
isTagCreated, err := createTag(gitRepo.Ctx, gitRepo, rel, "")
|
2021-03-23 00:09:51 +08:00
|
|
|
if err != nil {
|
2019-09-15 23:28:25 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
rel.LowerTagName = strings.ToLower(rel.TagName)
|
|
|
|
|
2023-09-25 21:17:37 +08:00
|
|
|
ctx, committer, err := db.TxContext(ctx)
|
2021-03-23 00:09:51 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-09-19 19:49:59 +08:00
|
|
|
defer committer.Close()
|
2021-03-23 00:09:51 +08:00
|
|
|
|
2024-05-16 05:34:38 +08:00
|
|
|
oldRelease, err := repo_model.GetReleaseByID(ctx, rel.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
isConvertedFromTag := oldRelease.IsTag && !rel.IsTag
|
|
|
|
|
2022-08-25 10:31:57 +08:00
|
|
|
if err = repo_model.UpdateRelease(ctx, rel); err != nil {
|
2019-09-15 23:28:25 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-25 10:31:57 +08:00
|
|
|
if err = repo_model.AddReleaseAttachments(ctx, rel.ID, addAttachmentUUIDs); err != nil {
|
2022-10-25 03:29:17 +08:00
|
|
|
return fmt.Errorf("AddReleaseAttachments: %w", err)
|
2021-03-23 00:09:51 +08:00
|
|
|
}
|
|
|
|
|
2022-10-12 13:18:26 +08:00
|
|
|
deletedUUIDs := make(container.Set[string])
|
2021-03-23 00:09:51 +08:00
|
|
|
if len(delAttachmentUUIDs) > 0 {
|
|
|
|
// Check attachments
|
2021-11-19 21:39:57 +08:00
|
|
|
attachments, err := repo_model.GetAttachmentsByUUIDs(ctx, delAttachmentUUIDs)
|
2021-03-23 00:09:51 +08:00
|
|
|
if err != nil {
|
2022-10-25 03:29:17 +08:00
|
|
|
return fmt.Errorf("GetAttachmentsByUUIDs [uuids: %v]: %w", delAttachmentUUIDs, err)
|
2021-03-23 00:09:51 +08:00
|
|
|
}
|
|
|
|
for _, attach := range attachments {
|
|
|
|
if attach.ReleaseID != rel.ID {
|
2022-12-09 14:35:56 +08:00
|
|
|
return util.SilentWrap{
|
|
|
|
Message: "delete attachment of release permission denied",
|
|
|
|
Err: util.ErrPermissionDenied,
|
|
|
|
}
|
2021-03-23 00:09:51 +08:00
|
|
|
}
|
2022-10-12 13:18:26 +08:00
|
|
|
deletedUUIDs.Add(attach.UUID)
|
2021-03-23 00:09:51 +08:00
|
|
|
}
|
|
|
|
|
2023-04-07 06:01:36 +08:00
|
|
|
if _, err := repo_model.DeleteAttachments(ctx, attachments, true); err != nil {
|
2022-10-25 03:29:17 +08:00
|
|
|
return fmt.Errorf("DeleteAttachments [uuids: %v]: %w", delAttachmentUUIDs, err)
|
2021-03-23 00:09:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(editAttachments) > 0 {
|
2022-01-21 01:46:10 +08:00
|
|
|
updateAttachmentsList := make([]string, 0, len(editAttachments))
|
2021-03-23 00:09:51 +08:00
|
|
|
for k := range editAttachments {
|
|
|
|
updateAttachmentsList = append(updateAttachmentsList, k)
|
|
|
|
}
|
|
|
|
// Check attachments
|
2021-11-19 21:39:57 +08:00
|
|
|
attachments, err := repo_model.GetAttachmentsByUUIDs(ctx, updateAttachmentsList)
|
2021-03-23 00:09:51 +08:00
|
|
|
if err != nil {
|
2022-10-25 03:29:17 +08:00
|
|
|
return fmt.Errorf("GetAttachmentsByUUIDs [uuids: %v]: %w", updateAttachmentsList, err)
|
2021-03-23 00:09:51 +08:00
|
|
|
}
|
|
|
|
for _, attach := range attachments {
|
|
|
|
if attach.ReleaseID != rel.ID {
|
2022-12-09 14:35:56 +08:00
|
|
|
return util.SilentWrap{
|
|
|
|
Message: "update attachment of release permission denied",
|
|
|
|
Err: util.ErrPermissionDenied,
|
|
|
|
}
|
2021-03-23 00:09:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for uuid, newName := range editAttachments {
|
2022-10-12 13:18:26 +08:00
|
|
|
if !deletedUUIDs.Contains(uuid) {
|
2021-11-19 21:39:57 +08:00
|
|
|
if err = repo_model.UpdateAttachmentByUUID(ctx, &repo_model.Attachment{
|
2021-03-23 00:09:51 +08:00
|
|
|
UUID: uuid,
|
|
|
|
Name: newName,
|
|
|
|
}, "name"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-07 13:31:56 +08:00
|
|
|
if err := committer.Commit(); err != nil {
|
|
|
|
return err
|
2021-03-23 00:09:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, uuid := range delAttachmentUUIDs {
|
2021-11-19 21:39:57 +08:00
|
|
|
if err := storage.Attachments.Delete(repo_model.AttachmentRelativePath(uuid)); err != nil {
|
2021-03-23 00:09:51 +08:00
|
|
|
// Even delete files failed, but the attachments has been removed from database, so we
|
|
|
|
// should not return error but only record the error on logs.
|
|
|
|
// users have to delete this attachments manually or we should have a
|
|
|
|
// synchronize between database attachment table and attachment storage
|
|
|
|
log.Error("delete attachment[uuid: %s] failed: %v", uuid, err)
|
|
|
|
}
|
2019-09-24 14:55:07 +08:00
|
|
|
}
|
|
|
|
|
2020-07-30 03:20:54 +08:00
|
|
|
if !rel.IsDraft {
|
2024-05-16 05:34:38 +08:00
|
|
|
if !isTagCreated && !isConvertedFromTag {
|
2024-02-02 12:18:12 +08:00
|
|
|
notify_service.UpdateRelease(gitRepo.Ctx, doer, rel)
|
|
|
|
return nil
|
|
|
|
}
|
2023-09-06 02:37:47 +08:00
|
|
|
notify_service.NewRelease(gitRepo.Ctx, rel)
|
2020-07-30 03:20:54 +08:00
|
|
|
}
|
2023-07-07 13:31:56 +08:00
|
|
|
return nil
|
2019-09-15 23:28:25 +08:00
|
|
|
}
|
2019-10-01 00:10:00 +08:00
|
|
|
|
|
|
|
// DeleteReleaseByID deletes a release and corresponding Git tag by given ID.
|
2023-11-26 01:21:21 +08:00
|
|
|
func DeleteReleaseByID(ctx context.Context, repo *repo_model.Repository, rel *repo_model.Release, doer *user_model.User, delTag bool) error {
|
2019-10-01 00:10:00 +08:00
|
|
|
if delTag {
|
2022-11-19 16:12:33 +08:00
|
|
|
protectedTags, err := git_model.GetProtectedTags(ctx, rel.RepoID)
|
2022-06-17 04:03:03 +08:00
|
|
|
if err != nil {
|
2022-10-25 03:29:17 +08:00
|
|
|
return fmt.Errorf("GetProtectedTags: %w", err)
|
2022-06-17 04:03:03 +08:00
|
|
|
}
|
2022-11-19 16:12:33 +08:00
|
|
|
isAllowed, err := git_model.IsUserAllowedToControlTag(ctx, protectedTags, rel.TagName, rel.PublisherID)
|
2022-06-17 04:03:03 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !isAllowed {
|
|
|
|
return models.ErrProtectedTagName{
|
|
|
|
TagName: rel.TagName,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-23 22:44:45 +08:00
|
|
|
if stdout, _, err := git.NewCommand(ctx, "tag", "-d").AddDashesAndList(rel.TagName).
|
2019-11-30 22:40:22 +08:00
|
|
|
SetDescription(fmt.Sprintf("DeleteReleaseByID (git tag -d): %d", rel.ID)).
|
2022-04-01 10:55:30 +08:00
|
|
|
RunStdString(&git.RunOpts{Dir: repo.RepoPath()}); err != nil && !strings.Contains(err.Error(), "not found") {
|
2019-11-30 22:40:22 +08:00
|
|
|
log.Error("DeleteReleaseByID (git tag -d): %d in %v Failed:\nStdout: %s\nError: %v", rel.ID, repo, stdout, err)
|
2022-10-25 03:29:17 +08:00
|
|
|
return fmt.Errorf("git tag -d: %w", err)
|
2019-10-01 00:10:00 +08:00
|
|
|
}
|
|
|
|
|
2023-05-26 09:04:48 +08:00
|
|
|
refName := git.RefNameFromTag(rel.TagName)
|
2024-03-11 05:30:36 +08:00
|
|
|
objectFormat := git.ObjectFormatFromName(repo.ObjectFormatName)
|
2023-09-06 02:37:47 +08:00
|
|
|
notify_service.PushCommits(
|
2022-11-19 16:12:33 +08:00
|
|
|
ctx, doer, repo,
|
2020-12-17 00:00:46 +08:00
|
|
|
&repository.PushUpdateOptions{
|
2023-05-26 09:04:48 +08:00
|
|
|
RefFullName: refName,
|
2020-12-17 00:00:46 +08:00
|
|
|
OldCommitID: rel.Sha1,
|
2023-12-17 19:56:08 +08:00
|
|
|
NewCommitID: objectFormat.EmptyObjectID().String(),
|
2020-12-17 00:00:46 +08:00
|
|
|
}, repository.NewPushCommits())
|
2023-09-06 02:37:47 +08:00
|
|
|
notify_service.DeleteRef(ctx, doer, repo, refName)
|
2020-12-17 00:00:46 +08:00
|
|
|
|
2023-12-26 04:25:29 +08:00
|
|
|
if _, err := db.DeleteByID[repo_model.Release](ctx, rel.ID); err != nil {
|
2022-10-25 03:29:17 +08:00
|
|
|
return fmt.Errorf("DeleteReleaseByID: %w", err)
|
2019-10-01 00:10:00 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
rel.IsTag = true
|
|
|
|
|
2023-11-26 01:21:21 +08:00
|
|
|
if err := repo_model.UpdateRelease(ctx, rel); err != nil {
|
2022-10-25 03:29:17 +08:00
|
|
|
return fmt.Errorf("Update: %w", err)
|
2019-10-01 00:10:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rel.Repo = repo
|
2023-11-26 01:21:21 +08:00
|
|
|
if err := rel.LoadAttributes(ctx); err != nil {
|
2022-10-25 03:29:17 +08:00
|
|
|
return fmt.Errorf("LoadAttributes: %w", err)
|
2019-10-01 00:10:00 +08:00
|
|
|
}
|
|
|
|
|
2022-11-19 16:12:33 +08:00
|
|
|
if err := repo_model.DeleteAttachmentsByRelease(ctx, rel.ID); err != nil {
|
2022-10-25 03:29:17 +08:00
|
|
|
return fmt.Errorf("DeleteAttachments: %w", err)
|
2019-10-01 00:10:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := range rel.Attachments {
|
|
|
|
attachment := rel.Attachments[i]
|
2020-08-18 12:23:45 +08:00
|
|
|
if err := storage.Attachments.Delete(attachment.RelativePath()); err != nil {
|
2019-10-01 00:10:00 +08:00
|
|
|
log.Error("Delete attachment %s of release %s failed: %v", attachment.UUID, rel.ID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-02 12:18:12 +08:00
|
|
|
if !rel.IsDraft {
|
|
|
|
notify_service.DeleteRelease(ctx, doer, rel)
|
|
|
|
}
|
2019-10-01 00:10:00 +08:00
|
|
|
return nil
|
|
|
|
}
|
2024-01-24 11:02:04 +08:00
|
|
|
|
|
|
|
// Init start release service
|
|
|
|
func Init() error {
|
|
|
|
return initTagSyncQueue(graceful.GetManager().ShutdownContext())
|
|
|
|
}
|