2014-04-27 09:05:13 +02:00
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2014-04-11 04:27:13 +02:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2021-09-23 17:45:36 +02:00
|
|
|
"context"
|
2014-06-23 22:22:34 +02:00
|
|
|
"fmt"
|
2014-04-11 04:27:13 +02:00
|
|
|
"strings"
|
2021-09-19 13:49:59 +02:00
|
|
|
|
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-12-10 02:27:50 +01:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2017-02-25 15:54:40 +01:00
|
|
|
)
|
2014-06-28 17:56:41 +02:00
|
|
|
|
2020-02-03 09:47:04 +01:00
|
|
|
// PushUpdateDeleteTagsContext updates a number of delete tags with context
|
2021-12-10 02:27:50 +01:00
|
|
|
func PushUpdateDeleteTagsContext(ctx context.Context, repo *repo_model.Repository, tags []string) error {
|
2021-09-23 17:45:36 +02:00
|
|
|
return pushUpdateDeleteTags(db.GetEngine(ctx), repo, tags)
|
2020-02-03 09:47:04 +01:00
|
|
|
}
|
|
|
|
|
2021-12-10 02:27:50 +01:00
|
|
|
func pushUpdateDeleteTags(e db.Engine, repo *repo_model.Repository, tags []string) error {
|
2019-12-26 12:29:45 +01:00
|
|
|
if len(tags) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
lowerTags := make([]string, 0, len(tags))
|
|
|
|
for _, tag := range tags {
|
|
|
|
lowerTags = append(lowerTags, strings.ToLower(tag))
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := e.
|
|
|
|
Where("repo_id = ? AND is_tag = ?", repo.ID, true).
|
|
|
|
In("lower_tag_name", lowerTags).
|
|
|
|
Delete(new(Release)); err != nil {
|
|
|
|
return fmt.Errorf("Delete: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := e.
|
|
|
|
Where("repo_id = ? AND is_tag = ?", repo.ID, false).
|
|
|
|
In("lower_tag_name", lowerTags).
|
2019-12-27 15:18:07 +01:00
|
|
|
Cols("is_draft", "num_commits", "sha1").
|
|
|
|
Update(&Release{
|
|
|
|
IsDraft: true,
|
|
|
|
}); err != nil {
|
2019-12-26 12:29:45 +01:00
|
|
|
return fmt.Errorf("Update: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-24 09:13:26 +02:00
|
|
|
// PushUpdateDeleteTag must be called for any push actions to delete tag
|
2021-12-10 02:27:50 +01:00
|
|
|
func PushUpdateDeleteTag(repo *repo_model.Repository, tagName string) error {
|
2017-09-20 07:26:49 +02:00
|
|
|
rel, err := GetRelease(repo.ID, tagName)
|
|
|
|
if err != nil {
|
|
|
|
if IsErrReleaseNotExist(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("GetRelease: %v", err)
|
|
|
|
}
|
|
|
|
if rel.IsTag {
|
2021-09-23 17:45:36 +02:00
|
|
|
if _, err = db.GetEngine(db.DefaultContext).ID(rel.ID).Delete(new(Release)); err != nil {
|
2017-09-20 07:26:49 +02:00
|
|
|
return fmt.Errorf("Delete: %v", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
rel.IsDraft = true
|
|
|
|
rel.NumCommits = 0
|
|
|
|
rel.Sha1 = ""
|
2021-09-23 17:45:36 +02:00
|
|
|
if _, err = db.GetEngine(db.DefaultContext).ID(rel.ID).AllCols().Update(rel); err != nil {
|
2017-09-20 07:26:49 +02:00
|
|
|
return fmt.Errorf("Update: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-10 10:34:21 +01:00
|
|
|
// SaveOrUpdateTag must be called for any push actions to add tag
|
2021-12-10 02:27:50 +01:00
|
|
|
func SaveOrUpdateTag(repo *repo_model.Repository, newRel *Release) error {
|
2020-01-10 10:34:21 +01:00
|
|
|
rel, err := GetRelease(repo.ID, newRel.TagName)
|
2017-09-20 07:26:49 +02:00
|
|
|
if err != nil && !IsErrReleaseNotExist(err) {
|
|
|
|
return fmt.Errorf("GetRelease: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rel == nil {
|
2020-01-10 10:34:21 +01:00
|
|
|
rel = newRel
|
2021-09-23 17:45:36 +02:00
|
|
|
if _, err = db.GetEngine(db.DefaultContext).Insert(rel); err != nil {
|
2017-09-20 07:26:49 +02:00
|
|
|
return fmt.Errorf("InsertOne: %v", err)
|
|
|
|
}
|
|
|
|
} else {
|
2020-01-10 10:34:21 +01:00
|
|
|
rel.Sha1 = newRel.Sha1
|
|
|
|
rel.CreatedUnix = newRel.CreatedUnix
|
|
|
|
rel.NumCommits = newRel.NumCommits
|
2017-09-20 07:26:49 +02:00
|
|
|
rel.IsDraft = false
|
2020-01-10 10:34:21 +01:00
|
|
|
if rel.IsTag && newRel.PublisherID > 0 {
|
|
|
|
rel.PublisherID = newRel.PublisherID
|
2017-09-20 07:26:49 +02:00
|
|
|
}
|
2021-09-23 17:45:36 +02:00
|
|
|
if _, err = db.GetEngine(db.DefaultContext).ID(rel.ID).AllCols().Update(rel); err != nil {
|
2017-09-20 07:26:49 +02:00
|
|
|
return fmt.Errorf("Update: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|