Skip to content

Commit 508c661

Browse files
committed
improve code
1 parent 8d3e224 commit 508c661

File tree

14 files changed

+130
-116
lines changed

14 files changed

+130
-116
lines changed

custom/conf/app.ini.sample

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -690,10 +690,10 @@ SCHEDULE = @every 24h
690690
; or only create new users if UPDATE_EXISTING is set to false
691691
UPDATE_EXISTING = true
692692

693-
; Update migrated repositories' issues and comments' posterid, it will always be started at start.
693+
; Update migrated repositories' issues and comments' posterid, it will always attempt synchronization when the instance starts.
694694
[cron.update_migration_post_id]
695-
; Interval as a duration between each synchronization (default every 10m)
696-
SCHEDULE = @every 10m
695+
; Interval as a duration between each synchronization. (default every 24h)
696+
SCHEDULE = @every 24h
697697

698698
[git]
699699
; The path of git executable. If empty, Gitea searches through the PATH environment.

docs/content/doc/advanced/config-cheat-sheet.en-us.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ NB: You must `REDIRECT_MACARON_LOG` and have `DISABLE_ROUTER_LOG` set to `false`
421421

422422
### Cron - Update Migration Poster ID (`cron.update_migration_post_id`)
423423

424-
- `SCHEDULE`: **@every 10m** : Interval as a duration between each synchronization, it will always be started at start.
424+
- `SCHEDULE`: **@every 24h** : Interval as a duration between each synchronization, it will always attempt synchronization when the instance starts.
425425

426426
## Git (`git`)
427427

docs/content/doc/advanced/config-cheat-sheet.zh-cn.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ menu:
200200

201201
### Cron - Update Migration Poster ID (`cron.update_migration_post_id`)
202202

203-
- `SCHEDULE`: **@every 10m** : 每次统计的间隔时间。此任务总是在启动时自动进行。
203+
- `SCHEDULE`: **@every 24h** : 每次统计的间隔时间。此任务总是在启动时自动进行。
204204

205205
## Git (`git`)
206206

models/external_login_user.go

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package models
77
import (
88
"time"
99

10+
"code.gitea.io/gitea/modules/structs"
1011
"github.com/markbates/goth"
1112
"xorm.io/builder"
1213
)
@@ -51,40 +52,15 @@ func ListAccountLinks(user *User) ([]*ExternalLoginUser, error) {
5152
return externalAccounts, nil
5253
}
5354

