diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2019-09-15 23:28:25 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-09-15 23:28:25 +0800 |
commit | 4959bf1c097917576472f9f66afa0fb615500c07 (patch) | |
tree | 9c2db8d5a91c1e6d97a1e66470347b647d01cfb0 /models/release.go | |
parent | be0f7ff9bf6da5b1642a98ff02f313ba39ee4518 (diff) | |
download | gitea-4959bf1c097917576472f9f66afa0fb615500c07.tar.gz gitea-4959bf1c097917576472f9f66afa0fb615500c07.zip |
Move create release from models to a standalone package (#7539)
* move create release from models to a standalone package
* fix lint
* fix comment year
* fix lint
* fix lint
* fix package import name
* fix vendor
* fix go mod
* some refactors
* fix vendor
* use go1.12 make vendor
* fix vendor
Diffstat (limited to 'models/release.go')
-rw-r--r-- | models/release.go | 124 |
1 files changed, 11 insertions, 113 deletions
diff --git a/models/release.go b/models/release.go index e35a55741a..e3386d074f 100644 --- a/models/release.go +++ b/models/release.go @@ -112,43 +112,20 @@ func IsReleaseExist(repoID int64, tagName string) (bool, error) { return x.Get(&Release{RepoID: repoID, LowerTagName: strings.ToLower(tagName)}) } -func createTag(gitRepo *git.Repository, rel *Release) error { - // Only actual create when publish. - if !rel.IsDraft { - if !gitRepo.IsTagExist(rel.TagName) { - commit, err := gitRepo.GetCommit(rel.Target) - if err != nil { - return fmt.Errorf("GetCommit: %v", err) - } - - // Trim '--' prefix to prevent command line argument vulnerability. - rel.TagName = strings.TrimPrefix(rel.TagName, "--") - if err = gitRepo.CreateTag(rel.TagName, commit.ID.String()); err != nil { - if strings.Contains(err.Error(), "is not a valid tag name") { - return ErrInvalidTagName{rel.TagName} - } - return err - } - rel.LowerTagName = strings.ToLower(rel.TagName) - } - commit, err := gitRepo.GetTagCommit(rel.TagName) - if err != nil { - return fmt.Errorf("GetTagCommit: %v", err) - } +// InsertRelease inserts a release +func InsertRelease(rel *Release) error { + _, err := x.Insert(rel) + return err +} - rel.Sha1 = commit.ID.String() - rel.CreatedUnix = timeutil.TimeStamp(commit.Author.When.Unix()) - rel.NumCommits, err = commit.CommitsCount() - if err != nil { - return fmt.Errorf("CommitsCount: %v", err) - } - } else { - rel.CreatedUnix = timeutil.TimeStampNow() - } - return nil +// UpdateRelease updates all columns of a release +func UpdateRelease(rel *Release) error { + _, err := x.ID(rel.ID).AllCols().Update(rel) + return err } -func addReleaseAttachments(releaseID int64, attachmentUUIDs []string) (err error) { +// AddReleaseAttachments adds a release attachments +func AddReleaseAttachments(releaseID int64, attachmentUUIDs []string) (err error) { // Check attachments var attachments = make([]*Attachment, 0) for _, uuid := range attachmentUUIDs { @@ -173,51 +150,6 @@ func addReleaseAttachments(releaseID int64, attachmentUUIDs []string) (err error return } -// CreateRelease creates a new release of repository. -func CreateRelease(gitRepo *git.Repository, rel *Release, attachmentUUIDs []string) error { - isExist, err := IsReleaseExist(rel.RepoID, rel.TagName) - if err != nil { - return err - } else if isExist { - return ErrReleaseAlreadyExist{rel.TagName} - } - - if err = createTag(gitRepo, rel); err != nil { - return err - } - rel.LowerTagName = strings.ToLower(rel.TagName) - - _, err = x.InsertOne(rel) - if err != nil { - return err - } - - err = addReleaseAttachments(rel.ID, attachmentUUIDs) - if err != nil { - return err - } - - if !rel.IsDraft { - if err := rel.LoadAttributes(); err != nil { - log.Error("LoadAttributes: %v", err) - } else { - mode, _ := AccessLevel(rel.Publisher, rel.Repo) - if err := PrepareWebhooks(rel.Repo, HookEventRelease, &api.ReleasePayload{ - Action: api.HookReleasePublished, - Release: rel.APIFormat(), - Repository: rel.Repo.APIFormat(mode), - Sender: rel.Publisher.APIFormat(), - }); err != nil { - log.Error("PrepareWebhooks: %v", err) - } else { - go HookQueue.Add(rel.Repo.ID) - } - } - } - - return nil -} - // GetRelease returns release by given ID. func GetRelease(repoID int64, tagName string) (*Release, error) { isExist, err := IsReleaseExist(repoID, tagName) @@ -385,40 +317,6 @@ func SortReleases(rels []*Release) { sort.Sort(sorter) } -// UpdateRelease updates information of a release. -func UpdateRelease(doer *User, gitRepo *git.Repository, rel *Release, attachmentUUIDs []string) (err error) { - if err = createTag(gitRepo, rel); err != nil { - return err - } - rel.LowerTagName = strings.ToLower(rel.TagName) - - _, err = x.ID(rel.ID).AllCols().Update(rel) - if err != nil { - return err - } - - err = rel.loadAttributes(x) - if err != nil { - return err - } - - err = addReleaseAttachments(rel.ID, attachmentUUIDs) - - mode, _ := AccessLevel(doer, rel.Repo) - if err1 := PrepareWebhooks(rel.Repo, HookEventRelease, &api.ReleasePayload{ - Action: api.HookReleaseUpdated, - Release: rel.APIFormat(), - Repository: rel.Repo.APIFormat(mode), - Sender: doer.APIFormat(), - }); err1 != nil { - log.Error("PrepareWebhooks: %v", err) - } else { - go HookQueue.Add(rel.Repo.ID) - } - - return err -} - // DeleteReleaseByID deletes a release and corresponding Git tag by given ID. func DeleteReleaseByID(id int64, doer *User, delTag bool) error { rel, err := GetReleaseByID(id) |