Skip to content

Commit 5233051

Browse files
authoredNov 17, 2021
Move some functions into services/repository (#17677)
1 parent 750a846 commit 5233051

File tree

15 files changed

+283
-285
lines changed

15 files changed

+283
-285
lines changed
 

‎cmd/admin.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"code.gitea.io/gitea/modules/storage"
2626
auth_service "code.gitea.io/gitea/services/auth"
2727
"code.gitea.io/gitea/services/auth/source/oauth2"
28+
repo_service "code.gitea.io/gitea/services/repository"
2829

2930
"github.com/urfave/cli"
3031
)
@@ -612,7 +613,7 @@ func runRegenerateHooks(_ *cli.Context) error {
612613
if err := initDB(ctx); err != nil {
613614
return err
614615
}
615-
return repo_module.SyncRepositoryHooks(graceful.GetManager().ShutdownContext())
616+
return repo_service.SyncRepositoryHooks(graceful.GetManager().ShutdownContext())
616617
}
617618

618619
func runRegenerateKeys(_ *cli.Context) error {

‎integrations/api_repo_get_contents_list_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import (
1313
"code.gitea.io/gitea/models"
1414
"code.gitea.io/gitea/models/unittest"
1515
"code.gitea.io/gitea/modules/git"
16-
repo_module "code.gitea.io/gitea/modules/repository"
1716
"code.gitea.io/gitea/modules/setting"
1817
api "code.gitea.io/gitea/modules/structs"
18+
repo_service "code.gitea.io/gitea/services/repository"
1919

2020
"github.com/stretchr/testify/assert"
2121
)
@@ -72,7 +72,7 @@ func testAPIGetContentsList(t *testing.T, u *url.URL) {
7272

7373
// Make a new branch in repo1
7474
newBranch := "test_branch"
75-
err := repo_module.CreateNewBranch(user2, repo1, repo1.DefaultBranch, newBranch)
75+
err := repo_service.CreateNewBranch(user2, repo1, repo1.DefaultBranch, newBranch)
7676
assert.NoError(t, err)
7777
// Get the commit ID of the default branch
7878
gitRepo, err := git.OpenRepository(repo1.RepoPath())

‎integrations/api_repo_get_contents_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import (
1212
"code.gitea.io/gitea/models"
1313
"code.gitea.io/gitea/models/unittest"
1414
"code.gitea.io/gitea/modules/git"
15-
repo_module "code.gitea.io/gitea/modules/repository"
1615
"code.gitea.io/gitea/modules/setting"
1716
api "code.gitea.io/gitea/modules/structs"
17+
repo_service "code.gitea.io/gitea/services/repository"
1818

1919
"github.com/stretchr/testify/assert"
2020
)
@@ -73,7 +73,7 @@ func testAPIGetContents(t *testing.T, u *url.URL) {
7373

7474
// Make a new branch in repo1
7575
newBranch := "test_branch"
76-
err := repo_module.CreateNewBranch(user2, repo1, repo1.DefaultBranch, newBranch)
76+
err := repo_service.CreateNewBranch(user2, repo1, repo1.DefaultBranch, newBranch)
7777
assert.NoError(t, err)
7878
// Get the commit ID of the default branch
7979
gitRepo, err := git.OpenRepository(repo1.RepoPath())

‎modules/repository/branch.go

-89
Original file line numberDiff line numberDiff line change
@@ -24,92 +24,3 @@ func GetBranch(repo *models.Repository, branch string) (*git.Branch, error) {
2424

2525
return gitRepo.GetBranch(branch)
2626
}
27-
28-
// GetBranches returns branches from the repository, skipping skip initial branches and
29-
// returning at most limit branches, or all branches if limit is 0.
30-
func GetBranches(repo *models.Repository, skip, limit int) ([]*git.Branch, int, error) {
31-
return git.GetBranchesByPath(repo.RepoPath(), skip, limit)
32-
}
33-
34-
// checkBranchName validates branch name with existing repository branches
35-
func checkBranchName(repo *models.Repository, name string) error {
36-
gitRepo, err := git.OpenRepository(repo.RepoPath())
37-
if err != nil {
38-
return err
39-
}
40-
defer gitRepo.Close()
41-
42-
branches, _, err := GetBranches(repo, 0, 0)
43-
if err != nil {
44-
return err
45-
}
46-
47-
for _, branch := range branches {
48-
if branch.Name == name {
49-
return models.ErrBranchAlreadyExists{
50-
BranchName: branch.Name,
51-
}
52-
} else if (len(branch.Name) < len(name) && branch.Name+"/" == name[0:len(branch.Name)+1]) ||
53-
(len(branch.Name) > len(name) && name+"/" == branch.Name[0:len(name)+1]) {
54-
return models.ErrBranchNameConflict{
55-
BranchName: branch.Name,
56-
}
57-
}
58-
}
59-
60-
if _, err := gitRepo.GetTag(name); err == nil {
61-
return models.ErrTagAlreadyExists{
62-
TagName: name,
63-
}
64-
}
65-
66-
return nil
67-
}
68-
69-
// CreateNewBranch creates a new repository branch
70-
func CreateNewBranch(doer *models.User, repo *models.Repository, oldBranchName, branchName string) (err error) {
71-
// Check if branch name can be used
72-
if err := checkBranchName(repo, branchName); err != nil {
73-
return err
74-
}
75-
76-
if !git.IsBranchExist(repo.RepoPath(), oldBranchName) {
77-
return models.ErrBranchDoesNotExist{
78-
BranchName: oldBranchName,
79-
}
80-
}
81-
82-
if err := git.Push(repo.RepoPath(), git.PushOptions{
83-
Remote: repo.RepoPath(),
84-
Branch: fmt.Sprintf("%s:%s%s", oldBranchName, git.BranchPrefix, branchName),
85-
Env: models.PushingEnvironment(doer, repo),
86-
}); err != nil {
87-
if git.IsErrPushOutOfDate(err) || git.IsErrPushRejected(err) {
88-
return err
89-
}
90-
return fmt.Errorf("Push: %v", err)
91-
}
92-
93-
return nil
94-
}
95-
96-
// CreateNewBranchFromCommit creates a new repository branch
97-
func CreateNewBranchFromCommit(doer *models.User, repo *models.Repository, commit, branchName string) (err error) {
98-
// Check if branch name can be used
99-
if err := checkBranchName(repo, branchName); err != nil {
100-
return err
101-
}
102-
103-
if err := git.Push(repo.RepoPath(), git.PushOptions{
104-
Remote: repo.RepoPath(),
105-
Branch: fmt.Sprintf("%s:%s%s", commit, git.BranchPrefix, branchName),
106-
Env: models.PushingEnvironment(doer, repo),
107-
}); err != nil {
108-
if git.IsErrPushOutOfDate(err) || git.IsErrPushRejected(err) {
109-
return err
110-
}
111-
return fmt.Errorf("Push: %v", err)
112-
}
113-
114-
return nil
115-
}

‎modules/repository/hooks.go

-41
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,13 @@
55
package repository
66

77
import (
8-
"context"
98
"fmt"
109
"os"
1110
"path/filepath"
1211

13-
"code.gitea.io/gitea/models"
14-
"code.gitea.io/gitea/models/db"
1512
"code.gitea.io/gitea/modules/git"
16-
"code.gitea.io/gitea/modules/log"
1713
"code.gitea.io/gitea/modules/setting"
1814
"code.gitea.io/gitea/modules/util"
19-
20-
"xorm.io/builder"
2115
)
2216

2317
func getHookTemplates() (hookNames, hookTpls, giteaHookTpls []string) {
@@ -240,38 +234,3 @@ func CheckDelegateHooks(repoPath string) ([]string, error) {
240234
}
241235
return results, nil
242236
}
243-
244-
// SyncRepositoryHooks rewrites all repositories' pre-receive, update and post-receive hooks
245-
// to make sure the binary and custom conf path are up-to-date.
246-
func SyncRepositoryHooks(ctx context.Context) error {
247-
log.Trace("Doing: SyncRepositoryHooks")
248-
249-
if err := db.Iterate(
250-
db.DefaultContext,
251-
new(models.Repository),
252-
builder.Gt{"id": 0},
253-
func(idx int, bean interface{}) error {
254-
repo := bean.(*models.Repository)
255-
select {
256-
case <-ctx.Done():
257-
return db.ErrCancelledf("before sync repository hooks for %s", repo.FullName())
258-
default:
259-
}
260-
261-
if err := createDelegateHooks(repo.RepoPath()); err != nil {
262-
return fmt.Errorf("SyncRepositoryHook: %v", err)
263-
}
264-
if repo.HasWiki() {
265-
if err := createDelegateHooks(repo.WikiPath()); err != nil {
266-
return fmt.Errorf("SyncRepositoryHook: %v", err)
267-
}
268-
}
269-
return nil
270-
},
271-
); err != nil {
272-
return err
273-
}
274-
275-
log.Trace("Finished: SyncRepositoryHooks")
276-
return nil
277-
}

‎modules/repository/update.go

-136
This file was deleted.

‎routers/api/v1/repo/branch.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ func CreateBranch(ctx *context.APIContext) {
176176
opt.OldBranchName = ctx.Repo.Repository.DefaultBranch
177177
}
178178

179-
err := repo_module.CreateNewBranch(ctx.User, ctx.Repo.Repository, opt.OldBranchName, opt.BranchName)
179+
err := repo_service.CreateNewBranch(ctx.User, ctx.Repo.Repository, opt.OldBranchName, opt.BranchName)
180180

181181
if err != nil {
182182
if models.IsErrBranchDoesNotExist(err) {
@@ -257,7 +257,7 @@ func ListBranches(ctx *context.APIContext) {
257257

258258
listOptions := utils.GetListOptions(ctx)
259259
skip, _ := listOptions.GetStartEnd()
260-
branches, totalNumOfBranches, err := repo_module.GetBranches(ctx.Repo.Repository, skip, listOptions.PageSize)
260+
branches, totalNumOfBranches, err := repo_service.GetBranches(ctx.Repo.Repository, skip, listOptions.PageSize)
261261
if err != nil {
262262
ctx.Error(http.StatusInternalServerError, "GetBranches", err)
263263
return

‎routers/init.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"code.gitea.io/gitea/modules/markup"
2626
"code.gitea.io/gitea/modules/markup/external"
2727
"code.gitea.io/gitea/modules/notification"
28-
repo_module "code.gitea.io/gitea/modules/repository"
2928
"code.gitea.io/gitea/modules/setting"
3029
"code.gitea.io/gitea/modules/ssh"
3130
"code.gitea.io/gitea/modules/storage"
@@ -45,7 +44,7 @@ import (
4544
repo_migrations "code.gitea.io/gitea/services/migrations"
4645
mirror_service "code.gitea.io/gitea/services/mirror"
4746
pull_service "code.gitea.io/gitea/services/pull"
48-
"code.gitea.io/gitea/services/repository"
47+
repo_service "code.gitea.io/gitea/services/repository"
4948
"code.gitea.io/gitea/services/webhook"
5049

5150
"gitea.com/go-chi/session"
@@ -73,7 +72,7 @@ func mustInitCtx(ctx context.Context, fn func(ctx context.Context) error) {
7372
func InitGitServices() {
7473
setting.NewServices()
7574
mustInit(storage.Init)
76-
mustInit(repository.NewContext)
75+
mustInit(repo_service.NewContext)
7776
}
7877

7978
func syncAppPathForGit(ctx context.Context) error {
@@ -85,7 +84,7 @@ func syncAppPathForGit(ctx context.Context) error {
8584
log.Info("AppPath changed from '%s' to '%s'", runtimeState.LastAppPath, setting.AppPath)
8685

8786
log.Info("re-sync repository hooks ...")
88-
mustInitCtx(ctx, repo_module.SyncRepositoryHooks)
87+
mustInitCtx(ctx, repo_service.SyncRepositoryHooks)
8988

9089
log.Info("re-write ssh public keys ...")
9190
mustInit(models.RewriteAllPublicKeys)

‎routers/web/repo/branch.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ func loadBranches(ctx *context.Context, skip, limit int) ([]*Branch, int) {
171171
return nil, 0
172172
}
173173

174-
rawBranches, totalNumOfBranches, err := repo_module.GetBranches(ctx.Repo.Repository, skip, limit)
174+
rawBranches, totalNumOfBranches, err := repo_service.GetBranches(ctx.Repo.Repository, skip, limit)
175175
if err != nil {
176176
log.Error("GetBranches: %v", err)
177177
ctx.ServerError("GetBranches", err)
@@ -350,11 +350,11 @@ func CreateBranch(ctx *context.Context) {
350350
err = release_service.CreateNewTag(ctx.User, ctx.Repo.Repository, ctx.Repo.BranchName, form.NewBranchName, "")
351351
}
352352
} else if ctx.Repo.IsViewBranch {
353-
err = repo_module.CreateNewBranch(ctx.User, ctx.Repo.Repository, ctx.Repo.BranchName, form.NewBranchName)
353+
err = repo_service.CreateNewBranch(ctx.User, ctx.Repo.Repository, ctx.Repo.BranchName, form.NewBranchName)
354354
} else if ctx.Repo.IsViewTag {
355-
err = repo_module.CreateNewBranchFromCommit(ctx.User, ctx.Repo.Repository, ctx.Repo.CommitID, form.NewBranchName)
355+
err = repo_service.CreateNewBranchFromCommit(ctx.User, ctx.Repo.Repository, ctx.Repo.CommitID, form.NewBranchName)
356356
} else {
357-
err = repo_module.CreateNewBranchFromCommit(ctx.User, ctx.Repo.Repository, ctx.Repo.BranchName, form.NewBranchName)
357+
err = repo_service.CreateNewBranchFromCommit(ctx.User, ctx.Repo.Repository, ctx.Repo.BranchName, form.NewBranchName)
358358
}
359359
if err != nil {
360360
if models.IsErrTagAlreadyExists(err) {

‎services/cron/tasks_extended.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func registerRepositoryUpdateHook() {
8585
RunAtStart: false,
8686
Schedule: "@every 72h",
8787
}, func(ctx context.Context, _ *models.User, _ Config) error {
88-
return repo_module.SyncRepositoryHooks(ctx)
88+
return repo_service.SyncRepositoryHooks(ctx)
8989
})
9090
}
9191

‎services/mirror/mirror_pull.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ func runSync(ctx context.Context, m *models.Mirror) ([]*mirrorSyncResult, bool)
333333
}
334334

335335
log.Trace("SyncMirrors [repo: %-v]: invalidating mirror branch caches...", m.Repo)
336-
branches, _, err := repo_module.GetBranches(m.Repo, 0, 0)
336+
branches, _, err := git.GetBranchesByPath(m.Repo.RepoPath(), 0, 0)
337337
if err != nil {
338338
log.Error("GetBranches: %v", err)
339339
return nil, false

‎services/repository/branch.go

+90
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package repository
66

77
import (
88
"errors"
9+
"fmt"
910

1011
"code.gitea.io/gitea/models"
1112
"code.gitea.io/gitea/modules/git"
@@ -15,6 +16,95 @@ import (
1516
pull_service "code.gitea.io/gitea/services/pull"
1617
)
1718

19+
// CreateNewBranch creates a new repository branch
20+
func CreateNewBranch(doer *models.User, repo *models.Repository, oldBranchName, branchName string) (err error) {
21+
// Check if branch name can be used
22+
if err := checkBranchName(repo, branchName); err != nil {
23+
return err
24+
}
25+
26+
if !git.IsBranchExist(repo.RepoPath(), oldBranchName) {
27+
return models.ErrBranchDoesNotExist{
28+
BranchName: oldBranchName,
29+
}
30+
}
31+
32+
if err := git.Push(repo.RepoPath(), git.PushOptions{
33+
Remote: repo.RepoPath(),
34+
Branch: fmt.Sprintf("%s:%s%s", oldBranchName, git.BranchPrefix, branchName),
35+
Env: models.PushingEnvironment(doer, repo),
36+
}); err != nil {
37+
if git.IsErrPushOutOfDate(err) || git.IsErrPushRejected(err) {
38+
return err
39+
}
40+
return fmt.Errorf("Push: %v", err)
41+
}
42+
43+
return nil
44+
}
45+
46+
// GetBranches returns branches from the repository, skipping skip initial branches and
47+
// returning at most limit branches, or all branches if limit is 0.
48+
func GetBranches(repo *models.Repository, skip, limit int) ([]*git.Branch, int, error) {
49+
return git.GetBranchesByPath(repo.RepoPath(), skip, limit)
50+
}
51+
52+
// checkBranchName validates branch name with existing repository branches
53+
func checkBranchName(repo *models.Repository, name string) error {
54+
gitRepo, err := git.OpenRepository(repo.RepoPath())
55+
if err != nil {
56+
return err
57+
}
58+
defer gitRepo.Close()
59+
60+
branches, _, err := GetBranches(repo, 0, 0)
61+
if err != nil {
62+
return err
63+
}
64+
65+
for _, branch := range branches {
66+
if branch.Name == name {
67+
return models.ErrBranchAlreadyExists{
68+
BranchName: branch.Name,
69+
}
70+
} else if (len(branch.Name) < len(name) && branch.Name+"/" == name[0:len(branch.Name)+1]) ||
71+
(len(branch.Name) > len(name) && name+"/" == branch.Name[0:len(name)+1]) {
72+
return models.ErrBranchNameConflict{
73+
BranchName: branch.Name,
74+
}
75+
}
76+
}
77+
78+
if _, err := gitRepo.GetTag(name); err == nil {
79+
return models.ErrTagAlreadyExists{
80+
TagName: name,
81+
}
82+
}
83+
84+
return nil
85+
}
86+
87+
// CreateNewBranchFromCommit creates a new repository branch
88+
func CreateNewBranchFromCommit(doer *models.User, repo *models.Repository, commit, branchName string) (err error) {
89+
// Check if branch name can be used
90+
if err := checkBranchName(repo, branchName); err != nil {
91+
return err
92+
}
93+
94+
if err := git.Push(repo.RepoPath(), git.PushOptions{
95+
Remote: repo.RepoPath(),
96+
Branch: fmt.Sprintf("%s:%s%s", commit, git.BranchPrefix, branchName),
97+
Env: models.PushingEnvironment(doer, repo),
98+
}); err != nil {
99+
if git.IsErrPushOutOfDate(err) || git.IsErrPushRejected(err) {
100+
return err
101+
}
102+
return fmt.Errorf("Push: %v", err)
103+
}
104+
105+
return nil
106+
}
107+
18108
// RenameBranch rename a branch
19109
func RenameBranch(repo *models.Repository, doer *models.User, gitRepo *git.Repository, from, to string) (string, error) {
20110
if from == to {
File renamed without changes.

‎services/repository/hooks.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2021 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package repository
6+
7+
import (
8+
"context"
9+
"fmt"
10+
11+
"code.gitea.io/gitea/models"
12+
"code.gitea.io/gitea/models/db"
13+
"code.gitea.io/gitea/modules/log"
14+
repo_module "code.gitea.io/gitea/modules/repository"
15+
16+
"xorm.io/builder"
17+
)
18+
19+
// SyncRepositoryHooks rewrites all repositories' pre-receive, update and post-receive hooks
20+
// to make sure the binary and custom conf path are up-to-date.
21+
func SyncRepositoryHooks(ctx context.Context) error {
22+
log.Trace("Doing: SyncRepositoryHooks")
23+
24+
if err := db.Iterate(
25+
db.DefaultContext,
26+
new(models.Repository),
27+
builder.Gt{"id": 0},
28+
func(idx int, bean interface{}) error {
29+
repo := bean.(*models.Repository)
30+
select {
31+
case <-ctx.Done():
32+
return db.ErrCancelledf("before sync repository hooks for %s", repo.FullName())
33+
default:
34+
}
35+
36+
if err := repo_module.CreateDelegateHooks(repo.RepoPath()); err != nil {
37+
return fmt.Errorf("SyncRepositoryHook: %v", err)
38+
}
39+
if repo.HasWiki() {
40+
if err := repo_module.CreateDelegateHooks(repo.WikiPath()); err != nil {
41+
return fmt.Errorf("SyncRepositoryHook: %v", err)
42+
}
43+
}
44+
return nil
45+
},
46+
); err != nil {
47+
return err
48+
}
49+
50+
log.Trace("Finished: SyncRepositoryHooks")
51+
return nil
52+
}

‎services/repository/push.go

+124-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
package repository
66

77
import (
8+
"context"
89
"errors"
910
"fmt"
11+
"strings"
1012
"time"
1113

1214
"code.gitea.io/gitea/models"
@@ -20,6 +22,7 @@ import (
2022
"code.gitea.io/gitea/modules/repofiles"
2123
repo_module "code.gitea.io/gitea/modules/repository"
2224
"code.gitea.io/gitea/modules/setting"
25+
"code.gitea.io/gitea/modules/timeutil"
2326
pull_service "code.gitea.io/gitea/services/pull"
2427
)
2528

@@ -210,7 +213,7 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error {
210213
}
211214

212215
// Cache for big repository
213-
if err := repo_module.CacheRef(graceful.GetManager().HammerContext(), repo, gitRepo, opts.RefFullName); err != nil {
216+
if err := CacheRef(graceful.GetManager().HammerContext(), repo, gitRepo, opts.RefFullName); err != nil {
214217
log.Error("repo_module.CacheRef %s/%s failed: %v", repo.ID, branch, err)
215218
}
216219
} else {
@@ -229,7 +232,7 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error {
229232
log.Trace("Non-tag and non-branch commits pushed.")
230233
}
231234
}
232-
if err := repo_module.PushUpdateAddDeleteTags(repo, gitRepo, addTags, delTags); err != nil {
235+
if err := PushUpdateAddDeleteTags(repo, gitRepo, addTags, delTags); err != nil {
233236
return fmt.Errorf("PushUpdateAddDeleteTags: %v", err)
234237
}
235238

@@ -240,3 +243,122 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error {
240243

241244
return nil
242245
}
246+
247+
// PushUpdateAddDeleteTags updates a number of added and delete tags
248+
func PushUpdateAddDeleteTags(repo *models.Repository, gitRepo *git.Repository, addTags, delTags []string) error {
249+
return db.WithTx(func(ctx context.Context) error {
250+
if err := models.PushUpdateDeleteTagsContext(ctx, repo, delTags); err != nil {
251+
return err
252+
}
253+
return pushUpdateAddTags(ctx, repo, gitRepo, addTags)
254+
})
255+
}
256+
257+
// pushUpdateAddTags updates a number of add tags
258+
func pushUpdateAddTags(ctx context.Context, repo *models.Repository, gitRepo *git.Repository, tags []string) error {
259+
if len(tags) == 0 {
260+
return nil
261+
}
262+
263+
lowerTags := make([]string, 0, len(tags))
264+
for _, tag := range tags {
265+
lowerTags = append(lowerTags, strings.ToLower(tag))
266+
}
267+
268+
releases, err := models.GetReleasesByRepoIDAndNames(ctx, repo.ID, lowerTags)
269+
if err != nil {
270+
return fmt.Errorf("GetReleasesByRepoIDAndNames: %v", err)
271+
}
272+
relMap := make(map[string]*models.Release)
273+
for _, rel := range releases {
274+
relMap[rel.LowerTagName] = rel
275+
}
276+
277+
newReleases := make([]*models.Release, 0, len(lowerTags)-len(relMap))
278+
279+
emailToUser := make(map[string]*models.User)
280+
281+
for i, lowerTag := range lowerTags {
282+
tag, err := gitRepo.GetTag(tags[i])
283+
if err != nil {
284+
return fmt.Errorf("GetTag: %v", err)
285+
}
286+
commit, err := tag.Commit()
287+
if err != nil {
288+
return fmt.Errorf("Commit: %v", err)
289+
}
290+
291+
sig := tag.Tagger
292+
if sig == nil {
293+
sig = commit.Author
294+
}
295+
if sig == nil {
296+
sig = commit.Committer
297+
}
298+
var author *models.User
299+
var createdAt = time.Unix(1, 0)
300+
301+
if sig != nil {
302+
var ok bool
303+
author, ok = emailToUser[sig.Email]
304+
if !ok {
305+
author, err = models.GetUserByEmailContext(ctx, sig.Email)
306+
if err != nil && !models.IsErrUserNotExist(err) {
307+
return fmt.Errorf("GetUserByEmail: %v", err)
308+
}
309+
if author != nil {
310+
emailToUser[sig.Email] = author
311+
}
312+
}
313+
createdAt = sig.When
314+
}
315+
316+
commitsCount, err := commit.CommitsCount()
317+
if err != nil {
318+
return fmt.Errorf("CommitsCount: %v", err)
319+
}
320+
321+
rel, has := relMap[lowerTag]
322+
323+
if !has {
324+
rel = &models.Release{
325+
RepoID: repo.ID,
326+
Title: "",
327+
TagName: tags[i],
328+
LowerTagName: lowerTag,
329+
Target: "",
330+
Sha1: commit.ID.String(),
331+
NumCommits: commitsCount,
332+
Note: "",
333+
IsDraft: false,
334+
IsPrerelease: false,
335+
IsTag: true,
336+
CreatedUnix: timeutil.TimeStamp(createdAt.Unix()),
337+
}
338+
if author != nil {
339+
rel.PublisherID = author.ID
340+
}
341+
342+
newReleases = append(newReleases, rel)
343+
} else {
344+
rel.Sha1 = commit.ID.String()
345+
rel.CreatedUnix = timeutil.TimeStamp(createdAt.Unix())
346+
rel.NumCommits = commitsCount
347+
rel.IsDraft = false
348+
if rel.IsTag && author != nil {
349+
rel.PublisherID = author.ID
350+
}
351+
if err = models.UpdateRelease(ctx, rel); err != nil {
352+
return fmt.Errorf("Update: %v", err)
353+
}
354+
}
355+
}
356+
357+
if len(newReleases) > 0 {
358+
if err = models.InsertReleasesContext(ctx, newReleases); err != nil {
359+
return fmt.Errorf("Insert: %v", err)
360+
}
361+
}
362+
363+
return nil
364+
}

0 commit comments

Comments
 (0)
Please sign in to comment.