2014-04-27 12:35:13 +05:30
|
|
|
// 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 07:57:13 +05:30
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2014-06-24 01:52:34 +05:30
|
|
|
"fmt"
|
2014-04-11 07:57:13 +05:30
|
|
|
"strings"
|
2017-02-25 20:24:40 +05:30
|
|
|
)
|
2014-06-28 21:26:41 +05:30
|
|
|
|
2019-12-26 16:59:45 +05:30
|
|
|
// PushUpdateDeleteTags updates a number of delete tags
|
|
|
|
func PushUpdateDeleteTags(repo *Repository, tags []string) error {
|
|
|
|
sess := x.NewSession()
|
|
|
|
defer sess.Close()
|
|
|
|
if err := sess.Begin(); err != nil {
|
|
|
|
return fmt.Errorf("Unable to begin sess in PushUpdateDeleteTags: %v", err)
|
|
|
|
}
|
|
|
|
if err := pushUpdateDeleteTags(sess, repo, tags); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return sess.Commit()
|
|
|
|
}
|
|
|
|
|
2020-02-03 14:17:04 +05:30
|
|
|
// PushUpdateDeleteTagsContext updates a number of delete tags with context
|
|
|
|
func PushUpdateDeleteTagsContext(ctx DBContext, repo *Repository, tags []string) error {
|
|
|
|
return pushUpdateDeleteTags(ctx.e, repo, tags)
|
|
|
|
}
|
|
|
|
|
2019-12-26 16:59:45 +05:30
|
|
|
func pushUpdateDeleteTags(e Engine, repo *Repository, tags []string) error {
|
|
|
|
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 19:48:07 +05:30
|
|
|
Cols("is_draft", "num_commits", "sha1").
|
|
|
|
Update(&Release{
|
|
|
|
IsDraft: true,
|
|
|
|
}); err != nil {
|
2019-12-26 16:59:45 +05:30
|
|
|
return fmt.Errorf("Update: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-24 12:43:26 +05:30
|
|
|
// PushUpdateDeleteTag must be called for any push actions to delete tag
|
|
|
|
func PushUpdateDeleteTag(repo *Repository, tagName string) error {
|
2017-09-20 10:56:49 +05:30
|
|
|
rel, err := GetRelease(repo.ID, tagName)
|
|
|
|
if err != nil {
|
|
|
|
if IsErrReleaseNotExist(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("GetRelease: %v", err)
|
|
|
|
}
|
|
|
|
if rel.IsTag {
|
2017-10-05 10:13:04 +05:30
|
|
|
if _, err = x.ID(rel.ID).Delete(new(Release)); err != nil {
|
2017-09-20 10:56:49 +05:30
|
|
|
return fmt.Errorf("Delete: %v", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
rel.IsDraft = true
|
|
|
|
rel.NumCommits = 0
|
|
|
|
rel.Sha1 = ""
|
2017-10-05 10:13:04 +05:30
|
|
|
if _, err = x.ID(rel.ID).AllCols().Update(rel); err != nil {
|
2017-09-20 10:56:49 +05:30
|
|
|
return fmt.Errorf("Update: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-10 15:04:21 +05:30
|
|
|
// SaveOrUpdateTag must be called for any push actions to add tag
|
|
|
|
func SaveOrUpdateTag(repo *Repository, newRel *Release) error {
|
|
|
|
rel, err := GetRelease(repo.ID, newRel.TagName)
|
2017-09-20 10:56:49 +05:30
|
|
|
if err != nil && !IsErrReleaseNotExist(err) {
|
|
|
|
return fmt.Errorf("GetRelease: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rel == nil {
|
2020-01-10 15:04:21 +05:30
|
|
|
rel = newRel
|
|
|
|
if _, err = x.Insert(rel); err != nil {
|
2017-09-20 10:56:49 +05:30
|
|
|
return fmt.Errorf("InsertOne: %v", err)
|
|
|
|
}
|
|
|
|
} else {
|
2020-01-10 15:04:21 +05:30
|
|
|
rel.Sha1 = newRel.Sha1
|
|
|
|
rel.CreatedUnix = newRel.CreatedUnix
|
|
|
|
rel.NumCommits = newRel.NumCommits
|
2017-09-20 10:56:49 +05:30
|
|
|
rel.IsDraft = false
|
2020-01-10 15:04:21 +05:30
|
|
|
if rel.IsTag && newRel.PublisherID > 0 {
|
|
|
|
rel.PublisherID = newRel.PublisherID
|
2017-09-20 10:56:49 +05:30
|
|
|
}
|
2017-10-05 10:13:04 +05:30
|
|
|
if _, err = x.ID(rel.ID).AllCols().Update(rel); err != nil {
|
2017-09-20 10:56:49 +05:30
|
|
|
return fmt.Errorf("Update: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|