Skip to content

Commit

Permalink
improvement some release related code (go-gitea#19867)
Browse files Browse the repository at this point in the history
  • Loading branch information
lunny authored Jun 3, 2022
1 parent 0c759fd commit e09fb30
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 52 deletions.
32 changes: 9 additions & 23 deletions models/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func init() {
func (r *Release) loadAttributes(ctx context.Context) error {
var err error
if r.Repo == nil {
r.Repo, err = repo_model.GetRepositoryByID(r.RepoID)
r.Repo, err = repo_model.GetRepositoryByIDCtx(ctx, r.RepoID)
if err != nil {
return err
}
Expand Down Expand Up @@ -99,24 +99,12 @@ func (r *Release) HTMLURL() string {
}

// IsReleaseExist returns true if release with given tag name already exists.
func IsReleaseExist(repoID int64, tagName string) (bool, error) {
func IsReleaseExist(ctx context.Context, repoID int64, tagName string) (bool, error) {
if len(tagName) == 0 {
return false, nil
}

return db.GetEngine(db.DefaultContext).Get(&Release{RepoID: repoID, LowerTagName: strings.ToLower(tagName)})
}

// InsertRelease inserts a release
func InsertRelease(rel *Release) error {
_, err := db.GetEngine(db.DefaultContext).Insert(rel)
return err
}

// InsertReleasesContext insert releases
func InsertReleasesContext(ctx context.Context, rels []*Release) error {
_, err := db.GetEngine(ctx).Insert(rels)
return err
return db.GetEngine(ctx).Exist(&Release{RepoID: repoID, LowerTagName: strings.ToLower(tagName)})
}

// UpdateRelease updates all columns of a release
Expand Down Expand Up @@ -149,22 +137,20 @@ func AddReleaseAttachments(ctx context.Context, releaseID int64, attachmentUUIDs

// GetRelease returns release by given ID.
func GetRelease(repoID int64, tagName string) (*Release, error) {
isExist, err := IsReleaseExist(repoID, tagName)
rel := &Release{RepoID: repoID, LowerTagName: strings.ToLower(tagName)}
has, err := db.GetEngine(db.DefaultContext).Get(rel)
if err != nil {
return nil, err
} else if !isExist {
} else if !has {
return nil, ErrReleaseNotExist{0, tagName}
}

rel := &Release{RepoID: repoID, LowerTagName: strings.ToLower(tagName)}
_, err = db.GetEngine(db.DefaultContext).Get(rel)
return rel, err
return rel, nil
}

// GetReleaseByID returns release with given ID.
func GetReleaseByID(id int64) (*Release, error) {
func GetReleaseByID(ctx context.Context, id int64) (*Release, error) {
rel := new(Release)
has, err := db.GetEngine(db.DefaultContext).
has, err := db.GetEngine(ctx).
ID(id).
Get(rel)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion models/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -1144,7 +1144,7 @@ func LinkedRepository(a *repo_model.Attachment) (*repo_model.Repository, unit.Ty
}
return repo, unitType, err
} else if a.ReleaseID != 0 {
rel, err := GetReleaseByID(a.ReleaseID)
rel, err := GetReleaseByID(db.DefaultContext, a.ReleaseID)
if err != nil {
return nil, unit.TypeReleases, err
}
Expand Down
19 changes: 10 additions & 9 deletions routers/api/v1/repo/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/web"
"code.gitea.io/gitea/routers/api/v1/utils"
releaseservice "code.gitea.io/gitea/services/release"
release_service "code.gitea.io/gitea/services/release"
)

// GetRelease get a single release of a repository
Expand Down Expand Up @@ -49,7 +49,7 @@ func GetRelease(ctx *context.APIContext) {
// "$ref": "#/responses/notFound"

id := ctx.ParamsInt64(":id")
release, err := models.GetReleaseByID(id)
release, err := models.GetReleaseByID(ctx, id)
if err != nil && !models.IsErrReleaseNotExist(err) {
ctx.Error(http.StatusInternalServerError, "GetReleaseByID", err)
return
Expand Down Expand Up @@ -202,7 +202,7 @@ func CreateRelease(ctx *context.APIContext) {
IsTag: false,
Repo: ctx.Repo.Repository,
}
if err := releaseservice.CreateRelease(ctx.Repo.GitRepo, rel, nil, ""); err != nil {
if err := release_service.CreateRelease(ctx.Repo.GitRepo, rel, nil, ""); err != nil {
if models.IsErrReleaseAlreadyExist(err) {
ctx.Error(http.StatusConflict, "ReleaseAlreadyExist", err)
} else {
Expand All @@ -225,7 +225,7 @@ func CreateRelease(ctx *context.APIContext) {
rel.Repo = ctx.Repo.Repository
rel.Publisher = ctx.Doer

if err = releaseservice.UpdateRelease(ctx.Doer, ctx.Repo.GitRepo, rel, nil, nil, nil); err != nil {
if err = release_service.UpdateRelease(ctx.Doer, ctx.Repo.GitRepo, rel, nil, nil, nil); err != nil {
ctx.Error(http.StatusInternalServerError, "UpdateRelease", err)
return
}
Expand Down Expand Up @@ -271,7 +271,7 @@ func EditRelease(ctx *context.APIContext) {

form := web.GetForm(ctx).(*api.EditReleaseOption)
id := ctx.ParamsInt64(":id")
rel, err := models.GetReleaseByID(id)
rel, err := models.GetReleaseByID(ctx, id)
if err != nil && !models.IsErrReleaseNotExist(err) {
ctx.Error(http.StatusInternalServerError, "GetReleaseByID", err)
return
Expand Down Expand Up @@ -300,12 +300,13 @@ func EditRelease(ctx *context.APIContext) {
if form.IsPrerelease != nil {
rel.IsPrerelease = *form.IsPrerelease
}
if err := releaseservice.UpdateRelease(ctx.Doer, ctx.Repo.GitRepo, rel, nil, nil, nil); err != nil {
if err := release_service.UpdateRelease(ctx.Doer, ctx.Repo.GitRepo, rel, nil, nil, nil); err != nil {
ctx.Error(http.StatusInternalServerError, "UpdateRelease", err)
return
}

rel, err = models.GetReleaseByID(id)
// reload data from database
rel, err = models.GetReleaseByID(ctx, id)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetReleaseByID", err)
return
Expand Down Expand Up @@ -346,7 +347,7 @@ func DeleteRelease(ctx *context.APIContext) {
// "$ref": "#/responses/notFound"

id := ctx.ParamsInt64(":id")
rel, err := models.GetReleaseByID(id)
rel, err := models.GetReleaseByID(ctx, id)
if err != nil && !models.IsErrReleaseNotExist(err) {
ctx.Error(http.StatusInternalServerError, "GetReleaseByID", err)
return
Expand All @@ -356,7 +357,7 @@ func DeleteRelease(ctx *context.APIContext) {
ctx.NotFound()
return
}
if err := releaseservice.DeleteReleaseByID(ctx, id, ctx.Doer, false); err != nil {
if err := release_service.DeleteReleaseByID(ctx, id, ctx.Doer, false); err != nil {
ctx.Error(http.StatusInternalServerError, "DeleteReleaseByID", err)
return
}
Expand Down
4 changes: 2 additions & 2 deletions routers/api/v1/repo/release_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func ListReleaseAttachments(ctx *context.APIContext) {
// "$ref": "#/responses/AttachmentList"

releaseID := ctx.ParamsInt64(":id")
release, err := models.GetReleaseByID(releaseID)
release, err := models.GetReleaseByID(ctx, releaseID)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetReleaseByID", err)
return
Expand Down Expand Up @@ -164,7 +164,7 @@ func CreateReleaseAttachment(ctx *context.APIContext) {

// Check if release exists an load release
releaseID := ctx.ParamsInt64(":id")
release, err := models.GetReleaseByID(releaseID)
release, err := models.GetReleaseByID(ctx, releaseID)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetReleaseByID", err)
return
Expand Down
20 changes: 8 additions & 12 deletions services/release/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@ func createTag(gitRepo *git.Repository, rel *models.Release, msg string) (bool,

// CreateRelease creates a new release of repository.
func CreateRelease(gitRepo *git.Repository, rel *models.Release, attachmentUUIDs []string, msg string) error {
isExist, err := models.IsReleaseExist(rel.RepoID, rel.TagName)
has, err := models.IsReleaseExist(gitRepo.Ctx, rel.RepoID, rel.TagName)
if err != nil {
return err
} else if isExist {
} else if has {
return models.ErrReleaseAlreadyExist{
TagName: rel.TagName,
}
Expand All @@ -126,7 +126,7 @@ func CreateRelease(gitRepo *git.Repository, rel *models.Release, attachmentUUIDs
}

rel.LowerTagName = strings.ToLower(rel.TagName)
if err = models.InsertRelease(rel); err != nil {
if err = db.Insert(gitRepo.Ctx, rel); err != nil {
return err
}

Expand All @@ -143,10 +143,10 @@ func CreateRelease(gitRepo *git.Repository, rel *models.Release, attachmentUUIDs

// CreateNewTag creates a new repository tag
func CreateNewTag(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, commit, tagName, msg string) error {
isExist, err := models.IsReleaseExist(repo.ID, tagName)
has, err := models.IsReleaseExist(ctx, repo.ID, tagName)
if err != nil {
return err
} else if isExist {
} else if has {
return models.ErrTagAlreadyExists{
TagName: tagName,
}
Expand Down Expand Up @@ -174,11 +174,7 @@ func CreateNewTag(ctx context.Context, doer *user_model.User, repo *repo_model.R
return err
}

if err = models.InsertRelease(rel); err != nil {
return err
}

return err
return db.Insert(ctx, rel)
}

// UpdateRelease updates information, attachments of a release and will create tag if it's not a draft and tag not exist.
Expand Down Expand Up @@ -286,12 +282,12 @@ func UpdateRelease(doer *user_model.User, gitRepo *git.Repository, rel *models.R

// DeleteReleaseByID deletes a release and corresponding Git tag by given ID.
func DeleteReleaseByID(ctx context.Context, id int64, doer *user_model.User, delTag bool) error {
rel, err := models.GetReleaseByID(id)
rel, err := models.GetReleaseByID(ctx, id)
if err != nil {
return fmt.Errorf("GetReleaseByID: %v", err)
}

repo, err := repo_model.GetRepositoryByID(rel.RepoID)
repo, err := repo_model.GetRepositoryByIDCtx(ctx, rel.RepoID)
if err != nil {
return fmt.Errorf("GetRepositoryByID: %v", err)
}
Expand Down
8 changes: 4 additions & 4 deletions services/release/release_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func TestRelease_Update(t *testing.T) {
time.Sleep(2 * time.Second) // sleep 2 seconds to ensure a different timestamp
release.Note = "Changed note"
assert.NoError(t, UpdateRelease(user, gitRepo, release, nil, nil, nil))
release, err = models.GetReleaseByID(release.ID)
release, err = models.GetReleaseByID(db.DefaultContext, release.ID)
assert.NoError(t, err)
assert.Equal(t, int64(releaseCreatedUnix), int64(release.CreatedUnix))

Expand All @@ -186,7 +186,7 @@ func TestRelease_Update(t *testing.T) {
time.Sleep(2 * time.Second) // sleep 2 seconds to ensure a different timestamp
release.Title = "Changed title"
assert.NoError(t, UpdateRelease(user, gitRepo, release, nil, nil, nil))
release, err = models.GetReleaseByID(release.ID)
release, err = models.GetReleaseByID(db.DefaultContext, release.ID)
assert.NoError(t, err)
assert.Less(t, int64(releaseCreatedUnix), int64(release.CreatedUnix))

Expand All @@ -211,7 +211,7 @@ func TestRelease_Update(t *testing.T) {
release.Title = "Changed title"
release.Note = "Changed note"
assert.NoError(t, UpdateRelease(user, gitRepo, release, nil, nil, nil))
release, err = models.GetReleaseByID(release.ID)
release, err = models.GetReleaseByID(db.DefaultContext, release.ID)
assert.NoError(t, err)
assert.Equal(t, int64(releaseCreatedUnix), int64(release.CreatedUnix))

Expand All @@ -236,7 +236,7 @@ func TestRelease_Update(t *testing.T) {
tagName := release.TagName

assert.NoError(t, UpdateRelease(user, gitRepo, release, nil, nil, nil))
release, err = models.GetReleaseByID(release.ID)
release, err = models.GetReleaseByID(db.DefaultContext, release.ID)
assert.NoError(t, err)
assert.Equal(t, tagName, release.TagName)

Expand Down
2 changes: 1 addition & 1 deletion services/repository/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ func pushUpdateAddTags(ctx context.Context, repo *repo_model.Repository, gitRepo
}

if len(newReleases) > 0 {
if err = models.InsertReleasesContext(ctx, newReleases); err != nil {
if err = db.Insert(ctx, newReleases); err != nil {
return fmt.Errorf("Insert: %v", err)
}
}
Expand Down

0 comments on commit e09fb30

Please sign in to comment.