2014-04-14 11:27:25 +05:30
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
2019-09-22 14:35:48 +05:30
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2014-04-14 11:27:25 +05:30
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2021-03-22 21:39:51 +05:30
|
|
|
"errors"
|
2015-11-20 13:08:41 +05:30
|
|
|
"fmt"
|
2014-06-13 03:17:23 +05:30
|
|
|
"sort"
|
2014-04-14 11:27:25 +05:30
|
|
|
"strings"
|
|
|
|
|
2016-12-31 22:21:22 +05:30
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2019-10-14 11:40:42 +05:30
|
|
|
"code.gitea.io/gitea/modules/structs"
|
2019-08-15 20:16:21 +05:30
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
2021-06-17 14:28:10 +05:30
|
|
|
"code.gitea.io/gitea/modules/util"
|
2019-03-27 15:03:00 +05:30
|
|
|
|
2019-06-23 20:52:43 +05:30
|
|
|
"xorm.io/builder"
|
2014-04-14 11:27:25 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
// Release represents a release of repository.
|
|
|
|
type Release struct {
|
2017-01-06 07:21:15 +05:30
|
|
|
ID int64 `xorm:"pk autoincr"`
|
2017-01-06 20:44:33 +05:30
|
|
|
RepoID int64 `xorm:"INDEX UNIQUE(n)"`
|
2016-12-31 22:21:22 +05:30
|
|
|
Repo *Repository `xorm:"-"`
|
2017-01-06 20:44:33 +05:30
|
|
|
PublisherID int64 `xorm:"INDEX"`
|
|
|
|
Publisher *User `xorm:"-"`
|
|
|
|
TagName string `xorm:"INDEX UNIQUE(n)"`
|
2019-10-05 16:39:27 +05:30
|
|
|
OriginalAuthor string
|
|
|
|
OriginalAuthorID int64 `xorm:"index"`
|
2014-04-14 11:27:25 +05:30
|
|
|
LowerTagName string
|
2014-06-12 18:40:39 +05:30
|
|
|
Target string
|
2014-06-13 03:17:23 +05:30
|
|
|
Title string
|
2014-06-12 18:40:39 +05:30
|
|
|
Sha1 string `xorm:"VARCHAR(40)"`
|
2015-12-10 07:16:05 +05:30
|
|
|
NumCommits int64
|
2019-08-15 20:16:21 +05:30
|
|
|
NumCommitsBehind int64 `xorm:"-"`
|
|
|
|
Note string `xorm:"TEXT"`
|
2020-08-23 20:33:18 +05:30
|
|
|
RenderedNote string `xorm:"-"`
|
2019-08-15 20:16:21 +05:30
|
|
|
IsDraft bool `xorm:"NOT NULL DEFAULT false"`
|
|
|
|
IsPrerelease bool `xorm:"NOT NULL DEFAULT false"`
|
|
|
|
IsTag bool `xorm:"NOT NULL DEFAULT false"`
|
|
|
|
Attachments []*Attachment `xorm:"-"`
|
|
|
|
CreatedUnix timeutil.TimeStamp `xorm:"INDEX"`
|
2015-08-24 18:31:23 +05:30
|
|
|
}
|
|
|
|
|
2016-12-31 22:21:22 +05:30
|
|
|
func (r *Release) loadAttributes(e Engine) error {
|
|
|
|
var err error
|
|
|
|
if r.Repo == nil {
|
|
|
|
r.Repo, err = GetRepositoryByID(r.RepoID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if r.Publisher == nil {
|
2019-06-13 01:11:28 +05:30
|
|
|
r.Publisher, err = getUserByID(e, r.PublisherID)
|
2016-12-31 22:21:22 +05:30
|
|
|
if err != nil {
|
2020-11-03 04:40:22 +05:30
|
|
|
if IsErrUserNotExist(err) {
|
|
|
|
r.Publisher = NewGhostUser()
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
2016-12-31 22:21:22 +05:30
|
|
|
}
|
|
|
|
}
|
2019-06-13 01:11:28 +05:30
|
|
|
return getReleaseAttachments(e, r)
|
2016-12-31 22:21:22 +05:30
|
|
|
}
|
|
|
|
|
2017-03-15 06:22:01 +05:30
|
|
|
// LoadAttributes load repo and publisher attributes for a release
|
2016-12-31 22:21:22 +05:30
|
|
|
func (r *Release) LoadAttributes() error {
|
|
|
|
return r.loadAttributes(x)
|
|
|
|
}
|
|
|
|
|
|
|
|
// APIURL the api url for a release. release must have attributes loaded
|
|
|
|
func (r *Release) APIURL() string {
|
2019-09-19 05:53:45 +05:30
|
|
|
return fmt.Sprintf("%sapi/v1/repos/%s/releases/%d",
|
2016-12-31 22:21:22 +05:30
|
|
|
setting.AppURL, r.Repo.FullName(), r.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ZipURL the zip url for a release. release must have attributes loaded
|
|
|
|
func (r *Release) ZipURL() string {
|
|
|
|
return fmt.Sprintf("%s/archive/%s.zip", r.Repo.HTMLURL(), r.TagName)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TarURL the tar.gz url for a release. release must have attributes loaded
|
|
|
|
func (r *Release) TarURL() string {
|
|
|
|
return fmt.Sprintf("%s/archive/%s.tar.gz", r.Repo.HTMLURL(), r.TagName)
|
|
|
|
}
|
|
|
|
|
2020-04-18 20:17:15 +05:30
|
|
|
// HTMLURL the url for a release on the web UI. release must have attributes loaded
|
|
|
|
func (r *Release) HTMLURL() string {
|
|
|
|
return fmt.Sprintf("%s/releases/tag/%s", r.Repo.HTMLURL(), r.TagName)
|
|
|
|
}
|
|
|
|
|
2014-04-14 11:27:25 +05:30
|
|
|
// IsReleaseExist returns true if release with given tag name already exists.
|
2015-11-16 10:22:46 +05:30
|
|
|
func IsReleaseExist(repoID int64, tagName string) (bool, error) {
|
2014-04-14 11:27:25 +05:30
|
|
|
if len(tagName) == 0 {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2015-11-16 10:22:46 +05:30
|
|
|
return x.Get(&Release{RepoID: repoID, LowerTagName: strings.ToLower(tagName)})
|
2014-04-14 11:27:25 +05:30
|
|
|
}
|
|
|
|
|
2019-09-15 20:58:25 +05:30
|
|
|
// InsertRelease inserts a release
|
|
|
|
func InsertRelease(rel *Release) error {
|
|
|
|
_, err := x.Insert(rel)
|
|
|
|
return err
|
|
|
|
}
|
2014-06-13 03:17:23 +05:30
|
|
|
|
2020-02-03 14:17:04 +05:30
|
|
|
// InsertReleasesContext insert releases
|
|
|
|
func InsertReleasesContext(ctx DBContext, rels []*Release) error {
|
|
|
|
_, err := ctx.e.Insert(rels)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-09-15 20:58:25 +05:30
|
|
|
// UpdateRelease updates all columns of a release
|
2020-02-03 14:17:04 +05:30
|
|
|
func UpdateRelease(ctx DBContext, rel *Release) error {
|
|
|
|
_, err := ctx.e.ID(rel.ID).AllCols().Update(rel)
|
2019-09-15 20:58:25 +05:30
|
|
|
return err
|
2014-06-13 03:17:23 +05:30
|
|
|
}
|
|
|
|
|
2019-09-15 20:58:25 +05:30
|
|
|
// AddReleaseAttachments adds a release attachments
|
2021-03-22 21:39:51 +05:30
|
|
|
func AddReleaseAttachments(ctx DBContext, releaseID int64, attachmentUUIDs []string) (err error) {
|
2017-01-15 20:27:00 +05:30
|
|
|
// Check attachments
|
2021-03-22 21:39:51 +05:30
|
|
|
attachments, err := getAttachmentsByUUIDs(ctx.e, attachmentUUIDs)
|
2019-12-11 05:31:52 +05:30
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("GetAttachmentsByUUIDs [uuids: %v]: %v", attachmentUUIDs, err)
|
2017-01-15 20:27:00 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
for i := range attachments {
|
2021-03-22 21:39:51 +05:30
|
|
|
if attachments[i].ReleaseID != 0 {
|
|
|
|
return errors.New("release permission denied")
|
|
|
|
}
|
2017-01-15 20:27:00 +05:30
|
|
|
attachments[i].ReleaseID = releaseID
|
|
|
|
// No assign value could be 0, so ignore AllCols().
|
2021-03-22 21:39:51 +05:30
|
|
|
if _, err = ctx.e.ID(attachments[i].ID).Update(attachments[i]); err != nil {
|
2017-01-15 20:27:00 +05:30
|
|
|
return fmt.Errorf("update attachment [%d]: %v", attachments[i].ID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-06-13 03:17:23 +05:30
|
|
|
// GetRelease returns release by given ID.
|
2015-11-16 10:22:46 +05:30
|
|
|
func GetRelease(repoID int64, tagName string) (*Release, error) {
|
|
|
|
isExist, err := IsReleaseExist(repoID, tagName)
|
2014-06-13 03:17:23 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if !isExist {
|
2015-11-20 13:08:41 +05:30
|
|
|
return nil, ErrReleaseNotExist{0, tagName}
|
2014-06-13 03:17:23 +05:30
|
|
|
}
|
2014-04-14 11:27:25 +05:30
|
|
|
|
2015-11-16 10:22:46 +05:30
|
|
|
rel := &Release{RepoID: repoID, LowerTagName: strings.ToLower(tagName)}
|
2014-06-21 10:21:41 +05:30
|
|
|
_, err = x.Get(rel)
|
2014-06-13 03:17:23 +05:30
|
|
|
return rel, err
|
|
|
|
}
|
|
|
|
|
2015-11-20 13:08:41 +05:30
|
|
|
// GetReleaseByID returns release with given ID.
|
|
|
|
func GetReleaseByID(id int64) (*Release, error) {
|
|
|
|
rel := new(Release)
|
2016-11-10 20:46:32 +05:30
|
|
|
has, err := x.
|
2017-10-05 10:13:04 +05:30
|
|
|
ID(id).
|
2016-11-10 20:46:32 +05:30
|
|
|
Get(rel)
|
2015-11-20 13:08:41 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if !has {
|
|
|
|
return nil, ErrReleaseNotExist{id, ""}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rel, nil
|
|
|
|
}
|
|
|
|
|
2017-06-29 20:41:38 +05:30
|
|
|
// FindReleasesOptions describes the conditions to Find releases
|
|
|
|
type FindReleasesOptions struct {
|
2020-01-25 00:30:29 +05:30
|
|
|
ListOptions
|
2017-06-29 20:41:38 +05:30
|
|
|
IncludeDrafts bool
|
2017-09-20 10:56:49 +05:30
|
|
|
IncludeTags bool
|
2021-06-17 14:28:10 +05:30
|
|
|
IsPreRelease util.OptionalBool
|
|
|
|
IsDraft util.OptionalBool
|
2017-06-29 20:41:38 +05:30
|
|
|
TagNames []string
|
2014-06-13 03:17:23 +05:30
|
|
|
}
|
|
|
|
|
2017-06-29 20:41:38 +05:30
|
|
|
func (opts *FindReleasesOptions) toConds(repoID int64) builder.Cond {
|
2021-03-15 00:22:12 +05:30
|
|
|
cond := builder.NewCond()
|
2017-06-28 20:17:00 +05:30
|
|
|
cond = cond.And(builder.Eq{"repo_id": repoID})
|
|
|
|
|
2017-06-29 20:41:38 +05:30
|
|
|
if !opts.IncludeDrafts {
|
|
|
|
cond = cond.And(builder.Eq{"is_draft": false})
|
2017-06-28 20:17:00 +05:30
|
|
|
}
|
2017-09-20 10:56:49 +05:30
|
|
|
if !opts.IncludeTags {
|
|
|
|
cond = cond.And(builder.Eq{"is_tag": false})
|
|
|
|
}
|
2017-06-29 20:41:38 +05:30
|
|
|
if len(opts.TagNames) > 0 {
|
|
|
|
cond = cond.And(builder.In("tag_name", opts.TagNames))
|
|
|
|
}
|
2021-06-17 14:28:10 +05:30
|
|
|
if !opts.IsPreRelease.IsNone() {
|
|
|
|
cond = cond.And(builder.Eq{"is_prerelease": opts.IsPreRelease.IsTrue()})
|
|
|
|
}
|
|
|
|
if !opts.IsDraft.IsNone() {
|
|
|
|
cond = cond.And(builder.Eq{"is_draft": opts.IsDraft.IsTrue()})
|
|
|
|
}
|
2017-06-29 20:41:38 +05:30
|
|
|
return cond
|
2017-06-28 20:17:00 +05:30
|
|
|
}
|
|
|
|
|
2017-06-29 20:41:38 +05:30
|
|
|
// GetReleasesByRepoID returns a list of releases of repository.
|
2020-01-25 00:30:29 +05:30
|
|
|
func GetReleasesByRepoID(repoID int64, opts FindReleasesOptions) ([]*Release, error) {
|
|
|
|
sess := x.
|
|
|
|
Desc("created_unix", "id").
|
|
|
|
Where(opts.toConds(repoID))
|
|
|
|
|
|
|
|
if opts.PageSize != 0 {
|
|
|
|
sess = opts.setSessionPagination(sess)
|
2017-06-29 20:41:38 +05:30
|
|
|
}
|
|
|
|
|
2020-01-25 00:30:29 +05:30
|
|
|
rels := make([]*Release, 0, opts.PageSize)
|
|
|
|
return rels, sess.Find(&rels)
|
2017-01-06 07:21:15 +05:30
|
|
|
}
|
|
|
|
|
2021-06-17 14:28:10 +05:30
|
|
|
// CountReleasesByRepoID returns a number of releases matching FindReleaseOptions and RepoID.
|
|
|
|
func CountReleasesByRepoID(repoID int64, opts FindReleasesOptions) (int64, error) {
|
|
|
|
return x.Where(opts.toConds(repoID)).Count(new(Release))
|
|
|
|
}
|
|
|
|
|
2020-04-18 20:17:15 +05:30
|
|
|
// GetLatestReleaseByRepoID returns the latest release for a repository
|
|
|
|
func GetLatestReleaseByRepoID(repoID int64) (*Release, error) {
|
|
|
|
cond := builder.NewCond().
|
|
|
|
And(builder.Eq{"repo_id": repoID}).
|
|
|
|
And(builder.Eq{"is_draft": false}).
|
|
|
|
And(builder.Eq{"is_prerelease": false}).
|
|
|
|
And(builder.Eq{"is_tag": false})
|
|
|
|
|
|
|
|
rel := new(Release)
|
|
|
|
has, err := x.
|
|
|
|
Desc("created_unix", "id").
|
|
|
|
Where(cond).
|
|
|
|
Get(rel)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if !has {
|
|
|
|
return nil, ErrReleaseNotExist{0, "latest"}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rel, nil
|
|
|
|
}
|
|
|
|
|
2019-01-07 04:07:30 +05:30
|
|
|
// GetReleasesByRepoIDAndNames returns a list of releases of repository according repoID and tagNames.
|
2020-02-03 14:17:04 +05:30
|
|
|
func GetReleasesByRepoIDAndNames(ctx DBContext, repoID int64, tagNames []string) (rels []*Release, err error) {
|
|
|
|
err = ctx.e.
|
2019-01-07 04:07:30 +05:30
|
|
|
In("tag_name", tagNames).
|
2020-02-03 14:17:04 +05:30
|
|
|
Desc("created_unix").
|
2019-01-07 04:07:30 +05:30
|
|
|
Find(&rels, Release{RepoID: repoID})
|
|
|
|
return rels, err
|
|
|
|
}
|
|
|
|
|
2017-06-29 20:41:38 +05:30
|
|
|
// GetReleaseCountByRepoID returns the count of releases of repository
|
|
|
|
func GetReleaseCountByRepoID(repoID int64, opts FindReleasesOptions) (int64, error) {
|
|
|
|
return x.Where(opts.toConds(repoID)).Count(&Release{})
|
|
|
|
}
|
|
|
|
|
2017-01-15 20:27:00 +05:30
|
|
|
type releaseMetaSearch struct {
|
2017-01-17 11:28:58 +05:30
|
|
|
ID []int64
|
|
|
|
Rel []*Release
|
2017-01-15 20:27:00 +05:30
|
|
|
}
|
2017-01-17 11:28:58 +05:30
|
|
|
|
2017-01-15 20:27:00 +05:30
|
|
|
func (s releaseMetaSearch) Len() int {
|
|
|
|
return len(s.ID)
|
|
|
|
}
|
2021-03-15 00:22:12 +05:30
|
|
|
|
2017-01-15 20:27:00 +05:30
|
|
|
func (s releaseMetaSearch) Swap(i, j int) {
|
|
|
|
s.ID[i], s.ID[j] = s.ID[j], s.ID[i]
|
|
|
|
s.Rel[i], s.Rel[j] = s.Rel[j], s.Rel[i]
|
|
|
|
}
|
2021-03-15 00:22:12 +05:30
|
|
|
|
2017-01-15 20:27:00 +05:30
|
|
|
func (s releaseMetaSearch) Less(i, j int) bool {
|
|
|
|
return s.ID[i] < s.ID[j]
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetReleaseAttachments retrieves the attachments for releases
|
2017-01-17 11:28:58 +05:30
|
|
|
func GetReleaseAttachments(rels ...*Release) (err error) {
|
2019-06-13 01:11:28 +05:30
|
|
|
return getReleaseAttachments(x, rels...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getReleaseAttachments(e Engine, rels ...*Release) (err error) {
|
2017-01-15 20:27:00 +05:30
|
|
|
if len(rels) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-01-17 11:28:58 +05:30
|
|
|
// To keep this efficient as possible sort all releases by id,
|
2017-01-15 20:27:00 +05:30
|
|
|
// select attachments by release id,
|
|
|
|
// then merge join them
|
|
|
|
|
|
|
|
// Sort
|
2021-03-15 00:22:12 +05:30
|
|
|
sortedRels := releaseMetaSearch{ID: make([]int64, len(rels)), Rel: make([]*Release, len(rels))}
|
2017-01-17 11:28:58 +05:30
|
|
|
var attachments []*Attachment
|
2017-01-15 20:27:00 +05:30
|
|
|
for index, element := range rels {
|
|
|
|
element.Attachments = []*Attachment{}
|
|
|
|
sortedRels.ID[index] = element.ID
|
|
|
|
sortedRels.Rel[index] = element
|
|
|
|
}
|
|
|
|
sort.Sort(sortedRels)
|
|
|
|
|
|
|
|
// Select attachments
|
2019-06-13 01:11:28 +05:30
|
|
|
err = e.
|
2021-03-17 14:55:49 +05:30
|
|
|
Asc("release_id", "name").
|
2017-01-15 20:27:00 +05:30
|
|
|
In("release_id", sortedRels.ID).
|
|
|
|
Find(&attachments, Attachment{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// merge join
|
2021-03-15 00:22:12 +05:30
|
|
|
currentIndex := 0
|
2017-01-15 20:27:00 +05:30
|
|
|
for _, attachment := range attachments {
|
|
|
|
for sortedRels.ID[currentIndex] < attachment.ReleaseID {
|
|
|
|
currentIndex++
|
|
|
|
}
|
|
|
|
sortedRels.Rel[currentIndex].Attachments = append(sortedRels.Rel[currentIndex].Attachments, attachment)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-11-25 13:41:12 +05:30
|
|
|
type releaseSorter struct {
|
2014-06-13 03:17:23 +05:30
|
|
|
rels []*Release
|
|
|
|
}
|
|
|
|
|
2016-11-25 13:41:12 +05:30
|
|
|
func (rs *releaseSorter) Len() int {
|
2014-06-13 03:17:23 +05:30
|
|
|
return len(rs.rels)
|
|
|
|
}
|
|
|
|
|
2016-11-25 13:41:12 +05:30
|
|
|
func (rs *releaseSorter) Less(i, j int) bool {
|
2014-06-13 03:17:23 +05:30
|
|
|
diffNum := rs.rels[i].NumCommits - rs.rels[j].NumCommits
|
|
|
|
if diffNum != 0 {
|
|
|
|
return diffNum > 0
|
2014-04-14 11:27:25 +05:30
|
|
|
}
|
2017-12-11 10:07:04 +05:30
|
|
|
return rs.rels[i].CreatedUnix > rs.rels[j].CreatedUnix
|
2014-06-13 03:17:23 +05:30
|
|
|
}
|
2014-04-14 11:27:25 +05:30
|
|
|
|
2016-11-25 13:41:12 +05:30
|
|
|
func (rs *releaseSorter) Swap(i, j int) {
|
2014-06-13 03:17:23 +05:30
|
|
|
rs.rels[i], rs.rels[j] = rs.rels[j], rs.rels[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
// SortReleases sorts releases by number of commits and created time.
|
|
|
|
func SortReleases(rels []*Release) {
|
2016-11-25 13:41:12 +05:30
|
|
|
sorter := &releaseSorter{rels: rels}
|
2014-06-13 03:17:23 +05:30
|
|
|
sort.Sort(sorter)
|
|
|
|
}
|
|
|
|
|
2019-09-30 21:40:00 +05:30
|
|
|
// DeleteReleaseByID deletes a release from database by given ID.
|
|
|
|
func DeleteReleaseByID(id int64) error {
|
|
|
|
_, err := x.ID(id).Delete(new(Release))
|
|
|
|
return err
|
2015-11-20 13:08:41 +05:30
|
|
|
}
|
2017-09-20 10:56:49 +05:30
|
|
|
|
2019-10-14 11:40:42 +05:30
|
|
|
// UpdateReleasesMigrationsByType updates all migrated repositories' releases from gitServiceType to replace originalAuthorID to posterID
|
2019-10-17 07:36:28 +05:30
|
|
|
func UpdateReleasesMigrationsByType(gitServiceType structs.GitServiceType, originalAuthorID string, posterID int64) error {
|
2019-10-14 11:40:42 +05:30
|
|
|
_, err := x.Table("release").
|
|
|
|
Where("repo_id IN (SELECT id FROM repository WHERE original_service_type = ?)", gitServiceType).
|
|
|
|
And("original_author_id = ?", originalAuthorID).
|
|
|
|
Update(map[string]interface{}{
|
|
|
|
"publisher_id": posterID,
|
|
|
|
"original_author": "",
|
|
|
|
"original_author_id": 0,
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|