54-
// LinkAccountToUser link the gothUser to the user
55-
func LinkAccountToUser(user *User, gothUser goth.User) error {
56-
loginSource, err := GetActiveOAuth2LoginSourceByName(gothUser.Provider)
57-
if err != nil {
58-
return err
59-
}
60-
61-
externalLoginUser := &ExternalLoginUser{
62-
ExternalID: gothUser.UserID,
63-
UserID: user.ID,
64-
LoginSourceID: loginSource.ID,
65-
RawData: gothUser.RawData,
66-
Provider: gothUser.Provider,
67-
Email: gothUser.Email,
68-
Name: gothUser.Name,
69-
FirstName: gothUser.FirstName,
70-
LastName: gothUser.LastName,
71-
NickName: gothUser.NickName,
72-
Description: gothUser.Description,
73-
AvatarURL: gothUser.AvatarURL,
74-
Location: gothUser.Location,
75-
AccessToken: gothUser.AccessToken,
76-
AccessTokenSecret: gothUser.AccessTokenSecret,
77-
RefreshToken: gothUser.RefreshToken,
78-
ExpiresAt: gothUser.ExpiresAt,
79-
}
80-
81-
has, err := x.Where("external_id=? AND login_source_id=?", gothUser.UserID, loginSource.ID).
55+
// LinkExternalToUser link the external user to the user
56+
func LinkExternalToUser(user *User, externalLoginUser *ExternalLoginUser) error {
57+
has, err := x.Where("external_id=? AND login_source_id=?", externalLoginUser.ExternalID, externalLoginUser.LoginSourceID).
8258
NoAutoCondition().
8359
Exist(externalLoginUser)
8460
if err != nil {
8561
return err
8662
} else if has {
87-
return ErrExternalLoginUserAlreadyExist{gothUser.UserID, user.ID, loginSource.ID}
63+
return ErrExternalLoginUserAlreadyExist{externalLoginUser.ExternalID, user.ID, externalLoginUser.LoginSourceID}
8864
}
8965

9066
_, err = x.Insert(externalLoginUser)
@@ -186,3 +162,12 @@ func FindExternalUsersByProvider(opts FindExternalUserOptions) ([]ExternalLoginU
186162
}
187163
return users, nil
188164
}
165+
166+
// UpdateMigrationsByType updates all migrated repositories' posterid from gitServiceType to replace originalAuthorID to posterID
167+
func UpdateMigrationsByType(tp structs.GitServiceType, externalUserID, userID int64) error {
168+
if err := UpdateIssuesMigrationsByType(tp, externalUserID, userID); err != nil {
169+
return err
170+
}
171+
172+
return UpdateCommentsMigrationsByType(tp, externalUserID, userID)
173+
}

models/issue.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"code.gitea.io/gitea/modules/base"
1515
"code.gitea.io/gitea/modules/log"
1616
"code.gitea.io/gitea/modules/setting"
17+
"code.gitea.io/gitea/modules/structs"
1718
api "code.gitea.io/gitea/modules/structs"
1819
"code.gitea.io/gitea/modules/timeutil"
1920
"code.gitea.io/gitea/modules/util"
@@ -1948,10 +1949,10 @@ func (issue *Issue) ResolveMentionsByVisibility(ctx DBContext, doer *User, menti
19481949
return
19491950
}
19501951

1951-
// UpdateIssuesMigrations updates issues' migrations information
1952-
func UpdateIssuesMigrations(repoID, originalAuthorID, posterID int64) error {
1952+
// UpdateIssuesMigrationsByType updates all migrated repositories' issues from gitServiceType to replace originalAuthorID to posterID
1953+
func UpdateIssuesMigrationsByType(gitServiceType structs.GitServiceType, originalAuthorID, posterID int64) error {
19531954
_, err := x.Table("issue").
1954-
Where("repo_id = ?", repoID).
1955+
Where("repo_id IN (SELECT id FROM repository WHERE original_service_type = ?)", gitServiceType).
19551956
And("original_author_id = ?", originalAuthorID).
19561957
Update(map[string]interface{}{
19571958
"poster_id": posterID,

models/issue_comment.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"code.gitea.io/gitea/modules/log"
1515
"code.gitea.io/gitea/modules/markup/markdown"
1616
"code.gitea.io/gitea/modules/references"
17+
"code.gitea.io/gitea/modules/structs"
1718
api "code.gitea.io/gitea/modules/structs"
1819
"code.gitea.io/gitea/modules/timeutil"
1920

@@ -1023,11 +1024,18 @@ func FetchCodeComments(issue *Issue, currentUser *User) (CodeComments, error) {
10231024
return fetchCodeComments(x, issue, currentUser)
10241025
}
10251026

1026-
// UpdateCommentsMigrations updates comments' migrations information
1027-
func UpdateCommentsMigrations(repoID, originalAuthorID, posterID int64) error {
1027+
// UpdateCommentsMigrationsByType updates comments' migrations information via given git service type and original id and poster id
1028+
func UpdateCommentsMigrationsByType(tp structs.GitServiceType, originalAuthorID, posterID int64) error {
10281029
_, err := x.Table("comment").
1029-
Where("issue_id IN (SELECT id FROM issue WHERE repo_id = ?)", repoID).
1030-
And("original_author_id = ?", originalAuthorID).
1030+
Where(builder.In("issue_id",
1031+
builder.Select("issue.id").
1032+
From("issue").
1033+
InnerJoin("repository", "issue.repo_id = repository.id").
1034+
Where(builder.Eq{
1035+
"repository.original_service_type": tp,
1036+
}),
1037+
)).
1038+
And("comment.original_author_id = ?", originalAuthorID).
10311039
Update(map[string]interface{}{
10321040
"poster_id": posterID,
10331041
"original_author": "",

models/repo.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2782,14 +2782,3 @@ func (repo *Repository) GetOriginalURLHostname() string {
27822782

27832783
return u.Host
27842784
}
2785-
2786-
// FindMigratedRepositoryIDs find all migrated
2787-
func FindMigratedRepositoryIDs(tp structs.GitServiceType, limit, start int) ([]int64, error) {
2788-
var ids []int64
2789-
err := x.Select("id").
2790-
Where("original_service_type = ?", tp).
2791-
Limit(limit, start).
2792-
Table("repository").
2793-
Find(&ids)
2794-
return ids, err
2795-
}

modules/migrations/github.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func (f *GithubDownloaderV3Factory) Match(opts base.MigrateOptions) (bool, error
4040
return false, err
4141
}
4242

43-
return u.Host == "github.com" && opts.AuthUsername != "", nil
43+
return strings.EqualFold(u.Host, "github.com") && opts.AuthUsername != "", nil
4444
}
4545

4646
// New returns a Downloader related to this factory according MigrateOptions

modules/migrations/update.go

Lines changed: 16 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,6 @@ import (
1212
"code.gitea.io/gitea/modules/structs"
1313
)
1414

15-
// UpdateRepoMigrations will update posterid on issues/comments/prs when external login user
16-
// login or when migrating
17-
func UpdateRepoMigrations(repoID, externalUserID, userID int64) error {
18-
if err := models.UpdateIssuesMigrations(repoID, externalUserID, userID); err != nil {
19-
return err
20-
}
21-
22-
return models.UpdateCommentsMigrations(repoID, externalUserID, userID)
23-
}
24-
2515
// UpdateMigrationPosterID updates all migrated repositories' issues and comments posterID
2616
func UpdateMigrationPosterID() {
2717
if err := updateMigrationPosterIDByGitService(structs.GithubService); err != nil {
@@ -35,47 +25,33 @@ func updateMigrationPosterIDByGitService(tp structs.GitServiceType) error {
3525
return nil
3626
}
3727

38-
var repoStart int
3928
const batchSize = 100
29+
var start int
4030
for {
41-
ids, err := models.FindMigratedRepositoryIDs(tp, batchSize, repoStart)
31+
users, err := models.FindExternalUsersByProvider(models.FindExternalUserOptions{
32+
Provider: provider,
33+
Start: start,
34+
Limit: batchSize,
35+
})
4236
if err != nil {
4337
return err
4438
}
4539

46-
var start int
47-
for {
48-
users, err := models.FindExternalUsersByProvider(models.FindExternalUserOptions{
49-
Provider: provider,
50-
Start: start,
51-
Limit: batchSize,
52-
})
40+
for _, user := range users {
41+
externalUserID, err := strconv.ParseInt(user.ExternalID, 10, 64)
5342
if err != nil {
54-
return err
43+
log.Warn("Parse externalUser %#v 's userID failed: %v", user, err)
44+
continue
5545
}
56-
57-
for _, user := range users {
58-
externalUserID, err := strconv.ParseInt(user.ExternalID, 10, 64)
59-
if err != nil {
60-
log.Warn("Parse externalUser %#v 's userID failed: %v", user, err)
61-
continue
62-
}
63-
for _, id := range ids {
64-
if err = UpdateRepoMigrations(id, externalUserID, user.UserID); err != nil {
65-
log.Error("UpdateRepoMigrations repo %v, github user id %v, user id %v failed: %v", id, user.ExternalID, user.UserID, err)
66-
}
67-
}
68-
}
69-
70-
if len(users) < batchSize {
71-
break
46+
if err := models.UpdateMigrationsByType(tp, externalUserID, user.UserID); err != nil {
47+
log.Error("UpdateMigrationsByType type %v, github user id %v, user id %v failed: %v", tp, user.ExternalID, user.UserID, err)
7248
}
73-
start += len(users)
7449
}
7550

76-
if len(ids) < batchSize {
77-
return nil
51+
if len(users) < batchSize {
52+
break
7853
}
79-
repoStart += len(ids)
54+
start += len(users)
8055
}
56+
return nil
8157
}

modules/setting/cron.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ var (
120120
UpdateMigrationPosterID: struct {
121121
Schedule string
122122
}{
123-
Schedule: "@every 10m",
123+
Schedule: "@every 24h",
124124
},
125125
}
126126
)

modules/structs/repo.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,14 @@ func (gt GitServiceType) Name() string {
180180
return ""
181181
}
182182

183+
var (
184+
// all git services supported to migrate issues/labels/prs and etc.
185+
// TODO: add to this list after new git service added
186+
SupportedFullGitService = []GitServiceType{
187+
GithubService,
188+
}
189+
)
190+
183191
// MigrateRepoOption options for migrating a repository from an external service
184192
type MigrateRepoOption struct {
185193
// required: true

routers/api/v1/repo/repo.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package repo
88
import (
99
"fmt"
1010
"net/http"
11+
"net/url"
1112
"strings"
1213

1314
"code.gitea.io/gitea/models"
@@ -399,8 +400,8 @@ func Migrate(ctx *context.APIContext, form auth.MigrateRepoForm) {
399400
}
400401

401402
var gitServiceType = structs.PlainGitService
402-
// TODO: this should be chosen by UI when from a customerize git service domain
403-
if strings.HasPrefix(remoteAddr, "https://github.com") || strings.HasPrefix(remoteAddr, "http://github.com") {
403+
u, err := url.Parse(remoteAddr)
404+
if err == nil && strings.EqualFold(u.Host, "github.com") {
404405
gitServiceType = structs.GithubService
405406
}
406407

routers/user/auth.go

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"errors"
1010
"fmt"
1111
"net/http"
12-
"strconv"
1312
"strings"
1413

1514
"code.gitea.io/gitea/models"
@@ -20,9 +19,9 @@ import (
2019
"code.gitea.io/gitea/modules/log"
2120
"code.gitea.io/gitea/modules/recaptcha"
2221
"code.gitea.io/gitea/modules/setting"
23-
"code.gitea.io/gitea/modules/structs"
2422
"code.gitea.io/gitea/modules/timeutil"
2523
"code.gitea.io/gitea/modules/util"
24+
"code.gitea.io/gitea/services/externalaccount"
2625
"code.gitea.io/gitea/services/mailer"
2726

2827
"gitea.com/macaron/captcha"
@@ -279,7 +278,7 @@ func TwoFactorPost(ctx *context.Context, form auth.TwoFactorAuthForm) {
279278
return
280279
}
281280

282-
err = models.LinkAccountToUser(u, gothUser.(goth.User))
281+
err = externalaccount.LinkAccountToUser(u, gothUser.(goth.User))
283282
if err != nil {
284283
ctx.ServerError("UserSignIn", err)
285284
return
@@ -454,7 +453,7 @@ func U2FSign(ctx *context.Context, signResp u2f.SignResponse) {
454453
return
455454
}
456455

457-
err = models.LinkAccountToUser(user, gothUser.(goth.User))
456+
err = externalaccount.LinkAccountToUser(user, gothUser.(goth.User))
458457
if err != nil {
459458
ctx.ServerError("UserSignIn", err)
460459
return
@@ -627,21 +626,6 @@ func handleOAuth2SignIn(u *models.User, gothUser goth.User, ctx *context.Context
627626
return
628627
}
629628

630-
// update user's migrated comments and issues original id
631-
if gothUser.Provider == "github" {
632-
ids, err := models.FindMigratedRepositoryIDs(structs.GithubService)
633-
if err != nil {
634-
log.Error("FindMigratedRepositoryIDs failed: %v", err)
635-
} else {
636-
for _, id := range ids {
637-
userID, _ := strconv.ParseInt(gothUser.UserID, 10, 64)
638-
if err = migrations.UpdateGithubMigrations(id, userID, u.ID); err != nil {
639-
log.Error("UpdateGithubMigrations repo %v, github user id %v, user id %v failed: %v", id, gothUser.UserID, u.ID, err)
640-
}
641-
}
642-
}
643-
}
644-
645629
// update external user information
646630
if err := models.UpdateExternalUser(u, gothUser); err != nil {
647631
log.Error("UpdateExternalUser failed: %v", err)
@@ -817,7 +801,7 @@ func LinkAccountPostSignIn(ctx *context.Context, signInForm auth.SignInForm) {
817801
return
818802
}
819803

820-
err = models.LinkAccountToUser(u, gothUser.(goth.User))
804+
err = externalaccount.LinkAccountToUser(u, gothUser.(goth.User))
821805
if err != nil {
822806
ctx.ServerError("UserLinkAccount", err)
823807
return

0 commit comments

Comments
 (0)