Skip to content

Commit

Permalink
Use *Context
Browse files Browse the repository at this point in the history
  • Loading branch information
lunny committed Sep 18, 2021
1 parent d52f451 commit 153793b
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 47 deletions.
2 changes: 1 addition & 1 deletion models/avatar.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func HashedAvatarLink(email string, size int) string {
Hash: sum,
}
// OK we're going to open a session just because I think that that might hide away any problems with postgres reporting errors
if err := db.WithTx(func(ctx db.Context) error {
if err := db.WithTx(func(ctx *db.Context) error {
has, err := ctx.Engine().Where("email = ? AND hash = ?", emailHash.Email, emailHash.Hash).Get(new(EmailHash))
if has || err != nil {
// Seriously we don't care about any DB problems just return the lowerEmail - we expect the transaction to fail most of the time
Expand Down
16 changes: 8 additions & 8 deletions models/db/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,30 +42,30 @@ type Committer interface {
}

// TxContext represents a transaction Context
func TxContext() (Context, Committer, error) {
func TxContext() (*Context, Committer, error) {
sess := x.NewSession()
if err := sess.Begin(); err != nil {
sess.Close()
return Context{}, nil, err
return nil, nil, err
}

return Context{sess}, sess, nil
return &Context{sess}, sess, nil
}

// WithContext represents executing database operations
func WithContext(f func(ctx Context) error) error {
return f(Context{x})
func WithContext(f func(ctx *Context) error) error {
return f(&Context{x})
}

// WithTx represents executing database operations on a transaction
func WithTx(f func(ctx Context) error) error {
func WithTx(f func(ctx *Context) error) error {
sess := x.NewSession()
defer sess.Close()
if err := sess.Begin(); err != nil {
return err
}

if err := f(Context{sess}); err != nil {
if err := f(&Context{sess}); err != nil {
return err
}

Expand All @@ -80,7 +80,7 @@ func Iterate(ctx *Context, tableBean interface{}, cond builder.Cond, fun func(id
}

// Insert inserts records into database
func Insert(ctx Context, beans ...interface{}) error {
func Insert(ctx *Context, beans ...interface{}) error {
_, err := ctx.e.Insert(beans...)
return err
}
8 changes: 4 additions & 4 deletions modules/repository/adopt.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func AdoptRepository(doer, u *models.User, opts models.CreateRepoOptions) (*mode
IsEmpty: !opts.AutoInit,
}

if err := db.WithTx(func(ctx db.Context) error {
if err := db.WithTx(func(ctx *db.Context) error {
repoPath := models.RepoPath(u.Name, repo.Name)
isExist, err := util.IsExist(repoPath)
if err != nil {
Expand All @@ -61,16 +61,16 @@ func AdoptRepository(doer, u *models.User, opts models.CreateRepoOptions) (*mode
}
}

if err := models.CreateRepository(&ctx, doer, u, repo, true); err != nil {
if err := models.CreateRepository(ctx, doer, u, repo, true); err != nil {
return err
}
if err := adoptRepository(&ctx, repoPath, doer, repo, opts); err != nil {
if err := adoptRepository(ctx, repoPath, doer, repo, opts); err != nil {
return fmt.Errorf("createDelegateHooks: %v", err)
}

// Initialize Issue Labels if selected
if len(opts.IssueLabels) > 0 {
if err := models.InitializeLabels(&ctx, repo.ID, opts.IssueLabels, false); err != nil {
if err := models.InitializeLabels(ctx, repo.ID, opts.IssueLabels, false); err != nil {
return fmt.Errorf("InitializeLabels: %v", err)
}
}
Expand Down
8 changes: 4 additions & 4 deletions modules/repository/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ func CreateRepository(doer, u *models.User, opts models.CreateRepoOptions) (*mod

var rollbackRepo *models.Repository

if err := db.WithTx(func(ctx db.Context) error {
if err := models.CreateRepository(&ctx, doer, u, repo, false); err != nil {
if err := db.WithTx(func(ctx *db.Context) error {
if err := models.CreateRepository(ctx, doer, u, repo, false); err != nil {
return err
}

Expand Down Expand Up @@ -86,7 +86,7 @@ func CreateRepository(doer, u *models.User, opts models.CreateRepoOptions) (*mod
}
}

if err = initRepository(&ctx, repoPath, doer, repo, opts); err != nil {
if err = initRepository(ctx, repoPath, doer, repo, opts); err != nil {
if err2 := util.RemoveAll(repoPath); err2 != nil {
log.Error("initRepository: %v", err)
return fmt.Errorf(
Expand All @@ -97,7 +97,7 @@ func CreateRepository(doer, u *models.User, opts models.CreateRepoOptions) (*mod

// Initialize Issue Labels if selected
if len(opts.IssueLabels) > 0 {
if err = models.InitializeLabels(&ctx, repo.ID, opts.IssueLabels, false); err != nil {
if err = models.InitializeLabels(ctx, repo.ID, opts.IssueLabels, false); err != nil {
rollbackRepo = repo
rollbackRepo.OwnerID = u.ID
return fmt.Errorf("InitializeLabels: %v", err)
Expand Down
16 changes: 8 additions & 8 deletions modules/repository/fork.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,17 @@ func ForkRepository(doer, owner *models.User, opts models.ForkRepoOptions) (_ *m
panic(panicErr)
}()

err = db.WithTx(func(ctx db.Context) error {
if err = models.CreateRepository(&ctx, doer, owner, repo, false); err != nil {
err = db.WithTx(func(ctx *db.Context) error {
if err = models.CreateRepository(ctx, doer, owner, repo, false); err != nil {
return err
}

if err = models.IncrementRepoForkNum(&ctx, opts.BaseRepo.ID); err != nil {
if err = models.IncrementRepoForkNum(ctx, opts.BaseRepo.ID); err != nil {
return err
}

// copy lfs files failure should not be ignored
if err = models.CopyLFS(&ctx, repo, opts.BaseRepo); err != nil {
if err = models.CopyLFS(ctx, repo, opts.BaseRepo); err != nil {
return err
}

Expand Down Expand Up @@ -136,8 +136,8 @@ func ForkRepository(doer, owner *models.User, opts models.ForkRepoOptions) (_ *m

// ConvertForkToNormalRepository convert the provided repo from a forked repo to normal repo
func ConvertForkToNormalRepository(repo *models.Repository) error {
err := db.WithTx(func(ctx db.Context) error {
repo, err := models.GetRepositoryByIDCtx(&ctx, repo.ID)
err := db.WithTx(func(ctx *db.Context) error {
repo, err := models.GetRepositoryByIDCtx(ctx, repo.ID)
if err != nil {
return err
}
Expand All @@ -146,15 +146,15 @@ func ConvertForkToNormalRepository(repo *models.Repository) error {
return nil
}

if err := models.DecrementRepoForkNum(&ctx, repo.ForkID); err != nil {
if err := models.DecrementRepoForkNum(ctx, repo.ForkID); err != nil {
log.Error("Unable to decrement repo fork num for old root repo %d of repository %-v whilst converting from fork. Error: %v", repo.ForkID, repo, err)
return err
}

repo.IsFork = false
repo.ForkID = 0

if err := models.UpdateRepositoryCtx(&ctx, repo, false); err != nil {
if err := models.UpdateRepositoryCtx(ctx, repo, false); err != nil {
log.Error("Unable to update repository %-v whilst converting from fork. Error: %v", repo, err)
return err
}
Expand Down
6 changes: 3 additions & 3 deletions modules/repository/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ import (

// PushUpdateAddDeleteTags updates a number of added and delete tags
func PushUpdateAddDeleteTags(repo *models.Repository, gitRepo *git.Repository, addTags, delTags []string) error {
return db.WithTx(func(ctx db.Context) error {
if err := models.PushUpdateDeleteTagsContext(&ctx, repo, delTags); err != nil {
return db.WithTx(func(ctx *db.Context) error {
if err := models.PushUpdateDeleteTagsContext(ctx, repo, delTags); err != nil {
return err
}
return pushUpdateAddTags(&ctx, repo, gitRepo, addTags)
return pushUpdateAddTags(ctx, repo, gitRepo, addTags)
})
}

Expand Down
8 changes: 4 additions & 4 deletions services/archiver/archiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func doArchive(r *ArchiveRequest) (*models.RepoArchiver, error) {
}
defer commiter.Close()

archiver, err := models.GetRepoArchiver(&ctx, r.RepoID, r.Type, r.CommitID)
archiver, err := models.GetRepoArchiver(ctx, r.RepoID, r.Type, r.CommitID)
if err != nil {
return nil, err
}
Expand All @@ -122,7 +122,7 @@ func doArchive(r *ArchiveRequest) (*models.RepoArchiver, error) {
CommitID: r.CommitID,
Status: models.RepoArchiverGenerating,
}
if err := models.AddRepoArchiver(&ctx, archiver); err != nil {
if err := models.AddRepoArchiver(ctx, archiver); err != nil {
return nil, err
}
}
Expand All @@ -136,7 +136,7 @@ func doArchive(r *ArchiveRequest) (*models.RepoArchiver, error) {
if err == nil {
if archiver.Status == models.RepoArchiverGenerating {
archiver.Status = models.RepoArchiverReady
return archiver, models.UpdateRepoArchiverStatus(&ctx, archiver)
return archiver, models.UpdateRepoArchiverStatus(ctx, archiver)
}
return archiver, nil
}
Expand Down Expand Up @@ -202,7 +202,7 @@ func doArchive(r *ArchiveRequest) (*models.RepoArchiver, error) {

if archiver.Status == models.RepoArchiverGenerating {
archiver.Status = models.RepoArchiverReady
if err = models.UpdateRepoArchiverStatus(&ctx, archiver); err != nil {
if err = models.UpdateRepoArchiverStatus(ctx, archiver); err != nil {
return nil, err
}
}
Expand Down
2 changes: 1 addition & 1 deletion services/attachment/attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func NewAttachment(attach *models.Attachment, file io.Reader) (*models.Attachmen
return nil, fmt.Errorf("attachment %s should belong to a repository", attach.Name)
}

err := db.WithTx(func(ctx db.Context) error {
err := db.WithTx(func(ctx *db.Context) error {
attach.UUID = uuid.New().String()
size, err := storage.Attachments.Save(attach.RelativePath(), file, -1)
if err != nil {
Expand Down
12 changes: 6 additions & 6 deletions services/release/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,18 +195,18 @@ func UpdateRelease(doer *models.User, gitRepo *git.Repository, rel *models.Relea
}
defer commiter.Close()

if err = models.UpdateRelease(&ctx, rel); err != nil {
if err = models.UpdateRelease(ctx, rel); err != nil {
return err
}

if err = models.AddReleaseAttachments(&ctx, rel.ID, addAttachmentUUIDs); err != nil {
if err = models.AddReleaseAttachments(ctx, rel.ID, addAttachmentUUIDs); err != nil {
return fmt.Errorf("AddReleaseAttachments: %v", err)
}

var deletedUUIDsMap = make(map[string]bool)
if len(delAttachmentUUIDs) > 0 {
// Check attachments
attachments, err := models.GetAttachmentsByUUIDs(&ctx, delAttachmentUUIDs)
attachments, err := models.GetAttachmentsByUUIDs(ctx, delAttachmentUUIDs)
if err != nil {
return fmt.Errorf("GetAttachmentsByUUIDs [uuids: %v]: %v", delAttachmentUUIDs, err)
}
Expand All @@ -217,7 +217,7 @@ func UpdateRelease(doer *models.User, gitRepo *git.Repository, rel *models.Relea
deletedUUIDsMap[attach.UUID] = true
}

if _, err := models.DeleteAttachments(&ctx, attachments, false); err != nil {
if _, err := models.DeleteAttachments(ctx, attachments, false); err != nil {
return fmt.Errorf("DeleteAttachments [uuids: %v]: %v", delAttachmentUUIDs, err)
}
}
Expand All @@ -228,7 +228,7 @@ func UpdateRelease(doer *models.User, gitRepo *git.Repository, rel *models.Relea
updateAttachmentsList = append(updateAttachmentsList, k)
}
// Check attachments
attachments, err := models.GetAttachmentsByUUIDs(&ctx, updateAttachmentsList)
attachments, err := models.GetAttachmentsByUUIDs(ctx, updateAttachmentsList)
if err != nil {
return fmt.Errorf("GetAttachmentsByUUIDs [uuids: %v]: %v", updateAttachmentsList, err)
}
Expand All @@ -240,7 +240,7 @@ func UpdateRelease(doer *models.User, gitRepo *git.Repository, rel *models.Relea

for uuid, newName := range editAttachments {
if !deletedUUIDsMap[uuid] {
if err = models.UpdateAttachmentByUUID(&ctx, &models.Attachment{
if err = models.UpdateAttachmentByUUID(ctx, &models.Attachment{
UUID: uuid,
Name: newName,
}, "name"); err != nil {
Expand Down
16 changes: 8 additions & 8 deletions services/repository/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,50 +21,50 @@ func GenerateRepository(doer, owner *models.User, templateRepo *models.Repositor
}

var generateRepo *models.Repository
if err = db.WithTx(func(ctx db.Context) error {
generateRepo, err = repo_module.GenerateRepository(&ctx, doer, owner, templateRepo, opts)
if err = db.WithTx(func(ctx *db.Context) error {
generateRepo, err = repo_module.GenerateRepository(ctx, doer, owner, templateRepo, opts)
if err != nil {
return err
}

// Git Content
if opts.GitContent && !templateRepo.IsEmpty {
if err = repo_module.GenerateGitContent(&ctx, templateRepo, generateRepo); err != nil {
if err = repo_module.GenerateGitContent(ctx, templateRepo, generateRepo); err != nil {
return err
}
}

// Topics
if opts.Topics {
if err = models.GenerateTopics(&ctx, templateRepo, generateRepo); err != nil {
if err = models.GenerateTopics(ctx, templateRepo, generateRepo); err != nil {
return err
}
}

// Git Hooks
if opts.GitHooks {
if err = models.GenerateGitHooks(&ctx, templateRepo, generateRepo); err != nil {
if err = models.GenerateGitHooks(ctx, templateRepo, generateRepo); err != nil {
return err
}
}

// Webhooks
if opts.Webhooks {
if err = models.GenerateWebhooks(&ctx, templateRepo, generateRepo); err != nil {
if err = models.GenerateWebhooks(ctx, templateRepo, generateRepo); err != nil {
return err
}
}

// Avatar
if opts.Avatar && len(templateRepo.Avatar) > 0 {
if err = models.GenerateAvatar(&ctx, templateRepo, generateRepo); err != nil {
if err = models.GenerateAvatar(ctx, templateRepo, generateRepo); err != nil {
return err
}
}

// Issue Labels
if opts.IssueLabels {
if err = models.GenerateIssueLabels(&ctx, templateRepo, generateRepo); err != nil {
if err = models.GenerateIssueLabels(ctx, templateRepo, generateRepo); err != nil {
return err
}
}
Expand Down

0 comments on commit 153793b

Please sign in to comment.