diff --git a/custom/conf/app.ini.sample b/custom/conf/app.ini.sample
index 806b5a60ce2e..5393db479629 100644
--- a/custom/conf/app.ini.sample
+++ b/custom/conf/app.ini.sample
@@ -721,6 +721,11 @@ SCHEDULE = @every 24h
; or only create new users if UPDATE_EXISTING is set to false
UPDATE_EXISTING = true
+; Update migrated repositories' issues and comments' posterid, it will always attempt synchronization when the instance starts.
+[cron.update_migration_post_id]
+; Interval as a duration between each synchronization. (default every 24h)
+SCHEDULE = @every 24h
+
[git]
; The path of git executable. If empty, Gitea searches through the PATH environment.
PATH =
diff --git a/docs/content/doc/advanced/config-cheat-sheet.en-us.md b/docs/content/doc/advanced/config-cheat-sheet.en-us.md
index 3b13c1a4c6c8..489de80dc2e1 100644
--- a/docs/content/doc/advanced/config-cheat-sheet.en-us.md
+++ b/docs/content/doc/advanced/config-cheat-sheet.en-us.md
@@ -438,6 +438,10 @@ NB: You must `REDIRECT_MACARON_LOG` and have `DISABLE_ROUTER_LOG` set to `false`
- `RUN_AT_START`: **true**: Run repository statistics check at start time.
- `SCHEDULE`: **@every 24h**: Cron syntax for scheduling repository statistics check.
+### Cron - Update Migration Poster ID (`cron.update_migration_post_id`)
+
+- `SCHEDULE`: **@every 24h** : Interval as a duration between each synchronization, it will always attempt synchronization when the instance starts.
+
## Git (`git`)
- `PATH`: **""**: The path of git executable. If empty, Gitea searches through the PATH environment.
diff --git a/docs/content/doc/advanced/config-cheat-sheet.zh-cn.md b/docs/content/doc/advanced/config-cheat-sheet.zh-cn.md
index 01ba821a47a3..ab73e2059eca 100644
--- a/docs/content/doc/advanced/config-cheat-sheet.zh-cn.md
+++ b/docs/content/doc/advanced/config-cheat-sheet.zh-cn.md
@@ -196,7 +196,11 @@ menu:
### Cron - Repository Statistics Check (`cron.check_repo_stats`)
- `RUN_AT_START`: 是否启动时自动运行仓库统计。
-- `SCHEDULE`: 藏亏统计时的Cron 语法,比如:`@every 24h`.
+- `SCHEDULE`: 仓库统计时的Cron 语法,比如:`@every 24h`.
+
+### Cron - Update Migration Poster ID (`cron.update_migration_post_id`)
+
+- `SCHEDULE`: **@every 24h** : 每次同步的间隔时间。此任务总是在启动时自动进行。
## Git (`git`)
diff --git a/docs/content/doc/help/seek-help.en-us.md b/docs/content/doc/help/seek-help.en-us.md
index e9d0211029ea..058a5bb592b5 100644
--- a/docs/content/doc/help/seek-help.en-us.md
+++ b/docs/content/doc/help/seek-help.en-us.md
@@ -33,4 +33,4 @@ If you found a bug, please create an [issue on GitHub](https://github.com/go-git
## Chinese Support
-Support for the Chinese language is provided at [gocn.io](https://gocn.io/topic/Gitea).
+Support for the Chinese language is provided at [gocn.vip](https://gocn.vip/topic/gitea).
diff --git a/docs/content/doc/help/seek-help.zh-cn.md b/docs/content/doc/help/seek-help.zh-cn.md
index ac62b9b8cf68..7e4e2f1beb3a 100644
--- a/docs/content/doc/help/seek-help.zh-cn.md
+++ b/docs/content/doc/help/seek-help.zh-cn.md
@@ -18,6 +18,6 @@ menu:
如果您在使用或者开发过程中遇到问题,请到以下渠道咨询:
- 到[Github issue](https://github.com/go-gitea/gitea/issues)提问(因为项目维护人员来自世界各地,为保证沟通顺畅,请使用英文提问)
-- 中文问题到[gocn.io](https://gocn.io/topic/Gitea)提问
+- 中文问题到[gocn.vip](https://gocn.vip/topic/gitea)提问
- 访问 [Discord server - 英文](https://discord.gg/NsatcWJ)
- 加入 QQ群 328432459 获得进一步的支持
diff --git a/go.mod b/go.mod
index d2520da73982..203b6ce4c1bf 100644
--- a/go.mod
+++ b/go.mod
@@ -120,7 +120,7 @@ require (
gopkg.in/src-d/go-git.v4 v4.13.1
gopkg.in/stretchr/testify.v1 v1.2.2 // indirect
gopkg.in/testfixtures.v2 v2.5.0
- mvdan.cc/xurls/v2 v2.0.0
+ mvdan.cc/xurls/v2 v2.1.0
strk.kbt.io/projects/go/libravatar v0.0.0-20191008002943-06d1c002b251
xorm.io/builder v0.3.6
xorm.io/core v0.7.2
diff --git a/go.sum b/go.sum
index 3adf91e926bf..7306966800bb 100644
--- a/go.sum
+++ b/go.sum
@@ -812,8 +812,8 @@ honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWh
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.1-2019.2.2/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
-mvdan.cc/xurls/v2 v2.0.0 h1:r1zSOSNS/kqtpmATyMMMvaZ4/djsesbYz5kr0+qMRWc=
-mvdan.cc/xurls/v2 v2.0.0/go.mod h1:2/webFPYOXN9jp/lzuj0zuAVlF+9g4KPFJANH1oJhRU=
+mvdan.cc/xurls/v2 v2.1.0 h1:KaMb5GLhlcSX+e+qhbRJODnUUBvlw01jt4yrjFIHAuA=
+mvdan.cc/xurls/v2 v2.1.0/go.mod h1:5GrSd9rOnKOpZaji1OZLYL/yeAAtGDlo/cFe+8K5n8E=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
strk.kbt.io/projects/go/libravatar v0.0.0-20191008002943-06d1c002b251 h1:mUcz5b3FJbP5Cvdq7Khzn6J9OCUQJaBwgBkCR+MOwSs=
strk.kbt.io/projects/go/libravatar v0.0.0-20191008002943-06d1c002b251/go.mod h1:FJGmPh3vz9jSos1L/F91iAgnC/aejc0wIIrF2ZwJxdY=
diff --git a/integrations/issue_test.go b/integrations/issue_test.go
index 0b153607ee64..aa17c44254fc 100644
--- a/integrations/issue_test.go
+++ b/integrations/issue_test.go
@@ -13,6 +13,7 @@ import (
"testing"
"code.gitea.io/gitea/models"
+ "code.gitea.io/gitea/modules/references"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/test"
@@ -207,7 +208,7 @@ func TestIssueCrossReference(t *testing.T) {
RefIssueID: issueRef.ID,
RefCommentID: 0,
RefIsPull: false,
- RefAction: models.XRefActionNone})
+ RefAction: references.XRefActionNone})
// Edit title, neuter ref
testIssueChangeInfo(t, "user2", issueRefURL, "title", "Title no ref")
@@ -217,7 +218,7 @@ func TestIssueCrossReference(t *testing.T) {
RefIssueID: issueRef.ID,
RefCommentID: 0,
RefIsPull: false,
- RefAction: models.XRefActionNeutered})
+ RefAction: references.XRefActionNeutered})
// Ref from issue content
issueRefURL, issueRef = testIssueWithBean(t, "user2", 1, "TitleXRef", fmt.Sprintf("Description ref #%d", issueBase.Index))
@@ -227,7 +228,7 @@ func TestIssueCrossReference(t *testing.T) {
RefIssueID: issueRef.ID,
RefCommentID: 0,
RefIsPull: false,
- RefAction: models.XRefActionNone})
+ RefAction: references.XRefActionNone})
// Edit content, neuter ref
testIssueChangeInfo(t, "user2", issueRefURL, "content", "Description no ref")
@@ -237,7 +238,7 @@ func TestIssueCrossReference(t *testing.T) {
RefIssueID: issueRef.ID,
RefCommentID: 0,
RefIsPull: false,
- RefAction: models.XRefActionNeutered})
+ RefAction: references.XRefActionNeutered})
// Ref from a comment
session := loginUser(t, "user2")
@@ -248,7 +249,7 @@ func TestIssueCrossReference(t *testing.T) {
RefIssueID: issueRef.ID,
RefCommentID: commentID,
RefIsPull: false,
- RefAction: models.XRefActionNone}
+ RefAction: references.XRefActionNone}
models.AssertExistsAndLoadBean(t, comment)
// Ref from a different repository
@@ -259,7 +260,7 @@ func TestIssueCrossReference(t *testing.T) {
RefIssueID: issueRef.ID,
RefCommentID: 0,
RefIsPull: false,
- RefAction: models.XRefActionNone})
+ RefAction: references.XRefActionNone})
}
func testIssueWithBean(t *testing.T, user string, repoID int64, title, content string) (string, *models.Issue) {
diff --git a/models/action.go b/models/action.go
index 87088101f97f..2d2999f8809a 100644
--- a/models/action.go
+++ b/models/action.go
@@ -10,15 +10,14 @@ import (
"fmt"
"html"
"path"
- "regexp"
"strconv"
"strings"
"time"
- "unicode"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
+ "code.gitea.io/gitea/modules/references"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/timeutil"
@@ -54,29 +53,6 @@ const (
ActionMirrorSyncDelete // 20
)
-var (
- // Same as GitHub. See
- // https://help.github.com/articles/closing-issues-via-commit-messages
- issueCloseKeywords = []string{"close", "closes", "closed", "fix", "fixes", "fixed", "resolve", "resolves", "resolved"}
- issueReopenKeywords = []string{"reopen", "reopens", "reopened"}
-
- issueCloseKeywordsPat, issueReopenKeywordsPat *regexp.Regexp
- issueReferenceKeywordsPat *regexp.Regexp
-)
-
-const issueRefRegexpStr = `(?:([0-9a-zA-Z-_\.]+)/([0-9a-zA-Z-_\.]+))?(#[0-9]+)+`
-const issueRefRegexpStrNoKeyword = `(?:\s|^|\(|\[)(?:([0-9a-zA-Z-_\.]+)/([0-9a-zA-Z-_\.]+))?(#[0-9]+)(?:\s|$|\)|\]|:|\.(\s|$))`
-
-func assembleKeywordsPattern(words []string) string {
- return fmt.Sprintf(`(?i)(?:%s)(?::?) %s`, strings.Join(words, "|"), issueRefRegexpStr)
-}
-
-func init() {
- issueCloseKeywordsPat = regexp.MustCompile(assembleKeywordsPattern(issueCloseKeywords))
- issueReopenKeywordsPat = regexp.MustCompile(assembleKeywordsPattern(issueReopenKeywords))
- issueReferenceKeywordsPat = regexp.MustCompile(issueRefRegexpStrNoKeyword)
-}
-
// Action represents user operation type and other information to
// repository. It implemented interface base.Actioner so that can be
// used in template render.
@@ -351,10 +327,6 @@ func RenameRepoAction(actUser *User, oldRepoName string, repo *Repository) error
return renameRepoAction(x, actUser, oldRepoName, repo)
}
-func issueIndexTrimRight(c rune) bool {
- return !unicode.IsDigit(c)
-}
-
// PushCommit represents a commit in a push operation.
type PushCommit struct {
Sha1 string
@@ -480,39 +452,9 @@ func (pc *PushCommits) AvatarLink(email string) string {
}
// getIssueFromRef returns the issue referenced by a ref. Returns a nil *Issue
-// if the provided ref is misformatted or references a non-existent issue.
-func getIssueFromRef(repo *Repository, ref string) (*Issue, error) {
- ref = ref[strings.IndexByte(ref, ' ')+1:]
- ref = strings.TrimRightFunc(ref, issueIndexTrimRight)
-
- var refRepo *Repository
- poundIndex := strings.IndexByte(ref, '#')
- if poundIndex < 0 {
- return nil, nil
- } else if poundIndex == 0 {
- refRepo = repo
- } else {
- slashIndex := strings.IndexByte(ref, '/')
- if slashIndex < 0 || slashIndex >= poundIndex {
- return nil, nil
- }
- ownerName := ref[:slashIndex]
- repoName := ref[slashIndex+1 : poundIndex]
- var err error
- refRepo, err = GetRepositoryByOwnerAndName(ownerName, repoName)
- if err != nil {
- if IsErrRepoNotExist(err) {
- return nil, nil
- }
- return nil, err
- }
- }
- issueIndex, err := strconv.ParseInt(ref[poundIndex+1:], 10, 64)
- if err != nil {
- return nil, nil
- }
-
- issue, err := GetIssueByIndex(refRepo.ID, issueIndex)
+// if the provided ref references a non-existent issue.
+func getIssueFromRef(repo *Repository, index int64) (*Issue, error) {
+ issue, err := GetIssueByIndex(repo.ID, index)
if err != nil {
if IsErrIssueNotExist(err) {
return nil, nil
@@ -522,20 +464,7 @@ func getIssueFromRef(repo *Repository, ref string) (*Issue, error) {
return issue, nil
}
-func changeIssueStatus(repo *Repository, doer *User, ref string, refMarked map[int64]bool, status bool) error {
- issue, err := getIssueFromRef(repo, ref)
- if err != nil {
- return err
- }
-
- if issue == nil || refMarked[issue.ID] {
- return nil
- }
- refMarked[issue.ID] = true
-
- if issue.RepoID != repo.ID || issue.IsClosed == status {
- return nil
- }
+func changeIssueStatus(repo *Repository, issue *Issue, doer *User, status bool) error {
stopTimerIfAvailable := func(doer *User, issue *Issue) error {
@@ -549,7 +478,7 @@ func changeIssueStatus(repo *Repository, doer *User, ref string, refMarked map[i
}
issue.Repo = repo
- if err = issue.ChangeStatus(doer, status); err != nil {
+ if err := issue.ChangeStatus(doer, status); err != nil {
// Don't return an error when dependencies are open as this would let the push fail
if IsErrDependenciesLeft(err) {
return stopTimerIfAvailable(doer, issue)
@@ -566,99 +495,67 @@ func UpdateIssuesCommit(doer *User, repo *Repository, commits []*PushCommit, bra
for i := len(commits) - 1; i >= 0; i-- {
c := commits[i]
- refMarked := make(map[int64]bool)
+ type markKey struct {
+ ID int64
+ Action references.XRefAction
+ }
+
+ refMarked := make(map[markKey]bool)
var refRepo *Repository
+ var refIssue *Issue
var err error
- for _, m := range issueReferenceKeywordsPat.FindAllStringSubmatch(c.Message, -1) {
- if len(m[3]) == 0 {
- continue
- }
- ref := m[3]
+ for _, ref := range references.FindAllIssueReferences(c.Message) {
// issue is from another repo
- if len(m[1]) > 0 && len(m[2]) > 0 {
- refRepo, err = GetRepositoryFromMatch(m[1], m[2])
+ if len(ref.Owner) > 0 && len(ref.Name) > 0 {
+ refRepo, err = GetRepositoryFromMatch(ref.Owner, ref.Name)
if err != nil {
continue
}
} else {
refRepo = repo
}
- issue, err := getIssueFromRef(refRepo, ref)
- if err != nil {
+ if refIssue, err = getIssueFromRef(refRepo, ref.Index); err != nil {
return err
}
-
- if issue == nil || refMarked[issue.ID] {
+ if refIssue == nil {
continue
}
- refMarked[issue.ID] = true
- message := fmt.Sprintf(`%s`, repo.Link(), c.Sha1, html.EscapeString(c.Message))
- if err = CreateRefComment(doer, refRepo, issue, message, c.Sha1); err != nil {
+ perm, err := GetUserRepoPermission(refRepo, doer)
+ if err != nil {
return err
}
- }
- // Change issue status only if the commit has been pushed to the default branch.
- // and if the repo is configured to allow only that
- if repo.DefaultBranch != branchName && !repo.CloseIssuesViaCommitInAnyBranch {
- continue
- }
- refMarked = make(map[int64]bool)
- for _, m := range issueCloseKeywordsPat.FindAllStringSubmatch(c.Message, -1) {
- if len(m[3]) == 0 {
+ key := markKey{ID: refIssue.ID, Action: ref.Action}
+ if refMarked[key] {
continue
}
- ref := m[3]
+ refMarked[key] = true
- // issue is from another repo
- if len(m[1]) > 0 && len(m[2]) > 0 {
- refRepo, err = GetRepositoryFromMatch(m[1], m[2])
- if err != nil {
- continue
- }
- } else {
- refRepo = repo
- }
-
- perm, err := GetUserRepoPermission(refRepo, doer)
- if err != nil {
- return err
- }
- // only close issues in another repo if user has push access
- if perm.CanWrite(UnitTypeCode) {
- if err := changeIssueStatus(refRepo, doer, ref, refMarked, true); err != nil {
+ // only create comments for issues if user has permission for it
+ if perm.IsAdmin() || perm.IsOwner() || perm.CanWrite(UnitTypeIssues) {
+ message := fmt.Sprintf(`%s`, repo.Link(), c.Sha1, html.EscapeString(c.Message))
+ if err = CreateRefComment(doer, refRepo, refIssue, message, c.Sha1); err != nil {
return err
}
}
- }
- // It is conflict to have close and reopen at same time, so refsMarked doesn't need to reinit here.
- for _, m := range issueReopenKeywordsPat.FindAllStringSubmatch(c.Message, -1) {
- if len(m[3]) == 0 {
+ // Process closing/reopening keywords
+ if ref.Action != references.XRefActionCloses && ref.Action != references.XRefActionReopens {
continue
}
- ref := m[3]
- // issue is from another repo
- if len(m[1]) > 0 && len(m[2]) > 0 {
- refRepo, err = GetRepositoryFromMatch(m[1], m[2])
- if err != nil {
- continue
- }
- } else {
- refRepo = repo
- }
-
- perm, err := GetUserRepoPermission(refRepo, doer)
- if err != nil {
- return err
+ // Change issue status only if the commit has been pushed to the default branch.
+ // and if the repo is configured to allow only that
+ // FIXME: we should be using Issue.ref if set instead of repo.DefaultBranch
+ if repo.DefaultBranch != branchName && !repo.CloseIssuesViaCommitInAnyBranch {
+ continue
}
- // only reopen issues in another repo if user has push access
- if perm.CanWrite(UnitTypeCode) {
- if err := changeIssueStatus(refRepo, doer, ref, refMarked, false); err != nil {
+ // only close issues in another repo if user has push access
+ if perm.IsAdmin() || perm.IsOwner() || perm.CanWrite(UnitTypeCode) {
+ if err := changeIssueStatus(refRepo, refIssue, doer, ref.Action == references.XRefActionCloses); err != nil {
return err
}
}
diff --git a/models/action_test.go b/models/action_test.go
index c90538ebe602..df41556850d0 100644
--- a/models/action_test.go
+++ b/models/action_test.go
@@ -1,7 +1,6 @@
package models
import (
- "fmt"
"path"
"strings"
"testing"
@@ -181,56 +180,6 @@ func TestPushCommits_AvatarLink(t *testing.T) {
pushCommits.AvatarLink("nonexistent@example.com"))
}
-func TestRegExp_issueReferenceKeywordsPat(t *testing.T) {
- trueTestCases := []string{
- "#2",
- "[#2]",
- "please see go-gitea/gitea#5",
- "#2:",
- }
- falseTestCases := []string{
- "kb#2",
- "#2xy",
- }
-
- for _, testCase := range trueTestCases {
- assert.True(t, issueReferenceKeywordsPat.MatchString(testCase))
- }
- for _, testCase := range falseTestCases {
- assert.False(t, issueReferenceKeywordsPat.MatchString(testCase))
- }
-}
-
-func Test_getIssueFromRef(t *testing.T) {
- assert.NoError(t, PrepareTestDatabase())
- repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
- for _, test := range []struct {
- Ref string
- ExpectedIssueID int64
- }{
- {"#2", 2},
- {"reopen #2", 2},
- {"user2/repo2#1", 4},
- {"fixes user2/repo2#1", 4},
- {"fixes: user2/repo2#1", 4},
- } {
- issue, err := getIssueFromRef(repo, test.Ref)
- assert.NoError(t, err)
- if assert.NotNil(t, issue) {
- assert.EqualValues(t, test.ExpectedIssueID, issue.ID)
- }
- }
-
- for _, badRef := range []string{
- "doesnotexist/doesnotexist#1",
- fmt.Sprintf("#%d", NonexistentID),
- } {
- issue, err := getIssueFromRef(repo, badRef)
- assert.NoError(t, err)
- assert.Nil(t, issue)
- }
-}
-
func TestUpdateIssuesCommit(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
pushCommits := []*PushCommit{
@@ -431,7 +380,7 @@ func TestUpdateIssuesCommit_AnotherRepoNoPermission(t *testing.T) {
AssertNotExistsBean(t, commentBean)
AssertNotExistsBean(t, issueBean, "is_closed=1")
assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, repo.DefaultBranch))
- AssertExistsAndLoadBean(t, commentBean)
+ AssertNotExistsBean(t, commentBean)
AssertNotExistsBean(t, issueBean, "is_closed=1")
CheckConsistencyFor(t, &Action{})
}
diff --git a/models/external_login_user.go b/models/external_login_user.go
index 21a3cbbd312b..59c37321844f 100644
--- a/models/external_login_user.go
+++ b/models/external_login_user.go
@@ -4,13 +4,34 @@
package models
-import "github.com/markbates/goth"
+import (
+ "time"
+
+ "code.gitea.io/gitea/modules/structs"
+
+ "github.com/markbates/goth"
+ "xorm.io/builder"
+)
// ExternalLoginUser makes the connecting between some existing user and additional external login sources
type ExternalLoginUser struct {
- ExternalID string `xorm:"pk NOT NULL"`
- UserID int64 `xorm:"INDEX NOT NULL"`
- LoginSourceID int64 `xorm:"pk NOT NULL"`
+ ExternalID string `xorm:"pk NOT NULL"`
+ UserID int64 `xorm:"INDEX NOT NULL"`
+ LoginSourceID int64 `xorm:"pk NOT NULL"`
+ RawData map[string]interface{} `xorm:"TEXT JSON"`
+ Provider string `xorm:"index VARCHAR(25)"`
+ Email string
+ Name string
+ FirstName string
+ LastName string
+ NickName string
+ Description string
+ AvatarURL string
+ Location string
+ AccessToken string
+ AccessTokenSecret string
+ RefreshToken string
+ ExpiresAt time.Time
}
// GetExternalLogin checks if a externalID in loginSourceID scope already exists
@@ -32,23 +53,15 @@ func ListAccountLinks(user *User) ([]*ExternalLoginUser, error) {
return externalAccounts, nil
}
-// LinkAccountToUser link the gothUser to the user
-func LinkAccountToUser(user *User, gothUser goth.User) error {
- loginSource, err := GetActiveOAuth2LoginSourceByName(gothUser.Provider)
- if err != nil {
- return err
- }
-
- externalLoginUser := &ExternalLoginUser{
- ExternalID: gothUser.UserID,
- UserID: user.ID,
- LoginSourceID: loginSource.ID,
- }
- has, err := x.Get(externalLoginUser)
+// LinkExternalToUser link the external user to the user
+func LinkExternalToUser(user *User, externalLoginUser *ExternalLoginUser) error {
+ has, err := x.Where("external_id=? AND login_source_id=?", externalLoginUser.ExternalID, externalLoginUser.LoginSourceID).
+ NoAutoCondition().
+ Exist(externalLoginUser)
if err != nil {
return err
} else if has {
- return ErrExternalLoginUserAlreadyExist{gothUser.UserID, user.ID, loginSource.ID}
+ return ErrExternalLoginUserAlreadyExist{externalLoginUser.ExternalID, user.ID, externalLoginUser.LoginSourceID}
}
_, err = x.Insert(externalLoginUser)
@@ -72,3 +85,97 @@ func removeAllAccountLinks(e Engine, user *User) error {
_, err := e.Delete(&ExternalLoginUser{UserID: user.ID})
return err
}
+
+// GetUserIDByExternalUserID get user id according to provider and userID
+func GetUserIDByExternalUserID(provider string, userID string) (int64, error) {
+ var id int64
+ _, err := x.Table("external_login_user").
+ Select("user_id").
+ Where("provider=?", provider).
+ And("external_id=?", userID).
+ Get(&id)
+ if err != nil {
+ return 0, err
+ }
+ return id, nil
+}
+
+// UpdateExternalUser updates external user's information
+func UpdateExternalUser(user *User, gothUser goth.User) error {
+ loginSource, err := GetActiveOAuth2LoginSourceByName(gothUser.Provider)
+ if err != nil {
+ return err
+ }
+ externalLoginUser := &ExternalLoginUser{
+ ExternalID: gothUser.UserID,
+ UserID: user.ID,
+ LoginSourceID: loginSource.ID,
+ RawData: gothUser.RawData,
+ Provider: gothUser.Provider,
+ Email: gothUser.Email,
+ Name: gothUser.Name,
+ FirstName: gothUser.FirstName,
+ LastName: gothUser.LastName,
+ NickName: gothUser.NickName,
+ Description: gothUser.Description,
+ AvatarURL: gothUser.AvatarURL,
+ Location: gothUser.Location,
+ AccessToken: gothUser.AccessToken,
+ AccessTokenSecret: gothUser.AccessTokenSecret,
+ RefreshToken: gothUser.RefreshToken,
+ ExpiresAt: gothUser.ExpiresAt,
+ }
+
+ has, err := x.Where("external_id=? AND login_source_id=?", gothUser.UserID, loginSource.ID).
+ NoAutoCondition().
+ Exist(externalLoginUser)
+ if err != nil {
+ return err
+ } else if !has {
+ return ErrExternalLoginUserNotExist{user.ID, loginSource.ID}
+ }
+
+ _, err = x.Where("external_id=? AND login_source_id=?", gothUser.UserID, loginSource.ID).AllCols().Update(externalLoginUser)
+ return err
+}
+
+// FindExternalUserOptions represents an options to find external users
+type FindExternalUserOptions struct {
+ Provider string
+ Limit int
+ Start int
+}
+
+func (opts FindExternalUserOptions) toConds() builder.Cond {
+ var cond = builder.NewCond()
+ if len(opts.Provider) > 0 {
+ cond = cond.And(builder.Eq{"provider": opts.Provider})
+ }
+ return cond
+}
+
+// FindExternalUsersByProvider represents external users via provider
+func FindExternalUsersByProvider(opts FindExternalUserOptions) ([]ExternalLoginUser, error) {
+ var users []ExternalLoginUser
+ err := x.Where(opts.toConds()).
+ Limit(opts.Limit, opts.Start).
+ OrderBy("login_source_id ASC, external_id ASC").
+ Find(&users)
+ if err != nil {
+ return nil, err
+ }
+ return users, nil
+}
+
+// UpdateMigrationsByType updates all migrated repositories' posterid from gitServiceType to replace originalAuthorID to posterID
+func UpdateMigrationsByType(tp structs.GitServiceType, externalUserID, userID int64) error {
+ if err := UpdateIssuesMigrationsByType(tp, externalUserID, userID); err != nil {
+ return err
+ }
+
+ if err := UpdateCommentsMigrationsByType(tp, externalUserID, userID); err != nil {
+ return err
+ }
+
+ return UpdateReleasesMigrationsByType(tp, externalUserID, userID)
+}
diff --git a/models/issue.go b/models/issue.go
index 8ce7d496ab46..fc675a3ffbe2 100644
--- a/models/issue.go
+++ b/models/issue.go
@@ -14,6 +14,7 @@ import (
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
+ "code.gitea.io/gitea/modules/structs"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/util"
@@ -32,7 +33,7 @@ type Issue struct {
PosterID int64 `xorm:"INDEX"`
Poster *User `xorm:"-"`
OriginalAuthor string
- OriginalAuthorID int64
+ OriginalAuthorID int64 `xorm:"index"`
Title string `xorm:"name"`
Content string `xorm:"TEXT"`
RenderedContent string `xorm:"-"`
@@ -1947,3 +1948,16 @@ func (issue *Issue) ResolveMentionsByVisibility(ctx DBContext, doer *User, menti
return
}
+
+// UpdateIssuesMigrationsByType updates all migrated repositories' issues from gitServiceType to replace originalAuthorID to posterID
+func UpdateIssuesMigrationsByType(gitServiceType structs.GitServiceType, originalAuthorID, posterID int64) error {
+ _, err := x.Table("issue").
+ Where("repo_id IN (SELECT id FROM repository WHERE original_service_type = ?)", gitServiceType).
+ And("original_author_id = ?", originalAuthorID).
+ Update(map[string]interface{}{
+ "poster_id": posterID,
+ "original_author": "",
+ "original_author_id": 0,
+ })
+ return err
+}
diff --git a/models/issue_comment.go b/models/issue_comment.go
index e8043c1ec7f3..3a090c3b19fd 100644
--- a/models/issue_comment.go
+++ b/models/issue_comment.go
@@ -13,6 +13,8 @@ import (
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/markup/markdown"
+ "code.gitea.io/gitea/modules/references"
+ "code.gitea.io/gitea/modules/structs"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/timeutil"
@@ -144,10 +146,10 @@ type Comment struct {
// Reference an issue or pull from another comment, issue or PR
// All information is about the origin of the reference
- RefRepoID int64 `xorm:"index"` // Repo where the referencing
- RefIssueID int64 `xorm:"index"`
- RefCommentID int64 `xorm:"index"` // 0 if origin is Issue title or content (or PR's)
- RefAction XRefAction `xorm:"SMALLINT"` // What hapens if RefIssueID resolves
+ RefRepoID int64 `xorm:"index"` // Repo where the referencing
+ RefIssueID int64 `xorm:"index"`
+ RefCommentID int64 `xorm:"index"` // 0 if origin is Issue title or content (or PR's)
+ RefAction references.XRefAction `xorm:"SMALLINT"` // What hapens if RefIssueID resolves
RefIsPull bool
RefRepo *Repository `xorm:"-"`
@@ -773,7 +775,7 @@ type CreateCommentOptions struct {
RefRepoID int64
RefIssueID int64
RefCommentID int64
- RefAction XRefAction
+ RefAction references.XRefAction
RefIsPull bool
}
@@ -1021,3 +1023,23 @@ func fetchCodeCommentsByReview(e Engine, issue *Issue, currentUser *User, review
func FetchCodeComments(issue *Issue, currentUser *User) (CodeComments, error) {
return fetchCodeComments(x, issue, currentUser)
}
+
+// UpdateCommentsMigrationsByType updates comments' migrations information via given git service type and original id and poster id
+func UpdateCommentsMigrationsByType(tp structs.GitServiceType, originalAuthorID, posterID int64) error {
+ _, err := x.Table("comment").
+ Where(builder.In("issue_id",
+ builder.Select("issue.id").
+ From("issue").
+ InnerJoin("repository", "issue.repo_id = repository.id").
+ Where(builder.Eq{
+ "repository.original_service_type": tp,
+ }),
+ )).
+ And("comment.original_author_id = ?", originalAuthorID).
+ Update(map[string]interface{}{
+ "poster_id": posterID,
+ "original_author": "",
+ "original_author_id": 0,
+ })
+ return err
+}
diff --git a/models/issue_xref.go b/models/issue_xref.go
index 1cc0bcfe6a11..141a7e0e8cc7 100644
--- a/models/issue_xref.go
+++ b/models/issue_xref.go
@@ -5,42 +5,16 @@
package models
import (
- "regexp"
- "strconv"
-
"code.gitea.io/gitea/modules/log"
+ "code.gitea.io/gitea/modules/references"
"github.com/go-xorm/xorm"
"github.com/unknwon/com"
)
-var (
- // TODO: Unify all regexp treatment of cross references in one place
-
- // issueNumericPattern matches string that references to a numeric issue, e.g. #1287
- issueNumericPattern = regexp.MustCompile(`(?:\s|^|\(|\[)(?:#)([0-9]+)(?:\s|$|\)|\]|:|\.(\s|$))`)
- // crossReferenceIssueNumericPattern matches string that references a numeric issue in a different repository
- // e.g. gogits/gogs#12345
- crossReferenceIssueNumericPattern = regexp.MustCompile(`(?:\s|^|\(|\[)([0-9a-zA-Z-_\.]+)/([0-9a-zA-Z-_\.]+)#([0-9]+)(?:\s|$|\)|\]|\.(\s|$))`)
-)
-
-// XRefAction represents the kind of effect a cross reference has once is resolved
-type XRefAction int64
-
-const (
- // XRefActionNone means the cross-reference is a mention (commit, etc.)
- XRefActionNone XRefAction = iota // 0
- // XRefActionCloses means the cross-reference should close an issue if it is resolved
- XRefActionCloses // 1 - not implemented yet
- // XRefActionReopens means the cross-reference should reopen an issue if it is resolved
- XRefActionReopens // 2 - Not implemented yet
- // XRefActionNeutered means the cross-reference will no longer affect the source
- XRefActionNeutered // 3
-)
-
type crossReference struct {
Issue *Issue
- Action XRefAction
+ Action references.XRefAction
}
// crossReferencesContext is context to pass along findCrossReference functions
@@ -72,7 +46,7 @@ func newCrossReference(e *xorm.Session, ctx *crossReferencesContext, xref *cross
func neuterCrossReferences(e Engine, issueID int64, commentID int64) error {
active := make([]*Comment, 0, 10)
- sess := e.Where("`ref_action` IN (?, ?, ?)", XRefActionNone, XRefActionCloses, XRefActionReopens)
+ sess := e.Where("`ref_action` IN (?, ?, ?)", references.XRefActionNone, references.XRefActionCloses, references.XRefActionReopens)
if issueID != 0 {
sess = sess.And("`ref_issue_id` = ?", issueID)
}
@@ -86,7 +60,7 @@ func neuterCrossReferences(e Engine, issueID int64, commentID int64) error {
for i, c := range active {
ids[i] = c.ID
}
- _, err := e.In("id", ids).Cols("`ref_action`").Update(&Comment{RefAction: XRefActionNeutered})
+ _, err := e.In("id", ids).Cols("`ref_action`").Update(&Comment{RefAction: references.XRefActionNeutered})
return err
}
@@ -110,11 +84,11 @@ func (issue *Issue) addCrossReferences(e *xorm.Session, doer *User) error {
Doer: doer,
OrigIssue: issue,
}
- return issue.createCrossReferences(e, ctx, issue.Title+"\n"+issue.Content)
+ return issue.createCrossReferences(e, ctx, issue.Title, issue.Content)
}
-func (issue *Issue) createCrossReferences(e *xorm.Session, ctx *crossReferencesContext, content string) error {
- xreflist, err := ctx.OrigIssue.getCrossReferences(e, ctx, content)
+func (issue *Issue) createCrossReferences(e *xorm.Session, ctx *crossReferencesContext, plaincontent, mdcontent string) error {
+ xreflist, err := ctx.OrigIssue.getCrossReferences(e, ctx, plaincontent, mdcontent)
if err != nil {
return err
}
@@ -126,47 +100,43 @@ func (issue *Issue) createCrossReferences(e *xorm.Session, ctx *crossReferencesC
return nil
}
-func (issue *Issue) getCrossReferences(e *xorm.Session, ctx *crossReferencesContext, content string) ([]*crossReference, error) {
+func (issue *Issue) getCrossReferences(e *xorm.Session, ctx *crossReferencesContext, plaincontent, mdcontent string) ([]*crossReference, error) {
xreflist := make([]*crossReference, 0, 5)
- var xref *crossReference
-
- // Issues in the same repository
- // FIXME: Should we support IssueNameStyleAlphanumeric?
- matches := issueNumericPattern.FindAllStringSubmatch(content, -1)
- for _, match := range matches {
- if index, err := strconv.ParseInt(match[1], 10, 64); err == nil {
- if err = ctx.OrigIssue.loadRepo(e); err != nil {
+ var (
+ refRepo *Repository
+ refIssue *Issue
+ err error
+ )
+
+ allrefs := append(references.FindAllIssueReferences(plaincontent), references.FindAllIssueReferencesMarkdown(mdcontent)...)
+
+ for _, ref := range allrefs {
+ if ref.Owner == "" && ref.Name == "" {
+ // Issues in the same repository
+ if err := ctx.OrigIssue.loadRepo(e); err != nil {
return nil, err
}
- if xref, err = ctx.OrigIssue.isValidCommentReference(e, ctx, issue.Repo, index); err != nil {
- return nil, err
- }
- if xref != nil {
- xreflist = ctx.OrigIssue.updateCrossReferenceList(xreflist, xref)
- }
- }
- }
-
- // Issues in other repositories
- matches = crossReferenceIssueNumericPattern.FindAllStringSubmatch(content, -1)
- for _, match := range matches {
- if index, err := strconv.ParseInt(match[3], 10, 64); err == nil {
- repo, err := getRepositoryByOwnerAndName(e, match[1], match[2])
+ refRepo = ctx.OrigIssue.Repo
+ } else {
+ // Issues in other repositories
+ refRepo, err = getRepositoryByOwnerAndName(e, ref.Owner, ref.Name)
if err != nil {
if IsErrRepoNotExist(err) {
continue
}
return nil, err
}
- if err = ctx.OrigIssue.loadRepo(e); err != nil {
- return nil, err
- }
- if xref, err = issue.isValidCommentReference(e, ctx, repo, index); err != nil {
- return nil, err
- }
- if xref != nil {
- xreflist = issue.updateCrossReferenceList(xreflist, xref)
- }
+ }
+ if refIssue, err = ctx.OrigIssue.findReferencedIssue(e, ctx, refRepo, ref.Index); err != nil {
+ return nil, err
+ }
+ if refIssue != nil {
+ xreflist = ctx.OrigIssue.updateCrossReferenceList(xreflist, &crossReference{
+ Issue: refIssue,
+ // FIXME: currently ignore keywords
+ // Action: ref.Action,
+ Action: references.XRefActionNone,
+ })
}
}
@@ -179,7 +149,7 @@ func (issue *Issue) updateCrossReferenceList(list []*crossReference, xref *cross
}
for i, r := range list {
if r.Issue.ID == xref.Issue.ID {
- if xref.Action != XRefActionNone {
+ if xref.Action != references.XRefActionNone {
list[i].Action = xref.Action
}
return list
@@ -188,7 +158,7 @@ func (issue *Issue) updateCrossReferenceList(list []*crossReference, xref *cross
return append(list, xref)
}
-func (issue *Issue) isValidCommentReference(e Engine, ctx *crossReferencesContext, repo *Repository, index int64) (*crossReference, error) {
+func (issue *Issue) findReferencedIssue(e Engine, ctx *crossReferencesContext, repo *Repository, index int64) (*Issue, error) {
refIssue := &Issue{RepoID: repo.ID, Index: index}
if has, _ := e.Get(refIssue); !has {
return nil, nil
@@ -206,10 +176,7 @@ func (issue *Issue) isValidCommentReference(e Engine, ctx *crossReferencesContex
return nil, nil
}
}
- return &crossReference{
- Issue: refIssue,
- Action: XRefActionNone,
- }, nil
+ return refIssue, nil
}
func (issue *Issue) neuterCrossReferences(e Engine) error {
@@ -237,7 +204,7 @@ func (comment *Comment) addCrossReferences(e *xorm.Session, doer *User) error {
OrigIssue: comment.Issue,
OrigComment: comment,
}
- return comment.Issue.createCrossReferences(e, ctx, comment.Content)
+ return comment.Issue.createCrossReferences(e, ctx, "", comment.Content)
}
func (comment *Comment) neuterCrossReferences(e Engine) error {
diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go
index ef5cd377a6c1..60a416c6e92e 100644
--- a/models/migrations/migrations.go
+++ b/models/migrations/migrations.go
@@ -254,6 +254,8 @@ var migrations = []Migration{
NewMigration("add original author name and id on migrated release", addOriginalAuthorOnMigratedReleases),
// v99 -> v100
NewMigration("add task table and status column for repository table", addTaskTable),
+ // v100 -> v101
+ NewMigration("update migration repositories' service type", updateMigrationServiceTypes),
}
// Migrate database to current version
diff --git a/models/migrations/v100.go b/models/migrations/v100.go
new file mode 100644
index 000000000000..ac3b73e2ad2a
--- /dev/null
+++ b/models/migrations/v100.go
@@ -0,0 +1,83 @@
+// Copyright 2019 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 migrations
+
+import (
+ "net/url"
+ "strings"
+ "time"
+
+ "github.com/go-xorm/xorm"
+)
+
+func updateMigrationServiceTypes(x *xorm.Engine) error {
+ type Repository struct {
+ ID int64
+ OriginalServiceType int `xorm:"index default(0)"`
+ OriginalURL string `xorm:"VARCHAR(2048)"`
+ }
+
+ if err := x.Sync2(new(Repository)); err != nil {
+ return err
+ }
+
+ var last int
+ const batchSize = 50
+ for {
+ var results = make([]Repository, 0, batchSize)
+ err := x.Where("original_url <> '' AND original_url IS NOT NULL").
+ And("original_service_type = 0 OR original_service_type IS NULL").
+ OrderBy("id").
+ Limit(batchSize, last).
+ Find(&results)
+ if err != nil {
+ return err
+ }
+ if len(results) == 0 {
+ break
+ }
+ last += len(results)
+
+ const PlainGitService = 1 // 1 plain git service
+ const GithubService = 2 // 2 github.com
+
+ for _, res := range results {
+ u, err := url.Parse(res.OriginalURL)
+ if err != nil {
+ return err
+ }
+ var serviceType = PlainGitService
+ if strings.EqualFold(u.Host, "github.com") {
+ serviceType = GithubService
+ }
+ _, err = x.Exec("UPDATE repository SET original_service_type = ? WHERE id = ?", serviceType, res.ID)
+ if err != nil {
+ return err
+ }
+ }
+ }
+
+ type ExternalLoginUser struct {
+ ExternalID string `xorm:"pk NOT NULL"`
+ UserID int64 `xorm:"INDEX NOT NULL"`
+ LoginSourceID int64 `xorm:"pk NOT NULL"`
+ RawData map[string]interface{} `xorm:"TEXT JSON"`
+ Provider string `xorm:"index VARCHAR(25)"`
+ Email string
+ Name string
+ FirstName string
+ LastName string
+ NickName string
+ Description string
+ AvatarURL string
+ Location string
+ AccessToken string
+ AccessTokenSecret string
+ RefreshToken string
+ ExpiresAt time.Time
+ }
+
+ return x.Sync2(new(ExternalLoginUser))
+}
diff --git a/models/release.go b/models/release.go
index 243cc2fa3c31..03685e0a44c7 100644
--- a/models/release.go
+++ b/models/release.go
@@ -12,6 +12,7 @@ import (
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/setting"
+ "code.gitea.io/gitea/modules/structs"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/timeutil"
@@ -366,3 +367,16 @@ func SyncReleasesWithTags(repo *Repository, gitRepo *git.Repository) error {
}
return nil
}
+
+// UpdateReleasesMigrationsByType updates all migrated repositories' releases from gitServiceType to replace originalAuthorID to posterID
+func UpdateReleasesMigrationsByType(gitServiceType structs.GitServiceType, originalAuthorID, posterID int64) error {
+ _, err := x.Table("release").
+ Where("repo_id IN (SELECT id FROM repository WHERE original_service_type = ?)", gitServiceType).
+ And("original_author_id = ?", originalAuthorID).
+ Update(map[string]interface{}{
+ "publisher_id": posterID,
+ "original_author": "",
+ "original_author_id": 0,
+ })
+ return err
+}
diff --git a/models/repo.go b/models/repo.go
index eab6338418d4..48e364d6f049 100644
--- a/models/repo.go
+++ b/models/repo.go
@@ -32,6 +32,7 @@ import (
"code.gitea.io/gitea/modules/options"
"code.gitea.io/gitea/modules/process"
"code.gitea.io/gitea/modules/setting"
+ "code.gitea.io/gitea/modules/structs"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/sync"
"code.gitea.io/gitea/modules/timeutil"
@@ -138,16 +139,17 @@ const (
// Repository represents a git repository.
type Repository struct {
- ID int64 `xorm:"pk autoincr"`
- OwnerID int64 `xorm:"UNIQUE(s) index"`
- OwnerName string `xorm:"-"`
- Owner *User `xorm:"-"`
- LowerName string `xorm:"UNIQUE(s) INDEX NOT NULL"`
- Name string `xorm:"INDEX NOT NULL"`
- Description string `xorm:"TEXT"`
- Website string `xorm:"VARCHAR(2048)"`
- OriginalURL string `xorm:"VARCHAR(2048)"`
- DefaultBranch string
+ ID int64 `xorm:"pk autoincr"`
+ OwnerID int64 `xorm:"UNIQUE(s) index"`
+ OwnerName string `xorm:"-"`
+ Owner *User `xorm:"-"`
+ LowerName string `xorm:"UNIQUE(s) INDEX NOT NULL"`
+ Name string `xorm:"INDEX NOT NULL"`
+ Description string `xorm:"TEXT"`
+ Website string `xorm:"VARCHAR(2048)"`
+ OriginalServiceType structs.GitServiceType `xorm:"index"`
+ OriginalURL string `xorm:"VARCHAR(2048)"`
+ DefaultBranch string
NumWatches int
NumStars int
diff --git a/modules/cron/cron.go b/modules/cron/cron.go
index 089f0fa767a6..795fafb51fda 100644
--- a/modules/cron/cron.go
+++ b/modules/cron/cron.go
@@ -10,6 +10,7 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/log"
+ "code.gitea.io/gitea/modules/migrations"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/sync"
mirror_service "code.gitea.io/gitea/services/mirror"
@@ -18,12 +19,13 @@ import (
)
const (
- mirrorUpdate = "mirror_update"
- gitFsck = "git_fsck"
- checkRepos = "check_repos"
- archiveCleanup = "archive_cleanup"
- syncExternalUsers = "sync_external_users"
- deletedBranchesCleanup = "deleted_branches_cleanup"
+ mirrorUpdate = "mirror_update"
+ gitFsck = "git_fsck"
+ checkRepos = "check_repos"
+ archiveCleanup = "archive_cleanup"
+ syncExternalUsers = "sync_external_users"
+ deletedBranchesCleanup = "deleted_branches_cleanup"
+ updateMigrationPosterID = "update_migration_post_id"
)
var c = cron.New()
@@ -117,6 +119,15 @@ func NewContext() {
go WithUnique(deletedBranchesCleanup, models.RemoveOldDeletedBranches)()
}
}
+
+ entry, err = c.AddFunc("Update migrated repositories' issues and comments' posterid", setting.Cron.UpdateMigrationPosterID.Schedule, WithUnique(updateMigrationPosterID, migrations.UpdateMigrationPosterID))
+ if err != nil {
+ log.Fatal("Cron[Update migrated repositories]: %v", err)
+ }
+ entry.Prev = time.Now()
+ entry.ExecTimes++
+ go WithUnique(updateMigrationPosterID, migrations.UpdateMigrationPosterID)()
+
c.Start()
}
diff --git a/modules/markup/html.go b/modules/markup/html.go
index f07993bc4cc8..fc823b1f308a 100644
--- a/modules/markup/html.go
+++ b/modules/markup/html.go
@@ -15,6 +15,7 @@ import (
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
+ "code.gitea.io/gitea/modules/references"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
@@ -36,17 +37,6 @@ var (
// While fast, this is also incorrect and lead to false positives.
// TODO: fix invalid linking issue
- // mentionPattern matches all mentions in the form of "@user"
- mentionPattern = regexp.MustCompile(`(?:\s|^|\(|\[)(@[0-9a-zA-Z-_\.]+)(?:\s|$|\)|\])`)
-
- // issueNumericPattern matches string that references to a numeric issue, e.g. #1287
- issueNumericPattern = regexp.MustCompile(`(?:\s|^|\(|\[)(#[0-9]+)(?:\s|$|\)|\]|:|\.(\s|$))`)
- // issueAlphanumericPattern matches string that references to an alphanumeric issue, e.g. ABC-1234
- issueAlphanumericPattern = regexp.MustCompile(`(?:\s|^|\(|\[)([A-Z]{1,10}-[1-9][0-9]*)(?:\s|$|\)|\]|:|\.(\s|$))`)
- // crossReferenceIssueNumericPattern matches string that references a numeric issue in a different repository
- // e.g. gogits/gogs#12345
- crossReferenceIssueNumericPattern = regexp.MustCompile(`(?:\s|^|\(|\[)([0-9a-zA-Z-_\.]+/[0-9a-zA-Z-_\.]+#[0-9]+)(?:\s|$|\)|\]|\.(\s|$))`)
-
// sha1CurrentPattern matches string that represents a commit SHA, e.g. d8a994ef243349f321568f9e36d5c3f444b99cae
// Although SHA1 hashes are 40 chars long, the regex matches the hash from 7 to 40 chars in length
// so that abbreviated hash links can be used as well. This matches git and github useability.
@@ -70,6 +60,9 @@ var (
linkRegex, _ = xurls.StrictMatchingScheme("https?://")
)
+// CSS class for action keywords (e.g. "closes: #1")
+const keywordClass = "issue-keyword"
+
// regexp for full links to issues/pulls
var issueFullPattern *regexp.Regexp
@@ -99,17 +92,6 @@ func getIssueFullPattern() *regexp.Regexp {
return issueFullPattern
}
-// FindAllMentions matches mention patterns in given content
-// and returns a list of found user names without @ prefix.
-func FindAllMentions(content string) []string {
- mentions := mentionPattern.FindAllStringSubmatch(content, -1)
- ret := make([]string, len(mentions))
- for i, val := range mentions {
- ret[i] = val[1][1:]
- }
- return ret
-}
-
// IsSameDomain checks if given url string has the same hostname as current Gitea instance
func IsSameDomain(s string) bool {
if strings.HasPrefix(s, "/") {
@@ -142,7 +124,6 @@ var defaultProcessors = []processor{
linkProcessor,
mentionProcessor,
issueIndexPatternProcessor,
- crossReferenceIssueIndexPatternProcessor,
sha1CurrentPatternProcessor,
emailAddressProcessor,
}
@@ -183,7 +164,6 @@ var commitMessageProcessors = []processor{
linkProcessor,
mentionProcessor,
issueIndexPatternProcessor,
- crossReferenceIssueIndexPatternProcessor,
sha1CurrentPatternProcessor,
emailAddressProcessor,
}
@@ -217,7 +197,6 @@ var commitMessageSubjectProcessors = []processor{
linkProcessor,
mentionProcessor,
issueIndexPatternProcessor,
- crossReferenceIssueIndexPatternProcessor,
sha1CurrentPatternProcessor,
}
@@ -330,6 +309,24 @@ func (ctx *postProcessCtx) textNode(node *html.Node) {
}
}
+// createKeyword() renders a highlighted version of an action keyword
+func createKeyword(content string) *html.Node {
+ span := &html.Node{
+ Type: html.ElementNode,
+ Data: atom.Span.String(),
+ Attr: []html.Attribute{},
+ }
+ span.Attr = append(span.Attr, html.Attribute{Key: "class", Val: keywordClass})
+
+ text := &html.Node{
+ Type: html.TextNode,
+ Data: content,
+ }
+ span.AppendChild(text)
+
+ return span
+}
+
func createLink(href, content, class string) *html.Node {
a := &html.Node{
Type: html.ElementNode,
@@ -377,10 +374,16 @@ func createCodeLink(href, content, class string) *html.Node {
return a
}
-// replaceContent takes a text node, and in its content it replaces a section of
-// it with the specified newNode. An example to visualize how this can work can
-// be found here: https://play.golang.org/p/5zP8NnHZ03s
+// replaceContent takes text node, and in its content it replaces a section of
+// it with the specified newNode.
func replaceContent(node *html.Node, i, j int, newNode *html.Node) {
+ replaceContentList(node, i, j, []*html.Node{newNode})
+}
+
+// replaceContentList takes text node, and in its content it replaces a section of
+// it with the specified newNodes. An example to visualize how this can work can
+// be found here: https://play.golang.org/p/5zP8NnHZ03s
+func replaceContentList(node *html.Node, i, j int, newNodes []*html.Node) {
// get the data before and after the match
before := node.Data[:i]
after := node.Data[j:]
@@ -392,7 +395,9 @@ func replaceContent(node *html.Node, i, j int, newNode *html.Node) {
// Get the current next sibling, before which we place the replaced data,
// and after that we place the new text node.
nextSibling := node.NextSibling
- node.Parent.InsertBefore(newNode, nextSibling)
+ for _, n := range newNodes {
+ node.Parent.InsertBefore(n, nextSibling)
+ }
if after != "" {
node.Parent.InsertBefore(&html.Node{
Type: html.TextNode,
@@ -402,13 +407,13 @@ func replaceContent(node *html.Node, i, j int, newNode *html.Node) {
}
func mentionProcessor(_ *postProcessCtx, node *html.Node) {
- m := mentionPattern.FindStringSubmatchIndex(node.Data)
- if m == nil {
+ // We replace only the first mention; other mentions will be addressed later
+ found, loc := references.FindFirstMentionBytes([]byte(node.Data))
+ if !found {
return
}
- // Replace the mention with a link to the specified user.
- mention := node.Data[m[2]:m[3]]
- replaceContent(node, m[2], m[3], createLink(util.URLJoin(setting.AppURL, mention[1:]), mention, "mention"))
+ mention := node.Data[loc.Start:loc.End]
+ replaceContent(node, loc.Start, loc.End, createLink(util.URLJoin(setting.AppURL, mention[1:]), mention, "mention"))
}
func shortLinkProcessor(ctx *postProcessCtx, node *html.Node) {
@@ -597,45 +602,44 @@ func issueIndexPatternProcessor(ctx *postProcessCtx, node *html.Node) {
if ctx.metas == nil {
return
}
- // default to numeric pattern, unless alphanumeric is requested.
- pattern := issueNumericPattern
+
+ var (
+ found bool
+ ref *references.RenderizableReference
+ )
+
if ctx.metas["style"] == IssueNameStyleAlphanumeric {
- pattern = issueAlphanumericPattern
+ found, ref = references.FindRenderizableReferenceAlphanumeric(node.Data)
+ } else {
+ found, ref = references.FindRenderizableReferenceNumeric(node.Data)
}
-
- match := pattern.FindStringSubmatchIndex(node.Data)
- if match == nil {
+ if !found {
return
}
- id := node.Data[match[2]:match[3]]
var link *html.Node
+ reftext := node.Data[ref.RefLocation.Start:ref.RefLocation.End]
if _, ok := ctx.metas["format"]; ok {
- // Support for external issue tracker
- if ctx.metas["style"] == IssueNameStyleAlphanumeric {
- ctx.metas["index"] = id
- } else {
- ctx.metas["index"] = id[1:]
- }
- link = createLink(com.Expand(ctx.metas["format"], ctx.metas), id, "issue")
+ ctx.metas["index"] = ref.Issue
+ link = createLink(com.Expand(ctx.metas["format"], ctx.metas), reftext, "issue")
+ } else if ref.Owner == "" {
+ link = createLink(util.URLJoin(setting.AppURL, ctx.metas["user"], ctx.metas["repo"], "issues", ref.Issue), reftext, "issue")
} else {
- link = createLink(util.URLJoin(setting.AppURL, ctx.metas["user"], ctx.metas["repo"], "issues", id[1:]), id, "issue")
+ link = createLink(util.URLJoin(setting.AppURL, ref.Owner, ref.Name, "issues", ref.Issue), reftext, "issue")
}
- replaceContent(node, match[2], match[3], link)
-}
-func crossReferenceIssueIndexPatternProcessor(ctx *postProcessCtx, node *html.Node) {
- m := crossReferenceIssueNumericPattern.FindStringSubmatchIndex(node.Data)
- if m == nil {
+ if ref.Action == references.XRefActionNone {
+ replaceContent(node, ref.RefLocation.Start, ref.RefLocation.End, link)
return
}
- ref := node.Data[m[2]:m[3]]
- parts := strings.SplitN(ref, "#", 2)
- repo, issue := parts[0], parts[1]
-
- replaceContent(node, m[2], m[3],
- createLink(util.URLJoin(setting.AppURL, repo, "issues", issue), ref, issue))
+ // Decorate action keywords
+ keyword := createKeyword(node.Data[ref.ActionLocation.Start:ref.ActionLocation.End])
+ spaces := &html.Node{
+ Type: html.TextNode,
+ Data: node.Data[ref.ActionLocation.End:ref.RefLocation.Start],
+ }
+ replaceContentList(node, ref.ActionLocation.Start, ref.RefLocation.End, []*html.Node{keyword, spaces, link})
}
// fullSha1PatternProcessor renders SHA containing URLs
diff --git a/modules/markup/html_internal_test.go b/modules/markup/html_internal_test.go
index 2824ce3e6817..9722063e177a 100644
--- a/modules/markup/html_internal_test.go
+++ b/modules/markup/html_internal_test.go
@@ -239,34 +239,6 @@ func TestRender_FullIssueURLs(t *testing.T) {
`#4`)
}
-func TestRegExp_issueNumericPattern(t *testing.T) {
- trueTestCases := []string{
- "#1234",
- "#0",
- "#1234567890987654321",
- " #12",
- "#12:",
- "ref: #12: msg",
- }
- falseTestCases := []string{
- "# 1234",
- "# 0",
- "# ",
- "#",
- "#ABC",
- "#1A2B",
- "",
- "ABC",
- }
-
- for _, testCase := range trueTestCases {
- assert.True(t, issueNumericPattern.MatchString(testCase))
- }
- for _, testCase := range falseTestCases {
- assert.False(t, issueNumericPattern.MatchString(testCase))
- }
-}
-
func TestRegExp_sha1CurrentPattern(t *testing.T) {
trueTestCases := []string{
"d8a994ef243349f321568f9e36d5c3f444b99cae",
@@ -325,70 +297,6 @@ func TestRegExp_anySHA1Pattern(t *testing.T) {
}
}
-func TestRegExp_mentionPattern(t *testing.T) {
- trueTestCases := []string{
- "@Unknwon",
- "@ANT_123",
- "@xxx-DiN0-z-A..uru..s-xxx",
- " @lol ",
- " @Te-st",
- "(@gitea)",
- "[@gitea]",
- }
- falseTestCases := []string{
- "@ 0",
- "@ ",
- "@",
- "",
- "ABC",
- "/home/gitea/@gitea",
- "\"@gitea\"",
- }
-
- for _, testCase := range trueTestCases {
- res := mentionPattern.MatchString(testCase)
- assert.True(t, res)
- }
- for _, testCase := range falseTestCases {
- res := mentionPattern.MatchString(testCase)
- assert.False(t, res)
- }
-}
-
-func TestRegExp_issueAlphanumericPattern(t *testing.T) {
- trueTestCases := []string{
- "ABC-1234",
- "A-1",
- "RC-80",
- "ABCDEFGHIJ-1234567890987654321234567890",
- "ABC-123.",
- "(ABC-123)",
- "[ABC-123]",
- "ABC-123:",
- }
- falseTestCases := []string{
- "RC-08",
- "PR-0",
- "ABCDEFGHIJK-1",
- "PR_1",
- "",
- "#ABC",
- "",
- "ABC",
- "GG-",
- "rm-1",
- "/home/gitea/ABC-1234",
- "MY-STRING-ABC-123",
- }
-
- for _, testCase := range trueTestCases {
- assert.True(t, issueAlphanumericPattern.MatchString(testCase))
- }
- for _, testCase := range falseTestCases {
- assert.False(t, issueAlphanumericPattern.MatchString(testCase))
- }
-}
-
func TestRegExp_shortLinkPattern(t *testing.T) {
trueTestCases := []string{
"[[stuff]]",
diff --git a/modules/markup/mdstripper/mdstripper.go b/modules/markup/mdstripper/mdstripper.go
new file mode 100644
index 000000000000..7a901b17a925
--- /dev/null
+++ b/modules/markup/mdstripper/mdstripper.go
@@ -0,0 +1,260 @@
+// Copyright 2019 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 mdstripper
+
+import (
+ "bytes"
+
+ "github.com/russross/blackfriday"
+)
+
+// MarkdownStripper extends blackfriday.Renderer
+type MarkdownStripper struct {
+ blackfriday.Renderer
+ links []string
+ coallesce bool
+}
+
+const (
+ blackfridayExtensions = 0 |
+ blackfriday.EXTENSION_NO_INTRA_EMPHASIS |
+ blackfriday.EXTENSION_TABLES |
+ blackfriday.EXTENSION_FENCED_CODE |
+ blackfriday.EXTENSION_STRIKETHROUGH |
+ blackfriday.EXTENSION_NO_EMPTY_LINE_BEFORE_BLOCK |
+ blackfriday.EXTENSION_DEFINITION_LISTS |
+ blackfriday.EXTENSION_FOOTNOTES |
+ blackfriday.EXTENSION_HEADER_IDS |
+ blackfriday.EXTENSION_AUTO_HEADER_IDS |
+ // Not included in modules/markup/markdown/markdown.go;
+ // required here to process inline links
+ blackfriday.EXTENSION_AUTOLINK
+)
+
+//revive:disable:var-naming Implementing the Rendering interface requires breaking some linting rules
+
+// StripMarkdown parses markdown content by removing all markup and code blocks
+// in order to extract links and other references
+func StripMarkdown(rawBytes []byte) (string, []string) {
+ stripper := &MarkdownStripper{
+ links: make([]string, 0, 10),
+ }
+ body := blackfriday.Markdown(rawBytes, stripper, blackfridayExtensions)
+ return string(body), stripper.GetLinks()
+}
+
+// StripMarkdownBytes parses markdown content by removing all markup and code blocks
+// in order to extract links and other references
+func StripMarkdownBytes(rawBytes []byte) ([]byte, []string) {
+ stripper := &MarkdownStripper{
+ links: make([]string, 0, 10),
+ }
+ body := blackfriday.Markdown(rawBytes, stripper, blackfridayExtensions)
+ return body, stripper.GetLinks()
+}
+
+// block-level callbacks
+
+// BlockCode dummy function to proceed with rendering
+func (r *MarkdownStripper) BlockCode(out *bytes.Buffer, text []byte, infoString string) {
+ // Not rendered
+ r.coallesce = false
+}
+
+// BlockQuote dummy function to proceed with rendering
+func (r *MarkdownStripper) BlockQuote(out *bytes.Buffer, text []byte) {
+ // FIXME: perhaps it's better to leave out block quote for this?
+ r.processString(out, text, false)
+}
+
+// BlockHtml dummy function to proceed with rendering
+func (r *MarkdownStripper) BlockHtml(out *bytes.Buffer, text []byte) { //nolint
+ // Not rendered
+ r.coallesce = false
+}
+
+// Header dummy function to proceed with rendering
+func (r *MarkdownStripper) Header(out *bytes.Buffer, text func() bool, level int, id string) {
+ text()
+ r.coallesce = false
+}
+
+// HRule dummy function to proceed with rendering
+func (r *MarkdownStripper) HRule(out *bytes.Buffer) {
+ // Not rendered
+ r.coallesce = false
+}
+
+// List dummy function to proceed with rendering
+func (r *MarkdownStripper) List(out *bytes.Buffer, text func() bool, flags int) {
+ text()
+ r.coallesce = false
+}
+
+// ListItem dummy function to proceed with rendering
+func (r *MarkdownStripper) ListItem(out *bytes.Buffer, text []byte, flags int) {
+ r.processString(out, text, false)
+}
+
+// Paragraph dummy function to proceed with rendering
+func (r *MarkdownStripper) Paragraph(out *bytes.Buffer, text func() bool) {
+ text()
+ r.coallesce = false
+}
+
+// Table dummy function to proceed with rendering
+func (r *MarkdownStripper) Table(out *bytes.Buffer, header []byte, body []byte, columnData []int) {
+ r.processString(out, header, false)
+ r.processString(out, body, false)
+}
+
+// TableRow dummy function to proceed with rendering
+func (r *MarkdownStripper) TableRow(out *bytes.Buffer, text []byte) {
+ r.processString(out, text, false)
+}
+
+// TableHeaderCell dummy function to proceed with rendering
+func (r *MarkdownStripper) TableHeaderCell(out *bytes.Buffer, text []byte, flags int) {
+ r.processString(out, text, false)
+}
+
+// TableCell dummy function to proceed with rendering
+func (r *MarkdownStripper) TableCell(out *bytes.Buffer, text []byte, flags int) {
+ r.processString(out, text, false)
+}
+
+// Footnotes dummy function to proceed with rendering
+func (r *MarkdownStripper) Footnotes(out *bytes.Buffer, text func() bool) {
+ text()
+}
+
+// FootnoteItem dummy function to proceed with rendering
+func (r *MarkdownStripper) FootnoteItem(out *bytes.Buffer, name, text []byte, flags int) {
+ r.processString(out, text, false)
+}
+
+// TitleBlock dummy function to proceed with rendering
+func (r *MarkdownStripper) TitleBlock(out *bytes.Buffer, text []byte) {
+ r.processString(out, text, false)
+}
+
+// Span-level callbacks
+
+// AutoLink dummy function to proceed with rendering
+func (r *MarkdownStripper) AutoLink(out *bytes.Buffer, link []byte, kind int) {
+ r.processLink(out, link, []byte{})
+}
+
+// CodeSpan dummy function to proceed with rendering
+func (r *MarkdownStripper) CodeSpan(out *bytes.Buffer, text []byte) {
+ // Not rendered
+ r.coallesce = false
+}
+
+// DoubleEmphasis dummy function to proceed with rendering
+func (r *MarkdownStripper) DoubleEmphasis(out *bytes.Buffer, text []byte) {
+ r.processString(out, text, false)
+}
+
+// Emphasis dummy function to proceed with rendering
+func (r *MarkdownStripper) Emphasis(out *bytes.Buffer, text []byte) {
+ r.processString(out, text, false)
+}
+
+// Image dummy function to proceed with rendering
+func (r *MarkdownStripper) Image(out *bytes.Buffer, link []byte, title []byte, alt []byte) {
+ // Not rendered
+ r.coallesce = false
+}
+
+// LineBreak dummy function to proceed with rendering
+func (r *MarkdownStripper) LineBreak(out *bytes.Buffer) {
+ // Not rendered
+ r.coallesce = false
+}
+
+// Link dummy function to proceed with rendering
+func (r *MarkdownStripper) Link(out *bytes.Buffer, link []byte, title []byte, content []byte) {
+ r.processLink(out, link, content)
+}
+
+// RawHtmlTag dummy function to proceed with rendering
+func (r *MarkdownStripper) RawHtmlTag(out *bytes.Buffer, tag []byte) { //nolint
+ // Not rendered
+ r.coallesce = false
+}
+
+// TripleEmphasis dummy function to proceed with rendering
+func (r *MarkdownStripper) TripleEmphasis(out *bytes.Buffer, text []byte) {
+ r.processString(out, text, false)
+}
+
+// StrikeThrough dummy function to proceed with rendering
+func (r *MarkdownStripper) StrikeThrough(out *bytes.Buffer, text []byte) {
+ r.processString(out, text, false)
+}
+
+// FootnoteRef dummy function to proceed with rendering
+func (r *MarkdownStripper) FootnoteRef(out *bytes.Buffer, ref []byte, id int) {
+ // Not rendered
+ r.coallesce = false
+}
+
+// Low-level callbacks
+
+// Entity dummy function to proceed with rendering
+func (r *MarkdownStripper) Entity(out *bytes.Buffer, entity []byte) {
+ // FIXME: literal entities are not parsed; perhaps they should
+ r.coallesce = false
+}
+
+// NormalText dummy function to proceed with rendering
+func (r *MarkdownStripper) NormalText(out *bytes.Buffer, text []byte) {
+ r.processString(out, text, true)
+}
+
+// Header and footer
+
+// DocumentHeader dummy function to proceed with rendering
+func (r *MarkdownStripper) DocumentHeader(out *bytes.Buffer) {
+ r.coallesce = false
+}
+
+// DocumentFooter dummy function to proceed with rendering
+func (r *MarkdownStripper) DocumentFooter(out *bytes.Buffer) {
+ r.coallesce = false
+}
+
+// GetFlags returns rendering flags
+func (r *MarkdownStripper) GetFlags() int {
+ return 0
+}
+
+//revive:enable:var-naming
+
+func doubleSpace(out *bytes.Buffer) {
+ if out.Len() > 0 {
+ out.WriteByte('\n')
+ }
+}
+
+func (r *MarkdownStripper) processString(out *bytes.Buffer, text []byte, coallesce bool) {
+ // Always break-up words
+ if !coallesce || !r.coallesce {
+ doubleSpace(out)
+ }
+ out.Write(text)
+ r.coallesce = coallesce
+}
+func (r *MarkdownStripper) processLink(out *bytes.Buffer, link []byte, content []byte) {
+ // Links are processed out of band
+ r.links = append(r.links, string(link))
+ r.coallesce = false
+}
+
+// GetLinks returns the list of link data collected while parsing
+func (r *MarkdownStripper) GetLinks() []string {
+ return r.links
+}
diff --git a/modules/markup/mdstripper/mdstripper_test.go b/modules/markup/mdstripper/mdstripper_test.go
new file mode 100644
index 000000000000..157fe1975b16
--- /dev/null
+++ b/modules/markup/mdstripper/mdstripper_test.go
@@ -0,0 +1,71 @@
+// Copyright 2019 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 mdstripper
+
+import (
+ "strings"
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestMarkdownStripper(t *testing.T) {
+ type testItem struct {
+ markdown string
+ expectedText []string
+ expectedLinks []string
+ }
+
+ list := []testItem{
+ {
+ `
+## This is a title
+
+This is [one](link) to paradise.
+This **is emphasized**.
+This: should coallesce.
+
+` + "```" + `
+This is a code block.
+This should not appear in the output at all.
+` + "```" + `
+
+* Bullet 1
+* Bullet 2
+
+A HIDDEN ` + "`" + `GHOST` + "`" + ` IN THIS LINE.
+ `,
+ []string{
+ "This is a title",
+ "This is",
+ "to paradise.",
+ "This",
+ "is emphasized",
+ ".",
+ "This: should coallesce.",
+ "Bullet 1",
+ "Bullet 2",
+ "A HIDDEN",
+ "IN THIS LINE.",
+ },
+ []string{
+ "link",
+ }},
+ }
+
+ for _, test := range list {
+ text, links := StripMarkdown([]byte(test.markdown))
+ rawlines := strings.Split(text, "\n")
+ lines := make([]string, 0, len(rawlines))
+ for _, line := range rawlines {
+ line := strings.TrimSpace(line)
+ if line != "" {
+ lines = append(lines, line)
+ }
+ }
+ assert.EqualValues(t, test.expectedText, lines)
+ assert.EqualValues(t, test.expectedLinks, links)
+ }
+}
diff --git a/modules/markup/sanitizer.go b/modules/markup/sanitizer.go
index 2ec43cf4fd3c..fd6f90b2ab1b 100644
--- a/modules/markup/sanitizer.go
+++ b/modules/markup/sanitizer.go
@@ -38,6 +38,9 @@ func NewSanitizer() {
// Custom URL-Schemes
sanitizer.policy.AllowURLSchemes(setting.Markdown.CustomURLSchemes...)
+
+ // Allow keyword markup
+ sanitizer.policy.AllowAttrs("class").Matching(regexp.MustCompile(`^` + keywordClass + `$`)).OnElements("span")
})
}
diff --git a/modules/migrations/base/downloader.go b/modules/migrations/base/downloader.go
index ab5ca6dec80f..69c2adb9e950 100644
--- a/modules/migrations/base/downloader.go
+++ b/modules/migrations/base/downloader.go
@@ -5,6 +5,8 @@
package base
+import "code.gitea.io/gitea/modules/structs"
+
// Downloader downloads the site repo informations
type Downloader interface {
GetRepoInfo() (*Repository, error)
@@ -21,4 +23,5 @@ type Downloader interface {
type DownloaderFactory interface {
Match(opts MigrateOptions) (bool, error)
New(opts MigrateOptions) (Downloader, error)
+ GitServiceType() structs.GitServiceType
}
diff --git a/modules/migrations/gitea.go b/modules/migrations/gitea.go
index ab3b0b9f694a..2452a7a88322 100644
--- a/modules/migrations/gitea.go
+++ b/modules/migrations/gitea.go
@@ -34,15 +34,17 @@ var (
// GiteaLocalUploader implements an Uploader to gitea sites
type GiteaLocalUploader struct {
- doer *models.User
- repoOwner string
- repoName string
- repo *models.Repository
- labels sync.Map
- milestones sync.Map
- issues sync.Map
- gitRepo *git.Repository
- prHeadCache map[string]struct{}
+ doer *models.User
+ repoOwner string
+ repoName string
+ repo *models.Repository
+ labels sync.Map
+ milestones sync.Map
+ issues sync.Map
+ gitRepo *git.Repository
+ prHeadCache map[string]struct{}
+ userMap map[int64]int64 // external user id mapping to user id
+ gitServiceType structs.GitServiceType
}
// NewGiteaLocalUploader creates an gitea Uploader via gitea API v1
@@ -52,6 +54,7 @@ func NewGiteaLocalUploader(doer *models.User, repoOwner, repoName string) *Gitea
repoOwner: repoOwner,
repoName: repoName,
prHeadCache: make(map[string]struct{}),
+ userMap: make(map[int64]int64),
}
}
@@ -109,13 +112,15 @@ func (g *GiteaLocalUploader) CreateRepo(repo *base.Repository, opts base.Migrate
}
r, err = models.MigrateRepositoryGitData(g.doer, owner, r, structs.MigrateRepoOption{
- RepoName: g.repoName,
- Description: repo.Description,
- Mirror: repo.IsMirror,
- CloneAddr: remoteAddr,
- Private: repo.IsPrivate,
- Wiki: opts.Wiki,
- Releases: opts.Releases, // if didn't get releases, then sync them from tags
+ RepoName: g.repoName,
+ Description: repo.Description,
+ OriginalURL: repo.OriginalURL,
+ GitServiceType: opts.GitServiceType,
+ Mirror: repo.IsMirror,
+ CloneAddr: remoteAddr,
+ Private: repo.IsPrivate,
+ Wiki: opts.Wiki,
+ Releases: opts.Releases, // if didn't get releases, then sync them from tags
})
g.repo = r
@@ -193,20 +198,38 @@ func (g *GiteaLocalUploader) CreateReleases(releases ...*base.Release) error {
var rels = make([]*models.Release, 0, len(releases))
for _, release := range releases {
var rel = models.Release{
- RepoID: g.repo.ID,
- PublisherID: g.doer.ID,
- TagName: release.TagName,
- LowerTagName: strings.ToLower(release.TagName),
- Target: release.TargetCommitish,
- Title: release.Name,
- Sha1: release.TargetCommitish,
- Note: release.Body,
- IsDraft: release.Draft,
- IsPrerelease: release.Prerelease,
- IsTag: false,
- CreatedUnix: timeutil.TimeStamp(release.Created.Unix()),
- OriginalAuthor: release.PublisherName,
- OriginalAuthorID: release.PublisherID,
+ RepoID: g.repo.ID,
+ TagName: release.TagName,
+ LowerTagName: strings.ToLower(release.TagName),
+ Target: release.TargetCommitish,
+ Title: release.Name,
+ Sha1: release.TargetCommitish,
+ Note: release.Body,
+ IsDraft: release.Draft,
+ IsPrerelease: release.Prerelease,
+ IsTag: false,
+ CreatedUnix: timeutil.TimeStamp(release.Created.Unix()),
+ }
+
+ userid, ok := g.userMap[release.PublisherID]
+ tp := g.gitServiceType.Name()
+ if !ok && tp != "" {
+ var err error
+ userid, err = models.GetUserIDByExternalUserID(tp, fmt.Sprintf("%v", release.PublisherID))
+ if err != nil {
+ log.Error("GetUserIDByExternalUserID: %v", err)
+ }
+ if userid > 0 {
+ g.userMap[release.PublisherID] = userid
+ }
+ }
+
+ if userid > 0 {
+ rel.PublisherID = userid
+ } else {
+ rel.PublisherID = g.doer.ID
+ rel.OriginalAuthor = release.PublisherName
+ rel.OriginalAuthorID = release.PublisherID
}
// calc NumCommits
@@ -284,20 +307,39 @@ func (g *GiteaLocalUploader) CreateIssues(issues ...*base.Issue) error {
}
var is = models.Issue{
- RepoID: g.repo.ID,
- Repo: g.repo,
- Index: issue.Number,
- PosterID: g.doer.ID,
- OriginalAuthor: issue.PosterName,
- OriginalAuthorID: issue.PosterID,
- Title: issue.Title,
- Content: issue.Content,
- IsClosed: issue.State == "closed",
- IsLocked: issue.IsLocked,
- MilestoneID: milestoneID,
- Labels: labels,
- CreatedUnix: timeutil.TimeStamp(issue.Created.Unix()),
+ RepoID: g.repo.ID,
+ Repo: g.repo,
+ Index: issue.Number,
+ Title: issue.Title,
+ Content: issue.Content,
+ IsClosed: issue.State == "closed",
+ IsLocked: issue.IsLocked,
+ MilestoneID: milestoneID,
+ Labels: labels,
+ CreatedUnix: timeutil.TimeStamp(issue.Created.Unix()),
+ }
+
+ userid, ok := g.userMap[issue.PosterID]
+ tp := g.gitServiceType.Name()
+ if !ok && tp != "" {
+ var err error
+ userid, err = models.GetUserIDByExternalUserID(tp, fmt.Sprintf("%v", issue.PosterID))
+ if err != nil {
+ log.Error("GetUserIDByExternalUserID: %v", err)
+ }
+ if userid > 0 {
+ g.userMap[issue.PosterID] = userid
+ }
+ }
+
+ if userid > 0 {
+ is.PosterID = userid
+ } else {
+ is.PosterID = g.doer.ID
+ is.OriginalAuthor = issue.PosterName
+ is.OriginalAuthorID = issue.PosterID
}
+
if issue.Closed != nil {
is.ClosedUnix = timeutil.TimeStamp(issue.Closed.Unix())
}
@@ -331,15 +373,35 @@ func (g *GiteaLocalUploader) CreateComments(comments ...*base.Comment) error {
issueID = issueIDStr.(int64)
}
- cms = append(cms, &models.Comment{
- IssueID: issueID,
- Type: models.CommentTypeComment,
- PosterID: g.doer.ID,
- OriginalAuthor: comment.PosterName,
- OriginalAuthorID: comment.PosterID,
- Content: comment.Content,
- CreatedUnix: timeutil.TimeStamp(comment.Created.Unix()),
- })
+ userid, ok := g.userMap[comment.PosterID]
+ tp := g.gitServiceType.Name()
+ if !ok && tp != "" {
+ var err error
+ userid, err = models.GetUserIDByExternalUserID(tp, fmt.Sprintf("%v", comment.PosterID))
+ if err != nil {
+ log.Error("GetUserIDByExternalUserID: %v", err)
+ }
+ if userid > 0 {
+ g.userMap[comment.PosterID] = userid
+ }
+ }
+
+ cm := models.Comment{
+ IssueID: issueID,
+ Type: models.CommentTypeComment,
+ Content: comment.Content,
+ CreatedUnix: timeutil.TimeStamp(comment.Created.Unix()),
+ }
+
+ if userid > 0 {
+ cm.PosterID = userid
+ } else {
+ cm.PosterID = g.doer.ID
+ cm.OriginalAuthor = comment.PosterName
+ cm.OriginalAuthorID = comment.PosterID
+ }
+
+ cms = append(cms, &cm)
// TODO: Reactions
}
@@ -355,6 +417,28 @@ func (g *GiteaLocalUploader) CreatePullRequests(prs ...*base.PullRequest) error
if err != nil {
return err
}
+
+ userid, ok := g.userMap[pr.PosterID]
+ tp := g.gitServiceType.Name()
+ if !ok && tp != "" {
+ var err error
+ userid, err = models.GetUserIDByExternalUserID(tp, fmt.Sprintf("%v", pr.PosterID))
+ if err != nil {
+ log.Error("GetUserIDByExternalUserID: %v", err)
+ }
+ if userid > 0 {
+ g.userMap[pr.PosterID] = userid
+ }
+ }
+
+ if userid > 0 {
+ gpr.Issue.PosterID = userid
+ } else {
+ gpr.Issue.PosterID = g.doer.ID
+ gpr.Issue.OriginalAuthor = pr.PosterName
+ gpr.Issue.OriginalAuthorID = pr.PosterID
+ }
+
gprs = append(gprs, gpr)
}
if err := models.InsertPullRequests(gprs...); err != nil {
@@ -460,6 +544,40 @@ func (g *GiteaLocalUploader) newPullRequest(pr *base.PullRequest) (*models.PullR
head = pr.Head.Ref
}
+ var issue = models.Issue{
+ RepoID: g.repo.ID,
+ Repo: g.repo,
+ Title: pr.Title,
+ Index: pr.Number,
+ Content: pr.Content,
+ MilestoneID: milestoneID,
+ IsPull: true,
+ IsClosed: pr.State == "closed",
+ IsLocked: pr.IsLocked,
+ Labels: labels,
+ CreatedUnix: timeutil.TimeStamp(pr.Created.Unix()),
+ }
+
+ userid, ok := g.userMap[pr.PosterID]
+ if !ok {
+ var err error
+ userid, err = models.GetUserIDByExternalUserID("github", fmt.Sprintf("%v", pr.PosterID))
+ if err != nil {
+ log.Error("GetUserIDByExternalUserID: %v", err)
+ }
+ if userid > 0 {
+ g.userMap[pr.PosterID] = userid
+ }
+ }
+
+ if userid > 0 {
+ issue.PosterID = userid
+ } else {
+ issue.PosterID = g.doer.ID
+ issue.OriginalAuthor = pr.PosterName
+ issue.OriginalAuthorID = pr.PosterID
+ }
+
var pullRequest = models.PullRequest{
HeadRepoID: g.repo.ID,
HeadBranch: head,
@@ -470,22 +588,7 @@ func (g *GiteaLocalUploader) newPullRequest(pr *base.PullRequest) (*models.PullR
Index: pr.Number,
HasMerged: pr.Merged,
- Issue: &models.Issue{
- RepoID: g.repo.ID,
- Repo: g.repo,
- Title: pr.Title,
- Index: pr.Number,
- PosterID: g.doer.ID,
- OriginalAuthor: pr.PosterName,
- OriginalAuthorID: pr.PosterID,
- Content: pr.Content,
- MilestoneID: milestoneID,
- IsPull: true,
- IsClosed: pr.State == "closed",
- IsLocked: pr.IsLocked,
- Labels: labels,
- CreatedUnix: timeutil.TimeStamp(pr.Created.Unix()),
- },
+ Issue: &issue,
}
if pullRequest.Issue.IsClosed && pr.Closed != nil {
diff --git a/modules/migrations/github.go b/modules/migrations/github.go
index 1c5d96c03d47..00d137a3de69 100644
--- a/modules/migrations/github.go
+++ b/modules/migrations/github.go
@@ -14,6 +14,7 @@ import (
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/migrations/base"
+ "code.gitea.io/gitea/modules/structs"
"github.com/google/go-github/v24/github"
"golang.org/x/oauth2"
@@ -39,7 +40,7 @@ func (f *GithubDownloaderV3Factory) Match(opts base.MigrateOptions) (bool, error
return false, err
}
- return u.Host == "github.com" && opts.AuthUsername != "", nil
+ return strings.EqualFold(u.Host, "github.com") && opts.AuthUsername != "", nil
}
// New returns a Downloader related to this factory according MigrateOptions
@@ -58,6 +59,11 @@ func (f *GithubDownloaderV3Factory) New(opts base.MigrateOptions) (base.Download
return NewGithubDownloaderV3(opts.AuthUsername, opts.AuthPassword, oldOwner, oldName), nil
}
+// GitServiceType returns the type of git service
+func (f *GithubDownloaderV3Factory) GitServiceType() structs.GitServiceType {
+ return structs.GithubService
+}
+
// GithubDownloaderV3 implements a Downloader interface to get repository informations
// from github via APIv3
type GithubDownloaderV3 struct {
diff --git a/modules/migrations/migrate.go b/modules/migrations/migrate.go
index 3f5c0d1118c3..bbc1dc2d5610 100644
--- a/modules/migrations/migrate.go
+++ b/modules/migrations/migrate.go
@@ -11,6 +11,7 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/migrations/base"
+ "code.gitea.io/gitea/modules/structs"
)
// MigrateOptions is equal to base.MigrateOptions
@@ -30,6 +31,7 @@ func MigrateRepository(doer *models.User, ownerName string, opts base.MigrateOpt
var (
downloader base.Downloader
uploader = NewGiteaLocalUploader(doer, ownerName, opts.RepoName)
+ theFactory base.DownloaderFactory
)
for _, factory := range factories {
@@ -40,6 +42,7 @@ func MigrateRepository(doer *models.User, ownerName string, opts base.MigrateOpt
if err != nil {
return nil, err
}
+ theFactory = factory
break
}
}
@@ -52,10 +55,14 @@ func MigrateRepository(doer *models.User, ownerName string, opts base.MigrateOpt
opts.Comments = false
opts.Issues = false
opts.PullRequests = false
+ opts.GitServiceType = structs.PlainGitService
downloader = NewPlainGitDownloader(ownerName, opts.RepoName, opts.CloneAddr)
log.Trace("Will migrate from git: %s", opts.CloneAddr)
+ } else if opts.GitServiceType == structs.NotMigrated {
+ opts.GitServiceType = theFactory.GitServiceType()
}
+ uploader.gitServiceType = opts.GitServiceType
if err := migrateRepository(downloader, uploader, opts); err != nil {
if err1 := uploader.Rollback(); err1 != nil {
log.Error("rollback failed: %v", err1)
diff --git a/modules/migrations/update.go b/modules/migrations/update.go
new file mode 100644
index 000000000000..df626ddd955e
--- /dev/null
+++ b/modules/migrations/update.go
@@ -0,0 +1,59 @@
+// Copyright 2019 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 migrations
+
+import (
+ "strconv"
+
+ "code.gitea.io/gitea/models"
+ "code.gitea.io/gitea/modules/log"
+ "code.gitea.io/gitea/modules/structs"
+)
+
+// UpdateMigrationPosterID updates all migrated repositories' issues and comments posterID
+func UpdateMigrationPosterID() {
+ for _, gitService := range structs.SupportedFullGitService {
+ if err := updateMigrationPosterIDByGitService(gitService); err != nil {
+ log.Error("updateMigrationPosterIDByGitService failed: %v", err)
+ }
+ }
+}
+
+func updateMigrationPosterIDByGitService(tp structs.GitServiceType) error {
+ provider := tp.Name()
+ if len(provider) == 0 {
+ return nil
+ }
+
+ const batchSize = 100
+ var start int
+ for {
+ users, err := models.FindExternalUsersByProvider(models.FindExternalUserOptions{
+ Provider: provider,
+ Start: start,
+ Limit: batchSize,
+ })
+ if err != nil {
+ return err
+ }
+
+ for _, user := range users {
+ externalUserID, err := strconv.ParseInt(user.ExternalID, 10, 64)
+ if err != nil {
+ log.Warn("Parse externalUser %#v 's userID failed: %v", user, err)
+ continue
+ }
+ if err := models.UpdateMigrationsByType(tp, externalUserID, user.UserID); err != nil {
+ log.Error("UpdateMigrationsByType type %s external user id %v to local user id %v failed: %v", tp.Name(), user.ExternalID, user.UserID, err)
+ }
+ }
+
+ if len(users) < batchSize {
+ break
+ }
+ start += len(users)
+ }
+ return nil
+}
diff --git a/modules/references/references.go b/modules/references/references.go
new file mode 100644
index 000000000000..9c74d0d081ae
--- /dev/null
+++ b/modules/references/references.go
@@ -0,0 +1,322 @@
+// Copyright 2019 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 references
+
+import (
+ "net/url"
+ "regexp"
+ "strconv"
+ "strings"
+ "sync"
+
+ "code.gitea.io/gitea/modules/markup/mdstripper"
+ "code.gitea.io/gitea/modules/setting"
+)
+
+var (
+ // validNamePattern performs only the most basic validation for user or repository names
+ // Repository name should contain only alphanumeric, dash ('-'), underscore ('_') and dot ('.') characters.
+ validNamePattern = regexp.MustCompile(`^[a-z0-9_.-]+$`)
+
+ // NOTE: All below regex matching do not perform any extra validation.
+ // Thus a link is produced even if the linked entity does not exist.
+ // While fast, this is also incorrect and lead to false positives.
+ // TODO: fix invalid linking issue
+
+ // mentionPattern matches all mentions in the form of "@user"
+ mentionPattern = regexp.MustCompile(`(?:\s|^|\(|\[)(@[0-9a-zA-Z-_\.]+)(?:\s|$|\)|\])`)
+ // issueNumericPattern matches string that references to a numeric issue, e.g. #1287
+ issueNumericPattern = regexp.MustCompile(`(?:\s|^|\(|\[)(#[0-9]+)(?:\s|$|\)|\]|:|\.(\s|$))`)
+ // issueAlphanumericPattern matches string that references to an alphanumeric issue, e.g. ABC-1234
+ issueAlphanumericPattern = regexp.MustCompile(`(?:\s|^|\(|\[)([A-Z]{1,10}-[1-9][0-9]*)(?:\s|$|\)|\]|:|\.(\s|$))`)
+ // crossReferenceIssueNumericPattern matches string that references a numeric issue in a different repository
+ // e.g. gogits/gogs#12345
+ crossReferenceIssueNumericPattern = regexp.MustCompile(`(?:\s|^|\(|\[)([0-9a-zA-Z-_\.]+/[0-9a-zA-Z-_\.]+#[0-9]+)(?:\s|$|\)|\]|\.(\s|$))`)
+
+ // Same as GitHub. See
+ // https://help.github.com/articles/closing-issues-via-commit-messages
+ issueCloseKeywords = []string{"close", "closes", "closed", "fix", "fixes", "fixed", "resolve", "resolves", "resolved"}
+ issueReopenKeywords = []string{"reopen", "reopens", "reopened"}
+
+ issueCloseKeywordsPat, issueReopenKeywordsPat *regexp.Regexp
+
+ giteaHostInit sync.Once
+ giteaHost string
+)
+
+// XRefAction represents the kind of effect a cross reference has once is resolved
+type XRefAction int64
+
+const (
+ // XRefActionNone means the cross-reference is simply a comment
+ XRefActionNone XRefAction = iota // 0
+ // XRefActionCloses means the cross-reference should close an issue if it is resolved
+ XRefActionCloses // 1
+ // XRefActionReopens means the cross-reference should reopen an issue if it is resolved
+ XRefActionReopens // 2
+ // XRefActionNeutered means the cross-reference will no longer affect the source
+ XRefActionNeutered // 3
+)
+
+// IssueReference contains an unverified cross-reference to a local issue or pull request
+type IssueReference struct {
+ Index int64
+ Owner string
+ Name string
+ Action XRefAction
+}
+
+// RenderizableReference contains an unverified cross-reference to with rendering information
+type RenderizableReference struct {
+ Issue string
+ Owner string
+ Name string
+ RefLocation *RefSpan
+ Action XRefAction
+ ActionLocation *RefSpan
+}
+
+type rawReference struct {
+ index int64
+ owner string
+ name string
+ action XRefAction
+ issue string
+ refLocation *RefSpan
+ actionLocation *RefSpan
+}
+
+func rawToIssueReferenceList(reflist []*rawReference) []IssueReference {
+ refarr := make([]IssueReference, len(reflist))
+ for i, r := range reflist {
+ refarr[i] = IssueReference{
+ Index: r.index,
+ Owner: r.owner,
+ Name: r.name,
+ Action: r.action,
+ }
+ }
+ return refarr
+}
+
+// RefSpan is the position where the reference was found within the parsed text
+type RefSpan struct {
+ Start int
+ End int
+}
+
+func makeKeywordsPat(keywords []string) *regexp.Regexp {
+ return regexp.MustCompile(`(?i)(?:\s|^|\(|\[)(` + strings.Join(keywords, `|`) + `):? $`)
+}
+
+func init() {
+ issueCloseKeywordsPat = makeKeywordsPat(issueCloseKeywords)
+ issueReopenKeywordsPat = makeKeywordsPat(issueReopenKeywords)
+}
+
+// getGiteaHostName returns a normalized string with the local host name, with no scheme or port information
+func getGiteaHostName() string {
+ giteaHostInit.Do(func() {
+ if uapp, err := url.Parse(setting.AppURL); err == nil {
+ giteaHost = strings.ToLower(uapp.Host)
+ } else {
+ giteaHost = ""
+ }
+ })
+ return giteaHost
+}
+
+// FindAllMentionsMarkdown matches mention patterns in given content and
+// returns a list of found unvalidated user names **not including** the @ prefix.
+func FindAllMentionsMarkdown(content string) []string {
+ bcontent, _ := mdstripper.StripMarkdownBytes([]byte(content))
+ locations := FindAllMentionsBytes(bcontent)
+ mentions := make([]string, len(locations))
+ for i, val := range locations {
+ mentions[i] = string(bcontent[val.Start+1 : val.End])
+ }
+ return mentions
+}
+
+// FindAllMentionsBytes matches mention patterns in given content
+// and returns a list of locations for the unvalidated user names, including the @ prefix.
+func FindAllMentionsBytes(content []byte) []RefSpan {
+ mentions := mentionPattern.FindAllSubmatchIndex(content, -1)
+ ret := make([]RefSpan, len(mentions))
+ for i, val := range mentions {
+ ret[i] = RefSpan{Start: val[2], End: val[3]}
+ }
+ return ret
+}
+
+// FindFirstMentionBytes matches the first mention in then given content
+// and returns the location of the unvalidated user name, including the @ prefix.
+func FindFirstMentionBytes(content []byte) (bool, RefSpan) {
+ mention := mentionPattern.FindSubmatchIndex(content)
+ if mention == nil {
+ return false, RefSpan{}
+ }
+ return true, RefSpan{Start: mention[2], End: mention[3]}
+}
+
+// FindAllIssueReferencesMarkdown strips content from markdown markup
+// and returns a list of unvalidated references found in it.
+func FindAllIssueReferencesMarkdown(content string) []IssueReference {
+ return rawToIssueReferenceList(findAllIssueReferencesMarkdown(content))
+}
+
+func findAllIssueReferencesMarkdown(content string) []*rawReference {
+ bcontent, links := mdstripper.StripMarkdownBytes([]byte(content))
+ return findAllIssueReferencesBytes(bcontent, links)
+}
+
+// FindAllIssueReferences returns a list of unvalidated references found in a string.
+func FindAllIssueReferences(content string) []IssueReference {
+ return rawToIssueReferenceList(findAllIssueReferencesBytes([]byte(content), []string{}))
+}
+
+// FindRenderizableReferenceNumeric returns the first unvalidated reference found in a string.
+func FindRenderizableReferenceNumeric(content string) (bool, *RenderizableReference) {
+ match := issueNumericPattern.FindStringSubmatchIndex(content)
+ if match == nil {
+ if match = crossReferenceIssueNumericPattern.FindStringSubmatchIndex(content); match == nil {
+ return false, nil
+ }
+ }
+ r := getCrossReference([]byte(content), match[2], match[3], false)
+ if r == nil {
+ return false, nil
+ }
+
+ return true, &RenderizableReference{
+ Issue: r.issue,
+ Owner: r.owner,
+ Name: r.name,
+ RefLocation: r.refLocation,
+ Action: r.action,
+ ActionLocation: r.actionLocation,
+ }
+}
+
+// FindRenderizableReferenceAlphanumeric returns the first alphanumeric unvalidated references found in a string.
+func FindRenderizableReferenceAlphanumeric(content string) (bool, *RenderizableReference) {
+ match := issueAlphanumericPattern.FindStringSubmatchIndex(content)
+ if match == nil {
+ return false, nil
+ }
+
+ action, location := findActionKeywords([]byte(content), match[2])
+
+ return true, &RenderizableReference{
+ Issue: string(content[match[2]:match[3]]),
+ RefLocation: &RefSpan{Start: match[2], End: match[3]},
+ Action: action,
+ ActionLocation: location,
+ }
+}
+
+// FindAllIssueReferencesBytes returns a list of unvalidated references found in a byte slice.
+func findAllIssueReferencesBytes(content []byte, links []string) []*rawReference {
+
+ ret := make([]*rawReference, 0, 10)
+
+ matches := issueNumericPattern.FindAllSubmatchIndex(content, -1)
+ for _, match := range matches {
+ if ref := getCrossReference(content, match[2], match[3], false); ref != nil {
+ ret = append(ret, ref)
+ }
+ }
+
+ matches = crossReferenceIssueNumericPattern.FindAllSubmatchIndex(content, -1)
+ for _, match := range matches {
+ if ref := getCrossReference(content, match[2], match[3], false); ref != nil {
+ ret = append(ret, ref)
+ }
+ }
+
+ localhost := getGiteaHostName()
+ for _, link := range links {
+ if u, err := url.Parse(link); err == nil {
+ // Note: we're not attempting to match the URL scheme (http/https)
+ host := strings.ToLower(u.Host)
+ if host != "" && host != localhost {
+ continue
+ }
+ parts := strings.Split(u.EscapedPath(), "/")
+ // /user/repo/issues/3
+ if len(parts) != 5 || parts[0] != "" {
+ continue
+ }
+ if parts[3] != "issues" && parts[3] != "pulls" {
+ continue
+ }
+ // Note: closing/reopening keywords not supported with URLs
+ bytes := []byte(parts[1] + "/" + parts[2] + "#" + parts[4])
+ if ref := getCrossReference(bytes, 0, len(bytes), true); ref != nil {
+ ref.refLocation = nil
+ ret = append(ret, ref)
+ }
+ }
+ }
+
+ return ret
+}
+
+func getCrossReference(content []byte, start, end int, fromLink bool) *rawReference {
+ refid := string(content[start:end])
+ parts := strings.Split(refid, "#")
+ if len(parts) != 2 {
+ return nil
+ }
+ repo, issue := parts[0], parts[1]
+ index, err := strconv.ParseInt(issue, 10, 64)
+ if err != nil {
+ return nil
+ }
+ if repo == "" {
+ if fromLink {
+ // Markdown links must specify owner/repo
+ return nil
+ }
+ action, location := findActionKeywords(content, start)
+ return &rawReference{
+ index: index,
+ action: action,
+ issue: issue,
+ refLocation: &RefSpan{Start: start, End: end},
+ actionLocation: location,
+ }
+ }
+ parts = strings.Split(strings.ToLower(repo), "/")
+ if len(parts) != 2 {
+ return nil
+ }
+ owner, name := parts[0], parts[1]
+ if !validNamePattern.MatchString(owner) || !validNamePattern.MatchString(name) {
+ return nil
+ }
+ action, location := findActionKeywords(content, start)
+ return &rawReference{
+ index: index,
+ owner: owner,
+ name: name,
+ action: action,
+ issue: issue,
+ refLocation: &RefSpan{Start: start, End: end},
+ actionLocation: location,
+ }
+}
+
+func findActionKeywords(content []byte, start int) (XRefAction, *RefSpan) {
+ m := issueCloseKeywordsPat.FindSubmatchIndex(content[:start])
+ if m != nil {
+ return XRefActionCloses, &RefSpan{Start: m[2], End: m[3]}
+ }
+ m = issueReopenKeywordsPat.FindSubmatchIndex(content[:start])
+ if m != nil {
+ return XRefActionReopens, &RefSpan{Start: m[2], End: m[3]}
+ }
+ return XRefActionNone, nil
+}
diff --git a/modules/references/references_test.go b/modules/references/references_test.go
new file mode 100644
index 000000000000..f8153ffe36da
--- /dev/null
+++ b/modules/references/references_test.go
@@ -0,0 +1,296 @@
+// Copyright 2019 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 references
+
+import (
+ "testing"
+
+ "code.gitea.io/gitea/modules/setting"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestFindAllIssueReferences(t *testing.T) {
+
+ type result struct {
+ Index int64
+ Owner string
+ Name string
+ Issue string
+ Action XRefAction
+ RefLocation *RefSpan
+ ActionLocation *RefSpan
+ }
+
+ type testFixture struct {
+ input string
+ expected []result
+ }
+
+ fixtures := []testFixture{
+ {
+ "Simply closes: #29 yes",
+ []result{
+ {29, "", "", "29", XRefActionCloses, &RefSpan{Start: 15, End: 18}, &RefSpan{Start: 7, End: 13}},
+ },
+ },
+ {
+ "#123 no, this is a title.",
+ []result{},
+ },
+ {
+ " #124 yes, this is a reference.",
+ []result{
+ {124, "", "", "124", XRefActionNone, &RefSpan{Start: 0, End: 4}, nil},
+ },
+ },
+ {
+ "```\nThis is a code block.\n#723 no, it's a code block.```",
+ []result{},
+ },
+ {
+ "This `#724` no, it's inline code.",
+ []result{},
+ },
+ {
+ "This user3/repo4#200 yes.",
+ []result{
+ {200, "user3", "repo4", "200", XRefActionNone, &RefSpan{Start: 5, End: 20}, nil},
+ },
+ },
+ {
+ "This [one](#919) no, this is a URL fragment.",
+ []result{},
+ },
+ {
+ "This [two](/user2/repo1/issues/921) yes.",
+ []result{
+ {921, "user2", "repo1", "921", XRefActionNone, nil, nil},
+ },
+ },
+ {
+ "This [three](/user2/repo1/pulls/922) yes.",
+ []result{
+ {922, "user2", "repo1", "922", XRefActionNone, nil, nil},
+ },
+ },
+ {
+ "This [four](http://gitea.com:3000/user3/repo4/issues/203) yes.",
+ []result{
+ {203, "user3", "repo4", "203", XRefActionNone, nil, nil},
+ },
+ },
+ {
+ "This [five](http://github.com/user3/repo4/issues/204) no.",
+ []result{},
+ },
+ {
+ "This http://gitea.com:3000/user4/repo5/201 no, bad URL.",
+ []result{},
+ },
+ {
+ "This http://gitea.com:3000/user4/repo5/pulls/202 yes.",
+ []result{
+ {202, "user4", "repo5", "202", XRefActionNone, nil, nil},
+ },
+ },
+ {
+ "This http://GiTeA.COM:3000/user4/repo6/pulls/205 yes.",
+ []result{
+ {205, "user4", "repo6", "205", XRefActionNone, nil, nil},
+ },
+ },
+ {
+ "Reopens #15 yes",
+ []result{
+ {15, "", "", "15", XRefActionReopens, &RefSpan{Start: 8, End: 11}, &RefSpan{Start: 0, End: 7}},
+ },
+ },
+ {
+ "This closes #20 for you yes",
+ []result{
+ {20, "", "", "20", XRefActionCloses, &RefSpan{Start: 12, End: 15}, &RefSpan{Start: 5, End: 11}},
+ },
+ },
+ {
+ "Do you fix user6/repo6#300 ? yes",
+ []result{
+ {300, "user6", "repo6", "300", XRefActionCloses, &RefSpan{Start: 11, End: 26}, &RefSpan{Start: 7, End: 10}},
+ },
+ },
+ {
+ "For 999 #1235 no keyword, but yes",
+ []result{
+ {1235, "", "", "1235", XRefActionNone, &RefSpan{Start: 8, End: 13}, nil},
+ },
+ },
+ {
+ "Which abc. #9434 same as above",
+ []result{
+ {9434, "", "", "9434", XRefActionNone, &RefSpan{Start: 11, End: 16}, nil},
+ },
+ },
+ {
+ "This closes #600 and reopens #599",
+ []result{
+ {600, "", "", "600", XRefActionCloses, &RefSpan{Start: 12, End: 16}, &RefSpan{Start: 5, End: 11}},
+ {599, "", "", "599", XRefActionReopens, &RefSpan{Start: 29, End: 33}, &RefSpan{Start: 21, End: 28}},
+ },
+ },
+ }
+
+ // Save original value for other tests that may rely on it
+ prevURL := setting.AppURL
+ setting.AppURL = "https://gitea.com:3000/"
+
+ for _, fixture := range fixtures {
+ expraw := make([]*rawReference, len(fixture.expected))
+ for i, e := range fixture.expected {
+ expraw[i] = &rawReference{
+ index: e.Index,
+ owner: e.Owner,
+ name: e.Name,
+ action: e.Action,
+ issue: e.Issue,
+ refLocation: e.RefLocation,
+ actionLocation: e.ActionLocation,
+ }
+ }
+ expref := rawToIssueReferenceList(expraw)
+ refs := FindAllIssueReferencesMarkdown(fixture.input)
+ assert.EqualValues(t, expref, refs, "Failed to parse: {%s}", fixture.input)
+ rawrefs := findAllIssueReferencesMarkdown(fixture.input)
+ assert.EqualValues(t, expraw, rawrefs, "Failed to parse: {%s}", fixture.input)
+ }
+
+ // Restore for other tests that may rely on the original value
+ setting.AppURL = prevURL
+
+ type alnumFixture struct {
+ input string
+ issue string
+ refLocation *RefSpan
+ action XRefAction
+ actionLocation *RefSpan
+ }
+
+ alnumFixtures := []alnumFixture{
+ {
+ "This ref ABC-123 is alphanumeric",
+ "ABC-123", &RefSpan{Start: 9, End: 16},
+ XRefActionNone, nil,
+ },
+ {
+ "This closes ABCD-1234 alphanumeric",
+ "ABCD-1234", &RefSpan{Start: 12, End: 21},
+ XRefActionCloses, &RefSpan{Start: 5, End: 11},
+ },
+ }
+
+ for _, fixture := range alnumFixtures {
+ found, ref := FindRenderizableReferenceAlphanumeric(fixture.input)
+ if fixture.issue == "" {
+ assert.False(t, found, "Failed to parse: {%s}", fixture.input)
+ } else {
+ assert.True(t, found, "Failed to parse: {%s}", fixture.input)
+ assert.Equal(t, fixture.issue, ref.Issue, "Failed to parse: {%s}", fixture.input)
+ assert.Equal(t, fixture.refLocation, ref.RefLocation, "Failed to parse: {%s}", fixture.input)
+ assert.Equal(t, fixture.action, ref.Action, "Failed to parse: {%s}", fixture.input)
+ assert.Equal(t, fixture.actionLocation, ref.ActionLocation, "Failed to parse: {%s}", fixture.input)
+ }
+ }
+}
+
+func TestRegExp_mentionPattern(t *testing.T) {
+ trueTestCases := []string{
+ "@Unknwon",
+ "@ANT_123",
+ "@xxx-DiN0-z-A..uru..s-xxx",
+ " @lol ",
+ " @Te-st",
+ "(@gitea)",
+ "[@gitea]",
+ }
+ falseTestCases := []string{
+ "@ 0",
+ "@ ",
+ "@",
+ "",
+ "ABC",
+ "/home/gitea/@gitea",
+ "\"@gitea\"",
+ }
+
+ for _, testCase := range trueTestCases {
+ res := mentionPattern.MatchString(testCase)
+ assert.True(t, res)
+ }
+ for _, testCase := range falseTestCases {
+ res := mentionPattern.MatchString(testCase)
+ assert.False(t, res)
+ }
+}
+
+func TestRegExp_issueNumericPattern(t *testing.T) {
+ trueTestCases := []string{
+ "#1234",
+ "#0",
+ "#1234567890987654321",
+ " #12",
+ "#12:",
+ "ref: #12: msg",
+ }
+ falseTestCases := []string{
+ "# 1234",
+ "# 0",
+ "# ",
+ "#",
+ "#ABC",
+ "#1A2B",
+ "",
+ "ABC",
+ }
+
+ for _, testCase := range trueTestCases {
+ assert.True(t, issueNumericPattern.MatchString(testCase))
+ }
+ for _, testCase := range falseTestCases {
+ assert.False(t, issueNumericPattern.MatchString(testCase))
+ }
+}
+
+func TestRegExp_issueAlphanumericPattern(t *testing.T) {
+ trueTestCases := []string{
+ "ABC-1234",
+ "A-1",
+ "RC-80",
+ "ABCDEFGHIJ-1234567890987654321234567890",
+ "ABC-123.",
+ "(ABC-123)",
+ "[ABC-123]",
+ "ABC-123:",
+ }
+ falseTestCases := []string{
+ "RC-08",
+ "PR-0",
+ "ABCDEFGHIJK-1",
+ "PR_1",
+ "",
+ "#ABC",
+ "",
+ "ABC",
+ "GG-",
+ "rm-1",
+ "/home/gitea/ABC-1234",
+ "MY-STRING-ABC-123",
+ }
+
+ for _, testCase := range trueTestCases {
+ assert.True(t, issueAlphanumericPattern.MatchString(testCase))
+ }
+ for _, testCase := range falseTestCases {
+ assert.False(t, issueAlphanumericPattern.MatchString(testCase))
+ }
+}
diff --git a/modules/setting/cron.go b/modules/setting/cron.go
index c544c6c22878..77f55168aa3b 100644
--- a/modules/setting/cron.go
+++ b/modules/setting/cron.go
@@ -49,6 +49,9 @@ var (
Schedule string
OlderThan time.Duration
} `ini:"cron.deleted_branches_cleanup"`
+ UpdateMigrationPosterID struct {
+ Schedule string
+ } `ini:"cron.update_migration_poster_id"`
}{
UpdateMirror: struct {
Enabled bool
@@ -114,6 +117,11 @@ var (
Schedule: "@every 24h",
OlderThan: 24 * time.Hour,
},
+ UpdateMigrationPosterID: struct {
+ Schedule string
+ }{
+ Schedule: "@every 24h",
+ },
}
)
diff --git a/modules/structs/repo.go b/modules/structs/repo.go
index 57f1768a0b94..be6a3d4b43e4 100644
--- a/modules/structs/repo.go
+++ b/modules/structs/repo.go
@@ -153,6 +153,43 @@ type EditRepoOption struct {
Archived *bool `json:"archived,omitempty"`
}
+// GitServiceType represents a git service
+type GitServiceType int
+
+// enumerate all GitServiceType
+const (
+ NotMigrated GitServiceType = iota // 0 not migrated from external sites
+ PlainGitService // 1 plain git service
+ GithubService // 2 github.com
+ GiteaService // 3 gitea service
+ GitlabService // 4 gitlab service
+ GogsService // 5 gogs service
+)
+
+// Name represents the service type's name
+// WARNNING: the name have to be equal to that on goth's library
+func (gt GitServiceType) Name() string {
+ switch gt {
+ case GithubService:
+ return "github"
+ case GiteaService:
+ return "gitea"
+ case GitlabService:
+ return "gitlab"
+ case GogsService:
+ return "gogs"
+ }
+ return ""
+}
+
+var (
+ // SupportedFullGitService represents all git services supported to migrate issues/labels/prs and etc.
+ // TODO: add to this list after new git service added
+ SupportedFullGitService = []GitServiceType{
+ GithubService,
+ }
+)
+
// MigrateRepoOption options for migrating a repository from an external service
type MigrateRepoOption struct {
// required: true
@@ -166,6 +203,8 @@ type MigrateRepoOption struct {
Mirror bool `json:"mirror"`
Private bool `json:"private"`
Description string `json:"description"`
+ OriginalURL string
+ GitServiceType GitServiceType
Wiki bool
Issues bool
Milestones bool
diff --git a/options/locale/locale_es-ES.ini b/options/locale/locale_es-ES.ini
index f5355a488fa7..b5fa9b3bddfb 100644
--- a/options/locale/locale_es-ES.ini
+++ b/options/locale/locale_es-ES.ini
@@ -1209,6 +1209,7 @@ settings.collaborator_deletion_desc=Eliminar un colaborador revocará su acceso
settings.remove_collaborator_success=El colaborador ha sido eliminado.
settings.search_user_placeholder=Buscar usuario…
settings.org_not_allowed_to_be_collaborator=Las organizaciones no pueden ser añadidas como colaboradoras.
+settings.team_not_in_organization=El equipo no pertenece a la misma organización que el repositorio
settings.add_team_duplicate=El equipo ya tiene acceso al repositorio
settings.add_team_success=Ahora el equipo ya tiene acceso al repositorio.
settings.remove_team_success=Se ha eliminado el acceso del equipo al repositorio.
diff --git a/public/css/index.css b/public/css/index.css
index e30fad005496..6fdcb5c22583 100644
--- a/public/css/index.css
+++ b/public/css/index.css
@@ -884,6 +884,7 @@ tbody.commit-list{vertical-align:baseline}
.repo-buttons .disabled-repo-button a.button:hover{background:0 0!important;color:rgba(0,0,0,.6)!important;box-shadow:0 0 0 1px rgba(34,36,38,.15) inset!important}
.repo-buttons .ui.labeled.button>.label{border-left:0!important;margin:0!important}
.tag-code,.tag-code td{background-color:#f0f0f0!important;border-color:#d3cfcf!important;padding-top:8px;padding-bottom:8px}
+.issue-keyword{border-bottom:1px dotted #959da5;display:inline-block}
.file-header{display:flex;justify-content:space-between;align-items:center;padding:8px 12px!important}
.file-info{display:flex;align-items:center}
.file-info-entry+.file-info-entry{border-left:1px solid currentColor;margin-left:8px;padding-left:8px}
diff --git a/public/less/_repository.less b/public/less/_repository.less
index a5e6c6113b0e..3586eeccf0a6 100644
--- a/public/less/_repository.less
+++ b/public/less/_repository.less
@@ -2393,6 +2393,11 @@ tbody.commit-list {
padding-bottom: 8px;
}
+.issue-keyword {
+ border-bottom: 1px dotted #959da5;
+ display: inline-block;
+}
+
.file-header {
display: flex;
justify-content: space-between;
diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go
index 08c0635bc312..a4417107ee38 100644
--- a/routers/api/v1/repo/repo.go
+++ b/routers/api/v1/repo/repo.go
@@ -8,6 +8,7 @@ package repo
import (
"fmt"
"net/http"
+ "net/url"
"strings"
"code.gitea.io/gitea/models"
@@ -17,6 +18,7 @@ import (
"code.gitea.io/gitea/modules/migrations"
"code.gitea.io/gitea/modules/notification"
"code.gitea.io/gitea/modules/setting"
+ "code.gitea.io/gitea/modules/structs"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/modules/validation"
@@ -397,21 +399,28 @@ func Migrate(ctx *context.APIContext, form auth.MigrateRepoForm) {
return
}
+ var gitServiceType = structs.PlainGitService
+ u, err := url.Parse(remoteAddr)
+ if err == nil && strings.EqualFold(u.Host, "github.com") {
+ gitServiceType = structs.GithubService
+ }
+
var opts = migrations.MigrateOptions{
- CloneAddr: remoteAddr,
- RepoName: form.RepoName,
- Description: form.Description,
- Private: form.Private || setting.Repository.ForcePrivate,
- Mirror: form.Mirror,
- AuthUsername: form.AuthUsername,
- AuthPassword: form.AuthPassword,
- Wiki: form.Wiki,
- Issues: form.Issues,
- Milestones: form.Milestones,
- Labels: form.Labels,
- Comments: true,
- PullRequests: form.PullRequests,
- Releases: form.Releases,
+ CloneAddr: remoteAddr,
+ RepoName: form.RepoName,
+ Description: form.Description,
+ Private: form.Private || setting.Repository.ForcePrivate,
+ Mirror: form.Mirror,
+ AuthUsername: form.AuthUsername,
+ AuthPassword: form.AuthPassword,
+ Wiki: form.Wiki,
+ Issues: form.Issues,
+ Milestones: form.Milestones,
+ Labels: form.Labels,
+ Comments: true,
+ PullRequests: form.PullRequests,
+ Releases: form.Releases,
+ GitServiceType: gitServiceType,
}
if opts.Mirror {
opts.Issues = false
diff --git a/routers/user/auth.go b/routers/user/auth.go
index 3def867f6456..212d535a0649 100644
--- a/routers/user/auth.go
+++ b/routers/user/auth.go
@@ -21,6 +21,7 @@ import (
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/util"
+ "code.gitea.io/gitea/services/externalaccount"
"code.gitea.io/gitea/services/mailer"
"gitea.com/macaron/captcha"
@@ -277,7 +278,7 @@ func TwoFactorPost(ctx *context.Context, form auth.TwoFactorAuthForm) {
return
}
- err = models.LinkAccountToUser(u, gothUser.(goth.User))
+ err = externalaccount.LinkAccountToUser(u, gothUser.(goth.User))
if err != nil {
ctx.ServerError("UserSignIn", err)
return
@@ -452,7 +453,7 @@ func U2FSign(ctx *context.Context, signResp u2f.SignResponse) {
return
}
- err = models.LinkAccountToUser(user, gothUser.(goth.User))
+ err = externalaccount.LinkAccountToUser(user, gothUser.(goth.User))
if err != nil {
ctx.ServerError("UserSignIn", err)
return
@@ -601,36 +602,42 @@ func handleOAuth2SignIn(u *models.User, gothUser goth.User, ctx *context.Context
// Instead, redirect them to the 2FA authentication page.
_, err = models.GetTwoFactorByUID(u.ID)
if err != nil {
- if models.IsErrTwoFactorNotEnrolled(err) {
- err = ctx.Session.Set("uid", u.ID)
- if err != nil {
- log.Error(fmt.Sprintf("Error setting session: %v", err))
- }
- err = ctx.Session.Set("uname", u.Name)
- if err != nil {
- log.Error(fmt.Sprintf("Error setting session: %v", err))
- }
+ if !models.IsErrTwoFactorNotEnrolled(err) {
+ ctx.ServerError("UserSignIn", err)
+ return
+ }
- // Clear whatever CSRF has right now, force to generate a new one
- ctx.SetCookie(setting.CSRFCookieName, "", -1, setting.AppSubURL, setting.SessionConfig.Domain, setting.SessionConfig.Secure, true)
+ err = ctx.Session.Set("uid", u.ID)
+ if err != nil {
+ log.Error(fmt.Sprintf("Error setting session: %v", err))
+ }
+ err = ctx.Session.Set("uname", u.Name)
+ if err != nil {
+ log.Error(fmt.Sprintf("Error setting session: %v", err))
+ }
- // Register last login
- u.SetLastLogin()
- if err := models.UpdateUserCols(u, "last_login_unix"); err != nil {
- ctx.ServerError("UpdateUserCols", err)
- return
- }
+ // Clear whatever CSRF has right now, force to generate a new one
+ ctx.SetCookie(setting.CSRFCookieName, "", -1, setting.AppSubURL, setting.SessionConfig.Domain, setting.SessionConfig.Secure, true)
- if redirectTo := ctx.GetCookie("redirect_to"); len(redirectTo) > 0 {
- ctx.SetCookie("redirect_to", "", -1, setting.AppSubURL, "", setting.SessionConfig.Secure, true)
- ctx.RedirectToFirst(redirectTo)
- return
- }
+ // Register last login
+ u.SetLastLogin()
+ if err := models.UpdateUserCols(u, "last_login_unix"); err != nil {
+ ctx.ServerError("UpdateUserCols", err)
+ return
+ }
- ctx.Redirect(setting.AppSubURL + "/")
- } else {
- ctx.ServerError("UserSignIn", err)
+ // update external user information
+ if err := models.UpdateExternalUser(u, gothUser); err != nil {
+ log.Error("UpdateExternalUser failed: %v", err)
+ }
+
+ if redirectTo := ctx.GetCookie("redirect_to"); len(redirectTo) > 0 {
+ ctx.SetCookie("redirect_to", "", -1, setting.AppSubURL, "", setting.SessionConfig.Secure, true)
+ ctx.RedirectToFirst(redirectTo)
+ return
}
+
+ ctx.Redirect(setting.AppSubURL + "/")
return
}
@@ -675,7 +682,7 @@ func oAuth2UserLoginCallback(loginSource *models.LoginSource, request *http.Requ
}
if hasUser {
- return user, goth.User{}, nil
+ return user, gothUser, nil
}
// search in external linked users
@@ -689,7 +696,7 @@ func oAuth2UserLoginCallback(loginSource *models.LoginSource, request *http.Requ
}
if hasUser {
user, err = models.GetUserByID(externalLoginUser.UserID)
- return user, goth.User{}, err
+ return user, gothUser, err
}
// no user found to login
@@ -789,16 +796,18 @@ func LinkAccountPostSignIn(ctx *context.Context, signInForm auth.SignInForm) {
// Instead, redirect them to the 2FA authentication page.
_, err = models.GetTwoFactorByUID(u.ID)
if err != nil {
- if models.IsErrTwoFactorNotEnrolled(err) {
- err = models.LinkAccountToUser(u, gothUser.(goth.User))
- if err != nil {
- ctx.ServerError("UserLinkAccount", err)
- } else {
- handleSignIn(ctx, u, signInForm.Remember)
- }
- } else {
+ if !models.IsErrTwoFactorNotEnrolled(err) {
+ ctx.ServerError("UserLinkAccount", err)
+ return
+ }
+
+ err = externalaccount.LinkAccountToUser(u, gothUser.(goth.User))
+ if err != nil {
ctx.ServerError("UserLinkAccount", err)
+ return
}
+
+ handleSignIn(ctx, u, signInForm.Remember)
return
}
@@ -947,6 +956,11 @@ func LinkAccountPostRegister(ctx *context.Context, cpt *captcha.Captcha, form au
}
}
+ // update external user information
+ if err := models.UpdateExternalUser(u, gothUser.(goth.User)); err != nil {
+ log.Error("UpdateExternalUser failed: %v", err)
+ }
+
// Send confirmation email
if setting.Service.RegisterEmailConfirm && u.ID > 1 {
mailer.SendActivateAccountMail(ctx.Locale, u)
diff --git a/services/externalaccount/user.go b/services/externalaccount/user.go
new file mode 100644
index 000000000000..800546f12311
--- /dev/null
+++ b/services/externalaccount/user.go
@@ -0,0 +1,66 @@
+// Copyright 2019 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 externalaccount
+
+import (
+ "strconv"
+ "strings"
+
+ "code.gitea.io/gitea/models"
+ "code.gitea.io/gitea/modules/structs"
+
+ "github.com/markbates/goth"
+)
+
+// LinkAccountToUser link the gothUser to the user
+func LinkAccountToUser(user *models.User, gothUser goth.User) error {
+ loginSource, err := models.GetActiveOAuth2LoginSourceByName(gothUser.Provider)
+ if err != nil {
+ return err
+ }
+
+ externalLoginUser := &models.ExternalLoginUser{
+ ExternalID: gothUser.UserID,
+ UserID: user.ID,
+ LoginSourceID: loginSource.ID,
+ RawData: gothUser.RawData,
+ Provider: gothUser.Provider,
+ Email: gothUser.Email,
+ Name: gothUser.Name,
+ FirstName: gothUser.FirstName,
+ LastName: gothUser.LastName,
+ NickName: gothUser.NickName,
+ Description: gothUser.Description,
+ AvatarURL: gothUser.AvatarURL,
+ Location: gothUser.Location,
+ AccessToken: gothUser.AccessToken,
+ AccessTokenSecret: gothUser.AccessTokenSecret,
+ RefreshToken: gothUser.RefreshToken,
+ ExpiresAt: gothUser.ExpiresAt,
+ }
+
+ if err := models.LinkExternalToUser(user, externalLoginUser); err != nil {
+ return err
+ }
+
+ externalID, err := strconv.ParseInt(externalLoginUser.ExternalID, 10, 64)
+ if err != nil {
+ return err
+ }
+
+ var tp structs.GitServiceType
+ for _, s := range structs.SupportedFullGitService {
+ if strings.EqualFold(s.Name(), gothUser.Provider) {
+ tp = s
+ break
+ }
+ }
+
+ if tp.Name() != "" {
+ return models.UpdateMigrationsByType(tp, externalID, user.ID)
+ }
+
+ return nil
+}
diff --git a/services/mailer/mail_comment.go b/services/mailer/mail_comment.go
index f64db04fffa9..d306c14f4291 100644
--- a/services/mailer/mail_comment.go
+++ b/services/mailer/mail_comment.go
@@ -9,7 +9,7 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/log"
- "code.gitea.io/gitea/modules/markup"
+ "code.gitea.io/gitea/modules/references"
)
// MailParticipantsComment sends new comment emails to repository watchers
@@ -19,7 +19,7 @@ func MailParticipantsComment(c *models.Comment, opType models.ActionType, issue
}
func mailParticipantsComment(ctx models.DBContext, c *models.Comment, opType models.ActionType, issue *models.Issue) (err error) {
- rawMentions := markup.FindAllMentions(c.Content)
+ rawMentions := references.FindAllMentionsMarkdown(c.Content)
userMentions, err := issue.ResolveMentionsByVisibility(ctx, c.Poster, rawMentions)
if err != nil {
return fmt.Errorf("ResolveMentionsByVisibility [%d]: %v", c.IssueID, err)
diff --git a/services/mailer/mail_issue.go b/services/mailer/mail_issue.go
index da0249d595da..b16323909ca5 100644
--- a/services/mailer/mail_issue.go
+++ b/services/mailer/mail_issue.go
@@ -9,7 +9,7 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/log"
- "code.gitea.io/gitea/modules/markup"
+ "code.gitea.io/gitea/modules/references"
"code.gitea.io/gitea/modules/setting"
"github.com/unknwon/com"
@@ -123,7 +123,7 @@ func MailParticipants(issue *models.Issue, doer *models.User, opType models.Acti
}
func mailParticipants(ctx models.DBContext, issue *models.Issue, doer *models.User, opType models.ActionType) (err error) {
- rawMentions := markup.FindAllMentions(issue.Content)
+ rawMentions := references.FindAllMentionsMarkdown(issue.Content)
userMentions, err := issue.ResolveMentionsByVisibility(ctx, doer, rawMentions)
if err != nil {
return fmt.Errorf("ResolveMentionsByVisibility [%d]: %v", issue.ID, err)
diff --git a/vendor/modules.txt b/vendor/modules.txt
index 8b2224f54abd..30b1370933fb 100644
--- a/vendor/modules.txt
+++ b/vendor/modules.txt
@@ -610,7 +610,7 @@ gopkg.in/testfixtures.v2
gopkg.in/warnings.v0
# gopkg.in/yaml.v2 v2.2.2
gopkg.in/yaml.v2
-# mvdan.cc/xurls/v2 v2.0.0
+# mvdan.cc/xurls/v2 v2.1.0
mvdan.cc/xurls/v2
# strk.kbt.io/projects/go/libravatar v0.0.0-20191008002943-06d1c002b251
strk.kbt.io/projects/go/libravatar
diff --git a/vendor/mvdan.cc/xurls/v2/.travis.yml b/vendor/mvdan.cc/xurls/v2/.travis.yml
deleted file mode 100644
index e9bd7a765031..000000000000
--- a/vendor/mvdan.cc/xurls/v2/.travis.yml
+++ /dev/null
@@ -1,17 +0,0 @@
-language: go
-
-go:
- - 1.10.x
- - 1.11.x
-
-go_import_path: mvdan.cc/xurls
-
-env:
- - GO111MODULE=on
-
-install: true
-
-script:
- - go get -t -d ./...
- - go build ./...
- - go test ./...
diff --git a/vendor/mvdan.cc/xurls/v2/README.md b/vendor/mvdan.cc/xurls/v2/README.md
index 5058efe7a794..07fbdb3b2507 100644
--- a/vendor/mvdan.cc/xurls/v2/README.md
+++ b/vendor/mvdan.cc/xurls/v2/README.md
@@ -1,18 +1,20 @@
# xurls
[![GoDoc](https://godoc.org/mvdan.cc/xurls?status.svg)](https://godoc.org/mvdan.cc/xurls)
-[![Travis](https://travis-ci.org/mvdan/xurls.svg?branch=master)](https://travis-ci.org/mvdan/xurls)
-Extract urls from text using regular expressions. Requires Go 1.10.3 or later.
+Extract urls from text using regular expressions. Requires Go 1.12 or later.
```go
import "mvdan.cc/xurls/v2"
func main() {
- xurls.Relaxed().FindString("Do gophers live in golang.org?")
- // "golang.org"
- xurls.Strict().FindAllString("foo.com is http://foo.com/.", -1)
- // []string{"http://foo.com/"}
+ rxRelaxed := xurls.Relaxed()
+ rxRelaxed.FindString("Do gophers live in golang.org?") // "golang.org"
+ rxRelaxed.FindString("This string does not have a URL") // ""
+
+ rxStrict := xurls.Strict()
+ rxStrict.FindAllString("must have scheme: http://foo.com/.", -1) // []string{"http://foo.com/"}
+ rxStrict.FindAllString("no scheme, no match: foo.com", -1) // []string{}
}
```
@@ -20,7 +22,9 @@ Note that the funcs compile regexes, so avoid calling them repeatedly.
#### cmd/xurls
- go get -u mvdan.cc/xurls/v2/cmd/xurls
+To install the tool globally:
+
+ go get mvdan.cc/xurls/cmd/xurls
```shell
$ echo "Do gophers live in http://golang.org?" | xurls
diff --git a/vendor/mvdan.cc/xurls/v2/go.mod b/vendor/mvdan.cc/xurls/v2/go.mod
index 6f0822a73722..d9d334543c30 100644
--- a/vendor/mvdan.cc/xurls/v2/go.mod
+++ b/vendor/mvdan.cc/xurls/v2/go.mod
@@ -1 +1,3 @@
module mvdan.cc/xurls/v2
+
+go 1.13
diff --git a/vendor/mvdan.cc/xurls/v2/schemes.go b/vendor/mvdan.cc/xurls/v2/schemes.go
index 01b7944ae33e..e8e6585f4793 100644
--- a/vendor/mvdan.cc/xurls/v2/schemes.go
+++ b/vendor/mvdan.cc/xurls/v2/schemes.go
@@ -12,13 +12,18 @@ var Schemes = []string{
`about`,
`acap`,
`acct`,
+ `acd`,
`acr`,
`adiumxtra`,
+ `adt`,
`afp`,
`afs`,
`aim`,
+ `amss`,
+ `android`,
`appdata`,
`apt`,
+ `ark`,
`attachment`,
`aw`,
`barion`,
@@ -28,8 +33,11 @@ var Schemes = []string{
`blob`,
`bolo`,
`browserext`,
+ `calculator`,
`callto`,
`cap`,
+ `cast`,
+ `casts`,
`chrome`,
`chrome-extension`,
`cid`,
@@ -44,6 +52,7 @@ var Schemes = []string{
`conti`,
`crid`,
`cvs`,
+ `dab`,
`data`,
`dav`,
`diaspora`,
@@ -54,6 +63,9 @@ var Schemes = []string{
`dlna-playsingle`,
`dns`,
`dntp`,
+ `dpp`,
+ `drm`,
+ `drop`,
`dtn`,
`dvb`,
`ed2k`,
@@ -66,8 +78,11 @@ var Schemes = []string{
`file`,
`filesystem`,
`finger`,
+ `first-run-pen-experience`,
`fish`,
+ `fm`,
`ftp`,
+ `fuchsia-pkg`,
`geo`,
`gg`,
`git`,
@@ -112,6 +127,8 @@ var Schemes = []string{
`lastfm`,
`ldap`,
`ldaps`,
+ `leaptofrogans`,
+ `lorawan`,
`lvlt`,
`magnet`,
`mailserver`,
@@ -129,9 +146,11 @@ var Schemes = []string{
`moz`,
`ms-access`,
`ms-browser-extension`,
+ `ms-calculator`,
`ms-drive-to`,
`ms-enrollment`,
`ms-excel`,
+ `ms-eyecontrolspeech`,
`ms-gamebarservices`,
`ms-gamingoverlay`,
`ms-getoffice`,
@@ -141,6 +160,7 @@ var Schemes = []string{
`ms-lockscreencomponent-config`,
`ms-media-stream-id`,
`ms-mixedrealitycapture`,
+ `ms-mobileplans`,
`ms-officeapp`,
`ms-people`,
`ms-project`,
@@ -186,6 +206,7 @@ var Schemes = []string{
`msnim`,
`msrp`,
`msrps`,
+ `mss`,
`mtqp`,
`mumble`,
`mupdate`,
@@ -205,6 +226,7 @@ var Schemes = []string{
`pack`,
`palm`,
`paparazzi`,
+ `payto`,
`pkcs11`,
`platform`,
`pop`,
@@ -213,6 +235,7 @@ var Schemes = []string{
`proxy`,
`pwid`,
`psyc`,
+ `pttp`,
`qb`,
`query`,
`redis`,
diff --git a/vendor/mvdan.cc/xurls/v2/tlds.go b/vendor/mvdan.cc/xurls/v2/tlds.go
index 084ab84d46d9..c98ce508d699 100644
--- a/vendor/mvdan.cc/xurls/v2/tlds.go
+++ b/vendor/mvdan.cc/xurls/v2/tlds.go
@@ -24,7 +24,6 @@ var TLDs = []string{
`accountant`,
`accountants`,
`aco`,
- `active`,
`actor`,
`ad`,
`adac`,
@@ -154,7 +153,6 @@ var TLDs = []string{
`bj`,
`black`,
`blackfriday`,
- `blanco`,
`blockbuster`,
`blog`,
`bloomberg`,
@@ -163,7 +161,6 @@ var TLDs = []string{
`bms`,
`bmw`,
`bn`,
- `bnl`,
`bnpparibas`,
`bo`,
`boats`,
@@ -307,6 +304,7 @@ var TLDs = []string{
`coupon`,
`coupons`,
`courses`,
+ `cpa`,
`cr`,
`credit`,
`creditcard`,
@@ -370,7 +368,6 @@ var TLDs = []string{
`doctor`,
`dodge`,
`dog`,
- `doha`,
`domains`,
`dot`,
`download`,
@@ -379,7 +376,6 @@ var TLDs = []string{
`dubai`,
`duck`,
`dunlop`,
- `duns`,
`dupont`,
`durban`,
`dvag`,
@@ -400,7 +396,6 @@ var TLDs = []string{
`engineer`,
`engineering`,
`enterprises`,
- `epost`,
`epson`,
`equipment`,
`er`,
@@ -496,6 +491,7 @@ var TLDs = []string{
`games`,
`gap`,
`garden`,
+ `gay`,
`gb`,
`gbiz`,
`gd`,
@@ -588,7 +584,6 @@ var TLDs = []string{
`homes`,
`homesense`,
`honda`,
- `honeywell`,
`horse`,
`hospital`,
`host`,
@@ -642,7 +637,6 @@ var TLDs = []string{
`ir`,
`irish`,
`is`,
- `iselect`,
`ismaili`,
`ist`,
`istanbul`,
@@ -752,6 +746,7 @@ var TLDs = []string{
`lixil`,
`lk`,
`llc`,
+ `llp`,
`loan`,
`loans`,
`locker`,
@@ -827,7 +822,6 @@ var TLDs = []string{
`mo`,
`mobi`,
`mobile`,
- `mobily`,
`moda`,
`moe`,
`moi`,
@@ -1161,21 +1155,19 @@ var TLDs = []string{
`sony`,
`soy`,
`space`,
- `spiegel`,
`sport`,
`spot`,
`spreadbetting`,
`sr`,
`srl`,
`srt`,
+ `ss`,
`st`,
`stada`,
`staples`,
`star`,
- `starhub`,
`statebank`,
`statefarm`,
- `statoil`,
`stc`,
`stcgroup`,
`stockholm`,
@@ -1391,7 +1383,6 @@ var TLDs = []string{
`zara`,
`zero`,
`zip`,
- `zippo`,
`zm`,
`zone`,
`zuerich`,
@@ -1449,7 +1440,7 @@ var TLDs = []string{
`كوم`,
`مصر`,
`مليسيا`,
- `موبايلي`,
+ `موريتانيا`,
`موقع`,
`همراه`,
`پاكستان`,
diff --git a/vendor/mvdan.cc/xurls/v2/xurls.go b/vendor/mvdan.cc/xurls/v2/xurls.go
index d6279ae60b56..7244c709a0b3 100644
--- a/vendor/mvdan.cc/xurls/v2/xurls.go
+++ b/vendor/mvdan.cc/xurls/v2/xurls.go
@@ -19,9 +19,9 @@ const (
iriChar = letter + mark + number
currency = `\p{Sc}`
otherSymb = `\p{So}`
- endChar = iriChar + `/\-+_&~*%=#` + currency + otherSymb
+ endChar = iriChar + `/\-+&~%=#` + currency + otherSymb
otherPunc = `\p{Po}`
- midChar = endChar + `|` + otherPunc
+ midChar = endChar + "_*" + otherPunc
wellParen = `\([` + midChar + `]*(\([` + midChar + `]*\)[` + midChar + `]*)*\)`
wellBrack = `\[[` + midChar + `]*(\[[` + midChar + `]*\][` + midChar + `]*)*\]`
wellBrace = `\{[` + midChar + `]*(\{[` + midChar + `]*\}[` + midChar + `]*)*\}`
@@ -72,9 +72,11 @@ func strictExp() string {
}
func relaxedExp() string {
- site := domain + `(?i)` + anyOf(append(TLDs, PseudoTLDs...)...) + `(?-i)`
+ punycode := `xn--[a-z0-9-]+`
+ knownTLDs := anyOf(append(TLDs, PseudoTLDs...)...)
+ site := domain + `(?i)(` + punycode + `|` + knownTLDs + `)(?-i)`
hostName := `(` + site + `|` + ipAddr + `)`
- webURL := hostName + port + `(/|/` + pathCont + `?|\b|$)`
+ webURL := hostName + port + `(/|/` + pathCont + `?|\b|(?m)$)`
return strictExp() + `|` + webURL
}