summaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2019-09-15 23:28:25 +0800
committerGitHub <noreply@github.com>2019-09-15 23:28:25 +0800
commit4959bf1c097917576472f9f66afa0fb615500c07 (patch)
tree9c2db8d5a91c1e6d97a1e66470347b647d01cfb0 /models
parentbe0f7ff9bf6da5b1642a98ff02f313ba39ee4518 (diff)
downloadgitea-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')
-rw-r--r--models/release.go124
-rw-r--r--models/release_test.go155
-rw-r--r--models/repo_mirror.go6
3 files changed, 17 insertions, 268 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)
diff --git a/models/release_test.go b/models/release_test.go
deleted file mode 100644
index 6da3cd7628..0000000000
--- a/models/release_test.go
+++ /dev/null
@@ -1,155 +0,0 @@
-// Copyright 2018 The Gitea Authors. All rights reserved.
-// Use of this source code is governed by a MIT-style
-// license that can be found in the LICENSE file.
-
-package models
-
-import (
- "testing"
-
- "code.gitea.io/gitea/modules/git"
-
- "github.com/stretchr/testify/assert"
-)
-
-func TestRelease_Create(t *testing.T) {
- assert.NoError(t, PrepareTestDatabase())
-
- user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
- repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
- repoPath := RepoPath(user.Name, repo.Name)
-
- gitRepo, err := git.OpenRepository(repoPath)
- assert.NoError(t, err)
-
- assert.NoError(t, CreateRelease(gitRepo, &Release{
- RepoID: repo.ID,
- PublisherID: user.ID,
- TagName: "v0.1",
- Target: "master",
- Title: "v0.1 is released",
- Note: "v0.1 is released",
- IsDraft: false,
- IsPrerelease: false,
- IsTag: false,
- }, nil))
-
- assert.NoError(t, CreateRelease(gitRepo, &Release{
- RepoID: repo.ID,
- PublisherID: user.ID,
- TagName: "v0.1.1",
- Target: "65f1bf27bc3bf70f64657658635e66094edbcb4d",
- Title: "v0.1.1 is released",
- Note: "v0.1.1 is released",
- IsDraft: false,
- IsPrerelease: false,
- IsTag: false,
- }, nil))
-
- assert.NoError(t, CreateRelease(gitRepo, &Release{
- RepoID: repo.ID,
- PublisherID: user.ID,
- TagName: "v0.1.2",
- Target: "65f1bf2",
- Title: "v0.1.2 is released",
- Note: "v0.1.2 is released",
- IsDraft: false,
- IsPrerelease: false,
- IsTag: false,
- }, nil))
-
- assert.NoError(t, CreateRelease(gitRepo, &Release{
- RepoID: repo.ID,
- PublisherID: user.ID,
- TagName: "v0.1.3",
- Target: "65f1bf2",
- Title: "v0.1.3 is released",
- Note: "v0.1.3 is released",
- IsDraft: true,
- IsPrerelease: false,
- IsTag: false,
- }, nil))
-
- assert.NoError(t, CreateRelease(gitRepo, &Release{
- RepoID: repo.ID,
- PublisherID: user.ID,
- TagName: "v0.1.4",
- Target: "65f1bf2",
- Title: "v0.1.4 is released",
- Note: "v0.1.4 is released",
- IsDraft: false,
- IsPrerelease: true,
- IsTag: false,
- }, nil))
-
- assert.NoError(t, CreateRelease(gitRepo, &Release{
- RepoID: repo.ID,
- PublisherID: user.ID,
- TagName: "v0.1.5",
- Target: "65f1bf2",
- Title: "v0.1.5 is released",
- Note: "v0.1.5 is released",
- IsDraft: false,
- IsPrerelease: false,
- IsTag: true,
- }, nil))
-}
-
-func TestRelease_MirrorDelete(t *testing.T) {
- assert.NoError(t, PrepareTestDatabase())
-
- user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
- repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
- repoPath := RepoPath(user.Name, repo.Name)
- migrationOptions := MigrateRepoOptions{
- Name: "test_mirror",
- Description: "Test mirror",
- IsPrivate: false,
- IsMirror: true,
- RemoteAddr: repoPath,
- Wiki: true,
- SyncReleasesWithTags: true,
- }
- mirror, err := MigrateRepository(user, user, migrationOptions)
- assert.NoError(t, err)
-
- gitRepo, err := git.OpenRepository(repoPath)
- assert.NoError(t, err)
-
- findOptions := FindReleasesOptions{IncludeDrafts: true, IncludeTags: true}
- initCount, err := GetReleaseCountByRepoID(mirror.ID, findOptions)
- assert.NoError(t, err)
-
- assert.NoError(t, CreateRelease(gitRepo, &Release{
- RepoID: repo.ID,
- PublisherID: user.ID,
- TagName: "v0.2",
- Target: "master",
- Title: "v0.2 is released",
- Note: "v0.2 is released",
- IsDraft: false,
- IsPrerelease: false,
- IsTag: true,
- }, nil))
-
- err = mirror.GetMirror()
- assert.NoError(t, err)
-
- _, ok := mirror.Mirror.runSync()
- assert.True(t, ok)
-
- count, err := GetReleaseCountByRepoID(mirror.ID, findOptions)
- assert.NoError(t, err)
- assert.EqualValues(t, initCount+1, count)
-
- release, err := GetRelease(repo.ID, "v0.2")
- assert.NoError(t, err)
- assert.NoError(t, DeleteReleaseByID(release.ID, user, true))
-
- _, ok = mirror.Mirror.runSync()
- assert.True(t, ok)
-
- count, err = GetReleaseCountByRepoID(mirror.ID, findOptions)
- assert.NoError(t, err)
- assert.EqualValues(t, initCount, count)
-}
diff --git a/models/repo_mirror.go b/models/repo_mirror.go
index 9801c05ae4..e1c1aefc39 100644
--- a/models/repo_mirror.go
+++ b/models/repo_mirror.go
@@ -318,6 +318,12 @@ func (m *Mirror) runSync() ([]*mirrorSyncResult, bool) {
return parseRemoteUpdateOutput(output), true
}
+// RunMirrorSync will invoke Mirror's runSync
+func RunMirrorSync(mirror *Mirror) bool {
+ _, ok := mirror.runSync()
+ return ok
+}
+
func getMirrorByRepoID(e Engine, repoID int64) (*Mirror, error) {
m := &Mirror{RepoID: repoID}
has, err := e.Get(m)