mirror of
https://github.com/go-gitea/gitea
synced 2025-12-07 13:28:25 +00:00
Merge remote-tracking branch 'upstream/master' into team-grant-all-repos
# Conflicts: # models/migrations/migrations.go # models/migrations/v90.go # models/repo.go
This commit is contained in:
@@ -55,13 +55,13 @@ func TestHasAccess(t *testing.T) {
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, has)
|
||||
|
||||
has, err = HasAccess(user1.ID, repo2)
|
||||
_, err = HasAccess(user1.ID, repo2)
|
||||
assert.NoError(t, err)
|
||||
|
||||
has, err = HasAccess(user2.ID, repo1)
|
||||
_, err = HasAccess(user2.ID, repo1)
|
||||
assert.NoError(t, err)
|
||||
|
||||
has, err = HasAccess(user2.ID, repo2)
|
||||
_, err = HasAccess(user2.ID, repo2)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
|
||||
+29
-196
@@ -21,9 +21,9 @@ import (
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
|
||||
"github.com/Unknwon/com"
|
||||
"github.com/unknwon/com"
|
||||
"xorm.io/builder"
|
||||
)
|
||||
|
||||
@@ -65,6 +65,7 @@ var (
|
||||
)
|
||||
|
||||
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)
|
||||
@@ -73,7 +74,7 @@ func assembleKeywordsPattern(words []string) string {
|
||||
func init() {
|
||||
issueCloseKeywordsPat = regexp.MustCompile(assembleKeywordsPattern(issueCloseKeywords))
|
||||
issueReopenKeywordsPat = regexp.MustCompile(assembleKeywordsPattern(issueReopenKeywords))
|
||||
issueReferenceKeywordsPat = regexp.MustCompile(issueRefRegexpStr)
|
||||
issueReferenceKeywordsPat = regexp.MustCompile(issueRefRegexpStrNoKeyword)
|
||||
}
|
||||
|
||||
// Action represents user operation type and other information to
|
||||
@@ -91,9 +92,9 @@ type Action struct {
|
||||
Comment *Comment `xorm:"-"`
|
||||
IsDeleted bool `xorm:"INDEX NOT NULL DEFAULT false"`
|
||||
RefName string
|
||||
IsPrivate bool `xorm:"INDEX NOT NULL DEFAULT false"`
|
||||
Content string `xorm:"TEXT"`
|
||||
CreatedUnix util.TimeStamp `xorm:"INDEX created"`
|
||||
IsPrivate bool `xorm:"INDEX NOT NULL DEFAULT false"`
|
||||
Content string `xorm:"TEXT"`
|
||||
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
|
||||
}
|
||||
|
||||
// GetOpType gets the ActionType of this action.
|
||||
@@ -385,7 +386,7 @@ func NewPushCommits() *PushCommits {
|
||||
|
||||
// ToAPIPayloadCommits converts a PushCommits object to
|
||||
// api.PayloadCommit format.
|
||||
func (pc *PushCommits) ToAPIPayloadCommits(repoLink string) []*api.PayloadCommit {
|
||||
func (pc *PushCommits) ToAPIPayloadCommits(repoPath, repoLink string) ([]*api.PayloadCommit, error) {
|
||||
commits := make([]*api.PayloadCommit, len(pc.Commits))
|
||||
|
||||
if pc.emailUsers == nil {
|
||||
@@ -417,6 +418,12 @@ func (pc *PushCommits) ToAPIPayloadCommits(repoLink string) []*api.PayloadCommit
|
||||
} else {
|
||||
committerUsername = committer.Name
|
||||
}
|
||||
|
||||
fileStatus, err := git.GetCommitFileStatus(repoPath, commit.Sha1)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("FileStatus [commit_sha1: %s]: %v", commit.Sha1, err)
|
||||
}
|
||||
|
||||
commits[i] = &api.PayloadCommit{
|
||||
ID: commit.Sha1,
|
||||
Message: commit.Message,
|
||||
@@ -431,15 +438,21 @@ func (pc *PushCommits) ToAPIPayloadCommits(repoLink string) []*api.PayloadCommit
|
||||
Email: commit.CommitterEmail,
|
||||
UserName: committerUsername,
|
||||
},
|
||||
Added: fileStatus.Added,
|
||||
Removed: fileStatus.Removed,
|
||||
Modified: fileStatus.Modified,
|
||||
Timestamp: commit.Timestamp,
|
||||
}
|
||||
}
|
||||
return commits
|
||||
return commits, nil
|
||||
}
|
||||
|
||||
// AvatarLink tries to match user in database with e-mail
|
||||
// in order to show custom avatar, and falls back to general avatar link.
|
||||
func (pc *PushCommits) AvatarLink(email string) string {
|
||||
if pc.avatars == nil {
|
||||
pc.avatars = make(map[string]string)
|
||||
}
|
||||
avatar, ok := pc.avatars[email]
|
||||
if ok {
|
||||
return avatar
|
||||
@@ -499,7 +512,7 @@ func getIssueFromRef(repo *Repository, ref string) (*Issue, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
issue, err := GetIssueByIndex(refRepo.ID, int64(issueIndex))
|
||||
issue, err := GetIssueByIndex(refRepo.ID, issueIndex)
|
||||
if err != nil {
|
||||
if IsErrIssueNotExist(err) {
|
||||
return nil, nil
|
||||
@@ -564,7 +577,7 @@ func UpdateIssuesCommit(doer *User, repo *Repository, commits []*PushCommit, bra
|
||||
|
||||
// issue is from another repo
|
||||
if len(m[1]) > 0 && len(m[2]) > 0 {
|
||||
refRepo, err = GetRepositoryFromMatch(string(m[1]), string(m[2]))
|
||||
refRepo, err = GetRepositoryFromMatch(m[1], m[2])
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
@@ -601,7 +614,7 @@ func UpdateIssuesCommit(doer *User, repo *Repository, commits []*PushCommit, bra
|
||||
|
||||
// issue is from another repo
|
||||
if len(m[1]) > 0 && len(m[2]) > 0 {
|
||||
refRepo, err = GetRepositoryFromMatch(string(m[1]), string(m[2]))
|
||||
refRepo, err = GetRepositoryFromMatch(m[1], m[2])
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
@@ -630,7 +643,7 @@ func UpdateIssuesCommit(doer *User, repo *Repository, commits []*PushCommit, bra
|
||||
|
||||
// issue is from another repo
|
||||
if len(m[1]) > 0 && len(m[2]) > 0 {
|
||||
refRepo, err = GetRepositoryFromMatch(string(m[1]), string(m[2]))
|
||||
refRepo, err = GetRepositoryFromMatch(m[1], m[2])
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
@@ -654,189 +667,6 @@ func UpdateIssuesCommit(doer *User, repo *Repository, commits []*PushCommit, bra
|
||||
return nil
|
||||
}
|
||||
|
||||
// CommitRepoActionOptions represent options of a new commit action.
|
||||
type CommitRepoActionOptions struct {
|
||||
PusherName string
|
||||
RepoOwnerID int64
|
||||
RepoName string
|
||||
RefFullName string
|
||||
OldCommitID string
|
||||
NewCommitID string
|
||||
Commits *PushCommits
|
||||
}
|
||||
|
||||
// CommitRepoAction adds new commit action to the repository, and prepare
|
||||
// corresponding webhooks.
|
||||
func CommitRepoAction(opts CommitRepoActionOptions) error {
|
||||
pusher, err := GetUserByName(opts.PusherName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("GetUserByName [%s]: %v", opts.PusherName, err)
|
||||
}
|
||||
|
||||
repo, err := GetRepositoryByName(opts.RepoOwnerID, opts.RepoName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("GetRepositoryByName [owner_id: %d, name: %s]: %v", opts.RepoOwnerID, opts.RepoName, err)
|
||||
}
|
||||
|
||||
refName := git.RefEndName(opts.RefFullName)
|
||||
|
||||
// Change default branch and empty status only if pushed ref is non-empty branch.
|
||||
if repo.IsEmpty && opts.NewCommitID != git.EmptySHA && strings.HasPrefix(opts.RefFullName, git.BranchPrefix) {
|
||||
repo.DefaultBranch = refName
|
||||
repo.IsEmpty = false
|
||||
}
|
||||
|
||||
// Change repository empty status and update last updated time.
|
||||
if err = UpdateRepository(repo, false); err != nil {
|
||||
return fmt.Errorf("UpdateRepository: %v", err)
|
||||
}
|
||||
|
||||
isNewBranch := false
|
||||
opType := ActionCommitRepo
|
||||
// Check it's tag push or branch.
|
||||
if strings.HasPrefix(opts.RefFullName, git.TagPrefix) {
|
||||
opType = ActionPushTag
|
||||
if opts.NewCommitID == git.EmptySHA {
|
||||
opType = ActionDeleteTag
|
||||
}
|
||||
opts.Commits = &PushCommits{}
|
||||
} else if opts.NewCommitID == git.EmptySHA {
|
||||
opType = ActionDeleteBranch
|
||||
opts.Commits = &PushCommits{}
|
||||
} else {
|
||||
// if not the first commit, set the compare URL.
|
||||
if opts.OldCommitID == git.EmptySHA {
|
||||
isNewBranch = true
|
||||
} else {
|
||||
opts.Commits.CompareURL = repo.ComposeCompareURL(opts.OldCommitID, opts.NewCommitID)
|
||||
}
|
||||
|
||||
if err = UpdateIssuesCommit(pusher, repo, opts.Commits.Commits, refName); err != nil {
|
||||
log.Error("updateIssuesCommit: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if len(opts.Commits.Commits) > setting.UI.FeedMaxCommitNum {
|
||||
opts.Commits.Commits = opts.Commits.Commits[:setting.UI.FeedMaxCommitNum]
|
||||
}
|
||||
|
||||
data, err := json.Marshal(opts.Commits)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Marshal: %v", err)
|
||||
}
|
||||
|
||||
if err = NotifyWatchers(&Action{
|
||||
ActUserID: pusher.ID,
|
||||
ActUser: pusher,
|
||||
OpType: opType,
|
||||
Content: string(data),
|
||||
RepoID: repo.ID,
|
||||
Repo: repo,
|
||||
RefName: refName,
|
||||
IsPrivate: repo.IsPrivate,
|
||||
}); err != nil {
|
||||
return fmt.Errorf("NotifyWatchers: %v", err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
go HookQueue.Add(repo.ID)
|
||||
}()
|
||||
|
||||
apiPusher := pusher.APIFormat()
|
||||
apiRepo := repo.APIFormat(AccessModeNone)
|
||||
|
||||
var shaSum string
|
||||
var isHookEventPush = false
|
||||
switch opType {
|
||||
case ActionCommitRepo: // Push
|
||||
isHookEventPush = true
|
||||
|
||||
if isNewBranch {
|
||||
gitRepo, err := git.OpenRepository(repo.RepoPath())
|
||||
if err != nil {
|
||||
log.Error("OpenRepository[%s]: %v", repo.RepoPath(), err)
|
||||
}
|
||||
|
||||
shaSum, err = gitRepo.GetBranchCommitID(refName)
|
||||
if err != nil {
|
||||
log.Error("GetBranchCommitID[%s]: %v", opts.RefFullName, err)
|
||||
}
|
||||
if err = PrepareWebhooks(repo, HookEventCreate, &api.CreatePayload{
|
||||
Ref: refName,
|
||||
Sha: shaSum,
|
||||
RefType: "branch",
|
||||
Repo: apiRepo,
|
||||
Sender: apiPusher,
|
||||
}); err != nil {
|
||||
return fmt.Errorf("PrepareWebhooks: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
case ActionDeleteBranch: // Delete Branch
|
||||
isHookEventPush = true
|
||||
|
||||
if err = PrepareWebhooks(repo, HookEventDelete, &api.DeletePayload{
|
||||
Ref: refName,
|
||||
RefType: "branch",
|
||||
PusherType: api.PusherTypeUser,
|
||||
Repo: apiRepo,
|
||||
Sender: apiPusher,
|
||||
}); err != nil {
|
||||
return fmt.Errorf("PrepareWebhooks.(delete branch): %v", err)
|
||||
}
|
||||
|
||||
case ActionPushTag: // Create
|
||||
isHookEventPush = true
|
||||
|
||||
gitRepo, err := git.OpenRepository(repo.RepoPath())
|
||||
if err != nil {
|
||||
log.Error("OpenRepository[%s]: %v", repo.RepoPath(), err)
|
||||
}
|
||||
shaSum, err = gitRepo.GetTagCommitID(refName)
|
||||
if err != nil {
|
||||
log.Error("GetTagCommitID[%s]: %v", opts.RefFullName, err)
|
||||
}
|
||||
if err = PrepareWebhooks(repo, HookEventCreate, &api.CreatePayload{
|
||||
Ref: refName,
|
||||
Sha: shaSum,
|
||||
RefType: "tag",
|
||||
Repo: apiRepo,
|
||||
Sender: apiPusher,
|
||||
}); err != nil {
|
||||
return fmt.Errorf("PrepareWebhooks: %v", err)
|
||||
}
|
||||
case ActionDeleteTag: // Delete Tag
|
||||
isHookEventPush = true
|
||||
|
||||
if err = PrepareWebhooks(repo, HookEventDelete, &api.DeletePayload{
|
||||
Ref: refName,
|
||||
RefType: "tag",
|
||||
PusherType: api.PusherTypeUser,
|
||||
Repo: apiRepo,
|
||||
Sender: apiPusher,
|
||||
}); err != nil {
|
||||
return fmt.Errorf("PrepareWebhooks.(delete tag): %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if isHookEventPush {
|
||||
if err = PrepareWebhooks(repo, HookEventPush, &api.PushPayload{
|
||||
Ref: opts.RefFullName,
|
||||
Before: opts.OldCommitID,
|
||||
After: opts.NewCommitID,
|
||||
CompareURL: setting.AppURL + opts.Commits.CompareURL,
|
||||
Commits: opts.Commits.ToAPIPayloadCommits(repo.HTMLURL()),
|
||||
Repo: apiRepo,
|
||||
Pusher: apiPusher,
|
||||
Sender: apiPusher,
|
||||
}); err != nil {
|
||||
return fmt.Errorf("PrepareWebhooks: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func transferRepoAction(e Engine, doer, oldOwner *User, repo *Repository) (err error) {
|
||||
if err = notifyWatchers(e, &Action{
|
||||
ActUserID: doer.ID,
|
||||
@@ -918,7 +748,10 @@ func MirrorSyncPushAction(repo *Repository, opts MirrorSyncPushActionOptions) er
|
||||
opts.Commits.Commits = opts.Commits.Commits[:setting.UI.FeedMaxCommitNum]
|
||||
}
|
||||
|
||||
apiCommits := opts.Commits.ToAPIPayloadCommits(repo.HTMLURL())
|
||||
apiCommits, err := opts.Commits.ToAPIPayloadCommits(repo.RepoPath(), repo.HTMLURL())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
opts.Commits.CompareURL = repo.ComposeCompareURL(opts.OldCommitID, opts.NewCommitID)
|
||||
apiPusher := repo.MustOwner().APIFormat()
|
||||
|
||||
+74
-141
@@ -6,7 +6,6 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -86,42 +85,69 @@ func TestPushCommits_ToAPIPayloadCommits(t *testing.T) {
|
||||
pushCommits := NewPushCommits()
|
||||
pushCommits.Commits = []*PushCommit{
|
||||
{
|
||||
Sha1: "abcdef1",
|
||||
Sha1: "69554a6",
|
||||
CommitterEmail: "user2@example.com",
|
||||
CommitterName: "User Two",
|
||||
AuthorEmail: "user4@example.com",
|
||||
AuthorName: "User Four",
|
||||
Message: "message1",
|
||||
CommitterName: "User2",
|
||||
AuthorEmail: "user2@example.com",
|
||||
AuthorName: "User2",
|
||||
Message: "not signed commit",
|
||||
},
|
||||
{
|
||||
Sha1: "abcdef2",
|
||||
Sha1: "27566bd",
|
||||
CommitterEmail: "user2@example.com",
|
||||
CommitterName: "User Two",
|
||||
CommitterName: "User2",
|
||||
AuthorEmail: "user2@example.com",
|
||||
AuthorName: "User Two",
|
||||
Message: "message2",
|
||||
AuthorName: "User2",
|
||||
Message: "good signed commit (with not yet validated email)",
|
||||
},
|
||||
{
|
||||
Sha1: "5099b81",
|
||||
CommitterEmail: "user2@example.com",
|
||||
CommitterName: "User2",
|
||||
AuthorEmail: "user2@example.com",
|
||||
AuthorName: "User2",
|
||||
Message: "good signed commit",
|
||||
},
|
||||
}
|
||||
pushCommits.Len = len(pushCommits.Commits)
|
||||
|
||||
payloadCommits := pushCommits.ToAPIPayloadCommits("/username/reponame")
|
||||
if assert.Len(t, payloadCommits, 2) {
|
||||
assert.Equal(t, "abcdef1", payloadCommits[0].ID)
|
||||
assert.Equal(t, "message1", payloadCommits[0].Message)
|
||||
assert.Equal(t, "/username/reponame/commit/abcdef1", payloadCommits[0].URL)
|
||||
assert.Equal(t, "User Two", payloadCommits[0].Committer.Name)
|
||||
assert.Equal(t, "user2", payloadCommits[0].Committer.UserName)
|
||||
assert.Equal(t, "User Four", payloadCommits[0].Author.Name)
|
||||
assert.Equal(t, "user4", payloadCommits[0].Author.UserName)
|
||||
repo := AssertExistsAndLoadBean(t, &Repository{ID: 16}).(*Repository)
|
||||
payloadCommits, err := pushCommits.ToAPIPayloadCommits(repo.RepoPath(), "/user2/repo16")
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, 3, len(payloadCommits))
|
||||
|
||||
assert.Equal(t, "abcdef2", payloadCommits[1].ID)
|
||||
assert.Equal(t, "message2", payloadCommits[1].Message)
|
||||
assert.Equal(t, "/username/reponame/commit/abcdef2", payloadCommits[1].URL)
|
||||
assert.Equal(t, "User Two", payloadCommits[1].Committer.Name)
|
||||
assert.Equal(t, "user2", payloadCommits[1].Committer.UserName)
|
||||
assert.Equal(t, "User Two", payloadCommits[1].Author.Name)
|
||||
assert.Equal(t, "user2", payloadCommits[1].Author.UserName)
|
||||
}
|
||||
assert.Equal(t, "69554a6", payloadCommits[0].ID)
|
||||
assert.Equal(t, "not signed commit", payloadCommits[0].Message)
|
||||
assert.Equal(t, "/user2/repo16/commit/69554a6", payloadCommits[0].URL)
|
||||
assert.Equal(t, "User2", payloadCommits[0].Committer.Name)
|
||||
assert.Equal(t, "user2", payloadCommits[0].Committer.UserName)
|
||||
assert.Equal(t, "User2", payloadCommits[0].Author.Name)
|
||||
assert.Equal(t, "user2", payloadCommits[0].Author.UserName)
|
||||
assert.EqualValues(t, []string{}, payloadCommits[0].Added)
|
||||
assert.EqualValues(t, []string{}, payloadCommits[0].Removed)
|
||||
assert.EqualValues(t, []string{"readme.md"}, payloadCommits[0].Modified)
|
||||
|
||||
assert.Equal(t, "27566bd", payloadCommits[1].ID)
|
||||
assert.Equal(t, "good signed commit (with not yet validated email)", payloadCommits[1].Message)
|
||||
assert.Equal(t, "/user2/repo16/commit/27566bd", payloadCommits[1].URL)
|
||||
assert.Equal(t, "User2", payloadCommits[1].Committer.Name)
|
||||
assert.Equal(t, "user2", payloadCommits[1].Committer.UserName)
|
||||
assert.Equal(t, "User2", payloadCommits[1].Author.Name)
|
||||
assert.Equal(t, "user2", payloadCommits[1].Author.UserName)
|
||||
assert.EqualValues(t, []string{}, payloadCommits[1].Added)
|
||||
assert.EqualValues(t, []string{}, payloadCommits[1].Removed)
|
||||
assert.EqualValues(t, []string{"readme.md"}, payloadCommits[1].Modified)
|
||||
|
||||
assert.Equal(t, "5099b81", payloadCommits[2].ID)
|
||||
assert.Equal(t, "good signed commit", payloadCommits[2].Message)
|
||||
assert.Equal(t, "/user2/repo16/commit/5099b81", payloadCommits[2].URL)
|
||||
assert.Equal(t, "User2", payloadCommits[2].Committer.Name)
|
||||
assert.Equal(t, "user2", payloadCommits[2].Committer.UserName)
|
||||
assert.Equal(t, "User2", payloadCommits[2].Author.Name)
|
||||
assert.Equal(t, "user2", payloadCommits[2].Author.UserName)
|
||||
assert.EqualValues(t, []string{"readme.md"}, payloadCommits[2].Added)
|
||||
assert.EqualValues(t, []string{}, payloadCommits[2].Removed)
|
||||
assert.EqualValues(t, []string{}, payloadCommits[2].Modified)
|
||||
}
|
||||
|
||||
func TestPushCommits_AvatarLink(t *testing.T) {
|
||||
@@ -147,7 +173,7 @@ func TestPushCommits_AvatarLink(t *testing.T) {
|
||||
pushCommits.Len = len(pushCommits.Commits)
|
||||
|
||||
assert.Equal(t,
|
||||
"https://secure.gravatar.com/avatar/ab53a2911ddf9b4817ac01ddcd3d975f?d=identicon",
|
||||
"/suburl/user/avatar/user2/-1",
|
||||
pushCommits.AvatarLink("user2@example.com"))
|
||||
|
||||
assert.Equal(t,
|
||||
@@ -155,6 +181,26 @@ 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)
|
||||
@@ -390,119 +436,6 @@ func TestUpdateIssuesCommit_AnotherRepoNoPermission(t *testing.T) {
|
||||
CheckConsistencyFor(t, &Action{})
|
||||
}
|
||||
|
||||
func testCorrectRepoAction(t *testing.T, opts CommitRepoActionOptions, actionBean *Action) {
|
||||
AssertNotExistsBean(t, actionBean)
|
||||
assert.NoError(t, CommitRepoAction(opts))
|
||||
AssertExistsAndLoadBean(t, actionBean)
|
||||
CheckConsistencyFor(t, &Action{})
|
||||
}
|
||||
|
||||
func TestCommitRepoAction(t *testing.T) {
|
||||
samples := []struct {
|
||||
userID int64
|
||||
repositoryID int64
|
||||
commitRepoActionOptions CommitRepoActionOptions
|
||||
action Action
|
||||
}{
|
||||
{
|
||||
userID: 2,
|
||||
repositoryID: 2,
|
||||
commitRepoActionOptions: CommitRepoActionOptions{
|
||||
RefFullName: "refName",
|
||||
OldCommitID: "oldCommitID",
|
||||
NewCommitID: "newCommitID",
|
||||
Commits: &PushCommits{
|
||||
avatars: make(map[string]string),
|
||||
Commits: []*PushCommit{
|
||||
{
|
||||
Sha1: "abcdef1",
|
||||
CommitterEmail: "user2@example.com",
|
||||
CommitterName: "User Two",
|
||||
AuthorEmail: "user4@example.com",
|
||||
AuthorName: "User Four",
|
||||
Message: "message1",
|
||||
},
|
||||
{
|
||||
Sha1: "abcdef2",
|
||||
CommitterEmail: "user2@example.com",
|
||||
CommitterName: "User Two",
|
||||
AuthorEmail: "user2@example.com",
|
||||
AuthorName: "User Two",
|
||||
Message: "message2",
|
||||
},
|
||||
},
|
||||
Len: 2,
|
||||
},
|
||||
},
|
||||
action: Action{
|
||||
OpType: ActionCommitRepo,
|
||||
RefName: "refName",
|
||||
},
|
||||
},
|
||||
{
|
||||
userID: 2,
|
||||
repositoryID: 1,
|
||||
commitRepoActionOptions: CommitRepoActionOptions{
|
||||
RefFullName: git.TagPrefix + "v1.1",
|
||||
OldCommitID: git.EmptySHA,
|
||||
NewCommitID: "newCommitID",
|
||||
Commits: &PushCommits{},
|
||||
},
|
||||
action: Action{
|
||||
OpType: ActionPushTag,
|
||||
RefName: "v1.1",
|
||||
},
|
||||
},
|
||||
{
|
||||
userID: 2,
|
||||
repositoryID: 1,
|
||||
commitRepoActionOptions: CommitRepoActionOptions{
|
||||
RefFullName: git.TagPrefix + "v1.1",
|
||||
OldCommitID: "oldCommitID",
|
||||
NewCommitID: git.EmptySHA,
|
||||
Commits: &PushCommits{},
|
||||
},
|
||||
action: Action{
|
||||
OpType: ActionDeleteTag,
|
||||
RefName: "v1.1",
|
||||
},
|
||||
},
|
||||
{
|
||||
userID: 2,
|
||||
repositoryID: 1,
|
||||
commitRepoActionOptions: CommitRepoActionOptions{
|
||||
RefFullName: git.BranchPrefix + "feature/1",
|
||||
OldCommitID: "oldCommitID",
|
||||
NewCommitID: git.EmptySHA,
|
||||
Commits: &PushCommits{},
|
||||
},
|
||||
action: Action{
|
||||
OpType: ActionDeleteBranch,
|
||||
RefName: "feature/1",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, s := range samples {
|
||||
PrepareTestEnv(t)
|
||||
|
||||
user := AssertExistsAndLoadBean(t, &User{ID: s.userID}).(*User)
|
||||
repo := AssertExistsAndLoadBean(t, &Repository{ID: s.repositoryID, OwnerID: user.ID}).(*Repository)
|
||||
repo.Owner = user
|
||||
|
||||
s.commitRepoActionOptions.PusherName = user.Name
|
||||
s.commitRepoActionOptions.RepoOwnerID = user.ID
|
||||
s.commitRepoActionOptions.RepoName = repo.Name
|
||||
|
||||
s.action.ActUserID = user.ID
|
||||
s.action.RepoID = repo.ID
|
||||
s.action.Repo = repo
|
||||
s.action.IsPrivate = repo.IsPrivate
|
||||
|
||||
testCorrectRepoAction(t, s.commitRepoActionOptions, &s.action)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTransferRepoAction(t *testing.T) {
|
||||
assert.NoError(t, PrepareTestDatabase())
|
||||
|
||||
|
||||
+4
-4
@@ -9,9 +9,9 @@ import (
|
||||
"os"
|
||||
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
|
||||
"github.com/Unknwon/com"
|
||||
"github.com/unknwon/com"
|
||||
)
|
||||
|
||||
//NoticeType describes the notice type
|
||||
@@ -26,8 +26,8 @@ const (
|
||||
type Notice struct {
|
||||
ID int64 `xorm:"pk autoincr"`
|
||||
Type NoticeType
|
||||
Description string `xorm:"TEXT"`
|
||||
CreatedUnix util.TimeStamp `xorm:"INDEX created"`
|
||||
Description string `xorm:"TEXT"`
|
||||
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
|
||||
}
|
||||
|
||||
// TrStr returns a translation format string.
|
||||
|
||||
+10
-4
@@ -12,7 +12,7 @@ import (
|
||||
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
|
||||
"github.com/go-xorm/xorm"
|
||||
gouuid "github.com/satori/go.uuid"
|
||||
@@ -27,9 +27,9 @@ type Attachment struct {
|
||||
UploaderID int64 `xorm:"INDEX DEFAULT 0"` // Notice: will be zero before this column added
|
||||
CommentID int64
|
||||
Name string
|
||||
DownloadCount int64 `xorm:"DEFAULT 0"`
|
||||
Size int64 `xorm:"DEFAULT 0"`
|
||||
CreatedUnix util.TimeStamp `xorm:"created"`
|
||||
DownloadCount int64 `xorm:"DEFAULT 0"`
|
||||
Size int64 `xorm:"DEFAULT 0"`
|
||||
CreatedUnix timeutil.TimeStamp `xorm:"created"`
|
||||
}
|
||||
|
||||
// IncreaseDownloadCount is update download count + 1
|
||||
@@ -255,3 +255,9 @@ func updateAttachment(e Engine, atta *Attachment) error {
|
||||
_, err := sess.Cols("name", "issue_id", "release_id", "comment_id", "download_count").Update(atta)
|
||||
return err
|
||||
}
|
||||
|
||||
// DeleteAttachmentsByRelease deletes all attachments associated with the given release.
|
||||
func DeleteAttachmentsByRelease(releaseID int64) error {
|
||||
_, err := x.Where("release_id = ?", releaseID).Delete(&Attachment{})
|
||||
return err
|
||||
}
|
||||
|
||||
+22
-24
@@ -11,9 +11,10 @@ import (
|
||||
"code.gitea.io/gitea/modules/base"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
|
||||
"github.com/Unknwon/com"
|
||||
"github.com/unknwon/com"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -30,16 +31,18 @@ type ProtectedBranch struct {
|
||||
BranchName string `xorm:"UNIQUE(s)"`
|
||||
CanPush bool `xorm:"NOT NULL DEFAULT false"`
|
||||
EnableWhitelist bool
|
||||
WhitelistUserIDs []int64 `xorm:"JSON TEXT"`
|
||||
WhitelistTeamIDs []int64 `xorm:"JSON TEXT"`
|
||||
EnableMergeWhitelist bool `xorm:"NOT NULL DEFAULT false"`
|
||||
MergeWhitelistUserIDs []int64 `xorm:"JSON TEXT"`
|
||||
MergeWhitelistTeamIDs []int64 `xorm:"JSON TEXT"`
|
||||
ApprovalsWhitelistUserIDs []int64 `xorm:"JSON TEXT"`
|
||||
ApprovalsWhitelistTeamIDs []int64 `xorm:"JSON TEXT"`
|
||||
RequiredApprovals int64 `xorm:"NOT NULL DEFAULT 0"`
|
||||
CreatedUnix util.TimeStamp `xorm:"created"`
|
||||
UpdatedUnix util.TimeStamp `xorm:"updated"`
|
||||
WhitelistUserIDs []int64 `xorm:"JSON TEXT"`
|
||||
WhitelistTeamIDs []int64 `xorm:"JSON TEXT"`
|
||||
EnableMergeWhitelist bool `xorm:"NOT NULL DEFAULT false"`
|
||||
MergeWhitelistUserIDs []int64 `xorm:"JSON TEXT"`
|
||||
MergeWhitelistTeamIDs []int64 `xorm:"JSON TEXT"`
|
||||
EnableStatusCheck bool `xorm:"NOT NULL DEFAULT false"`
|
||||
StatusCheckContexts []string `xorm:"JSON TEXT"`
|
||||
ApprovalsWhitelistUserIDs []int64 `xorm:"JSON TEXT"`
|
||||
ApprovalsWhitelistTeamIDs []int64 `xorm:"JSON TEXT"`
|
||||
RequiredApprovals int64 `xorm:"NOT NULL DEFAULT 0"`
|
||||
CreatedUnix timeutil.TimeStamp `xorm:"created"`
|
||||
UpdatedUnix timeutil.TimeStamp `xorm:"updated"`
|
||||
}
|
||||
|
||||
// IsProtected returns if the branch is protected
|
||||
@@ -101,7 +104,7 @@ func (protectBranch *ProtectedBranch) HasEnoughApprovals(pr *PullRequest) bool {
|
||||
|
||||
// GetGrantedApprovalsCount returns the number of granted approvals for pr. A granted approval must be authored by a user in an approval whitelist.
|
||||
func (protectBranch *ProtectedBranch) GetGrantedApprovalsCount(pr *PullRequest) int64 {
|
||||
reviews, err := GetReviewersByPullID(pr.Issue.ID)
|
||||
reviews, err := GetReviewersByPullID(pr.IssueID)
|
||||
if err != nil {
|
||||
log.Error("GetReviewersByPullID: %v", err)
|
||||
return 0
|
||||
@@ -374,13 +377,13 @@ func (repo *Repository) DeleteProtectedBranch(id int64) (err error) {
|
||||
|
||||
// DeletedBranch struct
|
||||
type DeletedBranch struct {
|
||||
ID int64 `xorm:"pk autoincr"`
|
||||
RepoID int64 `xorm:"UNIQUE(s) INDEX NOT NULL"`
|
||||
Name string `xorm:"UNIQUE(s) NOT NULL"`
|
||||
Commit string `xorm:"UNIQUE(s) NOT NULL"`
|
||||
DeletedByID int64 `xorm:"INDEX"`
|
||||
DeletedBy *User `xorm:"-"`
|
||||
DeletedUnix util.TimeStamp `xorm:"INDEX created"`
|
||||
ID int64 `xorm:"pk autoincr"`
|
||||
RepoID int64 `xorm:"UNIQUE(s) INDEX NOT NULL"`
|
||||
Name string `xorm:"UNIQUE(s) NOT NULL"`
|
||||
Commit string `xorm:"UNIQUE(s) NOT NULL"`
|
||||
DeletedByID int64 `xorm:"INDEX"`
|
||||
DeletedBy *User `xorm:"-"`
|
||||
DeletedUnix timeutil.TimeStamp `xorm:"INDEX created"`
|
||||
}
|
||||
|
||||
// AddDeletedBranch adds a deleted branch to the database
|
||||
@@ -458,11 +461,6 @@ func (deletedBranch *DeletedBranch) LoadUser() {
|
||||
|
||||
// RemoveOldDeletedBranches removes old deleted branches
|
||||
func RemoveOldDeletedBranches() {
|
||||
if !taskStatusTable.StartIfNotRunning(`deleted_branches_cleanup`) {
|
||||
return
|
||||
}
|
||||
defer taskStatusTable.Stop(`deleted_branches_cleanup`)
|
||||
|
||||
log.Trace("Doing: DeletedBranchesCleanup")
|
||||
|
||||
deleteBefore := time.Now().Add(-setting.Cron.DeletedBranchesCleanup.OlderThan)
|
||||
|
||||
+78
-7
@@ -9,11 +9,14 @@ import (
|
||||
"crypto/sha1"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
|
||||
"github.com/go-xorm/xorm"
|
||||
)
|
||||
|
||||
// CommitStatusState holds the state of a Status
|
||||
@@ -64,8 +67,8 @@ type CommitStatus struct {
|
||||
Creator *User `xorm:"-"`
|
||||
CreatorID int64
|
||||
|
||||
CreatedUnix util.TimeStamp `xorm:"INDEX created"`
|
||||
UpdatedUnix util.TimeStamp `xorm:"INDEX updated"`
|
||||
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
|
||||
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
|
||||
}
|
||||
|
||||
func (status *CommitStatus) loadRepo(e Engine) (err error) {
|
||||
@@ -87,7 +90,7 @@ func (status *CommitStatus) loadRepo(e Engine) (err error) {
|
||||
// APIURL returns the absolute APIURL to this commit-status.
|
||||
func (status *CommitStatus) APIURL() string {
|
||||
_ = status.loadRepo(x)
|
||||
return fmt.Sprintf("%sapi/v1/%s/statuses/%s",
|
||||
return fmt.Sprintf("%sapi/v1/repos/%s/statuses/%s",
|
||||
setting.AppURL, status.Repo.FullName(), status.SHA)
|
||||
}
|
||||
|
||||
@@ -132,10 +135,57 @@ func CalcCommitStatus(statuses []*CommitStatus) *CommitStatus {
|
||||
return lastStatus
|
||||
}
|
||||
|
||||
// CommitStatusOptions holds the options for query commit statuses
|
||||
type CommitStatusOptions struct {
|
||||
Page int
|
||||
State string
|
||||
SortType string
|
||||
}
|
||||
|
||||
// GetCommitStatuses returns all statuses for a given commit.
|
||||
func GetCommitStatuses(repo *Repository, sha string, page int) ([]*CommitStatus, error) {
|
||||
statuses := make([]*CommitStatus, 0, 10)
|
||||
return statuses, x.Limit(10, page*10).Where("repo_id = ?", repo.ID).And("sha = ?", sha).Find(&statuses)
|
||||
func GetCommitStatuses(repo *Repository, sha string, opts *CommitStatusOptions) ([]*CommitStatus, int64, error) {
|
||||
if opts.Page <= 0 {
|
||||
opts.Page = 1
|
||||
}
|
||||
|
||||
countSession := listCommitStatusesStatement(repo, sha, opts)
|
||||
maxResults, err := countSession.Count(new(CommitStatus))
|
||||
if err != nil {
|
||||
log.Error("Count PRs: %v", err)
|
||||
return nil, maxResults, err
|
||||
}
|
||||
|
||||
statuses := make([]*CommitStatus, 0, ItemsPerPage)
|
||||
findSession := listCommitStatusesStatement(repo, sha, opts)
|
||||
sortCommitStatusesSession(findSession, opts.SortType)
|
||||
findSession.Limit(ItemsPerPage, (opts.Page-1)*ItemsPerPage)
|
||||
return statuses, maxResults, findSession.Find(&statuses)
|
||||
}
|
||||
|
||||
func listCommitStatusesStatement(repo *Repository, sha string, opts *CommitStatusOptions) *xorm.Session {
|
||||
sess := x.Where("repo_id = ?", repo.ID).And("sha = ?", sha)
|
||||
switch opts.State {
|
||||
case "pending", "success", "error", "failure", "warning":
|
||||
sess.And("state = ?", opts.State)
|
||||
}
|
||||
return sess
|
||||
}
|
||||
|
||||
func sortCommitStatusesSession(sess *xorm.Session, sortType string) {
|
||||
switch sortType {
|
||||
case "oldest":
|
||||
sess.Asc("created_unix")
|
||||
case "recentupdate":
|
||||
sess.Desc("updated_unix")
|
||||
case "leastupdate":
|
||||
sess.Asc("updated_unix")
|
||||
case "leastindex":
|
||||
sess.Desc("index")
|
||||
case "highestindex":
|
||||
sess.Asc("index")
|
||||
default:
|
||||
sess.Desc("created_unix")
|
||||
}
|
||||
}
|
||||
|
||||
// GetLatestCommitStatus returns all statuses with a unique context for a given commit.
|
||||
@@ -156,6 +206,27 @@ func GetLatestCommitStatus(repo *Repository, sha string, page int) ([]*CommitSta
|
||||
return statuses, x.In("id", ids).Find(&statuses)
|
||||
}
|
||||
|
||||
// FindRepoRecentCommitStatusContexts returns repository's recent commit status contexts
|
||||
func FindRepoRecentCommitStatusContexts(repoID int64, before time.Duration) ([]string, error) {
|
||||
start := timeutil.TimeStampNow().AddDuration(-before)
|
||||
ids := make([]int64, 0, 10)
|
||||
if err := x.Table("commit_status").
|
||||
Where("repo_id = ?", repoID).
|
||||
And("updated_unix >= ?", start).
|
||||
Select("max( id ) as id").
|
||||
GroupBy("context_hash").OrderBy("max( id ) desc").
|
||||
Find(&ids); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var contexts = make([]string, 0, len(ids))
|
||||
if len(ids) == 0 {
|
||||
return contexts, nil
|
||||
}
|
||||
return contexts, x.Select("context").Table("commit_status").In("id", ids).Find(&contexts)
|
||||
|
||||
}
|
||||
|
||||
// NewCommitStatusOptions holds options for creating a CommitStatus
|
||||
type NewCommitStatusOptions struct {
|
||||
Repo *Repository
|
||||
|
||||
@@ -17,22 +17,28 @@ func TestGetCommitStatuses(t *testing.T) {
|
||||
|
||||
sha1 := "1234123412341234123412341234123412341234"
|
||||
|
||||
statuses, err := GetCommitStatuses(repo1, sha1, 0)
|
||||
statuses, maxResults, err := GetCommitStatuses(repo1, sha1, &CommitStatusOptions{})
|
||||
assert.NoError(t, err)
|
||||
if assert.Len(t, statuses, 5) {
|
||||
assert.Equal(t, statuses[0].Context, "ci/awesomeness")
|
||||
assert.Equal(t, statuses[0].State, CommitStatusPending)
|
||||
assert.Equal(t, int(maxResults), 5)
|
||||
assert.Len(t, statuses, 5)
|
||||
|
||||
assert.Equal(t, statuses[1].Context, "cov/awesomeness")
|
||||
assert.Equal(t, statuses[1].State, CommitStatusWarning)
|
||||
assert.Equal(t, "ci/awesomeness", statuses[0].Context)
|
||||
assert.Equal(t, CommitStatusPending, statuses[0].State)
|
||||
assert.Equal(t, "https://try.gitea.io/api/v1/repos/user2/repo1/statuses/1234123412341234123412341234123412341234", statuses[0].APIURL())
|
||||
|
||||
assert.Equal(t, statuses[2].Context, "cov/awesomeness")
|
||||
assert.Equal(t, statuses[2].State, CommitStatusSuccess)
|
||||
assert.Equal(t, "cov/awesomeness", statuses[1].Context)
|
||||
assert.Equal(t, CommitStatusWarning, statuses[1].State)
|
||||
assert.Equal(t, "https://try.gitea.io/api/v1/repos/user2/repo1/statuses/1234123412341234123412341234123412341234", statuses[1].APIURL())
|
||||
|
||||
assert.Equal(t, statuses[3].Context, "ci/awesomeness")
|
||||
assert.Equal(t, statuses[3].State, CommitStatusFailure)
|
||||
assert.Equal(t, "cov/awesomeness", statuses[2].Context)
|
||||
assert.Equal(t, CommitStatusSuccess, statuses[2].State)
|
||||
assert.Equal(t, "https://try.gitea.io/api/v1/repos/user2/repo1/statuses/1234123412341234123412341234123412341234", statuses[2].APIURL())
|
||||
|
||||
assert.Equal(t, statuses[4].Context, "deploy/awesomeness")
|
||||
assert.Equal(t, statuses[4].State, CommitStatusError)
|
||||
}
|
||||
assert.Equal(t, "ci/awesomeness", statuses[3].Context)
|
||||
assert.Equal(t, CommitStatusFailure, statuses[3].State)
|
||||
assert.Equal(t, "https://try.gitea.io/api/v1/repos/user2/repo1/statuses/1234123412341234123412341234123412341234", statuses[3].APIURL())
|
||||
|
||||
assert.Equal(t, "deploy/awesomeness", statuses[4].Context)
|
||||
assert.Equal(t, CommitStatusError, statuses[4].State)
|
||||
assert.Equal(t, "https://try.gitea.io/api/v1/repos/user2/repo1/statuses/1234123412341234123412341234123412341234", statuses[4].APIURL())
|
||||
}
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
// 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 models
|
||||
|
||||
// DBContext represents a db context
|
||||
type DBContext struct {
|
||||
e Engine
|
||||
}
|
||||
|
||||
// DefaultDBContext represents a DBContext with default Engine
|
||||
func DefaultDBContext() DBContext {
|
||||
return DBContext{x}
|
||||
}
|
||||
|
||||
// Committer represents an interface to Commit or Close the dbcontext
|
||||
type Committer interface {
|
||||
Commit() error
|
||||
Close()
|
||||
}
|
||||
|
||||
// TxDBContext represents a transaction DBContext
|
||||
func TxDBContext() (DBContext, Committer, error) {
|
||||
sess := x.NewSession()
|
||||
if err := sess.Begin(); err != nil {
|
||||
sess.Close()
|
||||
return DBContext{}, nil, err
|
||||
}
|
||||
|
||||
return DBContext{sess}, sess, nil
|
||||
}
|
||||
|
||||
// WithContext represents executing database operations
|
||||
func WithContext(f func(ctx DBContext) error) error {
|
||||
return f(DBContext{x})
|
||||
}
|
||||
|
||||
// WithTx represents executing database operations on a trasaction
|
||||
func WithTx(f func(ctx DBContext) error) error {
|
||||
sess := x.NewSession()
|
||||
if err := sess.Begin(); err != nil {
|
||||
sess.Close()
|
||||
return err
|
||||
}
|
||||
|
||||
if err := f(DBContext{sess}); err != nil {
|
||||
sess.Close()
|
||||
return err
|
||||
}
|
||||
|
||||
err := sess.Commit()
|
||||
sess.Close()
|
||||
return err
|
||||
}
|
||||
+6
-2
@@ -4,11 +4,15 @@
|
||||
|
||||
package models
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
)
|
||||
|
||||
// ConvertUtf8ToUtf8mb4 converts database and tables from utf8 to utf8mb4 if it's mysql
|
||||
func ConvertUtf8ToUtf8mb4() error {
|
||||
_, err := x.Exec(fmt.Sprintf("ALTER DATABASE `%s` CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci", DbCfg.Name))
|
||||
_, err := x.Exec(fmt.Sprintf("ALTER DATABASE `%s` CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci", setting.Database.Name))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -11,6 +11,21 @@ import (
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
)
|
||||
|
||||
// ErrNotExist represents a non-exist error.
|
||||
type ErrNotExist struct {
|
||||
ID int64
|
||||
}
|
||||
|
||||
// IsErrNotExist checks if an error is an ErrNotExist
|
||||
func IsErrNotExist(err error) bool {
|
||||
_, ok := err.(ErrNotExist)
|
||||
return ok
|
||||
}
|
||||
|
||||
func (err ErrNotExist) Error() string {
|
||||
return fmt.Sprintf("record does not exist [id: %d]", err.ID)
|
||||
}
|
||||
|
||||
// ErrNameReserved represents a "reserved name" error.
|
||||
type ErrNameReserved struct {
|
||||
Name string
|
||||
@@ -1058,6 +1073,37 @@ func (err ErrIssueNotExist) Error() string {
|
||||
return fmt.Sprintf("issue does not exist [id: %d, repo_id: %d, index: %d]", err.ID, err.RepoID, err.Index)
|
||||
}
|
||||
|
||||
// ErrIssueLabelTemplateLoad represents a "ErrIssueLabelTemplateLoad" kind of error.
|
||||
type ErrIssueLabelTemplateLoad struct {
|
||||
TemplateFile string
|
||||
OriginalError error
|
||||
}
|
||||
|
||||
// IsErrIssueLabelTemplateLoad checks if an error is a ErrIssueLabelTemplateLoad.
|
||||
func IsErrIssueLabelTemplateLoad(err error) bool {
|
||||
_, ok := err.(ErrIssueLabelTemplateLoad)
|
||||
return ok
|
||||
}
|
||||
|
||||
func (err ErrIssueLabelTemplateLoad) Error() string {
|
||||
return fmt.Sprintf("Failed to load label template file '%s': %v", err.TemplateFile, err.OriginalError)
|
||||
}
|
||||
|
||||
// ErrNewIssueInsert is used when the INSERT statement in newIssue fails
|
||||
type ErrNewIssueInsert struct {
|
||||
OriginalError error
|
||||
}
|
||||
|
||||
// IsErrNewIssueInsert checks if an error is a ErrNewIssueInsert.
|
||||
func IsErrNewIssueInsert(err error) bool {
|
||||
_, ok := err.(ErrNewIssueInsert)
|
||||
return ok
|
||||
}
|
||||
|
||||
func (err ErrNewIssueInsert) Error() string {
|
||||
return err.OriginalError.Error()
|
||||
}
|
||||
|
||||
// __________ .__ .__ __________ __
|
||||
// \______ \__ __| | | |\______ \ ____ ________ __ ____ _______/ |_
|
||||
// | ___/ | \ | | | | _// __ \/ ____/ | \_/ __ \ / ___/\ __\
|
||||
@@ -1354,6 +1400,23 @@ func (err ErrTeamAlreadyExist) Error() string {
|
||||
return fmt.Sprintf("team already exists [org_id: %d, name: %s]", err.OrgID, err.Name)
|
||||
}
|
||||
|
||||
// ErrTeamNotExist represents a "TeamNotExist" error
|
||||
type ErrTeamNotExist struct {
|
||||
OrgID int64
|
||||
TeamID int64
|
||||
Name string
|
||||
}
|
||||
|
||||
// IsErrTeamNotExist checks if an error is a ErrTeamNotExist.
|
||||
func IsErrTeamNotExist(err error) bool {
|
||||
_, ok := err.(ErrTeamNotExist)
|
||||
return ok
|
||||
}
|
||||
|
||||
func (err ErrTeamNotExist) Error() string {
|
||||
return fmt.Sprintf("team does not exist [org_id %d, team_id %d, name: %s]", err.OrgID, err.TeamID, err.Name)
|
||||
}
|
||||
|
||||
//
|
||||
// Two-factor authentication
|
||||
//
|
||||
|
||||
@@ -39,3 +39,9 @@
|
||||
uid: 20
|
||||
org_id: 19
|
||||
is_public: true
|
||||
|
||||
-
|
||||
id: 8
|
||||
uid: 24
|
||||
org_id: 25
|
||||
is_public: true
|
||||
|
||||
@@ -17,3 +17,11 @@
|
||||
-
|
||||
repo_id: 33
|
||||
topic_id: 4
|
||||
|
||||
-
|
||||
repo_id: 2
|
||||
topic_id: 5
|
||||
|
||||
-
|
||||
repo_id: 2
|
||||
topic_id: 6
|
||||
|
||||
@@ -402,4 +402,39 @@
|
||||
repo_id: 11
|
||||
type: 1
|
||||
config: "{}"
|
||||
created_unix: 946684810
|
||||
created_unix: 946684810
|
||||
|
||||
-
|
||||
id: 59
|
||||
repo_id: 42
|
||||
type: 1
|
||||
config: "{}"
|
||||
created_unix: 946684810
|
||||
|
||||
-
|
||||
id: 60
|
||||
repo_id: 42
|
||||
type: 4
|
||||
config: "{}"
|
||||
created_unix: 946684810
|
||||
|
||||
-
|
||||
id: 61
|
||||
repo_id: 42
|
||||
type: 5
|
||||
config: "{}"
|
||||
created_unix: 946684810
|
||||
|
||||
-
|
||||
id: 62
|
||||
repo_id: 42
|
||||
type: 2
|
||||
config: "{\"EnableTimetracker\":true,\"AllowOnlyContributorsToTrackTime\":true}"
|
||||
created_unix: 946684810
|
||||
|
||||
-
|
||||
id: 63
|
||||
repo_id: 42
|
||||
type: 3
|
||||
config: "{\"IgnoreWhitespaceConflicts\":false,\"AllowMerge\":true,\"AllowRebase\":true,\"AllowRebaseMerge\":true,\"AllowSquash\":true}"
|
||||
created_unix: 946684810
|
||||
|
||||
@@ -165,6 +165,7 @@
|
||||
owner_id: 14
|
||||
lower_name: test_repo_14
|
||||
name: test_repo_14
|
||||
description: test_description_14
|
||||
is_private: false
|
||||
num_issues: 0
|
||||
num_closed_issues: 0
|
||||
@@ -496,4 +497,26 @@
|
||||
num_stars: 0
|
||||
num_forks: 0
|
||||
num_issues: 0
|
||||
is_mirror: false
|
||||
|
||||
-
|
||||
id: 42
|
||||
owner_id: 2
|
||||
lower_name: glob
|
||||
name: glob
|
||||
is_private: false
|
||||
num_stars: 0
|
||||
num_forks: 0
|
||||
num_issues: 0
|
||||
is_mirror: false
|
||||
|
||||
-
|
||||
id: 43
|
||||
owner_id: 26
|
||||
lower_name: repo26
|
||||
name: repo26
|
||||
is_private: true
|
||||
num_stars: 0
|
||||
num_forks: 0
|
||||
num_issues: 0
|
||||
is_mirror: false
|
||||
@@ -77,4 +77,22 @@
|
||||
name: review_team
|
||||
authorize: 1 # read
|
||||
num_repos: 1
|
||||
num_members: 1
|
||||
num_members: 1
|
||||
|
||||
-
|
||||
id: 10
|
||||
org_id: 25
|
||||
lower_name: notowners
|
||||
name: NotOwners
|
||||
authorize: 1 # owner
|
||||
num_repos: 0
|
||||
num_members: 1
|
||||
|
||||
-
|
||||
id: 11
|
||||
org_id: 26
|
||||
lower_name: team11
|
||||
name: team11
|
||||
authorize: 1 # read
|
||||
num_repos: 0
|
||||
num_members: 0
|
||||
|
||||
@@ -62,4 +62,10 @@
|
||||
id: 11
|
||||
org_id: 17
|
||||
team_id: 9
|
||||
uid: 20
|
||||
uid: 20
|
||||
|
||||
-
|
||||
id: 12
|
||||
org_id: 25
|
||||
team_id: 10
|
||||
uid: 24
|
||||
|
||||
@@ -15,3 +15,11 @@
|
||||
- id: 4
|
||||
name: graphql
|
||||
repo_count: 1
|
||||
|
||||
- id: 5
|
||||
name: topicname1
|
||||
repo_count: 1
|
||||
|
||||
- id: 6
|
||||
name: topicname2
|
||||
repo_count: 2
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
-
|
||||
id: 1
|
||||
uid: 24
|
||||
secret: KlDporn6Ile4vFcKI8z7Z6sqK1Scj2Qp0ovtUzCZO6jVbRW2lAoT7UDxDPtrab8d2B9zKOocBRdBJnS8orsrUNrsyETY+jJHb79M82uZRioKbRUz15sfOpmJmEzkFeSg6S4LicUBQos=
|
||||
scratch_salt: Qb5bq2DyR2
|
||||
scratch_hash: 068eb9b8746e0bcfe332fac4457693df1bda55800eb0f6894d14ebb736ae6a24e0fc8fc5333c19f57f81599788f0b8e51ec1
|
||||
last_used_passcode:
|
||||
created_unix: 1564253724
|
||||
updated_unix: 1564253724
|
||||
@@ -6,6 +6,7 @@
|
||||
name: user1
|
||||
full_name: User One
|
||||
email: user1@example.com
|
||||
email_notifications_preference: enabled
|
||||
passwd: 7d93daa0d1e6f2305cc8fa496847d61dc7320bb16262f9c55dd753480207234cdd96a93194e408341971742f4701772a025a # password
|
||||
type: 0 # individual
|
||||
salt: ZogKvWdyEx
|
||||
@@ -22,13 +23,14 @@
|
||||
full_name: " < U<se>r Tw<o > >< "
|
||||
email: user2@example.com
|
||||
keep_email_private: true
|
||||
email_notifications_preference: enabled
|
||||
passwd: 7d93daa0d1e6f2305cc8fa496847d61dc7320bb16262f9c55dd753480207234cdd96a93194e408341971742f4701772a025a # password
|
||||
type: 0 # individual
|
||||
salt: ZogKvWdyEx
|
||||
is_admin: false
|
||||
avatar: avatar2
|
||||
avatar_email: user2@example.com
|
||||
num_repos: 8
|
||||
num_repos: 9
|
||||
num_stars: 2
|
||||
num_followers: 2
|
||||
num_following: 1
|
||||
@@ -40,6 +42,7 @@
|
||||
name: user3
|
||||
full_name: " <<<< >> >> > >> > >>> >> "
|
||||
email: user3@example.com
|
||||
email_notifications_preference: onmention
|
||||
passwd: 7d93daa0d1e6f2305cc8fa496847d61dc7320bb16262f9c55dd753480207234cdd96a93194e408341971742f4701772a025a # password
|
||||
type: 1 # organization
|
||||
salt: ZogKvWdyEx
|
||||
@@ -56,6 +59,7 @@
|
||||
name: user4
|
||||
full_name: " "
|
||||
email: user4@example.com
|
||||
email_notifications_preference: onmention
|
||||
passwd: 7d93daa0d1e6f2305cc8fa496847d61dc7320bb16262f9c55dd753480207234cdd96a93194e408341971742f4701772a025a # password
|
||||
type: 0 # individual
|
||||
salt: ZogKvWdyEx
|
||||
@@ -72,6 +76,7 @@
|
||||
name: user5
|
||||
full_name: User Five
|
||||
email: user5@example.com
|
||||
email_notifications_preference: enabled
|
||||
passwd: 7d93daa0d1e6f2305cc8fa496847d61dc7320bb16262f9c55dd753480207234cdd96a93194e408341971742f4701772a025a # password
|
||||
type: 0 # individual
|
||||
salt: ZogKvWdyEx
|
||||
@@ -89,6 +94,7 @@
|
||||
name: user6
|
||||
full_name: User Six
|
||||
email: user6@example.com
|
||||
email_notifications_preference: enabled
|
||||
passwd: 7d93daa0d1e6f2305cc8fa496847d61dc7320bb16262f9c55dd753480207234cdd96a93194e408341971742f4701772a025a # password
|
||||
type: 1 # organization
|
||||
salt: ZogKvWdyEx
|
||||
@@ -105,6 +111,7 @@
|
||||
name: user7
|
||||
full_name: User Seven
|
||||
email: user7@example.com
|
||||
email_notifications_preference: disabled
|
||||
passwd: 7d93daa0d1e6f2305cc8fa496847d61dc7320bb16262f9c55dd753480207234cdd96a93194e408341971742f4701772a025a # password
|
||||
type: 1 # organization
|
||||
salt: ZogKvWdyEx
|
||||
@@ -121,6 +128,7 @@
|
||||
name: user8
|
||||
full_name: User Eight
|
||||
email: user8@example.com
|
||||
email_notifications_preference: enabled
|
||||
passwd: 7d93daa0d1e6f2305cc8fa496847d61dc7320bb16262f9c55dd753480207234cdd96a93194e408341971742f4701772a025a # password
|
||||
type: 0 # individual
|
||||
salt: ZogKvWdyEx
|
||||
@@ -138,6 +146,7 @@
|
||||
name: user9
|
||||
full_name: User Nine
|
||||
email: user9@example.com
|
||||
email_notifications_preference: onmention
|
||||
passwd: 7d93daa0d1e6f2305cc8fa496847d61dc7320bb16262f9c55dd753480207234cdd96a93194e408341971742f4701772a025a # password
|
||||
type: 0 # individual
|
||||
salt: ZogKvWdyEx
|
||||
@@ -365,4 +374,57 @@
|
||||
is_active: true
|
||||
num_members: 0
|
||||
num_teams: 0
|
||||
visibility: 2
|
||||
visibility: 2
|
||||
|
||||
-
|
||||
id: 24
|
||||
lower_name: user24
|
||||
name: user24
|
||||
full_name: "user24"
|
||||
email: user24@example.com
|
||||
keep_email_private: true
|
||||
passwd: 7d93daa0d1e6f2305cc8fa496847d61dc7320bb16262f9c55dd753480207234cdd96a93194e408341971742f4701772a025a # password
|
||||
type: 0 # individual
|
||||
salt: ZogKvWdyEx
|
||||
is_admin: false
|
||||
avatar: avatar24
|
||||
avatar_email: user24@example.com
|
||||
num_repos: 0
|
||||
num_stars: 0
|
||||
num_followers: 0
|
||||
num_following: 0
|
||||
is_active: true
|
||||
|
||||
-
|
||||
id: 25
|
||||
lower_name: org25
|
||||
name: org25
|
||||
full_name: "org25"
|
||||
email: org25@example.com
|
||||
passwd: 7d93daa0d1e6f2305cc8fa496847d61dc7320bb16262f9c55dd753480207234cdd96a93194e408341971742f4701772a025a # password
|
||||
type: 1 # organization
|
||||
salt: ZogKvWdyEx
|
||||
is_admin: false
|
||||
avatar: avatar25
|
||||
avatar_email: org25@example.com
|
||||
num_repos: 0
|
||||
num_members: 1
|
||||
num_teams: 1
|
||||
|
||||
-
|
||||
id: 26
|
||||
lower_name: org26
|
||||
name: org26
|
||||
full_name: "Org26"
|
||||
email: org26@example.com
|
||||
email_notifications_preference: onmention
|
||||
passwd: 7d93daa0d1e6f2305cc8fa496847d61dc7320bb16262f9c55dd753480207234cdd96a93194e408341971742f4701772a025a # password
|
||||
type: 1 # organization
|
||||
salt: ZogKvWdyEx
|
||||
is_admin: false
|
||||
avatar: avatar26
|
||||
avatar_email: org26@example.com
|
||||
num_repos: 1
|
||||
num_members: 0
|
||||
num_teams: 1
|
||||
repo_admin_change_team_access: true
|
||||
@@ -22,3 +22,10 @@
|
||||
content_type: 1 # json
|
||||
events: '{"push_only":false,"send_everything":false,"choose_events":false,"events":{"create":false,"push":true,"pull_request":true}}'
|
||||
is_active: true
|
||||
-
|
||||
id: 4
|
||||
repo_id: 2
|
||||
url: www.example.com/url4
|
||||
content_type: 1 # json
|
||||
events: '{"push_only":true,"branch_filter":"{master,feature*}"}'
|
||||
is_active: true
|
||||
|
||||
@@ -1,799 +0,0 @@
|
||||
// Copyright 2014 The Gogs 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 models
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"html"
|
||||
"html/template"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/gitea/modules/base"
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/highlight"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/process"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
"github.com/Unknwon/com"
|
||||
"github.com/sergi/go-diff/diffmatchpatch"
|
||||
"golang.org/x/net/html/charset"
|
||||
"golang.org/x/text/transform"
|
||||
)
|
||||
|
||||
// DiffLineType represents the type of a DiffLine.
|
||||
type DiffLineType uint8
|
||||
|
||||
// DiffLineType possible values.
|
||||
const (
|
||||
DiffLinePlain DiffLineType = iota + 1
|
||||
DiffLineAdd
|
||||
DiffLineDel
|
||||
DiffLineSection
|
||||
)
|
||||
|
||||
// DiffFileType represents the type of a DiffFile.
|
||||
type DiffFileType uint8
|
||||
|
||||
// DiffFileType possible values.
|
||||
const (
|
||||
DiffFileAdd DiffFileType = iota + 1
|
||||
DiffFileChange
|
||||
DiffFileDel
|
||||
DiffFileRename
|
||||
)
|
||||
|
||||
// DiffLine represents a line difference in a DiffSection.
|
||||
type DiffLine struct {
|
||||
LeftIdx int
|
||||
RightIdx int
|
||||
Type DiffLineType
|
||||
Content string
|
||||
Comments []*Comment
|
||||
}
|
||||
|
||||
// GetType returns the type of a DiffLine.
|
||||
func (d *DiffLine) GetType() int {
|
||||
return int(d.Type)
|
||||
}
|
||||
|
||||
// CanComment returns whether or not a line can get commented
|
||||
func (d *DiffLine) CanComment() bool {
|
||||
return len(d.Comments) == 0 && d.Type != DiffLineSection
|
||||
}
|
||||
|
||||
// GetCommentSide returns the comment side of the first comment, if not set returns empty string
|
||||
func (d *DiffLine) GetCommentSide() string {
|
||||
if len(d.Comments) == 0 {
|
||||
return ""
|
||||
}
|
||||
return d.Comments[0].DiffSide()
|
||||
}
|
||||
|
||||
// GetLineTypeMarker returns the line type marker
|
||||
func (d *DiffLine) GetLineTypeMarker() string {
|
||||
if strings.IndexByte(" +-", d.Content[0]) > -1 {
|
||||
return d.Content[0:1]
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// escape a line's content or return <br> needed for copy/paste purposes
|
||||
func getLineContent(content string) string {
|
||||
if len(content) > 0 {
|
||||
return html.EscapeString(content)
|
||||
}
|
||||
return "<br>"
|
||||
}
|
||||
|
||||
// DiffSection represents a section of a DiffFile.
|
||||
type DiffSection struct {
|
||||
Name string
|
||||
Lines []*DiffLine
|
||||
}
|
||||
|
||||
var (
|
||||
addedCodePrefix = []byte(`<span class="added-code">`)
|
||||
removedCodePrefix = []byte(`<span class="removed-code">`)
|
||||
codeTagSuffix = []byte(`</span>`)
|
||||
)
|
||||
|
||||
func diffToHTML(diffs []diffmatchpatch.Diff, lineType DiffLineType) template.HTML {
|
||||
buf := bytes.NewBuffer(nil)
|
||||
|
||||
for i := range diffs {
|
||||
switch {
|
||||
case diffs[i].Type == diffmatchpatch.DiffInsert && lineType == DiffLineAdd:
|
||||
buf.Write(addedCodePrefix)
|
||||
buf.WriteString(getLineContent(diffs[i].Text))
|
||||
buf.Write(codeTagSuffix)
|
||||
case diffs[i].Type == diffmatchpatch.DiffDelete && lineType == DiffLineDel:
|
||||
buf.Write(removedCodePrefix)
|
||||
buf.WriteString(getLineContent(diffs[i].Text))
|
||||
buf.Write(codeTagSuffix)
|
||||
case diffs[i].Type == diffmatchpatch.DiffEqual:
|
||||
buf.WriteString(getLineContent(diffs[i].Text))
|
||||
}
|
||||
}
|
||||
|
||||
return template.HTML(buf.Bytes())
|
||||
}
|
||||
|
||||
// GetLine gets a specific line by type (add or del) and file line number
|
||||
func (diffSection *DiffSection) GetLine(lineType DiffLineType, idx int) *DiffLine {
|
||||
var (
|
||||
difference = 0
|
||||
addCount = 0
|
||||
delCount = 0
|
||||
matchDiffLine *DiffLine
|
||||
)
|
||||
|
||||
LOOP:
|
||||
for _, diffLine := range diffSection.Lines {
|
||||
switch diffLine.Type {
|
||||
case DiffLineAdd:
|
||||
addCount++
|
||||
case DiffLineDel:
|
||||
delCount++
|
||||
default:
|
||||
if matchDiffLine != nil {
|
||||
break LOOP
|
||||
}
|
||||
difference = diffLine.RightIdx - diffLine.LeftIdx
|
||||
addCount = 0
|
||||
delCount = 0
|
||||
}
|
||||
|
||||
switch lineType {
|
||||
case DiffLineDel:
|
||||
if diffLine.RightIdx == 0 && diffLine.LeftIdx == idx-difference {
|
||||
matchDiffLine = diffLine
|
||||
}
|
||||
case DiffLineAdd:
|
||||
if diffLine.LeftIdx == 0 && diffLine.RightIdx == idx+difference {
|
||||
matchDiffLine = diffLine
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if addCount == delCount {
|
||||
return matchDiffLine
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var diffMatchPatch = diffmatchpatch.New()
|
||||
|
||||
func init() {
|
||||
diffMatchPatch.DiffEditCost = 100
|
||||
}
|
||||
|
||||
// GetComputedInlineDiffFor computes inline diff for the given line.
|
||||
func (diffSection *DiffSection) GetComputedInlineDiffFor(diffLine *DiffLine) template.HTML {
|
||||
if setting.Git.DisableDiffHighlight {
|
||||
return template.HTML(getLineContent(diffLine.Content[1:]))
|
||||
}
|
||||
var (
|
||||
compareDiffLine *DiffLine
|
||||
diff1 string
|
||||
diff2 string
|
||||
)
|
||||
|
||||
// try to find equivalent diff line. ignore, otherwise
|
||||
switch diffLine.Type {
|
||||
case DiffLineAdd:
|
||||
compareDiffLine = diffSection.GetLine(DiffLineDel, diffLine.RightIdx)
|
||||
if compareDiffLine == nil {
|
||||
return template.HTML(getLineContent(diffLine.Content[1:]))
|
||||
}
|
||||
diff1 = compareDiffLine.Content
|
||||
diff2 = diffLine.Content
|
||||
case DiffLineDel:
|
||||
compareDiffLine = diffSection.GetLine(DiffLineAdd, diffLine.LeftIdx)
|
||||
if compareDiffLine == nil {
|
||||
return template.HTML(getLineContent(diffLine.Content[1:]))
|
||||
}
|
||||
diff1 = diffLine.Content
|
||||
diff2 = compareDiffLine.Content
|
||||
default:
|
||||
if strings.IndexByte(" +-", diffLine.Content[0]) > -1 {
|
||||
return template.HTML(getLineContent(diffLine.Content[1:]))
|
||||
}
|
||||
return template.HTML(getLineContent(diffLine.Content))
|
||||
}
|
||||
|
||||
diffRecord := diffMatchPatch.DiffMain(diff1[1:], diff2[1:], true)
|
||||
diffRecord = diffMatchPatch.DiffCleanupEfficiency(diffRecord)
|
||||
|
||||
return diffToHTML(diffRecord, diffLine.Type)
|
||||
}
|
||||
|
||||
// DiffFile represents a file diff.
|
||||
type DiffFile struct {
|
||||
Name string
|
||||
OldName string
|
||||
Index int
|
||||
Addition, Deletion int
|
||||
Type DiffFileType
|
||||
IsCreated bool
|
||||
IsDeleted bool
|
||||
IsBin bool
|
||||
IsLFSFile bool
|
||||
IsRenamed bool
|
||||
IsSubmodule bool
|
||||
Sections []*DiffSection
|
||||
IsIncomplete bool
|
||||
}
|
||||
|
||||
// GetType returns type of diff file.
|
||||
func (diffFile *DiffFile) GetType() int {
|
||||
return int(diffFile.Type)
|
||||
}
|
||||
|
||||
// GetHighlightClass returns highlight class for a filename.
|
||||
func (diffFile *DiffFile) GetHighlightClass() string {
|
||||
return highlight.FileNameToHighlightClass(diffFile.Name)
|
||||
}
|
||||
|
||||
// Diff represents a difference between two git trees.
|
||||
type Diff struct {
|
||||
TotalAddition, TotalDeletion int
|
||||
Files []*DiffFile
|
||||
IsIncomplete bool
|
||||
}
|
||||
|
||||
// LoadComments loads comments into each line
|
||||
func (diff *Diff) LoadComments(issue *Issue, currentUser *User) error {
|
||||
allComments, err := FetchCodeComments(issue, currentUser)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, file := range diff.Files {
|
||||
if lineCommits, ok := allComments[file.Name]; ok {
|
||||
for _, section := range file.Sections {
|
||||
for _, line := range section.Lines {
|
||||
if comments, ok := lineCommits[int64(line.LeftIdx*-1)]; ok {
|
||||
line.Comments = append(line.Comments, comments...)
|
||||
}
|
||||
if comments, ok := lineCommits[int64(line.RightIdx)]; ok {
|
||||
line.Comments = append(line.Comments, comments...)
|
||||
}
|
||||
sort.SliceStable(line.Comments, func(i, j int) bool {
|
||||
return line.Comments[i].CreatedUnix < line.Comments[j].CreatedUnix
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// NumFiles returns number of files changes in a diff.
|
||||
func (diff *Diff) NumFiles() int {
|
||||
return len(diff.Files)
|
||||
}
|
||||
|
||||
// Example: @@ -1,8 +1,9 @@ => [..., 1, 8, 1, 9]
|
||||
var hunkRegex = regexp.MustCompile(`^@@ -(?P<beginOld>[0-9]+)(,(?P<endOld>[0-9]+))? \+(?P<beginNew>[0-9]+)(,(?P<endNew>[0-9]+))? @@`)
|
||||
|
||||
func isHeader(lof string) bool {
|
||||
return strings.HasPrefix(lof, cmdDiffHead) || strings.HasPrefix(lof, "---") || strings.HasPrefix(lof, "+++")
|
||||
}
|
||||
|
||||
// CutDiffAroundLine cuts a diff of a file in way that only the given line + numberOfLine above it will be shown
|
||||
// it also recalculates hunks and adds the appropriate headers to the new diff.
|
||||
// Warning: Only one-file diffs are allowed.
|
||||
func CutDiffAroundLine(originalDiff io.Reader, line int64, old bool, numbersOfLine int) string {
|
||||
if line == 0 || numbersOfLine == 0 {
|
||||
// no line or num of lines => no diff
|
||||
return ""
|
||||
}
|
||||
scanner := bufio.NewScanner(originalDiff)
|
||||
hunk := make([]string, 0)
|
||||
// begin is the start of the hunk containing searched line
|
||||
// end is the end of the hunk ...
|
||||
// currentLine is the line number on the side of the searched line (differentiated by old)
|
||||
// otherLine is the line number on the opposite side of the searched line (differentiated by old)
|
||||
var begin, end, currentLine, otherLine int64
|
||||
var headerLines int
|
||||
for scanner.Scan() {
|
||||
lof := scanner.Text()
|
||||
// Add header to enable parsing
|
||||
if isHeader(lof) {
|
||||
hunk = append(hunk, lof)
|
||||
headerLines++
|
||||
}
|
||||
if currentLine > line {
|
||||
break
|
||||
}
|
||||
// Detect "hunk" with contains commented lof
|
||||
if strings.HasPrefix(lof, "@@") {
|
||||
// Already got our hunk. End of hunk detected!
|
||||
if len(hunk) > headerLines {
|
||||
break
|
||||
}
|
||||
// A map with named groups of our regex to recognize them later more easily
|
||||
submatches := hunkRegex.FindStringSubmatch(lof)
|
||||
groups := make(map[string]string)
|
||||
for i, name := range hunkRegex.SubexpNames() {
|
||||
if i != 0 && name != "" {
|
||||
groups[name] = submatches[i]
|
||||
}
|
||||
}
|
||||
if old {
|
||||
begin = com.StrTo(groups["beginOld"]).MustInt64()
|
||||
end = com.StrTo(groups["endOld"]).MustInt64()
|
||||
// init otherLine with begin of opposite side
|
||||
otherLine = com.StrTo(groups["beginNew"]).MustInt64()
|
||||
} else {
|
||||
begin = com.StrTo(groups["beginNew"]).MustInt64()
|
||||
if groups["endNew"] != "" {
|
||||
end = com.StrTo(groups["endNew"]).MustInt64()
|
||||
} else {
|
||||
end = 0
|
||||
}
|
||||
// init otherLine with begin of opposite side
|
||||
otherLine = com.StrTo(groups["beginOld"]).MustInt64()
|
||||
}
|
||||
end += begin // end is for real only the number of lines in hunk
|
||||
// lof is between begin and end
|
||||
if begin <= line && end >= line {
|
||||
hunk = append(hunk, lof)
|
||||
currentLine = begin
|
||||
continue
|
||||
}
|
||||
} else if len(hunk) > headerLines {
|
||||
hunk = append(hunk, lof)
|
||||
// Count lines in context
|
||||
switch lof[0] {
|
||||
case '+':
|
||||
if !old {
|
||||
currentLine++
|
||||
} else {
|
||||
otherLine++
|
||||
}
|
||||
case '-':
|
||||
if old {
|
||||
currentLine++
|
||||
} else {
|
||||
otherLine++
|
||||
}
|
||||
default:
|
||||
currentLine++
|
||||
otherLine++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// No hunk found
|
||||
if currentLine == 0 {
|
||||
return ""
|
||||
}
|
||||
// headerLines + hunkLine (1) = totalNonCodeLines
|
||||
if len(hunk)-headerLines-1 <= numbersOfLine {
|
||||
// No need to cut the hunk => return existing hunk
|
||||
return strings.Join(hunk, "\n")
|
||||
}
|
||||
var oldBegin, oldNumOfLines, newBegin, newNumOfLines int64
|
||||
if old {
|
||||
oldBegin = currentLine
|
||||
newBegin = otherLine
|
||||
} else {
|
||||
oldBegin = otherLine
|
||||
newBegin = currentLine
|
||||
}
|
||||
// headers + hunk header
|
||||
newHunk := make([]string, headerLines)
|
||||
// transfer existing headers
|
||||
copy(newHunk, hunk[:headerLines])
|
||||
// transfer last n lines
|
||||
newHunk = append(newHunk, hunk[len(hunk)-numbersOfLine-1:]...)
|
||||
// calculate newBegin, ... by counting lines
|
||||
for i := len(hunk) - 1; i >= len(hunk)-numbersOfLine; i-- {
|
||||
switch hunk[i][0] {
|
||||
case '+':
|
||||
newBegin--
|
||||
newNumOfLines++
|
||||
case '-':
|
||||
oldBegin--
|
||||
oldNumOfLines++
|
||||
default:
|
||||
oldBegin--
|
||||
newBegin--
|
||||
newNumOfLines++
|
||||
oldNumOfLines++
|
||||
}
|
||||
}
|
||||
// construct the new hunk header
|
||||
newHunk[headerLines] = fmt.Sprintf("@@ -%d,%d +%d,%d @@",
|
||||
oldBegin, oldNumOfLines, newBegin, newNumOfLines)
|
||||
return strings.Join(newHunk, "\n")
|
||||
}
|
||||
|
||||
const cmdDiffHead = "diff --git "
|
||||
|
||||
// ParsePatch builds a Diff object from a io.Reader and some
|
||||
// parameters.
|
||||
// TODO: move this function to gogits/git-module
|
||||
func ParsePatch(maxLines, maxLineCharacters, maxFiles int, reader io.Reader) (*Diff, error) {
|
||||
var (
|
||||
diff = &Diff{Files: make([]*DiffFile, 0)}
|
||||
|
||||
curFile = &DiffFile{}
|
||||
curSection = &DiffSection{
|
||||
Lines: make([]*DiffLine, 0, 10),
|
||||
}
|
||||
|
||||
leftLine, rightLine int
|
||||
lineCount int
|
||||
curFileLinesCount int
|
||||
curFileLFSPrefix bool
|
||||
)
|
||||
|
||||
input := bufio.NewReader(reader)
|
||||
isEOF := false
|
||||
for !isEOF {
|
||||
var linebuf bytes.Buffer
|
||||
for {
|
||||
b, err := input.ReadByte()
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
isEOF = true
|
||||
break
|
||||
} else {
|
||||
return nil, fmt.Errorf("ReadByte: %v", err)
|
||||
}
|
||||
}
|
||||
if b == '\n' {
|
||||
break
|
||||
}
|
||||
if linebuf.Len() < maxLineCharacters {
|
||||
linebuf.WriteByte(b)
|
||||
} else if linebuf.Len() == maxLineCharacters {
|
||||
curFile.IsIncomplete = true
|
||||
}
|
||||
}
|
||||
line := linebuf.String()
|
||||
|
||||
if strings.HasPrefix(line, "+++ ") || strings.HasPrefix(line, "--- ") || len(line) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
trimLine := strings.Trim(line, "+- ")
|
||||
|
||||
if trimLine == LFSMetaFileIdentifier {
|
||||
curFileLFSPrefix = true
|
||||
}
|
||||
|
||||
if curFileLFSPrefix && strings.HasPrefix(trimLine, LFSMetaFileOidPrefix) {
|
||||
oid := strings.TrimPrefix(trimLine, LFSMetaFileOidPrefix)
|
||||
|
||||
if len(oid) == 64 {
|
||||
m := &LFSMetaObject{Oid: oid}
|
||||
count, err := x.Count(m)
|
||||
|
||||
if err == nil && count > 0 {
|
||||
curFile.IsBin = true
|
||||
curFile.IsLFSFile = true
|
||||
curSection.Lines = nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
curFileLinesCount++
|
||||
lineCount++
|
||||
|
||||
// Diff data too large, we only show the first about maxLines lines
|
||||
if curFileLinesCount >= maxLines {
|
||||
curFile.IsIncomplete = true
|
||||
}
|
||||
switch {
|
||||
case line[0] == ' ':
|
||||
diffLine := &DiffLine{Type: DiffLinePlain, Content: line, LeftIdx: leftLine, RightIdx: rightLine}
|
||||
leftLine++
|
||||
rightLine++
|
||||
curSection.Lines = append(curSection.Lines, diffLine)
|
||||
continue
|
||||
case line[0] == '@':
|
||||
curSection = &DiffSection{}
|
||||
curFile.Sections = append(curFile.Sections, curSection)
|
||||
ss := strings.Split(line, "@@")
|
||||
diffLine := &DiffLine{Type: DiffLineSection, Content: line}
|
||||
curSection.Lines = append(curSection.Lines, diffLine)
|
||||
|
||||
// Parse line number.
|
||||
ranges := strings.Split(ss[1][1:], " ")
|
||||
leftLine, _ = com.StrTo(strings.Split(ranges[0], ",")[0][1:]).Int()
|
||||
if len(ranges) > 1 {
|
||||
rightLine, _ = com.StrTo(strings.Split(ranges[1], ",")[0]).Int()
|
||||
} else {
|
||||
log.Warn("Parse line number failed: %v", line)
|
||||
rightLine = leftLine
|
||||
}
|
||||
continue
|
||||
case line[0] == '+':
|
||||
curFile.Addition++
|
||||
diff.TotalAddition++
|
||||
diffLine := &DiffLine{Type: DiffLineAdd, Content: line, RightIdx: rightLine}
|
||||
rightLine++
|
||||
curSection.Lines = append(curSection.Lines, diffLine)
|
||||
continue
|
||||
case line[0] == '-':
|
||||
curFile.Deletion++
|
||||
diff.TotalDeletion++
|
||||
diffLine := &DiffLine{Type: DiffLineDel, Content: line, LeftIdx: leftLine}
|
||||
if leftLine > 0 {
|
||||
leftLine++
|
||||
}
|
||||
curSection.Lines = append(curSection.Lines, diffLine)
|
||||
case strings.HasPrefix(line, "Binary"):
|
||||
curFile.IsBin = true
|
||||
continue
|
||||
}
|
||||
|
||||
// Get new file.
|
||||
if strings.HasPrefix(line, cmdDiffHead) {
|
||||
middle := -1
|
||||
|
||||
// Note: In case file name is surrounded by double quotes (it happens only in git-shell).
|
||||
// e.g. diff --git "a/xxx" "b/xxx"
|
||||
hasQuote := line[len(cmdDiffHead)] == '"'
|
||||
if hasQuote {
|
||||
middle = strings.Index(line, ` "b/`)
|
||||
} else {
|
||||
middle = strings.Index(line, " b/")
|
||||
}
|
||||
|
||||
beg := len(cmdDiffHead)
|
||||
a := line[beg+2 : middle]
|
||||
b := line[middle+3:]
|
||||
|
||||
if hasQuote {
|
||||
// Keep the entire string in double quotes for now
|
||||
a = line[beg:middle]
|
||||
b = line[middle+1:]
|
||||
|
||||
var err error
|
||||
a, err = strconv.Unquote(a)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Unquote: %v", err)
|
||||
}
|
||||
b, err = strconv.Unquote(b)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Unquote: %v", err)
|
||||
}
|
||||
// Now remove the /a /b
|
||||
a = a[2:]
|
||||
b = b[2:]
|
||||
|
||||
}
|
||||
|
||||
curFile = &DiffFile{
|
||||
Name: b,
|
||||
OldName: a,
|
||||
Index: len(diff.Files) + 1,
|
||||
Type: DiffFileChange,
|
||||
Sections: make([]*DiffSection, 0, 10),
|
||||
IsRenamed: a != b,
|
||||
}
|
||||
diff.Files = append(diff.Files, curFile)
|
||||
if len(diff.Files) >= maxFiles {
|
||||
diff.IsIncomplete = true
|
||||
_, err := io.Copy(ioutil.Discard, reader)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Copy: %v", err)
|
||||
}
|
||||
break
|
||||
}
|
||||
curFileLinesCount = 0
|
||||
curFileLFSPrefix = false
|
||||
|
||||
// Check file diff type and is submodule.
|
||||
for {
|
||||
line, err := input.ReadString('\n')
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
isEOF = true
|
||||
} else {
|
||||
return nil, fmt.Errorf("ReadString: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
switch {
|
||||
case strings.HasPrefix(line, "new file"):
|
||||
curFile.Type = DiffFileAdd
|
||||
curFile.IsCreated = true
|
||||
case strings.HasPrefix(line, "deleted"):
|
||||
curFile.Type = DiffFileDel
|
||||
curFile.IsDeleted = true
|
||||
case strings.HasPrefix(line, "index"):
|
||||
curFile.Type = DiffFileChange
|
||||
case strings.HasPrefix(line, "similarity index 100%"):
|
||||
curFile.Type = DiffFileRename
|
||||
}
|
||||
if curFile.Type > 0 {
|
||||
if strings.HasSuffix(line, " 160000\n") {
|
||||
curFile.IsSubmodule = true
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// FIXME: detect encoding while parsing.
|
||||
var buf bytes.Buffer
|
||||
for _, f := range diff.Files {
|
||||
buf.Reset()
|
||||
for _, sec := range f.Sections {
|
||||
for _, l := range sec.Lines {
|
||||
buf.WriteString(l.Content)
|
||||
buf.WriteString("\n")
|
||||
}
|
||||
}
|
||||
charsetLabel, err := base.DetectEncoding(buf.Bytes())
|
||||
if charsetLabel != "UTF-8" && err == nil {
|
||||
encoding, _ := charset.Lookup(charsetLabel)
|
||||
if encoding != nil {
|
||||
d := encoding.NewDecoder()
|
||||
for _, sec := range f.Sections {
|
||||
for _, l := range sec.Lines {
|
||||
if c, _, err := transform.String(d, l.Content); err == nil {
|
||||
l.Content = c
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return diff, nil
|
||||
}
|
||||
|
||||
// GetDiffRange builds a Diff between two commits of a repository.
|
||||
// passing the empty string as beforeCommitID returns a diff from the
|
||||
// parent commit.
|
||||
func GetDiffRange(repoPath, beforeCommitID, afterCommitID string, maxLines, maxLineCharacters, maxFiles int) (*Diff, error) {
|
||||
return GetDiffRangeWithWhitespaceBehavior(repoPath, beforeCommitID, afterCommitID, maxLines, maxLineCharacters, maxFiles, "")
|
||||
}
|
||||
|
||||
// GetDiffRangeWithWhitespaceBehavior builds a Diff between two commits of a repository.
|
||||
// Passing the empty string as beforeCommitID returns a diff from the parent commit.
|
||||
// The whitespaceBehavior is either an empty string or a git flag
|
||||
func GetDiffRangeWithWhitespaceBehavior(repoPath, beforeCommitID, afterCommitID string, maxLines, maxLineCharacters, maxFiles int, whitespaceBehavior string) (*Diff, error) {
|
||||
gitRepo, err := git.OpenRepository(repoPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
commit, err := gitRepo.GetCommit(afterCommitID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var cmd *exec.Cmd
|
||||
if len(beforeCommitID) == 0 && commit.ParentCount() == 0 {
|
||||
cmd = exec.Command(git.GitExecutable, "show", afterCommitID)
|
||||
} else {
|
||||
actualBeforeCommitID := beforeCommitID
|
||||
if len(actualBeforeCommitID) == 0 {
|
||||
parentCommit, _ := commit.Parent(0)
|
||||
actualBeforeCommitID = parentCommit.ID.String()
|
||||
}
|
||||
diffArgs := []string{"diff", "-M"}
|
||||
if len(whitespaceBehavior) != 0 {
|
||||
diffArgs = append(diffArgs, whitespaceBehavior)
|
||||
}
|
||||
diffArgs = append(diffArgs, actualBeforeCommitID)
|
||||
diffArgs = append(diffArgs, afterCommitID)
|
||||
cmd = exec.Command(git.GitExecutable, diffArgs...)
|
||||
}
|
||||
cmd.Dir = repoPath
|
||||
cmd.Stderr = os.Stderr
|
||||
|
||||
stdout, err := cmd.StdoutPipe()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("StdoutPipe: %v", err)
|
||||
}
|
||||
|
||||
if err = cmd.Start(); err != nil {
|
||||
return nil, fmt.Errorf("Start: %v", err)
|
||||
}
|
||||
|
||||
pid := process.GetManager().Add(fmt.Sprintf("GetDiffRange [repo_path: %s]", repoPath), cmd)
|
||||
defer process.GetManager().Remove(pid)
|
||||
|
||||
diff, err := ParsePatch(maxLines, maxLineCharacters, maxFiles, stdout)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("ParsePatch: %v", err)
|
||||
}
|
||||
|
||||
if err = cmd.Wait(); err != nil {
|
||||
return nil, fmt.Errorf("Wait: %v", err)
|
||||
}
|
||||
|
||||
return diff, nil
|
||||
}
|
||||
|
||||
// RawDiffType type of a raw diff.
|
||||
type RawDiffType string
|
||||
|
||||
// RawDiffType possible values.
|
||||
const (
|
||||
RawDiffNormal RawDiffType = "diff"
|
||||
RawDiffPatch RawDiffType = "patch"
|
||||
)
|
||||
|
||||
// GetRawDiff dumps diff results of repository in given commit ID to io.Writer.
|
||||
// TODO: move this function to gogits/git-module
|
||||
func GetRawDiff(repoPath, commitID string, diffType RawDiffType, writer io.Writer) error {
|
||||
return GetRawDiffForFile(repoPath, "", commitID, diffType, "", writer)
|
||||
}
|
||||
|
||||
// GetRawDiffForFile dumps diff results of file in given commit ID to io.Writer.
|
||||
// TODO: move this function to gogits/git-module
|
||||
func GetRawDiffForFile(repoPath, startCommit, endCommit string, diffType RawDiffType, file string, writer io.Writer) error {
|
||||
repo, err := git.OpenRepository(repoPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("OpenRepository: %v", err)
|
||||
}
|
||||
|
||||
commit, err := repo.GetCommit(endCommit)
|
||||
if err != nil {
|
||||
return fmt.Errorf("GetCommit: %v", err)
|
||||
}
|
||||
fileArgs := make([]string, 0)
|
||||
if len(file) > 0 {
|
||||
fileArgs = append(fileArgs, "--", file)
|
||||
}
|
||||
var cmd *exec.Cmd
|
||||
switch diffType {
|
||||
case RawDiffNormal:
|
||||
if len(startCommit) != 0 {
|
||||
cmd = exec.Command(git.GitExecutable, append([]string{"diff", "-M", startCommit, endCommit}, fileArgs...)...)
|
||||
} else if commit.ParentCount() == 0 {
|
||||
cmd = exec.Command(git.GitExecutable, append([]string{"show", endCommit}, fileArgs...)...)
|
||||
} else {
|
||||
c, _ := commit.Parent(0)
|
||||
cmd = exec.Command(git.GitExecutable, append([]string{"diff", "-M", c.ID.String(), endCommit}, fileArgs...)...)
|
||||
}
|
||||
case RawDiffPatch:
|
||||
if len(startCommit) != 0 {
|
||||
query := fmt.Sprintf("%s...%s", endCommit, startCommit)
|
||||
cmd = exec.Command(git.GitExecutable, append([]string{"format-patch", "--no-signature", "--stdout", "--root", query}, fileArgs...)...)
|
||||
} else if commit.ParentCount() == 0 {
|
||||
cmd = exec.Command(git.GitExecutable, append([]string{"format-patch", "--no-signature", "--stdout", "--root", endCommit}, fileArgs...)...)
|
||||
} else {
|
||||
c, _ := commit.Parent(0)
|
||||
query := fmt.Sprintf("%s...%s", endCommit, c.ID.String())
|
||||
cmd = exec.Command(git.GitExecutable, append([]string{"format-patch", "--no-signature", "--stdout", query}, fileArgs...)...)
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("invalid diffType: %s", diffType)
|
||||
}
|
||||
|
||||
stderr := new(bytes.Buffer)
|
||||
|
||||
cmd.Dir = repoPath
|
||||
cmd.Stdout = writer
|
||||
cmd.Stderr = stderr
|
||||
if err = cmd.Run(); err != nil {
|
||||
return fmt.Errorf("Run: %v - %s", err, stderr)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetDiffCommit builds a Diff representing the given commitID.
|
||||
func GetDiffCommit(repoPath, commitID string, maxLines, maxLineCharacters, maxFiles int) (*Diff, error) {
|
||||
return GetDiffRange(repoPath, "", commitID, maxLines, maxLineCharacters, maxFiles)
|
||||
}
|
||||
@@ -1,190 +0,0 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
|
||||
dmp "github.com/sergi/go-diff/diffmatchpatch"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func assertEqual(t *testing.T, s1 string, s2 template.HTML) {
|
||||
if s1 != string(s2) {
|
||||
t.Errorf("%s should be equal %s", s2, s1)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDiffToHTML(t *testing.T) {
|
||||
assertEqual(t, "foo <span class=\"added-code\">bar</span> biz", diffToHTML([]dmp.Diff{
|
||||
{Type: dmp.DiffEqual, Text: "foo "},
|
||||
{Type: dmp.DiffInsert, Text: "bar"},
|
||||
{Type: dmp.DiffDelete, Text: " baz"},
|
||||
{Type: dmp.DiffEqual, Text: " biz"},
|
||||
}, DiffLineAdd))
|
||||
|
||||
assertEqual(t, "foo <span class=\"removed-code\">bar</span> biz", diffToHTML([]dmp.Diff{
|
||||
{Type: dmp.DiffEqual, Text: "foo "},
|
||||
{Type: dmp.DiffDelete, Text: "bar"},
|
||||
{Type: dmp.DiffInsert, Text: " baz"},
|
||||
{Type: dmp.DiffEqual, Text: " biz"},
|
||||
}, DiffLineDel))
|
||||
}
|
||||
|
||||
const exampleDiff = `diff --git a/README.md b/README.md
|
||||
--- a/README.md
|
||||
+++ b/README.md
|
||||
@@ -1,3 +1,6 @@
|
||||
# gitea-github-migrator
|
||||
+
|
||||
+ Build Status
|
||||
- Latest Release
|
||||
Docker Pulls
|
||||
+ cut off
|
||||
+ cut off`
|
||||
|
||||
func TestCutDiffAroundLine(t *testing.T) {
|
||||
result := CutDiffAroundLine(strings.NewReader(exampleDiff), 4, false, 3)
|
||||
resultByLine := strings.Split(result, "\n")
|
||||
assert.Len(t, resultByLine, 7)
|
||||
// Check if headers got transferred
|
||||
assert.Equal(t, "diff --git a/README.md b/README.md", resultByLine[0])
|
||||
assert.Equal(t, "--- a/README.md", resultByLine[1])
|
||||
assert.Equal(t, "+++ b/README.md", resultByLine[2])
|
||||
// Check if hunk header is calculated correctly
|
||||
assert.Equal(t, "@@ -2,2 +3,2 @@", resultByLine[3])
|
||||
// Check if line got transferred
|
||||
assert.Equal(t, "+ Build Status", resultByLine[4])
|
||||
|
||||
// Must be same result as before since old line 3 == new line 5
|
||||
newResult := CutDiffAroundLine(strings.NewReader(exampleDiff), 3, true, 3)
|
||||
assert.Equal(t, result, newResult, "Must be same result as before since old line 3 == new line 5")
|
||||
|
||||
newResult = CutDiffAroundLine(strings.NewReader(exampleDiff), 6, false, 300)
|
||||
assert.Equal(t, exampleDiff, newResult)
|
||||
|
||||
emptyResult := CutDiffAroundLine(strings.NewReader(exampleDiff), 6, false, 0)
|
||||
assert.Empty(t, emptyResult)
|
||||
|
||||
// Line is out of scope
|
||||
emptyResult = CutDiffAroundLine(strings.NewReader(exampleDiff), 434, false, 0)
|
||||
assert.Empty(t, emptyResult)
|
||||
}
|
||||
|
||||
func BenchmarkCutDiffAroundLine(b *testing.B) {
|
||||
for n := 0; n < b.N; n++ {
|
||||
CutDiffAroundLine(strings.NewReader(exampleDiff), 3, true, 3)
|
||||
}
|
||||
}
|
||||
|
||||
func ExampleCutDiffAroundLine() {
|
||||
const diff = `diff --git a/README.md b/README.md
|
||||
--- a/README.md
|
||||
+++ b/README.md
|
||||
@@ -1,3 +1,6 @@
|
||||
# gitea-github-migrator
|
||||
+
|
||||
+ Build Status
|
||||
- Latest Release
|
||||
Docker Pulls
|
||||
+ cut off
|
||||
+ cut off`
|
||||
result := CutDiffAroundLine(strings.NewReader(diff), 4, false, 3)
|
||||
println(result)
|
||||
}
|
||||
|
||||
func TestParsePatch(t *testing.T) {
|
||||
var diff = `diff --git "a/README.md" "b/README.md"
|
||||
--- a/README.md
|
||||
+++ b/README.md
|
||||
@@ -1,3 +1,6 @@
|
||||
# gitea-github-migrator
|
||||
+
|
||||
+ Build Status
|
||||
- Latest Release
|
||||
Docker Pulls
|
||||
+ cut off
|
||||
+ cut off`
|
||||
result, err := ParsePatch(setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, strings.NewReader(diff))
|
||||
if err != nil {
|
||||
t.Errorf("ParsePatch failed: %s", err)
|
||||
}
|
||||
println(result)
|
||||
|
||||
var diff2 = `diff --git "a/A \\ B" "b/A \\ B"
|
||||
--- "a/A \\ B"
|
||||
+++ "b/A \\ B"
|
||||
@@ -1,3 +1,6 @@
|
||||
# gitea-github-migrator
|
||||
+
|
||||
+ Build Status
|
||||
- Latest Release
|
||||
Docker Pulls
|
||||
+ cut off
|
||||
+ cut off`
|
||||
result, err = ParsePatch(setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, strings.NewReader(diff2))
|
||||
if err != nil {
|
||||
t.Errorf("ParsePatch failed: %s", err)
|
||||
}
|
||||
println(result)
|
||||
|
||||
var diff3 = `diff --git a/README.md b/README.md
|
||||
--- a/README.md
|
||||
+++ b/README.md
|
||||
@@ -1,3 +1,6 @@
|
||||
# gitea-github-migrator
|
||||
+
|
||||
+ Build Status
|
||||
- Latest Release
|
||||
Docker Pulls
|
||||
+ cut off
|
||||
+ cut off`
|
||||
result, err = ParsePatch(setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, strings.NewReader(diff3))
|
||||
if err != nil {
|
||||
t.Errorf("ParsePatch failed: %s", err)
|
||||
}
|
||||
println(result)
|
||||
}
|
||||
|
||||
func setupDefaultDiff() *Diff {
|
||||
return &Diff{
|
||||
Files: []*DiffFile{
|
||||
{
|
||||
Name: "README.md",
|
||||
Sections: []*DiffSection{
|
||||
{
|
||||
Lines: []*DiffLine{
|
||||
{
|
||||
LeftIdx: 4,
|
||||
RightIdx: 4,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
func TestDiff_LoadComments(t *testing.T) {
|
||||
issue := AssertExistsAndLoadBean(t, &Issue{ID: 2}).(*Issue)
|
||||
user := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
|
||||
diff := setupDefaultDiff()
|
||||
assert.NoError(t, PrepareTestDatabase())
|
||||
assert.NoError(t, diff.LoadComments(issue, user))
|
||||
assert.Len(t, diff.Files[0].Sections[0].Lines[0].Comments, 2)
|
||||
}
|
||||
|
||||
func TestDiffLine_CanComment(t *testing.T) {
|
||||
assert.False(t, (&DiffLine{Type: DiffLineSection}).CanComment())
|
||||
assert.False(t, (&DiffLine{Type: DiffLineAdd, Comments: []*Comment{{Content: "bla"}}}).CanComment())
|
||||
assert.True(t, (&DiffLine{Type: DiffLineAdd}).CanComment())
|
||||
assert.True(t, (&DiffLine{Type: DiffLineDel}).CanComment())
|
||||
assert.True(t, (&DiffLine{Type: DiffLinePlain}).CanComment())
|
||||
}
|
||||
|
||||
func TestDiffLine_GetCommentSide(t *testing.T) {
|
||||
assert.Equal(t, "previous", (&DiffLine{Comments: []*Comment{{Line: -3}}}).GetCommentSide())
|
||||
assert.Equal(t, "proposed", (&DiffLine{Comments: []*Comment{{Line: 3}}}).GetCommentSide())
|
||||
}
|
||||
+14
-14
@@ -17,7 +17,7 @@ import (
|
||||
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
|
||||
"github.com/go-xorm/xorm"
|
||||
"github.com/keybase/go-crypto/openpgp"
|
||||
@@ -27,14 +27,14 @@ import (
|
||||
|
||||
// GPGKey represents a GPG key.
|
||||
type GPGKey struct {
|
||||
ID int64 `xorm:"pk autoincr"`
|
||||
OwnerID int64 `xorm:"INDEX NOT NULL"`
|
||||
KeyID string `xorm:"INDEX CHAR(16) NOT NULL"`
|
||||
PrimaryKeyID string `xorm:"CHAR(16)"`
|
||||
Content string `xorm:"TEXT NOT NULL"`
|
||||
CreatedUnix util.TimeStamp `xorm:"created"`
|
||||
ExpiredUnix util.TimeStamp
|
||||
AddedUnix util.TimeStamp
|
||||
ID int64 `xorm:"pk autoincr"`
|
||||
OwnerID int64 `xorm:"INDEX NOT NULL"`
|
||||
KeyID string `xorm:"INDEX CHAR(16) NOT NULL"`
|
||||
PrimaryKeyID string `xorm:"CHAR(16)"`
|
||||
Content string `xorm:"TEXT NOT NULL"`
|
||||
CreatedUnix timeutil.TimeStamp `xorm:"created"`
|
||||
ExpiredUnix timeutil.TimeStamp
|
||||
AddedUnix timeutil.TimeStamp
|
||||
SubsKey []*GPGKey `xorm:"-"`
|
||||
Emails []*EmailAddress
|
||||
CanSign bool
|
||||
@@ -51,7 +51,7 @@ type GPGKeyImport struct {
|
||||
|
||||
// BeforeInsert will be invoked by XORM before inserting a record
|
||||
func (key *GPGKey) BeforeInsert() {
|
||||
key.AddedUnix = util.TimeStampNow()
|
||||
key.AddedUnix = timeutil.TimeStampNow()
|
||||
}
|
||||
|
||||
// AfterLoad is invoked from XORM after setting the values of all fields of this object.
|
||||
@@ -223,8 +223,8 @@ func parseSubGPGKey(ownerID int64, primaryID string, pubkey *packet.PublicKey, e
|
||||
KeyID: pubkey.KeyIdString(),
|
||||
PrimaryKeyID: primaryID,
|
||||
Content: content,
|
||||
CreatedUnix: util.TimeStamp(pubkey.CreationTime.Unix()),
|
||||
ExpiredUnix: util.TimeStamp(expiry.Unix()),
|
||||
CreatedUnix: timeutil.TimeStamp(pubkey.CreationTime.Unix()),
|
||||
ExpiredUnix: timeutil.TimeStamp(expiry.Unix()),
|
||||
CanSign: pubkey.CanSign(),
|
||||
CanEncryptComms: pubkey.PubKeyAlgo.CanEncrypt(),
|
||||
CanEncryptStorage: pubkey.PubKeyAlgo.CanEncrypt(),
|
||||
@@ -301,8 +301,8 @@ func parseGPGKey(ownerID int64, e *openpgp.Entity) (*GPGKey, error) {
|
||||
KeyID: pubkey.KeyIdString(),
|
||||
PrimaryKeyID: "",
|
||||
Content: content,
|
||||
CreatedUnix: util.TimeStamp(pubkey.CreationTime.Unix()),
|
||||
ExpiredUnix: util.TimeStamp(expiry.Unix()),
|
||||
CreatedUnix: timeutil.TimeStamp(pubkey.CreationTime.Unix()),
|
||||
ExpiredUnix: timeutil.TimeStamp(expiry.Unix()),
|
||||
Emails: emails,
|
||||
SubsKey: subkeys,
|
||||
CanSign: pubkey.CanSign(),
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
@@ -112,7 +112,7 @@ MkM/fdpyc2hY7Dl/+qFmN5MG5yGmMpQcX+RNNR222ibNC1D3wg==
|
||||
key := &GPGKey{
|
||||
KeyID: pubkey.KeyIdString(),
|
||||
Content: content,
|
||||
CreatedUnix: util.TimeStamp(pubkey.CreationTime.Unix()),
|
||||
CreatedUnix: timeutil.TimeStamp(pubkey.CreationTime.Unix()),
|
||||
CanSign: pubkey.CanSign(),
|
||||
CanEncryptComms: pubkey.PubKeyAlgo.CanEncrypt(),
|
||||
CanEncryptStorage: pubkey.PubKeyAlgo.CanEncrypt(),
|
||||
@@ -122,7 +122,7 @@ MkM/fdpyc2hY7Dl/+qFmN5MG5yGmMpQcX+RNNR222ibNC1D3wg==
|
||||
cannotsignkey := &GPGKey{
|
||||
KeyID: pubkey.KeyIdString(),
|
||||
Content: content,
|
||||
CreatedUnix: util.TimeStamp(pubkey.CreationTime.Unix()),
|
||||
CreatedUnix: timeutil.TimeStamp(pubkey.CreationTime.Unix()),
|
||||
CanSign: false,
|
||||
CanEncryptComms: false,
|
||||
CanEncryptStorage: false,
|
||||
|
||||
@@ -14,7 +14,7 @@ import (
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
|
||||
"github.com/Unknwon/com"
|
||||
"github.com/unknwon/com"
|
||||
)
|
||||
|
||||
// LocalCopyPath returns the local repository temporary copy path.
|
||||
|
||||
@@ -12,13 +12,13 @@ import (
|
||||
|
||||
// PushingEnvironment returns an os environment to allow hooks to work on push
|
||||
func PushingEnvironment(doer *User, repo *Repository) []string {
|
||||
return FullPushingEnvironment(doer, doer, repo, 0)
|
||||
return FullPushingEnvironment(doer, doer, repo, repo.Name, 0)
|
||||
}
|
||||
|
||||
// FullPushingEnvironment returns an os environment to allow hooks to work on push
|
||||
func FullPushingEnvironment(author, committer *User, repo *Repository, prID int64) []string {
|
||||
func FullPushingEnvironment(author, committer *User, repo *Repository, repoName string, prID int64) []string {
|
||||
isWiki := "false"
|
||||
if strings.HasSuffix(repo.Name, ".wiki") {
|
||||
if strings.HasSuffix(repoName, ".wiki") {
|
||||
isWiki = "true"
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ func FullPushingEnvironment(author, committer *User, repo *Repository, prID int6
|
||||
"GIT_AUTHOR_EMAIL="+authorSig.Email,
|
||||
"GIT_COMMITTER_NAME="+committerSig.Name,
|
||||
"GIT_COMMITTER_EMAIL="+committerSig.Email,
|
||||
EnvRepoName+"="+repo.Name,
|
||||
EnvRepoName+"="+repoName,
|
||||
EnvRepoUsername+"="+repo.MustOwnerName(),
|
||||
EnvRepoIsWiki+"="+isWiki,
|
||||
EnvPusherName+"="+committer.Name,
|
||||
|
||||
+153
-81
@@ -5,7 +5,6 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"path"
|
||||
"regexp"
|
||||
@@ -16,10 +15,11 @@ import (
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
|
||||
"github.com/Unknwon/com"
|
||||
"github.com/go-xorm/xorm"
|
||||
"github.com/unknwon/com"
|
||||
"xorm.io/builder"
|
||||
)
|
||||
|
||||
@@ -49,11 +49,11 @@ type Issue struct {
|
||||
NumComments int
|
||||
Ref string
|
||||
|
||||
DeadlineUnix util.TimeStamp `xorm:"INDEX"`
|
||||
DeadlineUnix timeutil.TimeStamp `xorm:"INDEX"`
|
||||
|
||||
CreatedUnix util.TimeStamp `xorm:"INDEX created"`
|
||||
UpdatedUnix util.TimeStamp `xorm:"INDEX updated"`
|
||||
ClosedUnix util.TimeStamp `xorm:"INDEX"`
|
||||
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
|
||||
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
|
||||
ClosedUnix timeutil.TimeStamp `xorm:"INDEX"`
|
||||
|
||||
Attachments []*Attachment `xorm:"-"`
|
||||
Comments []*Comment `xorm:"-"`
|
||||
@@ -73,6 +73,7 @@ var (
|
||||
|
||||
const issueTasksRegexpStr = `(^\s*[-*]\s\[[\sx]\]\s.)|(\n\s*[-*]\s\[[\sx]\]\s.)`
|
||||
const issueTasksDoneRegexpStr = `(^\s*[-*]\s\[[x]\]\s.)|(\n\s*[-*]\s\[[x]\]\s.)`
|
||||
const issueMaxDupIndexAttempts = 3
|
||||
|
||||
func init() {
|
||||
issueTasksPat = regexp.MustCompile(issueTasksRegexpStr)
|
||||
@@ -90,7 +91,7 @@ func (issue *Issue) loadTotalTimes(e Engine) (err error) {
|
||||
|
||||
// IsOverdue checks if the issue is overdue
|
||||
func (issue *Issue) IsOverdue() bool {
|
||||
return util.TimeStampNow() >= issue.DeadlineUnix
|
||||
return timeutil.TimeStampNow() >= issue.DeadlineUnix
|
||||
}
|
||||
|
||||
// LoadRepo loads issue's repository
|
||||
@@ -472,6 +473,18 @@ func (issue *Issue) sendLabelUpdatedWebhook(doer *User) {
|
||||
}
|
||||
}
|
||||
|
||||
// ReplyReference returns tokenized address to use for email reply headers
|
||||
func (issue *Issue) ReplyReference() string {
|
||||
var path string
|
||||
if issue.IsPull {
|
||||
path = "pulls"
|
||||
} else {
|
||||
path = "issues"
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%s/%s/%d@%s", issue.Repo.FullName(), path, issue.Index, setting.Domain)
|
||||
}
|
||||
|
||||
func (issue *Issue) addLabel(e *xorm.Session, label *Label, doer *User) error {
|
||||
return newIssueLabel(e, issue, label, doer)
|
||||
}
|
||||
@@ -582,8 +595,9 @@ func (issue *Issue) ClearLabels(doer *User) (err error) {
|
||||
if err = sess.Commit(); err != nil {
|
||||
return fmt.Errorf("Commit: %v", err)
|
||||
}
|
||||
sess.Close()
|
||||
|
||||
if err = issue.loadPoster(x); err != nil {
|
||||
if err = issue.LoadPoster(); err != nil {
|
||||
return fmt.Errorf("loadPoster: %v", err)
|
||||
}
|
||||
|
||||
@@ -732,7 +746,7 @@ func (issue *Issue) changeStatus(e *xorm.Session, doer *User, isClosed bool) (er
|
||||
|
||||
issue.IsClosed = isClosed
|
||||
if isClosed {
|
||||
issue.ClosedUnix = util.TimeStampNow()
|
||||
issue.ClosedUnix = timeutil.TimeStampNow()
|
||||
} else {
|
||||
issue.ClosedUnix = 0
|
||||
}
|
||||
@@ -746,11 +760,6 @@ func (issue *Issue) changeStatus(e *xorm.Session, doer *User, isClosed bool) (er
|
||||
return err
|
||||
}
|
||||
for idx := range issue.Labels {
|
||||
if issue.IsClosed {
|
||||
issue.Labels[idx].NumClosedIssues++
|
||||
} else {
|
||||
issue.Labels[idx].NumClosedIssues--
|
||||
}
|
||||
if err = updateLabel(e, issue.Labels[idx]); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -857,9 +866,18 @@ func (issue *Issue) ChangeTitle(doer *User, title string) (err error) {
|
||||
return fmt.Errorf("createChangeTitleComment: %v", err)
|
||||
}
|
||||
|
||||
if err = issue.neuterCrossReferences(sess); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = issue.addCrossReferences(sess, doer); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = sess.Commit(); err != nil {
|
||||
return err
|
||||
}
|
||||
sess.Close()
|
||||
|
||||
mode, _ := AccessLevel(issue.Poster, issue.Repo)
|
||||
if issue.IsPull {
|
||||
@@ -926,9 +944,26 @@ func (issue *Issue) ChangeContent(doer *User, content string) (err error) {
|
||||
oldContent := issue.Content
|
||||
issue.Content = content
|
||||
|
||||
if err = UpdateIssueCols(issue, "content"); err != nil {
|
||||
sess := x.NewSession()
|
||||
defer sess.Close()
|
||||
if err = sess.Begin(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = updateIssueCols(sess, issue, "content"); err != nil {
|
||||
return fmt.Errorf("UpdateIssueCols: %v", err)
|
||||
}
|
||||
if err = issue.neuterCrossReferences(sess); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = issue.addCrossReferences(sess, doer); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = sess.Commit(); err != nil {
|
||||
return err
|
||||
}
|
||||
sess.Close()
|
||||
|
||||
mode, _ := AccessLevel(issue.Poster, issue.Repo)
|
||||
if issue.IsPull {
|
||||
@@ -979,7 +1014,7 @@ func (issue *Issue) GetTasksDone() int {
|
||||
}
|
||||
|
||||
// GetLastEventTimestamp returns the last user visible event timestamp, either the creation of this issue or the close.
|
||||
func (issue *Issue) GetLastEventTimestamp() util.TimeStamp {
|
||||
func (issue *Issue) GetLastEventTimestamp() timeutil.TimeStamp {
|
||||
if issue.IsClosed {
|
||||
return issue.ClosedUnix
|
||||
}
|
||||
@@ -1018,36 +1053,9 @@ type NewIssueOptions struct {
|
||||
IsPull bool
|
||||
}
|
||||
|
||||
// GetMaxIndexOfIssue returns the max index on issue
|
||||
func GetMaxIndexOfIssue(repoID int64) (int64, error) {
|
||||
return getMaxIndexOfIssue(x, repoID)
|
||||
}
|
||||
|
||||
func getMaxIndexOfIssue(e Engine, repoID int64) (int64, error) {
|
||||
var (
|
||||
maxIndex int64
|
||||
has bool
|
||||
err error
|
||||
)
|
||||
|
||||
has, err = e.SQL("SELECT COALESCE((SELECT MAX(`index`) FROM issue WHERE repo_id = ?),0)", repoID).Get(&maxIndex)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
} else if !has {
|
||||
return 0, errors.New("Retrieve Max index from issue failed")
|
||||
}
|
||||
return maxIndex, nil
|
||||
}
|
||||
|
||||
func newIssue(e *xorm.Session, doer *User, opts NewIssueOptions) (err error) {
|
||||
opts.Issue.Title = strings.TrimSpace(opts.Issue.Title)
|
||||
|
||||
maxIndex, err := getMaxIndexOfIssue(e, opts.Issue.RepoID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
opts.Issue.Index = maxIndex + 1
|
||||
|
||||
if opts.Issue.MilestoneID > 0 {
|
||||
milestone, err := getMilestoneByRepoID(e, opts.Issue.RepoID, opts.Issue.MilestoneID)
|
||||
if err != nil && !IsErrMilestoneNotExist(err) {
|
||||
@@ -1096,10 +1104,20 @@ func newIssue(e *xorm.Session, doer *User, opts NewIssueOptions) (err error) {
|
||||
}
|
||||
|
||||
// Milestone and assignee validation should happen before insert actual object.
|
||||
if _, err = e.Insert(opts.Issue); err != nil {
|
||||
if _, err := e.SetExpr("`index`", "coalesce(MAX(`index`),0)+1").
|
||||
Where("repo_id=?", opts.Issue.RepoID).
|
||||
Insert(opts.Issue); err != nil {
|
||||
return ErrNewIssueInsert{err}
|
||||
}
|
||||
|
||||
inserted, err := getIssueByID(e, opts.Issue.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Patch Index with the value calculated by the database
|
||||
opts.Issue.Index = inserted.Index
|
||||
|
||||
if opts.Issue.MilestoneID > 0 {
|
||||
if err = changeMilestoneAssign(e, doer, opts.Issue, -1); err != nil {
|
||||
return err
|
||||
@@ -1164,12 +1182,32 @@ func newIssue(e *xorm.Session, doer *User, opts NewIssueOptions) (err error) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return opts.Issue.loadAttributes(e)
|
||||
if err = opts.Issue.loadAttributes(e); err != nil {
|
||||
return err
|
||||
}
|
||||
return opts.Issue.addCrossReferences(e, doer)
|
||||
}
|
||||
|
||||
// NewIssue creates new issue with labels for repository.
|
||||
func NewIssue(repo *Repository, issue *Issue, labelIDs []int64, assigneeIDs []int64, uuids []string) (err error) {
|
||||
// Retry several times in case INSERT fails due to duplicate key for (repo_id, index); see #7887
|
||||
i := 0
|
||||
for {
|
||||
if err = newIssueAttempt(repo, issue, labelIDs, assigneeIDs, uuids); err == nil {
|
||||
return nil
|
||||
}
|
||||
if !IsErrNewIssueInsert(err) {
|
||||
return err
|
||||
}
|
||||
if i++; i == issueMaxDupIndexAttempts {
|
||||
break
|
||||
}
|
||||
log.Error("NewIssue: error attempting to insert the new issue; will retry. Original error: %v", err)
|
||||
}
|
||||
return fmt.Errorf("NewIssue: too many errors attempting to insert the new issue. Last error was: %v", err)
|
||||
}
|
||||
|
||||
func newIssueAttempt(repo *Repository, issue *Issue, labelIDs []int64, assigneeIDs []int64, uuids []string) (err error) {
|
||||
sess := x.NewSession()
|
||||
defer sess.Close()
|
||||
if err = sess.Begin(); err != nil {
|
||||
@@ -1183,7 +1221,7 @@ func NewIssue(repo *Repository, issue *Issue, labelIDs []int64, assigneeIDs []in
|
||||
Attachments: uuids,
|
||||
AssigneeIDs: assigneeIDs,
|
||||
}); err != nil {
|
||||
if IsErrUserDoesNotHaveAccessToRepo(err) {
|
||||
if IsErrUserDoesNotHaveAccessToRepo(err) || IsErrNewIssueInsert(err) {
|
||||
return err
|
||||
}
|
||||
return fmt.Errorf("newIssue: %v", err)
|
||||
@@ -1193,31 +1231,6 @@ func NewIssue(repo *Repository, issue *Issue, labelIDs []int64, assigneeIDs []in
|
||||
return fmt.Errorf("Commit: %v", err)
|
||||
}
|
||||
|
||||
if err = NotifyWatchers(&Action{
|
||||
ActUserID: issue.Poster.ID,
|
||||
ActUser: issue.Poster,
|
||||
OpType: ActionCreateIssue,
|
||||
Content: fmt.Sprintf("%d|%s", issue.Index, issue.Title),
|
||||
RepoID: repo.ID,
|
||||
Repo: repo,
|
||||
IsPrivate: repo.IsPrivate,
|
||||
}); err != nil {
|
||||
log.Error("NotifyWatchers: %v", err)
|
||||
}
|
||||
|
||||
mode, _ := AccessLevel(issue.Poster, issue.Repo)
|
||||
if err = PrepareWebhooks(repo, HookEventIssues, &api.IssuePayload{
|
||||
Action: api.HookIssueOpened,
|
||||
Index: issue.Index,
|
||||
Issue: issue.APIFormat(),
|
||||
Repository: repo.APIFormat(mode),
|
||||
Sender: issue.Poster.APIFormat(),
|
||||
}); err != nil {
|
||||
log.Error("PrepareWebhooks: %v", err)
|
||||
} else {
|
||||
go HookQueue.Add(issue.RepoID)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1448,7 +1461,7 @@ func getParticipantsByIssueID(e Engine, issueID int64) ([]*User, error) {
|
||||
userIDs := make([]int64, 0, 5)
|
||||
if err := e.Table("comment").Cols("poster_id").
|
||||
Where("`comment`.issue_id = ?", issueID).
|
||||
And("`comment`.type = ?", CommentTypeComment).
|
||||
And("`comment`.type in (?,?,?)", CommentTypeComment, CommentTypeCode, CommentTypeReview).
|
||||
And("`user`.is_active = ?", true).
|
||||
And("`user`.prohibit_login = ?", false).
|
||||
Join("INNER", "`user`", "`user`.id = `comment`.poster_id").
|
||||
@@ -1466,7 +1479,7 @@ func getParticipantsByIssueID(e Engine, issueID int64) ([]*User, error) {
|
||||
|
||||
// UpdateIssueMentions extracts mentioned people from content and
|
||||
// updates issue-user relations for them.
|
||||
func UpdateIssueMentions(e Engine, issueID int64, mentions []string) error {
|
||||
func UpdateIssueMentions(ctx DBContext, issueID int64, mentions []string) error {
|
||||
if len(mentions) == 0 {
|
||||
return nil
|
||||
}
|
||||
@@ -1476,7 +1489,7 @@ func UpdateIssueMentions(e Engine, issueID int64, mentions []string) error {
|
||||
}
|
||||
users := make([]*User, 0, len(mentions))
|
||||
|
||||
if err := e.In("lower_name", mentions).Asc("lower_name").Find(&users); err != nil {
|
||||
if err := ctx.e.In("lower_name", mentions).Asc("lower_name").Find(&users); err != nil {
|
||||
return fmt.Errorf("find mentioned users: %v", err)
|
||||
}
|
||||
|
||||
@@ -1488,7 +1501,7 @@ func UpdateIssueMentions(e Engine, issueID int64, mentions []string) error {
|
||||
}
|
||||
|
||||
memberIDs := make([]int64, 0, user.NumMembers)
|
||||
orgUsers, err := getOrgUsersByOrgID(e, user.ID)
|
||||
orgUsers, err := getOrgUsersByOrgID(ctx.e, user.ID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("GetOrgUsersByOrgID [%d]: %v", user.ID, err)
|
||||
}
|
||||
@@ -1500,7 +1513,7 @@ func UpdateIssueMentions(e Engine, issueID int64, mentions []string) error {
|
||||
ids = append(ids, memberIDs...)
|
||||
}
|
||||
|
||||
if err := UpdateIssueUsersByMentions(e, issueID, ids); err != nil {
|
||||
if err := UpdateIssueUsersByMentions(ctx, issueID, ids); err != nil {
|
||||
return fmt.Errorf("UpdateIssueUsersByMentions: %v", err)
|
||||
}
|
||||
|
||||
@@ -1648,14 +1661,14 @@ func GetUserIssueStats(opts UserIssueStatsOptions) (*IssueStats, error) {
|
||||
return nil, err
|
||||
}
|
||||
case FilterModeAssign:
|
||||
stats.OpenCount, err = x.Where(cond).And("is_closed = ?", false).
|
||||
stats.OpenCount, err = x.Where(cond).And("issue.is_closed = ?", false).
|
||||
Join("INNER", "issue_assignees", "issue.id = issue_assignees.issue_id").
|
||||
And("issue_assignees.assignee_id = ?", opts.UserID).
|
||||
Count(new(Issue))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
stats.ClosedCount, err = x.Where(cond).And("is_closed = ?", true).
|
||||
stats.ClosedCount, err = x.Where(cond).And("issue.is_closed = ?", true).
|
||||
Join("INNER", "issue_assignees", "issue.id = issue_assignees.issue_id").
|
||||
And("issue_assignees.assignee_id = ?", opts.UserID).
|
||||
Count(new(Issue))
|
||||
@@ -1675,6 +1688,21 @@ func GetUserIssueStats(opts UserIssueStatsOptions) (*IssueStats, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
case FilterModeMention:
|
||||
stats.OpenCount, err = x.Where(cond).And("issue.is_closed = ?", false).
|
||||
Join("INNER", "issue_user", "issue.id = issue_user.issue_id and issue_user.is_mentioned = ?", true).
|
||||
And("issue_user.uid = ?", opts.UserID).
|
||||
Count(new(Issue))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
stats.ClosedCount, err = x.Where(cond).And("issue.is_closed = ?", true).
|
||||
Join("INNER", "issue_user", "issue.id = issue_user.issue_id and issue_user.is_mentioned = ?", true).
|
||||
And("issue_user.uid = ?", opts.UserID).
|
||||
Count(new(Issue))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
cond = cond.And(builder.Eq{"issue.is_closed": opts.IsClosed})
|
||||
@@ -1693,6 +1721,14 @@ func GetUserIssueStats(opts UserIssueStatsOptions) (*IssueStats, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
stats.MentionCount, err = x.Where(cond).
|
||||
Join("INNER", "issue_user", "issue.id = issue_user.issue_id and issue_user.is_mentioned = ?", true).
|
||||
And("issue_user.uid = ?", opts.UserID).
|
||||
Count(new(Issue))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
stats.YourRepositoriesCount, err = x.Where(cond).
|
||||
And(builder.In("issue.repo_id", opts.UserRepoIDs)).
|
||||
Count(new(Issue))
|
||||
@@ -1778,11 +1814,28 @@ func updateIssue(e Engine, issue *Issue) error {
|
||||
|
||||
// UpdateIssue updates all fields of given issue.
|
||||
func UpdateIssue(issue *Issue) error {
|
||||
return updateIssue(x, issue)
|
||||
sess := x.NewSession()
|
||||
defer sess.Close()
|
||||
if err := sess.Begin(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := updateIssue(sess, issue); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := issue.neuterCrossReferences(sess); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := issue.loadPoster(sess); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := issue.addCrossReferences(sess, issue.Poster); err != nil {
|
||||
return err
|
||||
}
|
||||
return sess.Commit()
|
||||
}
|
||||
|
||||
// UpdateIssueDeadline updates an issue deadline and adds comments. Setting a deadline to 0 means deleting it.
|
||||
func UpdateIssueDeadline(issue *Issue, deadlineUnix util.TimeStamp, doer *User) (err error) {
|
||||
func UpdateIssueDeadline(issue *Issue, deadlineUnix timeutil.TimeStamp, doer *User) (err error) {
|
||||
|
||||
// if the deadline hasn't changed do nothing
|
||||
if issue.DeadlineUnix == deadlineUnix {
|
||||
@@ -1837,3 +1890,22 @@ func (issue *Issue) BlockedByDependencies() ([]*Issue, error) {
|
||||
func (issue *Issue) BlockingDependencies() ([]*Issue, error) {
|
||||
return issue.getBlockingDependencies(x)
|
||||
}
|
||||
|
||||
func (issue *Issue) updateClosedNum(e Engine) (err error) {
|
||||
if issue.IsPull {
|
||||
_, err = e.Exec("UPDATE `repository` SET num_closed_pulls=(SELECT count(*) FROM issue WHERE repo_id=? AND is_pull=? AND is_closed=?) WHERE id=?",
|
||||
issue.RepoID,
|
||||
true,
|
||||
true,
|
||||
issue.RepoID,
|
||||
)
|
||||
} else {
|
||||
_, err = e.Exec("UPDATE `repository` SET num_closed_issues=(SELECT count(*) FROM issue WHERE repo_id=? AND is_pull=? AND is_closed=?) WHERE id=?",
|
||||
issue.RepoID,
|
||||
false,
|
||||
true,
|
||||
issue.RepoID,
|
||||
)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -8,8 +8,8 @@ import (
|
||||
"fmt"
|
||||
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
|
||||
"github.com/go-xorm/xorm"
|
||||
)
|
||||
|
||||
@@ -142,11 +142,15 @@ func (issue *Issue) ChangeAssignee(doer *User, assigneeID int64) (err error) {
|
||||
return err
|
||||
}
|
||||
|
||||
return sess.Commit()
|
||||
if err := sess.Commit(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
go HookQueue.Add(issue.RepoID)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (issue *Issue) changeAssignee(sess *xorm.Session, doer *User, assigneeID int64, isCreate bool) (err error) {
|
||||
|
||||
// Update the assignee
|
||||
removed, err := updateIssueAssignee(sess, issue, assigneeID)
|
||||
if err != nil {
|
||||
@@ -209,7 +213,6 @@ func (issue *Issue) changeAssignee(sess *xorm.Session, doer *User, assigneeID in
|
||||
return nil
|
||||
}
|
||||
}
|
||||
go HookQueue.Add(issue.RepoID)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
+64
-247
@@ -7,22 +7,18 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/markup/markdown"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
"github.com/Unknwon/com"
|
||||
"github.com/go-xorm/xorm"
|
||||
"xorm.io/builder"
|
||||
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/markup"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
"code.gitea.io/gitea/modules/markup/markdown"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
|
||||
"github.com/go-xorm/xorm"
|
||||
"github.com/unknwon/com"
|
||||
"xorm.io/builder"
|
||||
)
|
||||
|
||||
// CommentType defines whether a comment is just a simple comment, an action (like close) or a reference.
|
||||
@@ -99,10 +95,10 @@ const (
|
||||
|
||||
// Comment represents a comment in commit and issue page.
|
||||
type Comment struct {
|
||||
ID int64 `xorm:"pk autoincr"`
|
||||
Type CommentType
|
||||
PosterID int64 `xorm:"INDEX"`
|
||||
Poster *User `xorm:"-"`
|
||||
ID int64 `xorm:"pk autoincr"`
|
||||
Type CommentType `xorm:"index"`
|
||||
PosterID int64 `xorm:"INDEX"`
|
||||
Poster *User `xorm:"-"`
|
||||
OriginalAuthor string
|
||||
OriginalAuthorID int64
|
||||
IssueID int64 `xorm:"INDEX"`
|
||||
@@ -130,8 +126,8 @@ type Comment struct {
|
||||
// Path represents the 4 lines of code cemented by this comment
|
||||
Patch string `xorm:"TEXT"`
|
||||
|
||||
CreatedUnix util.TimeStamp `xorm:"INDEX created"`
|
||||
UpdatedUnix util.TimeStamp `xorm:"INDEX updated"`
|
||||
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
|
||||
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
|
||||
|
||||
// Reference issue in commit message
|
||||
CommitSHA string `xorm:"VARCHAR(40)"`
|
||||
@@ -143,21 +139,37 @@ type Comment struct {
|
||||
ShowTag CommentTag `xorm:"-"`
|
||||
|
||||
Review *Review `xorm:"-"`
|
||||
ReviewID int64
|
||||
ReviewID int64 `xorm:"index"`
|
||||
Invalidated bool
|
||||
|
||||
// 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
|
||||
RefIsPull bool
|
||||
|
||||
RefRepo *Repository `xorm:"-"`
|
||||
RefIssue *Issue `xorm:"-"`
|
||||
RefComment *Comment `xorm:"-"`
|
||||
}
|
||||
|
||||
// LoadIssue loads issue from database
|
||||
func (c *Comment) LoadIssue() (err error) {
|
||||
return c.loadIssue(x)
|
||||
}
|
||||
|
||||
func (c *Comment) loadIssue(e Engine) (err error) {
|
||||
if c.Issue != nil {
|
||||
return nil
|
||||
}
|
||||
c.Issue, err = GetIssueByID(c.IssueID)
|
||||
c.Issue, err = getIssueByID(e, c.IssueID)
|
||||
return
|
||||
}
|
||||
|
||||
func (c *Comment) loadPoster(e Engine) (err error) {
|
||||
if c.Poster != nil {
|
||||
if c.PosterID <= 0 || c.Poster != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -326,21 +338,7 @@ func (c *Comment) LoadMilestone() error {
|
||||
|
||||
// LoadPoster loads comment poster
|
||||
func (c *Comment) LoadPoster() error {
|
||||
if c.PosterID <= 0 || c.Poster != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
var err error
|
||||
c.Poster, err = getUserByID(x, c.PosterID)
|
||||
if err != nil {
|
||||
if IsErrUserNotExist(err) {
|
||||
c.PosterID = -1
|
||||
c.Poster = NewGhostUser()
|
||||
} else {
|
||||
log.Error("getUserByID[%d]: %v", c.ID, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
return c.loadPoster(x)
|
||||
}
|
||||
|
||||
// LoadAttachments loads attachments
|
||||
@@ -382,40 +380,6 @@ func (c *Comment) LoadDepIssueDetails() (err error) {
|
||||
return err
|
||||
}
|
||||
|
||||
// MailParticipants sends new comment emails to repository watchers
|
||||
// and mentioned people.
|
||||
func (c *Comment) MailParticipants(opType ActionType, issue *Issue) (err error) {
|
||||
return c.mailParticipants(x, opType, issue)
|
||||
}
|
||||
|
||||
func (c *Comment) mailParticipants(e Engine, opType ActionType, issue *Issue) (err error) {
|
||||
mentions := markup.FindAllMentions(c.Content)
|
||||
if err = UpdateIssueMentions(e, c.IssueID, mentions); err != nil {
|
||||
return fmt.Errorf("UpdateIssueMentions [%d]: %v", c.IssueID, err)
|
||||
}
|
||||
|
||||
if len(c.Content) > 0 {
|
||||
if err = mailIssueCommentToParticipants(e, issue, c.Poster, c.Content, c, mentions); err != nil {
|
||||
log.Error("mailIssueCommentToParticipants: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
switch opType {
|
||||
case ActionCloseIssue:
|
||||
ct := fmt.Sprintf("Closed #%d.", issue.Index)
|
||||
if err = mailIssueCommentToParticipants(e, issue, c.Poster, ct, c, mentions); err != nil {
|
||||
log.Error("mailIssueCommentToParticipants: %v", err)
|
||||
}
|
||||
case ActionReopenIssue:
|
||||
ct := fmt.Sprintf("Reopened #%d.", issue.Index)
|
||||
if err = mailIssueCommentToParticipants(e, issue, c.Poster, ct, c, mentions); err != nil {
|
||||
log.Error("mailIssueCommentToParticipants: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Comment) loadReactions(e Engine) (err error) {
|
||||
if c.Reactions != nil {
|
||||
return nil
|
||||
@@ -462,7 +426,7 @@ func (c *Comment) checkInvalidation(doer *User, repo *git.Repository, branch str
|
||||
}
|
||||
if c.CommitSHA != "" && c.CommitSHA != commit.ID.String() {
|
||||
c.Invalidated = true
|
||||
return UpdateComment(doer, c, "")
|
||||
return UpdateComment(c, doer)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -489,32 +453,6 @@ func (c *Comment) UnsignedLine() uint64 {
|
||||
return uint64(c.Line)
|
||||
}
|
||||
|
||||
// AsDiff returns c.Patch as *Diff
|
||||
func (c *Comment) AsDiff() (*Diff, error) {
|
||||
diff, err := ParsePatch(setting.Git.MaxGitDiffLines,
|
||||
setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, strings.NewReader(c.Patch))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(diff.Files) == 0 {
|
||||
return nil, fmt.Errorf("no file found for comment ID: %d", c.ID)
|
||||
}
|
||||
secs := diff.Files[0].Sections
|
||||
if len(secs) == 0 {
|
||||
return nil, fmt.Errorf("no sections found for comment ID: %d", c.ID)
|
||||
}
|
||||
return diff, nil
|
||||
}
|
||||
|
||||
// MustAsDiff executes AsDiff and logs the error instead of returning
|
||||
func (c *Comment) MustAsDiff() *Diff {
|
||||
diff, err := c.AsDiff()
|
||||
if err != nil {
|
||||
log.Warn("MustAsDiff: %v", err)
|
||||
}
|
||||
return diff
|
||||
}
|
||||
|
||||
// CodeCommentURL returns the url to a comment in code
|
||||
func (c *Comment) CodeCommentURL() string {
|
||||
err := c.LoadIssue()
|
||||
@@ -556,6 +494,11 @@ func createComment(e *xorm.Session, opts *CreateCommentOptions) (_ *Comment, err
|
||||
TreePath: opts.TreePath,
|
||||
ReviewID: opts.ReviewID,
|
||||
Patch: opts.Patch,
|
||||
RefRepoID: opts.RefRepoID,
|
||||
RefIssueID: opts.RefIssueID,
|
||||
RefCommentID: opts.RefCommentID,
|
||||
RefAction: opts.RefAction,
|
||||
RefIsPull: opts.RefIsPull,
|
||||
}
|
||||
if _, err = e.Insert(comment); err != nil {
|
||||
return nil, err
|
||||
@@ -569,6 +512,10 @@ func createComment(e *xorm.Session, opts *CreateCommentOptions) (_ *Comment, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err = comment.addCrossReferences(e, opts.Doer); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return comment, nil
|
||||
}
|
||||
|
||||
@@ -634,12 +581,7 @@ func sendCreateCommentAction(e *xorm.Session, opts *CreateCommentOptions, commen
|
||||
act.OpType = ActionReopenPullRequest
|
||||
}
|
||||
|
||||
if opts.Issue.IsPull {
|
||||
_, err = e.Exec("UPDATE `repository` SET num_closed_pulls=num_closed_pulls-1 WHERE id=?", opts.Repo.ID)
|
||||
} else {
|
||||
_, err = e.Exec("UPDATE `repository` SET num_closed_issues=num_closed_issues-1 WHERE id=?", opts.Repo.ID)
|
||||
}
|
||||
if err != nil {
|
||||
if err = opts.Issue.updateClosedNum(e); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -649,12 +591,7 @@ func sendCreateCommentAction(e *xorm.Session, opts *CreateCommentOptions, commen
|
||||
act.OpType = ActionClosePullRequest
|
||||
}
|
||||
|
||||
if opts.Issue.IsPull {
|
||||
_, err = e.Exec("UPDATE `repository` SET num_closed_pulls=num_closed_pulls+1 WHERE id=?", opts.Repo.ID)
|
||||
} else {
|
||||
_, err = e.Exec("UPDATE `repository` SET num_closed_issues=num_closed_issues+1 WHERE id=?", opts.Repo.ID)
|
||||
}
|
||||
if err != nil {
|
||||
if err = opts.Issue.updateClosedNum(e); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -721,7 +658,7 @@ func createAssigneeComment(e *xorm.Session, doer *User, repo *Repository, issue
|
||||
})
|
||||
}
|
||||
|
||||
func createDeadlineComment(e *xorm.Session, doer *User, issue *Issue, newDeadlineUnix util.TimeStamp) (*Comment, error) {
|
||||
func createDeadlineComment(e *xorm.Session, doer *User, issue *Issue, newDeadlineUnix timeutil.TimeStamp) (*Comment, error) {
|
||||
|
||||
var content string
|
||||
var commentType CommentType
|
||||
@@ -833,6 +770,11 @@ type CreateCommentOptions struct {
|
||||
ReviewID int64
|
||||
Content string
|
||||
Attachments []string // UUIDs of attachments
|
||||
RefRepoID int64
|
||||
RefIssueID int64
|
||||
RefCommentID int64
|
||||
RefAction XRefAction
|
||||
RefIsPull bool
|
||||
}
|
||||
|
||||
// CreateComment creates comment of issue or commit.
|
||||
@@ -855,88 +797,6 @@ func CreateComment(opts *CreateCommentOptions) (comment *Comment, err error) {
|
||||
return comment, nil
|
||||
}
|
||||
|
||||
// CreateIssueComment creates a plain issue comment.
|
||||
func CreateIssueComment(doer *User, repo *Repository, issue *Issue, content string, attachments []string) (*Comment, error) {
|
||||
comment, err := CreateComment(&CreateCommentOptions{
|
||||
Type: CommentTypeComment,
|
||||
Doer: doer,
|
||||
Repo: repo,
|
||||
Issue: issue,
|
||||
Content: content,
|
||||
Attachments: attachments,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("CreateComment: %v", err)
|
||||
}
|
||||
|
||||
mode, _ := AccessLevel(doer, repo)
|
||||
if err = PrepareWebhooks(repo, HookEventIssueComment, &api.IssueCommentPayload{
|
||||
Action: api.HookIssueCommentCreated,
|
||||
Issue: issue.APIFormat(),
|
||||
Comment: comment.APIFormat(),
|
||||
Repository: repo.APIFormat(mode),
|
||||
Sender: doer.APIFormat(),
|
||||
}); err != nil {
|
||||
log.Error("PrepareWebhooks [comment_id: %d]: %v", comment.ID, err)
|
||||
} else {
|
||||
go HookQueue.Add(repo.ID)
|
||||
}
|
||||
return comment, nil
|
||||
}
|
||||
|
||||
// CreateCodeComment creates a plain code comment at the specified line / path
|
||||
func CreateCodeComment(doer *User, repo *Repository, issue *Issue, content, treePath string, line, reviewID int64) (*Comment, error) {
|
||||
var commitID, patch string
|
||||
pr, err := GetPullRequestByIssueID(issue.ID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("GetPullRequestByIssueID: %v", err)
|
||||
}
|
||||
if err := pr.GetBaseRepo(); err != nil {
|
||||
return nil, fmt.Errorf("GetHeadRepo: %v", err)
|
||||
}
|
||||
gitRepo, err := git.OpenRepository(pr.BaseRepo.RepoPath())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("OpenRepository: %v", err)
|
||||
}
|
||||
|
||||
// FIXME validate treePath
|
||||
// Get latest commit referencing the commented line
|
||||
// No need for get commit for base branch changes
|
||||
if line > 0 {
|
||||
commit, err := gitRepo.LineBlame(pr.GetGitRefName(), gitRepo.Path, treePath, uint(line))
|
||||
if err == nil {
|
||||
commitID = commit.ID.String()
|
||||
} else if !strings.Contains(err.Error(), "exit status 128 - fatal: no such path") {
|
||||
return nil, fmt.Errorf("LineBlame[%s, %s, %s, %d]: %v", pr.GetGitRefName(), gitRepo.Path, treePath, line, err)
|
||||
}
|
||||
}
|
||||
|
||||
// Only fetch diff if comment is review comment
|
||||
if reviewID != 0 {
|
||||
headCommitID, err := gitRepo.GetRefCommitID(pr.GetGitRefName())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("GetRefCommitID[%s]: %v", pr.GetGitRefName(), err)
|
||||
}
|
||||
patchBuf := new(bytes.Buffer)
|
||||
if err := GetRawDiffForFile(gitRepo.Path, pr.MergeBase, headCommitID, RawDiffNormal, treePath, patchBuf); err != nil {
|
||||
return nil, fmt.Errorf("GetRawDiffForLine[%s, %s, %s, %s]: %v", err, gitRepo.Path, pr.MergeBase, headCommitID, treePath)
|
||||
}
|
||||
patch = CutDiffAroundLine(strings.NewReader(patchBuf.String()), int64((&Comment{Line: line}).UnsignedLine()), line < 0, setting.UI.CodeCommentLines)
|
||||
}
|
||||
return CreateComment(&CreateCommentOptions{
|
||||
Type: CommentTypeCode,
|
||||
Doer: doer,
|
||||
Repo: repo,
|
||||
Issue: issue,
|
||||
Content: content,
|
||||
LineNum: line,
|
||||
TreePath: treePath,
|
||||
CommitSHA: commitID,
|
||||
ReviewID: reviewID,
|
||||
Patch: patch,
|
||||
})
|
||||
}
|
||||
|
||||
// CreateRefComment creates a commit reference comment to issue.
|
||||
func CreateRefComment(doer *User, repo *Repository, issue *Issue, content, commitSHA string) error {
|
||||
if len(commitSHA) == 0 {
|
||||
@@ -1025,48 +885,34 @@ func FindComments(opts FindCommentsOptions) ([]*Comment, error) {
|
||||
}
|
||||
|
||||
// UpdateComment updates information of comment.
|
||||
func UpdateComment(doer *User, c *Comment, oldContent string) error {
|
||||
if _, err := x.ID(c.ID).AllCols().Update(c); err != nil {
|
||||
func UpdateComment(c *Comment, doer *User) error {
|
||||
sess := x.NewSession()
|
||||
defer sess.Close()
|
||||
if err := sess.Begin(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := c.LoadPoster(); err != nil {
|
||||
if _, err := sess.ID(c.ID).AllCols().Update(c); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := c.LoadIssue(); err != nil {
|
||||
if err := c.loadIssue(sess); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := c.Issue.LoadAttributes(); err != nil {
|
||||
if err := c.neuterCrossReferences(sess); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := c.loadPoster(x); err != nil {
|
||||
if err := c.addCrossReferences(sess, doer); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
mode, _ := AccessLevel(doer, c.Issue.Repo)
|
||||
if err := PrepareWebhooks(c.Issue.Repo, HookEventIssueComment, &api.IssueCommentPayload{
|
||||
Action: api.HookIssueCommentEdited,
|
||||
Issue: c.Issue.APIFormat(),
|
||||
Comment: c.APIFormat(),
|
||||
Changes: &api.ChangesPayload{
|
||||
Body: &api.ChangesFromPayload{
|
||||
From: oldContent,
|
||||
},
|
||||
},
|
||||
Repository: c.Issue.Repo.APIFormat(mode),
|
||||
Sender: doer.APIFormat(),
|
||||
}); err != nil {
|
||||
log.Error("PrepareWebhooks [comment_id: %d]: %v", c.ID, err)
|
||||
} else {
|
||||
go HookQueue.Add(c.Issue.Repo.ID)
|
||||
if err := sess.Commit(); err != nil {
|
||||
return fmt.Errorf("Commit: %v", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteComment deletes the comment
|
||||
func DeleteComment(doer *User, comment *Comment) error {
|
||||
func DeleteComment(comment *Comment, doer *User) error {
|
||||
sess := x.NewSession()
|
||||
defer sess.Close()
|
||||
if err := sess.Begin(); err != nil {
|
||||
@@ -1088,40 +934,11 @@ func DeleteComment(doer *User, comment *Comment) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := sess.Commit(); err != nil {
|
||||
return err
|
||||
}
|
||||
sess.Close()
|
||||
|
||||
if err := comment.LoadPoster(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := comment.LoadIssue(); err != nil {
|
||||
if err := comment.neuterCrossReferences(sess); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := comment.Issue.LoadAttributes(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := comment.loadPoster(x); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
mode, _ := AccessLevel(doer, comment.Issue.Repo)
|
||||
|
||||
if err := PrepareWebhooks(comment.Issue.Repo, HookEventIssueComment, &api.IssueCommentPayload{
|
||||
Action: api.HookIssueCommentDeleted,
|
||||
Issue: comment.Issue.APIFormat(),
|
||||
Comment: comment.APIFormat(),
|
||||
Repository: comment.Issue.Repo.APIFormat(mode),
|
||||
Sender: doer.APIFormat(),
|
||||
}); err != nil {
|
||||
log.Error("PrepareWebhooks [comment_id: %d]: %v", comment.ID, err)
|
||||
} else {
|
||||
go HookQueue.Add(comment.Issue.Repo.ID)
|
||||
}
|
||||
|
||||
return nil
|
||||
return sess.Commit()
|
||||
}
|
||||
|
||||
// CodeComments represents comments on code by using this structure: FILENAME -> LINE (+ == proposed; - == previous) -> COMMENTS
|
||||
|
||||
@@ -7,17 +7,17 @@ package models
|
||||
import (
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
)
|
||||
|
||||
// IssueDependency represents an issue dependency
|
||||
type IssueDependency struct {
|
||||
ID int64 `xorm:"pk autoincr"`
|
||||
UserID int64 `xorm:"NOT NULL"`
|
||||
IssueID int64 `xorm:"UNIQUE(issue_dependency) NOT NULL"`
|
||||
DependencyID int64 `xorm:"UNIQUE(issue_dependency) NOT NULL"`
|
||||
CreatedUnix util.TimeStamp `xorm:"created"`
|
||||
UpdatedUnix util.TimeStamp `xorm:"updated"`
|
||||
ID int64 `xorm:"pk autoincr"`
|
||||
UserID int64 `xorm:"NOT NULL"`
|
||||
IssueID int64 `xorm:"UNIQUE(issue_dependency) NOT NULL"`
|
||||
DependencyID int64 `xorm:"UNIQUE(issue_dependency) NOT NULL"`
|
||||
CreatedUnix timeutil.TimeStamp `xorm:"created"`
|
||||
UpdatedUnix timeutil.TimeStamp `xorm:"updated"`
|
||||
}
|
||||
|
||||
// DependencyType Defines Dependency Type Constants
|
||||
|
||||
+45
-11
@@ -11,9 +11,10 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/go-xorm/xorm"
|
||||
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
|
||||
"github.com/go-xorm/xorm"
|
||||
"xorm.io/builder"
|
||||
)
|
||||
|
||||
var labelColorPattern = regexp.MustCompile("#([a-fA-F0-9]{6})")
|
||||
@@ -127,6 +128,34 @@ func (label *Label) ForegroundColor() template.CSS {
|
||||
return template.CSS("#000")
|
||||
}
|
||||
|
||||
func initalizeLabels(e Engine, repoID int64, labelTemplate string) error {
|
||||
list, err := GetLabelTemplateFile(labelTemplate)
|
||||
if err != nil {
|
||||
return ErrIssueLabelTemplateLoad{labelTemplate, err}
|
||||
}
|
||||
|
||||
labels := make([]*Label, len(list))
|
||||
for i := 0; i < len(list); i++ {
|
||||
labels[i] = &Label{
|
||||
RepoID: repoID,
|
||||
Name: list[i][0],
|
||||
Description: list[i][2],
|
||||
Color: list[i][1],
|
||||
}
|
||||
}
|
||||
for _, label := range labels {
|
||||
if err = newLabel(e, label); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// InitalizeLabels adds a label set to a repository using a template
|
||||
func InitalizeLabels(repoID int64, labelTemplate string) error {
|
||||
return initalizeLabels(x, repoID, labelTemplate)
|
||||
}
|
||||
|
||||
func newLabel(e Engine, label *Label) error {
|
||||
_, err := e.Insert(label)
|
||||
return err
|
||||
@@ -266,7 +295,20 @@ func GetLabelsByIssueID(issueID int64) ([]*Label, error) {
|
||||
}
|
||||
|
||||
func updateLabel(e Engine, l *Label) error {
|
||||
_, err := e.ID(l.ID).AllCols().Update(l)
|
||||
_, err := e.ID(l.ID).
|
||||
SetExpr("num_issues",
|
||||
builder.Select("count(*)").From("issue_label").
|
||||
Where(builder.Eq{"label_id": l.ID}),
|
||||
).
|
||||
SetExpr("num_closed_issues",
|
||||
builder.Select("count(*)").From("issue_label").
|
||||
InnerJoin("issue", "issue_label.issue_id = issue.id").
|
||||
Where(builder.Eq{
|
||||
"issue_label.label_id": l.ID,
|
||||
"issue.is_closed": true,
|
||||
}),
|
||||
).
|
||||
AllCols().Update(l)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -347,10 +389,6 @@ func newIssueLabel(e *xorm.Session, issue *Issue, label *Label, doer *User) (err
|
||||
return err
|
||||
}
|
||||
|
||||
label.NumIssues++
|
||||
if issue.IsClosed {
|
||||
label.NumClosedIssues++
|
||||
}
|
||||
return updateLabel(e, label)
|
||||
}
|
||||
|
||||
@@ -420,10 +458,6 @@ func deleteIssueLabel(e *xorm.Session, issue *Issue, label *Label, doer *User) (
|
||||
return err
|
||||
}
|
||||
|
||||
label.NumIssues--
|
||||
if issue.IsClosed {
|
||||
label.NumClosedIssues--
|
||||
}
|
||||
return updateLabel(e, label)
|
||||
}
|
||||
|
||||
|
||||
@@ -205,6 +205,7 @@ func TestNewIssueLabel(t *testing.T) {
|
||||
LabelID: label.ID,
|
||||
Content: "1",
|
||||
})
|
||||
label = AssertExistsAndLoadBean(t, &Label{ID: 2}).(*Label)
|
||||
assert.EqualValues(t, prevNumIssues+1, label.NumIssues)
|
||||
|
||||
// re-add existing IssueLabel
|
||||
|
||||
@@ -1,159 +0,0 @@
|
||||
// Copyright 2016 The Gogs Authors. All rights reserved.
|
||||
// Copyright 2018 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 models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/Unknwon/com"
|
||||
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/markup"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
)
|
||||
|
||||
func (issue *Issue) mailSubject() string {
|
||||
return fmt.Sprintf("[%s] %s (#%d)", issue.Repo.FullName(), issue.Title, issue.Index)
|
||||
}
|
||||
|
||||
// mailIssueCommentToParticipants can be used for both new issue creation and comment.
|
||||
// This function sends two list of emails:
|
||||
// 1. Repository watchers and users who are participated in comments.
|
||||
// 2. Users who are not in 1. but get mentioned in current issue/comment.
|
||||
func mailIssueCommentToParticipants(e Engine, issue *Issue, doer *User, content string, comment *Comment, mentions []string) error {
|
||||
if !setting.Service.EnableNotifyMail {
|
||||
return nil
|
||||
}
|
||||
|
||||
watchers, err := getWatchers(e, issue.RepoID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("getWatchers [repo_id: %d]: %v", issue.RepoID, err)
|
||||
}
|
||||
participants, err := getParticipantsByIssueID(e, issue.ID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("getParticipantsByIssueID [issue_id: %d]: %v", issue.ID, err)
|
||||
}
|
||||
|
||||
// In case the issue poster is not watching the repository and is active,
|
||||
// even if we have duplicated in watchers, can be safely filtered out.
|
||||
err = issue.loadPoster(e)
|
||||
if err != nil {
|
||||
return fmt.Errorf("GetUserByID [%d]: %v", issue.PosterID, err)
|
||||
}
|
||||
if issue.PosterID != doer.ID && issue.Poster.IsActive && !issue.Poster.ProhibitLogin {
|
||||
participants = append(participants, issue.Poster)
|
||||
}
|
||||
|
||||
// Assignees must receive any communications
|
||||
assignees, err := getAssigneesByIssue(e, issue)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, assignee := range assignees {
|
||||
if assignee.ID != doer.ID {
|
||||
participants = append(participants, assignee)
|
||||
}
|
||||
}
|
||||
|
||||
tos := make([]string, 0, len(watchers)) // List of email addresses.
|
||||
names := make([]string, 0, len(watchers))
|
||||
for i := range watchers {
|
||||
if watchers[i].UserID == doer.ID {
|
||||
continue
|
||||
}
|
||||
|
||||
to, err := getUserByID(e, watchers[i].UserID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("GetUserByID [%d]: %v", watchers[i].UserID, err)
|
||||
}
|
||||
if to.IsOrganization() {
|
||||
continue
|
||||
}
|
||||
|
||||
tos = append(tos, to.Email)
|
||||
names = append(names, to.Name)
|
||||
}
|
||||
for i := range participants {
|
||||
if participants[i].ID == doer.ID {
|
||||
continue
|
||||
} else if com.IsSliceContainsStr(names, participants[i].Name) {
|
||||
continue
|
||||
}
|
||||
|
||||
tos = append(tos, participants[i].Email)
|
||||
names = append(names, participants[i].Name)
|
||||
}
|
||||
|
||||
if err := issue.loadRepo(e); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, to := range tos {
|
||||
SendIssueCommentMail(issue, doer, content, comment, []string{to})
|
||||
}
|
||||
|
||||
// Mail mentioned people and exclude watchers.
|
||||
names = append(names, doer.Name)
|
||||
tos = make([]string, 0, len(mentions)) // list of user names.
|
||||
for i := range mentions {
|
||||
if com.IsSliceContainsStr(names, mentions[i]) {
|
||||
continue
|
||||
}
|
||||
|
||||
tos = append(tos, mentions[i])
|
||||
}
|
||||
|
||||
emails := getUserEmailsByNames(e, tos)
|
||||
|
||||
for _, to := range emails {
|
||||
SendIssueMentionMail(issue, doer, content, comment, []string{to})
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// MailParticipants sends new issue thread created emails to repository watchers
|
||||
// and mentioned people.
|
||||
func (issue *Issue) MailParticipants(doer *User, opType ActionType) (err error) {
|
||||
return issue.mailParticipants(x, doer, opType)
|
||||
}
|
||||
|
||||
func (issue *Issue) mailParticipants(e Engine, doer *User, opType ActionType) (err error) {
|
||||
mentions := markup.FindAllMentions(issue.Content)
|
||||
|
||||
if err = UpdateIssueMentions(e, issue.ID, mentions); err != nil {
|
||||
return fmt.Errorf("UpdateIssueMentions [%d]: %v", issue.ID, err)
|
||||
}
|
||||
|
||||
if len(issue.Content) > 0 {
|
||||
if err = mailIssueCommentToParticipants(e, issue, doer, issue.Content, nil, mentions); err != nil {
|
||||
log.Error("mailIssueCommentToParticipants: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
switch opType {
|
||||
case ActionCreateIssue, ActionCreatePullRequest:
|
||||
if len(issue.Content) == 0 {
|
||||
ct := fmt.Sprintf("Created #%d.", issue.Index)
|
||||
if err = mailIssueCommentToParticipants(e, issue, doer, ct, nil, mentions); err != nil {
|
||||
log.Error("mailIssueCommentToParticipants: %v", err)
|
||||
}
|
||||
}
|
||||
case ActionCloseIssue, ActionClosePullRequest:
|
||||
ct := fmt.Sprintf("Closed #%d.", issue.Index)
|
||||
if err = mailIssueCommentToParticipants(e, issue, doer, ct, nil, mentions); err != nil {
|
||||
log.Error("mailIssueCommentToParticipants: %v", err)
|
||||
}
|
||||
case ActionReopenIssue, ActionReopenPullRequest:
|
||||
ct := fmt.Sprintf("Reopened #%d.", issue.Index)
|
||||
if err = mailIssueCommentToParticipants(e, issue, doer, ct, nil, mentions); err != nil {
|
||||
log.Error("mailIssueCommentToParticipants: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -7,10 +7,10 @@ package models
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
|
||||
"github.com/go-xorm/xorm"
|
||||
)
|
||||
|
||||
@@ -29,8 +29,8 @@ type Milestone struct {
|
||||
IsOverdue bool `xorm:"-"`
|
||||
|
||||
DeadlineString string `xorm:"-"`
|
||||
DeadlineUnix util.TimeStamp
|
||||
ClosedDateUnix util.TimeStamp
|
||||
DeadlineUnix timeutil.TimeStamp
|
||||
ClosedDateUnix timeutil.TimeStamp
|
||||
|
||||
TotalTrackedTime int64 `xorm:"-"`
|
||||
}
|
||||
@@ -53,7 +53,7 @@ func (m *Milestone) AfterLoad() {
|
||||
}
|
||||
|
||||
m.DeadlineString = m.DeadlineUnix.Format("2006-01-02")
|
||||
if util.TimeStampNow() >= m.DeadlineUnix {
|
||||
if timeutil.TimeStampNow() >= m.DeadlineUnix {
|
||||
m.IsOverdue = true
|
||||
}
|
||||
}
|
||||
@@ -393,46 +393,6 @@ func ChangeMilestoneAssign(issue *Issue, doer *User, oldMilestoneID int64) (err
|
||||
if err = sess.Commit(); err != nil {
|
||||
return fmt.Errorf("Commit: %v", err)
|
||||
}
|
||||
|
||||
var hookAction api.HookIssueAction
|
||||
if issue.MilestoneID > 0 {
|
||||
hookAction = api.HookIssueMilestoned
|
||||
} else {
|
||||
hookAction = api.HookIssueDemilestoned
|
||||
}
|
||||
|
||||
if err = issue.LoadAttributes(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
mode, _ := AccessLevel(doer, issue.Repo)
|
||||
if issue.IsPull {
|
||||
err = issue.PullRequest.LoadIssue()
|
||||
if err != nil {
|
||||
log.Error("LoadIssue: %v", err)
|
||||
return
|
||||
}
|
||||
err = PrepareWebhooks(issue.Repo, HookEventPullRequest, &api.PullRequestPayload{
|
||||
Action: hookAction,
|
||||
Index: issue.Index,
|
||||
PullRequest: issue.PullRequest.APIFormat(),
|
||||
Repository: issue.Repo.APIFormat(mode),
|
||||
Sender: doer.APIFormat(),
|
||||
})
|
||||
} else {
|
||||
err = PrepareWebhooks(issue.Repo, HookEventIssues, &api.IssuePayload{
|
||||
Action: hookAction,
|
||||
Index: issue.Index,
|
||||
Issue: issue.APIFormat(),
|
||||
Repository: issue.Repo.APIFormat(mode),
|
||||
Sender: doer.APIFormat(),
|
||||
})
|
||||
}
|
||||
if err != nil {
|
||||
log.Error("PrepareWebhooks [is_pull: %v]: %v", issue.IsPull, err)
|
||||
} else {
|
||||
go HookQueue.Add(issue.RepoID)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
"time"
|
||||
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
@@ -29,7 +29,7 @@ func TestMilestone_APIFormat(t *testing.T) {
|
||||
IsClosed: false,
|
||||
NumOpenIssues: 5,
|
||||
NumClosedIssues: 6,
|
||||
DeadlineUnix: util.TimeStamp(time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC).Unix()),
|
||||
DeadlineUnix: timeutil.TimeStamp(time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC).Unix()),
|
||||
}
|
||||
assert.Equal(t, api.Milestone{
|
||||
ID: milestone.ID,
|
||||
@@ -237,7 +237,7 @@ func TestChangeMilestoneIssueStats(t *testing.T) {
|
||||
"is_closed=0").(*Issue)
|
||||
|
||||
issue.IsClosed = true
|
||||
issue.ClosedUnix = util.TimeStampNow()
|
||||
issue.ClosedUnix = timeutil.TimeStampNow()
|
||||
_, err := x.Cols("is_closed", "closed_unix").Update(issue)
|
||||
assert.NoError(t, err)
|
||||
assert.NoError(t, changeMilestoneIssueStats(x.NewSession(), issue))
|
||||
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
"fmt"
|
||||
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
|
||||
"github.com/go-xorm/xorm"
|
||||
"xorm.io/builder"
|
||||
@@ -17,13 +17,13 @@ import (
|
||||
|
||||
// Reaction represents a reactions on issues and comments.
|
||||
type Reaction struct {
|
||||
ID int64 `xorm:"pk autoincr"`
|
||||
Type string `xorm:"INDEX UNIQUE(s) NOT NULL"`
|
||||
IssueID int64 `xorm:"INDEX UNIQUE(s) NOT NULL"`
|
||||
CommentID int64 `xorm:"INDEX UNIQUE(s)"`
|
||||
UserID int64 `xorm:"INDEX UNIQUE(s) NOT NULL"`
|
||||
User *User `xorm:"-"`
|
||||
CreatedUnix util.TimeStamp `xorm:"INDEX created"`
|
||||
ID int64 `xorm:"pk autoincr"`
|
||||
Type string `xorm:"INDEX UNIQUE(s) NOT NULL"`
|
||||
IssueID int64 `xorm:"INDEX UNIQUE(s) NOT NULL"`
|
||||
CommentID int64 `xorm:"INDEX UNIQUE(s)"`
|
||||
UserID int64 `xorm:"INDEX UNIQUE(s) NOT NULL"`
|
||||
User *User `xorm:"-"`
|
||||
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
|
||||
}
|
||||
|
||||
// FindReactionsOptions describes the conditions to Find reactions
|
||||
|
||||
@@ -8,15 +8,15 @@ import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
)
|
||||
|
||||
// Stopwatch represents a stopwatch for time tracking.
|
||||
type Stopwatch struct {
|
||||
ID int64 `xorm:"pk autoincr"`
|
||||
IssueID int64 `xorm:"INDEX"`
|
||||
UserID int64 `xorm:"INDEX"`
|
||||
CreatedUnix util.TimeStamp `xorm:"created"`
|
||||
ID int64 `xorm:"pk autoincr"`
|
||||
IssueID int64 `xorm:"INDEX"`
|
||||
UserID int64 `xorm:"INDEX"`
|
||||
CreatedUnix timeutil.TimeStamp `xorm:"created"`
|
||||
}
|
||||
|
||||
func getStopwatch(e Engine, userID, issueID int64) (sw *Stopwatch, exists bool, err error) {
|
||||
|
||||
@@ -3,7 +3,7 @@ package models
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
@@ -63,7 +63,7 @@ func TestCreateOrStopIssueStopwatch(t *testing.T) {
|
||||
|
||||
assert.NoError(t, CreateOrStopIssueStopwatch(user3, issue1))
|
||||
sw := AssertExistsAndLoadBean(t, &Stopwatch{UserID: 3, IssueID: 1}).(*Stopwatch)
|
||||
assert.Equal(t, true, sw.CreatedUnix <= util.TimeStampNow())
|
||||
assert.Equal(t, true, sw.CreatedUnix <= timeutil.TimeStampNow())
|
||||
|
||||
assert.NoError(t, CreateOrStopIssueStopwatch(user2, issue2))
|
||||
AssertNotExistsBean(t, &Stopwatch{UserID: 2, IssueID: 2})
|
||||
|
||||
@@ -279,6 +279,19 @@ func TestGetUserIssueStats(t *testing.T) {
|
||||
ClosedCount: 2,
|
||||
},
|
||||
},
|
||||
{
|
||||
UserIssueStatsOptions{
|
||||
UserID: 1,
|
||||
FilterMode: FilterModeMention,
|
||||
},
|
||||
IssueStats{
|
||||
YourRepositoriesCount: 0,
|
||||
AssignCount: 2,
|
||||
CreateCount: 2,
|
||||
OpenCount: 0,
|
||||
ClosedCount: 0,
|
||||
},
|
||||
},
|
||||
} {
|
||||
stats, err := GetUserIssueStats(test.Opts)
|
||||
if !assert.NoError(t, err) {
|
||||
@@ -320,3 +333,36 @@ func TestIssue_SearchIssueIDsByKeyword(t *testing.T) {
|
||||
assert.EqualValues(t, 1, total)
|
||||
assert.EqualValues(t, []int64{1}, ids)
|
||||
}
|
||||
|
||||
func testInsertIssue(t *testing.T, title, content string) {
|
||||
repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
|
||||
user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
|
||||
|
||||
var issue = Issue{
|
||||
RepoID: repo.ID,
|
||||
PosterID: user.ID,
|
||||
Title: title,
|
||||
Content: content,
|
||||
}
|
||||
err := NewIssue(repo, &issue, nil, nil, nil)
|
||||
assert.NoError(t, err)
|
||||
|
||||
var newIssue Issue
|
||||
has, err := x.ID(issue.ID).Get(&newIssue)
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, has)
|
||||
assert.EqualValues(t, issue.Title, newIssue.Title)
|
||||
assert.EqualValues(t, issue.Content, newIssue.Content)
|
||||
// there are 4 issues and max index is 4 on repository 1, so this one should 5
|
||||
assert.EqualValues(t, 5, newIssue.Index)
|
||||
|
||||
_, err = x.ID(issue.ID).Delete(new(Issue))
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestIssue_InsertIssue(t *testing.T) {
|
||||
assert.NoError(t, PrepareTestDatabase())
|
||||
|
||||
testInsertIssue(t, "my issue1", "special issue's comments?")
|
||||
testInsertIssue(t, `my issue2, this is my son's love \n \r \ `, "special issue's '' comments?")
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ type TrackedTime struct {
|
||||
|
||||
// AfterLoad is invoked from XORM after setting the values of all fields of this object.
|
||||
func (t *TrackedTime) AfterLoad() {
|
||||
t.Created = time.Unix(t.CreatedUnix, 0).In(setting.UILocation)
|
||||
t.Created = time.Unix(t.CreatedUnix, 0).In(setting.DefaultUILocation)
|
||||
}
|
||||
|
||||
// APIFormat converts TrackedTime to API format
|
||||
|
||||
@@ -94,22 +94,22 @@ func UpdateIssueUserByRead(uid, issueID int64) error {
|
||||
}
|
||||
|
||||
// UpdateIssueUsersByMentions updates issue-user pairs by mentioning.
|
||||
func UpdateIssueUsersByMentions(e Engine, issueID int64, uids []int64) error {
|
||||
func UpdateIssueUsersByMentions(ctx DBContext, issueID int64, uids []int64) error {
|
||||
for _, uid := range uids {
|
||||
iu := &IssueUser{
|
||||
UID: uid,
|
||||
IssueID: issueID,
|
||||
}
|
||||
has, err := e.Get(iu)
|
||||
has, err := ctx.e.Get(iu)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
iu.IsMentioned = true
|
||||
if has {
|
||||
_, err = e.ID(iu.ID).Cols("is_mentioned").Update(iu)
|
||||
_, err = ctx.e.ID(iu.ID).Cols("is_mentioned").Update(iu)
|
||||
} else {
|
||||
_, err = e.Insert(iu)
|
||||
_, err = ctx.e.Insert(iu)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -50,7 +50,7 @@ func TestUpdateIssueUsersByMentions(t *testing.T) {
|
||||
issue := AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue)
|
||||
|
||||
uids := []int64{2, 5}
|
||||
assert.NoError(t, UpdateIssueUsersByMentions(x, issue.ID, uids))
|
||||
assert.NoError(t, UpdateIssueUsersByMentions(DefaultDBContext(), issue.ID, uids))
|
||||
for _, uid := range uids {
|
||||
AssertExistsAndLoadBean(t, &IssueUser{IssueID: issue.ID, UID: uid}, "is_mentioned=1")
|
||||
}
|
||||
|
||||
@@ -4,16 +4,16 @@
|
||||
|
||||
package models
|
||||
|
||||
import "code.gitea.io/gitea/modules/util"
|
||||
import "code.gitea.io/gitea/modules/timeutil"
|
||||
|
||||
// IssueWatch is connection request for receiving issue notification.
|
||||
type IssueWatch struct {
|
||||
ID int64 `xorm:"pk autoincr"`
|
||||
UserID int64 `xorm:"UNIQUE(watch) NOT NULL"`
|
||||
IssueID int64 `xorm:"UNIQUE(watch) NOT NULL"`
|
||||
IsWatching bool `xorm:"NOT NULL"`
|
||||
CreatedUnix util.TimeStamp `xorm:"created NOT NULL"`
|
||||
UpdatedUnix util.TimeStamp `xorm:"updated NOT NULL"`
|
||||
ID int64 `xorm:"pk autoincr"`
|
||||
UserID int64 `xorm:"UNIQUE(watch) NOT NULL"`
|
||||
IssueID int64 `xorm:"UNIQUE(watch) NOT NULL"`
|
||||
IsWatching bool `xorm:"NOT NULL"`
|
||||
CreatedUnix timeutil.TimeStamp `xorm:"created NOT NULL"`
|
||||
UpdatedUnix timeutil.TimeStamp `xorm:"updated NOT NULL"`
|
||||
}
|
||||
|
||||
// CreateOrUpdateIssueWatch set watching for a user and issue
|
||||
|
||||
@@ -0,0 +1,308 @@
|
||||
// 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 models
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strconv"
|
||||
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
|
||||
"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
|
||||
}
|
||||
|
||||
// crossReferencesContext is context to pass along findCrossReference functions
|
||||
type crossReferencesContext struct {
|
||||
Type CommentType
|
||||
Doer *User
|
||||
OrigIssue *Issue
|
||||
OrigComment *Comment
|
||||
}
|
||||
|
||||
func newCrossReference(e *xorm.Session, ctx *crossReferencesContext, xref *crossReference) error {
|
||||
var refCommentID int64
|
||||
if ctx.OrigComment != nil {
|
||||
refCommentID = ctx.OrigComment.ID
|
||||
}
|
||||
_, err := createComment(e, &CreateCommentOptions{
|
||||
Type: ctx.Type,
|
||||
Doer: ctx.Doer,
|
||||
Repo: xref.Issue.Repo,
|
||||
Issue: xref.Issue,
|
||||
RefRepoID: ctx.OrigIssue.RepoID,
|
||||
RefIssueID: ctx.OrigIssue.ID,
|
||||
RefCommentID: refCommentID,
|
||||
RefAction: xref.Action,
|
||||
RefIsPull: xref.Issue.IsPull,
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func neuterCrossReferences(e Engine, issueID int64, commentID int64) error {
|
||||
active := make([]*Comment, 0, 10)
|
||||
sess := e.Where("`ref_action` IN (?, ?, ?)", XRefActionNone, XRefActionCloses, XRefActionReopens)
|
||||
if issueID != 0 {
|
||||
sess = sess.And("`ref_issue_id` = ?", issueID)
|
||||
}
|
||||
if commentID != 0 {
|
||||
sess = sess.And("`ref_comment_id` = ?", commentID)
|
||||
}
|
||||
if err := sess.Find(&active); err != nil || len(active) == 0 {
|
||||
return err
|
||||
}
|
||||
ids := make([]int64, len(active))
|
||||
for i, c := range active {
|
||||
ids[i] = c.ID
|
||||
}
|
||||
_, err := e.In("id", ids).Cols("`ref_action`").Update(&Comment{RefAction: XRefActionNeutered})
|
||||
return err
|
||||
}
|
||||
|
||||
// .___
|
||||
// | | ______ ________ __ ____
|
||||
// | |/ ___// ___/ | \_/ __ \
|
||||
// | |\___ \ \___ \| | /\ ___/
|
||||
// |___/____ >____ >____/ \___ >
|
||||
// \/ \/ \/
|
||||
//
|
||||
|
||||
func (issue *Issue) addCrossReferences(e *xorm.Session, doer *User) error {
|
||||
var commentType CommentType
|
||||
if issue.IsPull {
|
||||
commentType = CommentTypePullRef
|
||||
} else {
|
||||
commentType = CommentTypeIssueRef
|
||||
}
|
||||
ctx := &crossReferencesContext{
|
||||
Type: commentType,
|
||||
Doer: doer,
|
||||
OrigIssue: issue,
|
||||
}
|
||||
return issue.createCrossReferences(e, ctx, issue.Title+"\n"+issue.Content)
|
||||
}
|
||||
|
||||
func (issue *Issue) createCrossReferences(e *xorm.Session, ctx *crossReferencesContext, content string) error {
|
||||
xreflist, err := ctx.OrigIssue.getCrossReferences(e, ctx, content)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, xref := range xreflist {
|
||||
if err = newCrossReference(e, ctx, xref); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (issue *Issue) getCrossReferences(e *xorm.Session, ctx *crossReferencesContext, content 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 {
|
||||
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])
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return xreflist, nil
|
||||
}
|
||||
|
||||
func (issue *Issue) updateCrossReferenceList(list []*crossReference, xref *crossReference) []*crossReference {
|
||||
if xref.Issue.ID == issue.ID {
|
||||
return list
|
||||
}
|
||||
for i, r := range list {
|
||||
if r.Issue.ID == xref.Issue.ID {
|
||||
if xref.Action != XRefActionNone {
|
||||
list[i].Action = xref.Action
|
||||
}
|
||||
return list
|
||||
}
|
||||
}
|
||||
return append(list, xref)
|
||||
}
|
||||
|
||||
func (issue *Issue) isValidCommentReference(e Engine, ctx *crossReferencesContext, repo *Repository, index int64) (*crossReference, error) {
|
||||
refIssue := &Issue{RepoID: repo.ID, Index: index}
|
||||
if has, _ := e.Get(refIssue); !has {
|
||||
return nil, nil
|
||||
}
|
||||
if err := refIssue.loadRepo(e); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Check user permissions
|
||||
if refIssue.RepoID != ctx.OrigIssue.RepoID {
|
||||
perm, err := getUserRepoPermission(e, refIssue.Repo, ctx.Doer)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !perm.CanReadIssuesOrPulls(refIssue.IsPull) {
|
||||
return nil, nil
|
||||
}
|
||||
}
|
||||
return &crossReference{
|
||||
Issue: refIssue,
|
||||
Action: XRefActionNone,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (issue *Issue) neuterCrossReferences(e Engine) error {
|
||||
return neuterCrossReferences(e, issue.ID, 0)
|
||||
}
|
||||
|
||||
// _________ __
|
||||
// \_ ___ \ ____ _____ _____ ____ _____/ |_
|
||||
// / \ \/ / _ \ / \ / \_/ __ \ / \ __\
|
||||
// \ \___( <_> ) Y Y \ Y Y \ ___/| | \ |
|
||||
// \______ /\____/|__|_| /__|_| /\___ >___| /__|
|
||||
// \/ \/ \/ \/ \/
|
||||
//
|
||||
|
||||
func (comment *Comment) addCrossReferences(e *xorm.Session, doer *User) error {
|
||||
if comment.Type != CommentTypeCode && comment.Type != CommentTypeComment {
|
||||
return nil
|
||||
}
|
||||
if err := comment.loadIssue(e); err != nil {
|
||||
return err
|
||||
}
|
||||
ctx := &crossReferencesContext{
|
||||
Type: CommentTypeCommentRef,
|
||||
Doer: doer,
|
||||
OrigIssue: comment.Issue,
|
||||
OrigComment: comment,
|
||||
}
|
||||
return comment.Issue.createCrossReferences(e, ctx, comment.Content)
|
||||
}
|
||||
|
||||
func (comment *Comment) neuterCrossReferences(e Engine) error {
|
||||
return neuterCrossReferences(e, 0, comment.ID)
|
||||
}
|
||||
|
||||
// LoadRefComment loads comment that created this reference from database
|
||||
func (comment *Comment) LoadRefComment() (err error) {
|
||||
if comment.RefComment != nil {
|
||||
return nil
|
||||
}
|
||||
comment.RefComment, err = GetCommentByID(comment.RefCommentID)
|
||||
return
|
||||
}
|
||||
|
||||
// LoadRefIssue loads comment that created this reference from database
|
||||
func (comment *Comment) LoadRefIssue() (err error) {
|
||||
if comment.RefIssue != nil {
|
||||
return nil
|
||||
}
|
||||
comment.RefIssue, err = GetIssueByID(comment.RefIssueID)
|
||||
if err == nil {
|
||||
err = comment.RefIssue.loadRepo(x)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// CommentTypeIsRef returns true if CommentType is a reference from another issue
|
||||
func CommentTypeIsRef(t CommentType) bool {
|
||||
return t == CommentTypeCommentRef || t == CommentTypePullRef || t == CommentTypeIssueRef
|
||||
}
|
||||
|
||||
// RefCommentHTMLURL returns the HTML URL for the comment that created this reference
|
||||
func (comment *Comment) RefCommentHTMLURL() string {
|
||||
if err := comment.LoadRefComment(); err != nil { // Silently dropping errors :unamused:
|
||||
log.Error("LoadRefComment(%d): %v", comment.RefCommentID, err)
|
||||
return ""
|
||||
}
|
||||
return comment.RefComment.HTMLURL()
|
||||
}
|
||||
|
||||
// RefIssueHTMLURL returns the HTML URL of the issue where this reference was created
|
||||
func (comment *Comment) RefIssueHTMLURL() string {
|
||||
if err := comment.LoadRefIssue(); err != nil { // Silently dropping errors :unamused:
|
||||
log.Error("LoadRefIssue(%d): %v", comment.RefCommentID, err)
|
||||
return ""
|
||||
}
|
||||
return comment.RefIssue.HTMLURL()
|
||||
}
|
||||
|
||||
// RefIssueTitle returns the title of the issue where this reference was created
|
||||
func (comment *Comment) RefIssueTitle() string {
|
||||
if err := comment.LoadRefIssue(); err != nil { // Silently dropping errors :unamused:
|
||||
log.Error("LoadRefIssue(%d): %v", comment.RefCommentID, err)
|
||||
return ""
|
||||
}
|
||||
return comment.RefIssue.Title
|
||||
}
|
||||
|
||||
// RefIssueIdent returns the user friendly identity (e.g. "#1234") of the issue where this reference was created
|
||||
func (comment *Comment) RefIssueIdent() string {
|
||||
if err := comment.LoadRefIssue(); err != nil { // Silently dropping errors :unamused:
|
||||
log.Error("LoadRefIssue(%d): %v", comment.RefCommentID, err)
|
||||
return ""
|
||||
}
|
||||
// FIXME: check this name for cross-repository references (#7901 if it gets merged)
|
||||
return "#" + com.ToStr(comment.RefIssue.Index)
|
||||
}
|
||||
+7
-7
@@ -7,17 +7,17 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
)
|
||||
|
||||
// LFSMetaObject stores metadata for LFS tracked files.
|
||||
type LFSMetaObject struct {
|
||||
ID int64 `xorm:"pk autoincr"`
|
||||
Oid string `xorm:"UNIQUE(s) INDEX NOT NULL"`
|
||||
Size int64 `xorm:"NOT NULL"`
|
||||
RepositoryID int64 `xorm:"UNIQUE(s) INDEX NOT NULL"`
|
||||
Existing bool `xorm:"-"`
|
||||
CreatedUnix util.TimeStamp `xorm:"created"`
|
||||
ID int64 `xorm:"pk autoincr"`
|
||||
Oid string `xorm:"UNIQUE(s) INDEX NOT NULL"`
|
||||
Size int64 `xorm:"NOT NULL"`
|
||||
RepositoryID int64 `xorm:"UNIQUE(s) INDEX NOT NULL"`
|
||||
Existing bool `xorm:"-"`
|
||||
CreatedUnix timeutil.TimeStamp `xorm:"created"`
|
||||
}
|
||||
|
||||
// Pointer returns the string representation of an LFS pointer file
|
||||
|
||||
+2
-1
@@ -13,6 +13,7 @@ import (
|
||||
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
|
||||
"github.com/go-xorm/xorm"
|
||||
)
|
||||
|
||||
@@ -56,7 +57,7 @@ func (l *LFSLock) APIFormat() *api.LFSLock {
|
||||
return &api.LFSLock{
|
||||
ID: strconv.FormatInt(l.ID, 10),
|
||||
Path: l.Path,
|
||||
LockedAt: l.Created,
|
||||
LockedAt: l.Created.Round(time.Second),
|
||||
Owner: &api.LFSLockOwner{
|
||||
Name: l.Owner.DisplayName(),
|
||||
},
|
||||
|
||||
@@ -14,16 +14,16 @@ import (
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/Unknwon/com"
|
||||
"github.com/go-xorm/xorm"
|
||||
"xorm.io/core"
|
||||
|
||||
"code.gitea.io/gitea/modules/auth/ldap"
|
||||
"code.gitea.io/gitea/modules/auth/oauth2"
|
||||
"code.gitea.io/gitea/modules/auth/pam"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
|
||||
"github.com/go-xorm/xorm"
|
||||
"github.com/unknwon/com"
|
||||
"xorm.io/core"
|
||||
)
|
||||
|
||||
// LoginType represents an login type.
|
||||
@@ -148,8 +148,8 @@ type LoginSource struct {
|
||||
IsSyncEnabled bool `xorm:"INDEX NOT NULL DEFAULT false"`
|
||||
Cfg core.Conversion `xorm:"TEXT"`
|
||||
|
||||
CreatedUnix util.TimeStamp `xorm:"INDEX created"`
|
||||
UpdatedUnix util.TimeStamp `xorm:"INDEX updated"`
|
||||
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
|
||||
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
|
||||
}
|
||||
|
||||
// Cell2Int64 converts a xorm.Cell type to int64,
|
||||
|
||||
-200
@@ -1,200 +0,0 @@
|
||||
// Copyright 2016 The Gogs 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 models
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"path"
|
||||
|
||||
"code.gitea.io/gitea/modules/base"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/mailer"
|
||||
"code.gitea.io/gitea/modules/markup"
|
||||
"code.gitea.io/gitea/modules/markup/markdown"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
"gopkg.in/gomail.v2"
|
||||
)
|
||||
|
||||
const (
|
||||
mailAuthActivate base.TplName = "auth/activate"
|
||||
mailAuthActivateEmail base.TplName = "auth/activate_email"
|
||||
mailAuthResetPassword base.TplName = "auth/reset_passwd"
|
||||
mailAuthRegisterNotify base.TplName = "auth/register_notify"
|
||||
|
||||
mailIssueComment base.TplName = "issue/comment"
|
||||
mailIssueMention base.TplName = "issue/mention"
|
||||
|
||||
mailNotifyCollaborator base.TplName = "notify/collaborator"
|
||||
)
|
||||
|
||||
var templates *template.Template
|
||||
|
||||
// InitMailRender initializes the mail renderer
|
||||
func InitMailRender(tmpls *template.Template) {
|
||||
templates = tmpls
|
||||
}
|
||||
|
||||
// SendTestMail sends a test mail
|
||||
func SendTestMail(email string) error {
|
||||
return gomail.Send(mailer.Sender, mailer.NewMessage([]string{email}, "Gitea Test Email!", "Gitea Test Email!").Message)
|
||||
}
|
||||
|
||||
// SendUserMail sends a mail to the user
|
||||
func SendUserMail(language string, u *User, tpl base.TplName, code, subject, info string) {
|
||||
data := map[string]interface{}{
|
||||
"DisplayName": u.DisplayName(),
|
||||
"ActiveCodeLives": base.MinutesToFriendly(setting.Service.ActiveCodeLives, language),
|
||||
"ResetPwdCodeLives": base.MinutesToFriendly(setting.Service.ResetPwdCodeLives, language),
|
||||
"Code": code,
|
||||
}
|
||||
|
||||
var content bytes.Buffer
|
||||
|
||||
if err := templates.ExecuteTemplate(&content, string(tpl), data); err != nil {
|
||||
log.Error("Template: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
msg := mailer.NewMessage([]string{u.Email}, subject, content.String())
|
||||
msg.Info = fmt.Sprintf("UID: %d, %s", u.ID, info)
|
||||
|
||||
mailer.SendAsync(msg)
|
||||
}
|
||||
|
||||
// Locale represents an interface to translation
|
||||
type Locale interface {
|
||||
Language() string
|
||||
Tr(string, ...interface{}) string
|
||||
}
|
||||
|
||||
// SendActivateAccountMail sends an activation mail to the user (new user registration)
|
||||
func SendActivateAccountMail(locale Locale, u *User) {
|
||||
SendUserMail(locale.Language(), u, mailAuthActivate, u.GenerateActivateCode(), locale.Tr("mail.activate_account"), "activate account")
|
||||
}
|
||||
|
||||
// SendResetPasswordMail sends a password reset mail to the user
|
||||
func SendResetPasswordMail(locale Locale, u *User) {
|
||||
SendUserMail(locale.Language(), u, mailAuthResetPassword, u.GenerateActivateCode(), locale.Tr("mail.reset_password"), "recover account")
|
||||
}
|
||||
|
||||
// SendActivateEmailMail sends confirmation email to confirm new email address
|
||||
func SendActivateEmailMail(locale Locale, u *User, email *EmailAddress) {
|
||||
data := map[string]interface{}{
|
||||
"DisplayName": u.DisplayName(),
|
||||
"ActiveCodeLives": base.MinutesToFriendly(setting.Service.ActiveCodeLives, locale.Language()),
|
||||
"Code": u.GenerateEmailActivateCode(email.Email),
|
||||
"Email": email.Email,
|
||||
}
|
||||
|
||||
var content bytes.Buffer
|
||||
|
||||
if err := templates.ExecuteTemplate(&content, string(mailAuthActivateEmail), data); err != nil {
|
||||
log.Error("Template: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
msg := mailer.NewMessage([]string{email.Email}, locale.Tr("mail.activate_email"), content.String())
|
||||
msg.Info = fmt.Sprintf("UID: %d, activate email", u.ID)
|
||||
|
||||
mailer.SendAsync(msg)
|
||||
}
|
||||
|
||||
// SendRegisterNotifyMail triggers a notify e-mail by admin created a account.
|
||||
func SendRegisterNotifyMail(locale Locale, u *User) {
|
||||
data := map[string]interface{}{
|
||||
"DisplayName": u.DisplayName(),
|
||||
"Username": u.Name,
|
||||
}
|
||||
|
||||
var content bytes.Buffer
|
||||
|
||||
if err := templates.ExecuteTemplate(&content, string(mailAuthRegisterNotify), data); err != nil {
|
||||
log.Error("Template: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
msg := mailer.NewMessage([]string{u.Email}, locale.Tr("mail.register_notify"), content.String())
|
||||
msg.Info = fmt.Sprintf("UID: %d, registration notify", u.ID)
|
||||
|
||||
mailer.SendAsync(msg)
|
||||
}
|
||||
|
||||
// SendCollaboratorMail sends mail notification to new collaborator.
|
||||
func SendCollaboratorMail(u, doer *User, repo *Repository) {
|
||||
repoName := path.Join(repo.Owner.Name, repo.Name)
|
||||
subject := fmt.Sprintf("%s added you to %s", doer.DisplayName(), repoName)
|
||||
|
||||
data := map[string]interface{}{
|
||||
"Subject": subject,
|
||||
"RepoName": repoName,
|
||||
"Link": repo.HTMLURL(),
|
||||
}
|
||||
|
||||
var content bytes.Buffer
|
||||
|
||||
if err := templates.ExecuteTemplate(&content, string(mailNotifyCollaborator), data); err != nil {
|
||||
log.Error("Template: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
msg := mailer.NewMessage([]string{u.Email}, subject, content.String())
|
||||
msg.Info = fmt.Sprintf("UID: %d, add collaborator", u.ID)
|
||||
|
||||
mailer.SendAsync(msg)
|
||||
}
|
||||
|
||||
func composeTplData(subject, body, link string) map[string]interface{} {
|
||||
data := make(map[string]interface{}, 10)
|
||||
data["Subject"] = subject
|
||||
data["Body"] = body
|
||||
data["Link"] = link
|
||||
return data
|
||||
}
|
||||
|
||||
func composeIssueCommentMessage(issue *Issue, doer *User, content string, comment *Comment, tplName base.TplName, tos []string, info string) *mailer.Message {
|
||||
subject := issue.mailSubject()
|
||||
err := issue.LoadRepo()
|
||||
if err != nil {
|
||||
log.Error("LoadRepo: %v", err)
|
||||
}
|
||||
body := string(markup.RenderByType(markdown.MarkupName, []byte(content), issue.Repo.HTMLURL(), issue.Repo.ComposeMetas()))
|
||||
|
||||
var data = make(map[string]interface{}, 10)
|
||||
if comment != nil {
|
||||
data = composeTplData(subject, body, issue.HTMLURL()+"#"+comment.HashTag())
|
||||
} else {
|
||||
data = composeTplData(subject, body, issue.HTMLURL())
|
||||
}
|
||||
data["Doer"] = doer
|
||||
|
||||
var mailBody bytes.Buffer
|
||||
|
||||
if err := templates.ExecuteTemplate(&mailBody, string(tplName), data); err != nil {
|
||||
log.Error("Template: %v", err)
|
||||
}
|
||||
|
||||
msg := mailer.NewMessageFrom(tos, doer.DisplayName(), setting.MailService.FromEmail, subject, mailBody.String())
|
||||
msg.Info = fmt.Sprintf("Subject: %s, %s", subject, info)
|
||||
return msg
|
||||
}
|
||||
|
||||
// SendIssueCommentMail composes and sends issue comment emails to target receivers.
|
||||
func SendIssueCommentMail(issue *Issue, doer *User, content string, comment *Comment, tos []string) {
|
||||
if len(tos) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
mailer.SendAsync(composeIssueCommentMessage(issue, doer, content, comment, mailIssueComment, tos, "issue comment"))
|
||||
}
|
||||
|
||||
// SendIssueMentionMail composes and sends issue mention emails to target receivers.
|
||||
func SendIssueMentionMail(issue *Issue, doer *User, content string, comment *Comment, tos []string) {
|
||||
if len(tos) == 0 {
|
||||
return
|
||||
}
|
||||
mailer.SendAsync(composeIssueCommentMessage(issue, doer, content, comment, mailIssueMention, tos, "issue mention"))
|
||||
}
|
||||
+16
-4
@@ -62,38 +62,50 @@ func insertIssue(sess *xorm.Session, issue *Issue) error {
|
||||
if _, err := sess.Insert(issueLabels); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cols := make([]string, 0)
|
||||
if !issue.IsPull {
|
||||
sess.ID(issue.RepoID).Incr("num_issues")
|
||||
cols = append(cols, "num_issues")
|
||||
if issue.IsClosed {
|
||||
sess.Incr("num_closed_issues")
|
||||
cols = append(cols, "num_closed_issues")
|
||||
}
|
||||
} else {
|
||||
sess.ID(issue.RepoID).Incr("num_pulls")
|
||||
cols = append(cols, "num_pulls")
|
||||
if issue.IsClosed {
|
||||
sess.Incr("num_closed_pulls")
|
||||
cols = append(cols, "num_closed_pulls")
|
||||
}
|
||||
}
|
||||
if _, err := sess.NoAutoTime().Update(issue.Repo); err != nil {
|
||||
if _, err := sess.NoAutoTime().Cols(cols...).Update(issue.Repo); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cols = []string{"num_issues"}
|
||||
sess.Incr("num_issues")
|
||||
if issue.IsClosed {
|
||||
sess.Incr("num_closed_issues")
|
||||
cols = append(cols, "num_closed_issues")
|
||||
}
|
||||
if _, err := sess.In("id", labelIDs).NoAutoTime().Update(new(Label)); err != nil {
|
||||
if _, err := sess.In("id", labelIDs).NoAutoTime().Cols(cols...).Update(new(Label)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if issue.MilestoneID > 0 {
|
||||
cols = []string{"num_issues"}
|
||||
sess.Incr("num_issues")
|
||||
cl := "num_closed_issues"
|
||||
if issue.IsClosed {
|
||||
sess.Incr("num_closed_issues")
|
||||
cols = append(cols, "num_closed_issues")
|
||||
cl = "(num_closed_issues + 1)"
|
||||
}
|
||||
|
||||
if _, err := sess.ID(issue.MilestoneID).
|
||||
SetExpr("completeness", "num_closed_issues * 100 / num_issues").
|
||||
NoAutoTime().
|
||||
SetExpr("completeness", cl+" * 100 / (num_issues + 1)").
|
||||
NoAutoTime().Cols(cols...).
|
||||
Update(new(Milestone)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -17,14 +17,14 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/Unknwon/com"
|
||||
"github.com/go-xorm/xorm"
|
||||
gouuid "github.com/satori/go.uuid"
|
||||
ini "gopkg.in/ini.v1"
|
||||
|
||||
"code.gitea.io/gitea/modules/generate"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
|
||||
"github.com/go-xorm/xorm"
|
||||
gouuid "github.com/satori/go.uuid"
|
||||
"github.com/unknwon/com"
|
||||
ini "gopkg.in/ini.v1"
|
||||
)
|
||||
|
||||
const minDBVersion = 4
|
||||
@@ -235,7 +235,26 @@ var migrations = []Migration{
|
||||
// v89 -> v90
|
||||
NewMigration("add original author/url migration info to issues, comments, and repo ", addOriginalMigrationInfo),
|
||||
// v90 -> v91
|
||||
NewMigration("change length of some repository columns", changeSomeColumnsLengthOfRepo),
|
||||
// v91 -> v92
|
||||
NewMigration("add index on owner_id of repository and type, review_id of comment", addIndexOnRepositoryAndComment),
|
||||
// v92 -> v93
|
||||
NewMigration("remove orphaned repository index statuses", removeLingeringIndexStatus),
|
||||
// v93 -> v94
|
||||
NewMigration("add email notification enabled preference to user", addEmailNotificationEnabledToUser),
|
||||
// v94 -> v95
|
||||
NewMigration("add enable_status_check, status_check_contexts to protected_branch", addStatusCheckColumnsForProtectedBranches),
|
||||
// v95 -> v96
|
||||
NewMigration("add table columns for cross referencing issues", addCrossReferenceColumns),
|
||||
// v96 -> v97
|
||||
NewMigration("delete orphaned attachments", deleteOrphanedAttachments),
|
||||
// v97 -> v98
|
||||
NewMigration("add repo_admin_change_team_access to user", addRepoAdminChangeTeamAccessColumnForUser),
|
||||
// v98 -> v99
|
||||
NewMigration("add original author name and id on migrated release", addOriginalAuthorOnMigratedReleases),
|
||||
// v99 -> v100
|
||||
NewMigration("add includes_all_repositories to teams", addTeamIncludesAllRepositories),
|
||||
|
||||
}
|
||||
|
||||
// Migrate database to current version
|
||||
@@ -292,7 +311,7 @@ func dropTableColumns(sess *xorm.Session, tableName string, columnNames ...strin
|
||||
// TODO: This will not work if there are foreign keys
|
||||
|
||||
switch {
|
||||
case setting.UseSQLite3:
|
||||
case setting.Database.UseSQLite3:
|
||||
// First drop the indexes on the columns
|
||||
res, errIndex := sess.Query(fmt.Sprintf("PRAGMA index_list(`%s`)", tableName))
|
||||
if errIndex != nil {
|
||||
@@ -325,11 +344,25 @@ func dropTableColumns(sess *xorm.Session, tableName string, columnNames ...strin
|
||||
return err
|
||||
}
|
||||
tableSQL := string(res[0]["sql"])
|
||||
|
||||
// Separate out the column definitions
|
||||
tableSQL = tableSQL[strings.Index(tableSQL, "("):]
|
||||
|
||||
// Remove the required columnNames
|
||||
for _, name := range columnNames {
|
||||
tableSQL = regexp.MustCompile(regexp.QuoteMeta("`"+name+"`")+"[^`,)]*[,)]").ReplaceAllString(tableSQL, "")
|
||||
tableSQL = regexp.MustCompile(regexp.QuoteMeta("`"+name+"`")+"[^`,)]*?[,)]").ReplaceAllString(tableSQL, "")
|
||||
}
|
||||
|
||||
// Ensure the query is ended properly
|
||||
tableSQL = strings.TrimSpace(tableSQL)
|
||||
if tableSQL[len(tableSQL)-1] != ')' {
|
||||
if tableSQL[len(tableSQL)-1] == ',' {
|
||||
tableSQL = tableSQL[:len(tableSQL)-1]
|
||||
}
|
||||
tableSQL += ")"
|
||||
}
|
||||
|
||||
// Find all the columns in the table
|
||||
columns := regexp.MustCompile("`([^`]*)`").FindAllString(tableSQL, -1)
|
||||
|
||||
tableSQL = fmt.Sprintf("CREATE TABLE `new_%s_new` ", tableName) + tableSQL
|
||||
@@ -354,7 +387,7 @@ func dropTableColumns(sess *xorm.Session, tableName string, columnNames ...strin
|
||||
return err
|
||||
}
|
||||
|
||||
case setting.UsePostgreSQL:
|
||||
case setting.Database.UsePostgreSQL:
|
||||
cols := ""
|
||||
for _, col := range columnNames {
|
||||
if cols != "" {
|
||||
@@ -365,7 +398,7 @@ func dropTableColumns(sess *xorm.Session, tableName string, columnNames ...strin
|
||||
if _, err := sess.Exec(fmt.Sprintf("ALTER TABLE `%s` %s", tableName, cols)); err != nil {
|
||||
return fmt.Errorf("Drop table `%s` columns %v: %v", tableName, columnNames, err)
|
||||
}
|
||||
case setting.UseMySQL, setting.UseTiDB:
|
||||
case setting.Database.UseMySQL:
|
||||
// Drop indexes on columns first
|
||||
sql := fmt.Sprintf("SHOW INDEX FROM %s WHERE column_name IN ('%s')", tableName, strings.Join(columnNames, "','"))
|
||||
res, err := sess.Query(sql)
|
||||
@@ -391,7 +424,7 @@ func dropTableColumns(sess *xorm.Session, tableName string, columnNames ...strin
|
||||
if _, err := sess.Exec(fmt.Sprintf("ALTER TABLE `%s` %s", tableName, cols)); err != nil {
|
||||
return fmt.Errorf("Drop table `%s` columns %v: %v", tableName, columnNames, err)
|
||||
}
|
||||
case setting.UseMSSQL:
|
||||
case setting.Database.UseMSSQL:
|
||||
cols := ""
|
||||
for _, col := range columnNames {
|
||||
if cols != "" {
|
||||
|
||||
@@ -9,8 +9,8 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/Unknwon/com"
|
||||
"github.com/go-xorm/xorm"
|
||||
"github.com/unknwon/com"
|
||||
)
|
||||
|
||||
func ldapUseSSLToSecurityProtocol(x *xorm.Engine) error {
|
||||
|
||||
@@ -13,8 +13,8 @@ import (
|
||||
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
|
||||
"github.com/Unknwon/com"
|
||||
"github.com/go-xorm/xorm"
|
||||
"github.com/unknwon/com"
|
||||
)
|
||||
|
||||
func generateAndMigrateGitHooks(x *xorm.Engine) (err error) {
|
||||
@@ -42,7 +42,7 @@ func generateAndMigrateGitHooks(x *xorm.Engine) (err error) {
|
||||
}
|
||||
)
|
||||
|
||||
return x.Where("id > 0").BufferSize(setting.IterateBufferSize).Iterate(new(Repository),
|
||||
return x.Where("id > 0").BufferSize(setting.Database.IterateBufferSize).Iterate(new(Repository),
|
||||
func(idx int, bean interface{}) error {
|
||||
repo := bean.(*Repository)
|
||||
user := new(User)
|
||||
|
||||
@@ -11,8 +11,8 @@ import (
|
||||
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
|
||||
"github.com/Unknwon/com"
|
||||
"github.com/go-xorm/xorm"
|
||||
"github.com/unknwon/com"
|
||||
)
|
||||
|
||||
const (
|
||||
|
||||
@@ -13,8 +13,8 @@ import (
|
||||
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
|
||||
"github.com/Unknwon/com"
|
||||
"github.com/go-xorm/xorm"
|
||||
"github.com/unknwon/com"
|
||||
)
|
||||
|
||||
func generateAndMigrateWikiGitHooks(x *xorm.Engine) (err error) {
|
||||
@@ -42,7 +42,7 @@ func generateAndMigrateWikiGitHooks(x *xorm.Engine) (err error) {
|
||||
}
|
||||
)
|
||||
|
||||
return x.Where("id > 0").BufferSize(setting.IterateBufferSize).Iterate(new(Repository),
|
||||
return x.Where("id > 0").BufferSize(setting.Database.IterateBufferSize).Iterate(new(Repository),
|
||||
func(idx int, bean interface{}) error {
|
||||
repo := bean.(*Repository)
|
||||
user := new(User)
|
||||
|
||||
@@ -16,8 +16,8 @@ import (
|
||||
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
|
||||
"github.com/Unknwon/com"
|
||||
"github.com/go-xorm/xorm"
|
||||
"github.com/unknwon/com"
|
||||
)
|
||||
|
||||
func generateAndMigrateGitHookChains(x *xorm.Engine) (err error) {
|
||||
@@ -36,7 +36,7 @@ func generateAndMigrateGitHookChains(x *xorm.Engine) (err error) {
|
||||
hookTpl = fmt.Sprintf("#!/usr/bin/env %s\ndata=$(cat)\nexitcodes=\"\"\nhookname=$(basename $0)\nGIT_DIR=${GIT_DIR:-$(dirname $0)}\n\nfor hook in ${GIT_DIR}/hooks/${hookname}.d/*; do\ntest -x \"${hook}\" || continue\necho \"${data}\" | \"${hook}\"\nexitcodes=\"${exitcodes} $?\"\ndone\n\nfor i in ${exitcodes}; do\n[ ${i} -eq 0 ] || exit ${i}\ndone\n", setting.ScriptType)
|
||||
)
|
||||
|
||||
return x.Where("id > 0").BufferSize(setting.IterateBufferSize).Iterate(new(Repository),
|
||||
return x.Where("id > 0").BufferSize(setting.Database.IterateBufferSize).Iterate(new(Repository),
|
||||
func(idx int, bean interface{}) error {
|
||||
repo := bean.(*Repository)
|
||||
user := new(User)
|
||||
|
||||
@@ -41,8 +41,6 @@ func convertIntervalToDuration(x *xorm.Engine) (err error) {
|
||||
_, err = sess.Exec("ALTER TABLE mirror MODIFY `interval` BIGINT")
|
||||
case "postgres":
|
||||
_, err = sess.Exec("ALTER TABLE mirror ALTER COLUMN \"interval\" SET DATA TYPE bigint")
|
||||
case "tidb":
|
||||
_, err = sess.Exec("ALTER TABLE mirror MODIFY `interval` BIGINT")
|
||||
case "mssql":
|
||||
_, err = sess.Exec("ALTER TABLE mirror ALTER COLUMN \"interval\" BIGINT")
|
||||
case "sqlite3":
|
||||
|
||||
@@ -15,9 +15,9 @@ import (
|
||||
|
||||
func removeActionColumns(x *xorm.Engine) error {
|
||||
switch {
|
||||
case setting.UseSQLite3:
|
||||
case setting.Database.UseSQLite3:
|
||||
log.Warn("Unable to drop columns in SQLite")
|
||||
case setting.UseMySQL, setting.UsePostgreSQL, setting.UseMSSQL, setting.UseTiDB:
|
||||
case setting.Database.UseMySQL, setting.Database.UsePostgreSQL, setting.Database.UseMSSQL:
|
||||
if _, err := x.Exec("ALTER TABLE action DROP COLUMN act_user_name"); err != nil {
|
||||
return fmt.Errorf("DROP COLUMN act_user_name: %v", err)
|
||||
} else if _, err = x.Exec("ALTER TABLE action DROP COLUMN repo_user_name"); err != nil {
|
||||
|
||||
@@ -13,9 +13,9 @@ import (
|
||||
|
||||
func removeIndexColumnFromRepoUnitTable(x *xorm.Engine) (err error) {
|
||||
switch {
|
||||
case setting.UseSQLite3:
|
||||
case setting.Database.UseSQLite3:
|
||||
log.Warn("Unable to drop columns in SQLite")
|
||||
case setting.UseMySQL, setting.UsePostgreSQL, setting.UseMSSQL, setting.UseTiDB:
|
||||
case setting.Database.UseMySQL, setting.Database.UsePostgreSQL, setting.Database.UseMSSQL:
|
||||
if _, err := x.Exec("ALTER TABLE repo_unit DROP COLUMN `index`"); err != nil {
|
||||
// Ignoring this error in case we run this migration second time (after migration reordering)
|
||||
log.Warn("DROP COLUMN index: %v", err)
|
||||
|
||||
@@ -40,9 +40,9 @@ func migrateProtectedBranchStruct(x *xorm.Engine) error {
|
||||
}
|
||||
|
||||
switch {
|
||||
case setting.UseSQLite3:
|
||||
case setting.Database.UseSQLite3:
|
||||
log.Warn("Unable to drop columns in SQLite")
|
||||
case setting.UseMySQL, setting.UsePostgreSQL, setting.UseMSSQL, setting.UseTiDB:
|
||||
case setting.Database.UseMySQL, setting.Database.UsePostgreSQL, setting.Database.UseMSSQL:
|
||||
if _, err := x.Exec("ALTER TABLE protected_branch DROP COLUMN can_push"); err != nil {
|
||||
// Ignoring this error in case we run this migration second time (after migration reordering)
|
||||
log.Warn("DROP COLUMN can_push (skipping): %v", err)
|
||||
|
||||
@@ -7,7 +7,7 @@ package migrations
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
|
||||
"github.com/go-xorm/xorm"
|
||||
)
|
||||
@@ -19,7 +19,7 @@ func addPullRequestOptions(x *xorm.Engine) error {
|
||||
RepoID int64 `xorm:"INDEX(s)"`
|
||||
Type int `xorm:"INDEX(s)"`
|
||||
Config map[string]interface{} `xorm:"JSON"`
|
||||
CreatedUnix util.TimeStamp `xorm:"INDEX CREATED"`
|
||||
CreatedUnix timeutil.TimeStamp `xorm:"INDEX CREATED"`
|
||||
}
|
||||
|
||||
sess := x.NewSession()
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"fmt"
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
|
||||
"github.com/go-xorm/xorm"
|
||||
)
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ package migrations
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
|
||||
"github.com/go-xorm/xorm"
|
||||
)
|
||||
@@ -15,7 +15,7 @@ import (
|
||||
func addIssueClosedTime(x *xorm.Engine) error {
|
||||
// Issue see models/issue.go
|
||||
type Issue struct {
|
||||
ClosedUnix util.TimeStamp `xorm:"INDEX"`
|
||||
ClosedUnix timeutil.TimeStamp `xorm:"INDEX"`
|
||||
}
|
||||
|
||||
if err := x.Sync2(new(Issue)); err != nil {
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
package migrations
|
||||
|
||||
import (
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
|
||||
"github.com/go-xorm/xorm"
|
||||
)
|
||||
@@ -27,10 +27,10 @@ func addMultipleAssignees(x *xorm.Engine) error {
|
||||
IsPull bool `xorm:"INDEX"` // Indicates whether is a pull request or not.
|
||||
NumComments int
|
||||
|
||||
DeadlineUnix util.TimeStamp `xorm:"INDEX"`
|
||||
CreatedUnix util.TimeStamp `xorm:"INDEX created"`
|
||||
UpdatedUnix util.TimeStamp `xorm:"INDEX updated"`
|
||||
ClosedUnix util.TimeStamp `xorm:"INDEX"`
|
||||
DeadlineUnix timeutil.TimeStamp `xorm:"INDEX"`
|
||||
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
|
||||
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
|
||||
ClosedUnix timeutil.TimeStamp `xorm:"INDEX"`
|
||||
}
|
||||
|
||||
// Updated the comment table
|
||||
@@ -53,8 +53,8 @@ func addMultipleAssignees(x *xorm.Engine) error {
|
||||
Content string `xorm:"TEXT"`
|
||||
RenderedContent string `xorm:"-"`
|
||||
|
||||
CreatedUnix util.TimeStamp `xorm:"INDEX created"`
|
||||
UpdatedUnix util.TimeStamp `xorm:"INDEX updated"`
|
||||
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
|
||||
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
|
||||
|
||||
// Reference issue in commit message
|
||||
CommitSHA string `xorm:"VARCHAR(40)"`
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
package migrations
|
||||
|
||||
import (
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
|
||||
"github.com/go-xorm/xorm"
|
||||
)
|
||||
|
||||
@@ -12,8 +13,8 @@ func addU2FReg(x *xorm.Engine) error {
|
||||
UserID int64 `xorm:"INDEX"`
|
||||
Raw []byte
|
||||
Counter uint32
|
||||
CreatedUnix util.TimeStamp `xorm:"INDEX created"`
|
||||
UpdatedUnix util.TimeStamp `xorm:"INDEX updated"`
|
||||
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
|
||||
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
|
||||
}
|
||||
return x.Sync2(&U2FRegistration{})
|
||||
}
|
||||
|
||||
@@ -80,7 +80,7 @@ func removeStaleWatches(x *xorm.Engine) error {
|
||||
}
|
||||
|
||||
repoCache := make(map[int64]*Repository)
|
||||
err := sess.BufferSize(setting.IterateBufferSize).Iterate(new(Watch),
|
||||
err := sess.BufferSize(setting.Database.IterateBufferSize).Iterate(new(Watch),
|
||||
func(idx int, bean interface{}) error {
|
||||
watch := bean.(*Watch)
|
||||
|
||||
@@ -117,7 +117,7 @@ func removeStaleWatches(x *xorm.Engine) error {
|
||||
}
|
||||
|
||||
repoCache = make(map[int64]*Repository)
|
||||
err = sess.BufferSize(setting.IterateBufferSize).
|
||||
err = sess.BufferSize(setting.Database.IterateBufferSize).
|
||||
Distinct("issue_watch.user_id", "issue.repo_id").
|
||||
Join("INNER", "issue", "issue_watch.issue_id = issue.id").
|
||||
Where("issue_watch.is_watching = ?", true).
|
||||
|
||||
@@ -8,11 +8,11 @@ import (
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
|
||||
"code.gitea.io/gitea/modules/generate"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
|
||||
"github.com/go-xorm/xorm"
|
||||
"golang.org/x/crypto/pbkdf2"
|
||||
|
||||
"code.gitea.io/gitea/modules/generate"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
)
|
||||
|
||||
func addScratchHash(x *xorm.Engine) error {
|
||||
@@ -24,9 +24,9 @@ func addScratchHash(x *xorm.Engine) error {
|
||||
ScratchToken string
|
||||
ScratchSalt string
|
||||
ScratchHash string
|
||||
LastUsedPasscode string `xorm:"VARCHAR(10)"`
|
||||
CreatedUnix util.TimeStamp `xorm:"INDEX created"`
|
||||
UpdatedUnix util.TimeStamp `xorm:"INDEX updated"`
|
||||
LastUsedPasscode string `xorm:"VARCHAR(10)"`
|
||||
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
|
||||
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
|
||||
}
|
||||
|
||||
if err := x.Sync2(new(TwoFactor)); err != nil {
|
||||
|
||||
@@ -7,7 +7,7 @@ package migrations
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
|
||||
"github.com/go-xorm/xorm"
|
||||
)
|
||||
@@ -20,8 +20,8 @@ func addReview(x *xorm.Engine) error {
|
||||
ReviewerID int64 `xorm:"index"`
|
||||
IssueID int64 `xorm:"index"`
|
||||
Content string
|
||||
CreatedUnix util.TimeStamp `xorm:"INDEX created"`
|
||||
UpdatedUnix util.TimeStamp `xorm:"INDEX updated"`
|
||||
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
|
||||
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
|
||||
}
|
||||
|
||||
if err := x.Sync2(new(Review)); err != nil {
|
||||
|
||||
@@ -7,7 +7,7 @@ package migrations
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
|
||||
"github.com/go-xorm/xorm"
|
||||
)
|
||||
@@ -19,7 +19,7 @@ func addPullRequestRebaseWithMerge(x *xorm.Engine) error {
|
||||
RepoID int64 `xorm:"INDEX(s)"`
|
||||
Type int `xorm:"INDEX(s)"`
|
||||
Config map[string]interface{} `xorm:"JSON"`
|
||||
CreatedUnix util.TimeStamp `xorm:"INDEX CREATED"`
|
||||
CreatedUnix timeutil.TimeStamp `xorm:"INDEX CREATED"`
|
||||
}
|
||||
|
||||
sess := x.NewSession()
|
||||
|
||||
@@ -5,13 +5,7 @@
|
||||
package migrations
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
|
||||
"github.com/go-xorm/xorm"
|
||||
"xorm.io/core"
|
||||
)
|
||||
|
||||
func renameRepoIsBareToIsEmpty(x *xorm.Engine) error {
|
||||
@@ -21,73 +15,28 @@ func renameRepoIsBareToIsEmpty(x *xorm.Engine) error {
|
||||
IsEmpty bool `xorm:"INDEX"`
|
||||
}
|
||||
|
||||
// First remove the index
|
||||
sess := x.NewSession()
|
||||
defer sess.Close()
|
||||
if err := sess.Begin(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var err error
|
||||
if models.DbCfg.Type == core.POSTGRES || models.DbCfg.Type == core.SQLITE {
|
||||
_, err = sess.Exec("DROP INDEX IF EXISTS IDX_repository_is_bare")
|
||||
} else if models.DbCfg.Type == core.MSSQL {
|
||||
_, err = sess.Exec(`DECLARE @ConstraintName VARCHAR(256)
|
||||
DECLARE @SQL NVARCHAR(256)
|
||||
SELECT @ConstraintName = obj.name FROM sys.columns col LEFT OUTER JOIN sys.objects obj ON obj.object_id = col.default_object_id AND obj.type = 'D' WHERE col.object_id = OBJECT_ID('repository') AND obj.name IS NOT NULL AND col.name = 'is_bare'
|
||||
SET @SQL = N'ALTER TABLE [repository] DROP CONSTRAINT [' + @ConstraintName + N']'
|
||||
EXEC sp_executesql @SQL`)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else if models.DbCfg.Type == core.MYSQL {
|
||||
indexes, err := sess.QueryString(`SHOW INDEX FROM repository WHERE KEY_NAME = 'IDX_repository_is_bare'`)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(indexes) >= 1 {
|
||||
_, err = sess.Exec("DROP INDEX IDX_repository_is_bare ON repository")
|
||||
if err != nil {
|
||||
return fmt.Errorf("Drop index failed: %v", err)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
_, err = sess.Exec("DROP INDEX IDX_repository_is_bare ON repository")
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("Drop index failed: %v", err)
|
||||
}
|
||||
|
||||
if err = sess.Commit(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := sess.Begin(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := sess.Sync2(new(Repository)); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := sess.Exec("UPDATE repository SET is_empty = is_bare;"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if models.DbCfg.Type != core.SQLITE {
|
||||
_, err = sess.Exec("ALTER TABLE repository DROP COLUMN is_bare")
|
||||
if err != nil {
|
||||
return fmt.Errorf("Drop column failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if err = sess.Commit(); err != nil {
|
||||
if err := sess.Commit(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if models.DbCfg.Type == core.SQLITE {
|
||||
log.Warn("TABLE repository's COLUMN is_bare should be DROP but sqlite is not supported, you could manually do that.")
|
||||
if err := sess.Begin(); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
if err := dropTableColumns(sess, "repository", "is_bare"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return sess.Commit()
|
||||
}
|
||||
|
||||
@@ -14,5 +14,4 @@ func addIsLockedToIssues(x *xorm.Engine) error {
|
||||
}
|
||||
|
||||
return x.Sync2(new(Issue))
|
||||
|
||||
}
|
||||
|
||||
@@ -14,8 +14,6 @@ func changeU2FCounterType(x *xorm.Engine) error {
|
||||
var err error
|
||||
|
||||
switch x.Dialect().DriverName() {
|
||||
case "tidb":
|
||||
fallthrough
|
||||
case "mysql":
|
||||
_, err = x.Exec("ALTER TABLE `u2f_registration` MODIFY `counter` BIGINT")
|
||||
case "postgres":
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
package migrations
|
||||
|
||||
import (
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
|
||||
"github.com/go-xorm/xorm"
|
||||
)
|
||||
@@ -19,9 +19,9 @@ func addUploaderIDForAttachment(x *xorm.Engine) error {
|
||||
UploaderID int64 `xorm:"INDEX DEFAULT 0"`
|
||||
CommentID int64
|
||||
Name string
|
||||
DownloadCount int64 `xorm:"DEFAULT 0"`
|
||||
Size int64 `xorm:"DEFAULT 0"`
|
||||
CreatedUnix util.TimeStamp `xorm:"created"`
|
||||
DownloadCount int64 `xorm:"DEFAULT 0"`
|
||||
Size int64 `xorm:"DEFAULT 0"`
|
||||
CreatedUnix timeutil.TimeStamp `xorm:"created"`
|
||||
}
|
||||
|
||||
return x.Sync2(new(Attachment))
|
||||
|
||||
@@ -7,13 +7,11 @@ package migrations
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/go-xorm/xorm"
|
||||
"xorm.io/core"
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
"code.gitea.io/gitea/modules/generate"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
|
||||
"github.com/go-xorm/xorm"
|
||||
)
|
||||
|
||||
func hashAppToken(x *xorm.Engine) error {
|
||||
@@ -28,50 +26,15 @@ func hashAppToken(x *xorm.Engine) error {
|
||||
TokenSalt string
|
||||
TokenLastEight string `xorm:"token_last_eight"`
|
||||
|
||||
CreatedUnix util.TimeStamp `xorm:"INDEX created"`
|
||||
UpdatedUnix util.TimeStamp `xorm:"INDEX updated"`
|
||||
HasRecentActivity bool `xorm:"-"`
|
||||
HasUsed bool `xorm:"-"`
|
||||
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
|
||||
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
|
||||
HasRecentActivity bool `xorm:"-"`
|
||||
HasUsed bool `xorm:"-"`
|
||||
}
|
||||
|
||||
// First remove the index
|
||||
sess := x.NewSession()
|
||||
defer sess.Close()
|
||||
if err := sess.Begin(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var err error
|
||||
if models.DbCfg.Type == core.POSTGRES || models.DbCfg.Type == core.SQLITE {
|
||||
_, err = sess.Exec("DROP INDEX IF EXISTS UQE_access_token_sha1")
|
||||
} else if models.DbCfg.Type == core.MSSQL {
|
||||
_, err = sess.Exec(`DECLARE @ConstraintName VARCHAR(256)
|
||||
DECLARE @SQL NVARCHAR(256)
|
||||
SELECT @ConstraintName = obj.name FROM sys.columns col LEFT OUTER JOIN sys.objects obj ON obj.object_id = col.default_object_id AND obj.type = 'D' WHERE col.object_id = OBJECT_ID('access_token') AND obj.name IS NOT NULL AND col.name = 'sha1'
|
||||
SET @SQL = N'ALTER TABLE [access_token] DROP CONSTRAINT [' + @ConstraintName + N']'
|
||||
EXEC sp_executesql @SQL`)
|
||||
} else if models.DbCfg.Type == core.MYSQL {
|
||||
indexes, err := sess.QueryString(`SHOW INDEX FROM access_token WHERE KEY_NAME = 'UQE_access_token_sha1'`)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(indexes) >= 1 {
|
||||
_, err = sess.Exec("DROP INDEX UQE_access_token_sha1 ON access_token")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
} else {
|
||||
_, err = sess.Exec("DROP INDEX UQE_access_token_sha1 ON access_token")
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("Drop index failed: %v", err)
|
||||
}
|
||||
|
||||
if err = sess.Commit(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := sess.Begin(); err != nil {
|
||||
return err
|
||||
@@ -81,7 +44,7 @@ func hashAppToken(x *xorm.Engine) error {
|
||||
return fmt.Errorf("Sync2: %v", err)
|
||||
}
|
||||
|
||||
if err = sess.Commit(); err != nil {
|
||||
if err := sess.Commit(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
@@ -4,22 +4,15 @@
|
||||
|
||||
package migrations
|
||||
|
||||
import (
|
||||
"github.com/go-xorm/xorm"
|
||||
)
|
||||
import "github.com/go-xorm/xorm"
|
||||
|
||||
func addTeamIncludesAllRepositories(x *xorm.Engine) error {
|
||||
|
||||
type Team struct {
|
||||
ID int64 `xorm:"pk autoincr"`
|
||||
IncludesAllRepositories bool `xorm:"NOT NULL DEFAULT false"`
|
||||
func changeSomeColumnsLengthOfRepo(x *xorm.Engine) error {
|
||||
type Repository struct {
|
||||
ID int64 `xorm:"pk autoincr"`
|
||||
Description string `xorm:"TEXT"`
|
||||
Website string `xorm:"VARCHAR(2048)"`
|
||||
OriginalURL string `xorm:"VARCHAR(2048)"`
|
||||
}
|
||||
|
||||
if err := x.Sync2(new(Team)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err := x.Exec("UPDATE `team` SET `includes_all_repositories` = ? WHERE `name`=?",
|
||||
true, "Owners")
|
||||
return err
|
||||
return x.Sync2(new(Repository))
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
// 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 "github.com/go-xorm/xorm"
|
||||
|
||||
func addIndexOnRepositoryAndComment(x *xorm.Engine) error {
|
||||
type Repository struct {
|
||||
ID int64 `xorm:"pk autoincr"`
|
||||
OwnerID int64 `xorm:"index"`
|
||||
}
|
||||
|
||||
if err := x.Sync2(new(Repository)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
type Comment struct {
|
||||
ID int64 `xorm:"pk autoincr"`
|
||||
Type int `xorm:"index"`
|
||||
ReviewID int64 `xorm:"index"`
|
||||
}
|
||||
|
||||
return x.Sync2(new(Comment))
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// 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 (
|
||||
"github.com/go-xorm/xorm"
|
||||
"xorm.io/builder"
|
||||
)
|
||||
|
||||
func removeLingeringIndexStatus(x *xorm.Engine) error {
|
||||
|
||||
_, err := x.Exec(builder.Delete(builder.NotIn("`repo_id`", builder.Select("`id`").From("`repository`"))).From("`repo_indexer_status`"))
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// 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 "github.com/go-xorm/xorm"
|
||||
|
||||
func addEmailNotificationEnabledToUser(x *xorm.Engine) error {
|
||||
// User see models/user.go
|
||||
type User struct {
|
||||
EmailNotificationsPreference string `xorm:"VARCHAR(20) NOT NULL DEFAULT 'enabled'"`
|
||||
}
|
||||
|
||||
return x.Sync2(new(User))
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
// 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 "github.com/go-xorm/xorm"
|
||||
|
||||
func addStatusCheckColumnsForProtectedBranches(x *xorm.Engine) error {
|
||||
type ProtectedBranch struct {
|
||||
EnableStatusCheck bool `xorm:"NOT NULL DEFAULT false"`
|
||||
StatusCheckContexts []string `xorm:"JSON TEXT"`
|
||||
}
|
||||
|
||||
if err := x.Sync2(new(ProtectedBranch)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err := x.Cols("enable_status_check", "status_check_contexts").Update(&ProtectedBranch{
|
||||
EnableStatusCheck: false,
|
||||
StatusCheckContexts: []string{},
|
||||
})
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
// 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 "github.com/go-xorm/xorm"
|
||||
|
||||
func addCrossReferenceColumns(x *xorm.Engine) error {
|
||||
// Comment see models/comment.go
|
||||
type Comment struct {
|
||||
RefRepoID int64 `xorm:"index"`
|
||||
RefIssueID int64 `xorm:"index"`
|
||||
RefCommentID int64 `xorm:"index"`
|
||||
RefAction int64 `xorm:"SMALLINT"`
|
||||
RefIsPull bool
|
||||
}
|
||||
|
||||
return x.Sync2(new(Comment))
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
// 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 (
|
||||
"os"
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
|
||||
"github.com/go-xorm/xorm"
|
||||
)
|
||||
|
||||
func deleteOrphanedAttachments(x *xorm.Engine) error {
|
||||
|
||||
type Attachment struct {
|
||||
ID int64 `xorm:"pk autoincr"`
|
||||
UUID string `xorm:"uuid UNIQUE"`
|
||||
IssueID int64 `xorm:"INDEX"`
|
||||
ReleaseID int64 `xorm:"INDEX"`
|
||||
CommentID int64
|
||||
}
|
||||
|
||||
sess := x.NewSession()
|
||||
defer sess.Close()
|
||||
|
||||
err := sess.BufferSize(setting.Database.IterateBufferSize).
|
||||
Where("`comment_id` = 0 and (`release_id` = 0 or `release_id` not in (select `id` from `release`))").Cols("uuid").
|
||||
Iterate(new(Attachment),
|
||||
func(idx int, bean interface{}) error {
|
||||
attachment := bean.(*Attachment)
|
||||
|
||||
if err := os.RemoveAll(models.AttachmentLocalPath(attachment.UUID)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err := sess.ID(attachment.ID).NoAutoCondition().Delete(attachment)
|
||||
return err
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return sess.Commit()
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
// 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 "github.com/go-xorm/xorm"
|
||||
|
||||
func addRepoAdminChangeTeamAccessColumnForUser(x *xorm.Engine) error {
|
||||
type User struct {
|
||||
RepoAdminChangeTeamAccess bool `xorm:"NOT NULL DEFAULT false"`
|
||||
}
|
||||
|
||||
return x.Sync2(new(User))
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
// 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 "github.com/go-xorm/xorm"
|
||||
|
||||
func addOriginalAuthorOnMigratedReleases(x *xorm.Engine) error {
|
||||
type Release struct {
|
||||
ID int64
|
||||
OriginalAuthor string
|
||||
OriginalAuthorID int64 `xorm:"index"`
|
||||
}
|
||||
|
||||
return x.Sync2(new(Release))
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
// 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 (
|
||||
"github.com/go-xorm/xorm"
|
||||
)
|
||||
|
||||
func addTeamIncludesAllRepositories(x *xorm.Engine) error {
|
||||
|
||||
type Team struct {
|
||||
ID int64 `xorm:"pk autoincr"`
|
||||
IncludesAllRepositories bool `xorm:"NOT NULL DEFAULT false"`
|
||||
}
|
||||
|
||||
if err := x.Sync2(new(Team)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err := x.Exec("UPDATE `team` SET `includes_all_repositories` = ? WHERE `name`=?",
|
||||
true, "Owners")
|
||||
return err
|
||||
}
|
||||
+18
-137
@@ -9,12 +9,6 @@ import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
|
||||
@@ -52,24 +46,11 @@ type Engine interface {
|
||||
}
|
||||
|
||||
var (
|
||||
x *xorm.Engine
|
||||
supportedDatabases = []string{"mysql", "postgres", "mssql"}
|
||||
tables []interface{}
|
||||
x *xorm.Engine
|
||||
tables []interface{}
|
||||
|
||||
// HasEngine specifies if we have a xorm.Engine
|
||||
HasEngine bool
|
||||
|
||||
// DbCfg holds the database settings
|
||||
DbCfg struct {
|
||||
Type, Host, Name, User, Passwd, Path, SSLMode, Charset string
|
||||
Timeout int
|
||||
}
|
||||
|
||||
// EnableSQLite3 use SQLite3
|
||||
EnableSQLite3 bool
|
||||
|
||||
// EnableTiDB enable TiDB
|
||||
EnableTiDB bool
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -139,120 +120,13 @@ func init() {
|
||||
}
|
||||
}
|
||||
|
||||
// LoadConfigs loads the database settings
|
||||
func LoadConfigs() {
|
||||
sec := setting.Cfg.Section("database")
|
||||
DbCfg.Type = sec.Key("DB_TYPE").String()
|
||||
switch DbCfg.Type {
|
||||
case "sqlite3":
|
||||
setting.UseSQLite3 = true
|
||||
case "mysql":
|
||||
setting.UseMySQL = true
|
||||
case "postgres":
|
||||
setting.UsePostgreSQL = true
|
||||
case "tidb":
|
||||
setting.UseTiDB = true
|
||||
case "mssql":
|
||||
setting.UseMSSQL = true
|
||||
}
|
||||
DbCfg.Host = sec.Key("HOST").String()
|
||||
DbCfg.Name = sec.Key("NAME").String()
|
||||
DbCfg.User = sec.Key("USER").String()
|
||||
if len(DbCfg.Passwd) == 0 {
|
||||
DbCfg.Passwd = sec.Key("PASSWD").String()
|
||||
}
|
||||
DbCfg.SSLMode = sec.Key("SSL_MODE").MustString("disable")
|
||||
DbCfg.Charset = sec.Key("CHARSET").In("utf8", []string{"utf8", "utf8mb4"})
|
||||
DbCfg.Path = sec.Key("PATH").MustString(filepath.Join(setting.AppDataPath, "gitea.db"))
|
||||
DbCfg.Timeout = sec.Key("SQLITE_TIMEOUT").MustInt(500)
|
||||
}
|
||||
|
||||
// parsePostgreSQLHostPort parses given input in various forms defined in
|
||||
// https://www.postgresql.org/docs/current/static/libpq-connect.html#LIBPQ-CONNSTRING
|
||||
// and returns proper host and port number.
|
||||
func parsePostgreSQLHostPort(info string) (string, string) {
|
||||
host, port := "127.0.0.1", "5432"
|
||||
if strings.Contains(info, ":") && !strings.HasSuffix(info, "]") {
|
||||
idx := strings.LastIndex(info, ":")
|
||||
host = info[:idx]
|
||||
port = info[idx+1:]
|
||||
} else if len(info) > 0 {
|
||||
host = info
|
||||
}
|
||||
return host, port
|
||||
}
|
||||
|
||||
func getPostgreSQLConnectionString(dbHost, dbUser, dbPasswd, dbName, dbParam, dbsslMode string) (connStr string) {
|
||||
host, port := parsePostgreSQLHostPort(dbHost)
|
||||
if host[0] == '/' { // looks like a unix socket
|
||||
connStr = fmt.Sprintf("postgres://%s:%s@:%s/%s%ssslmode=%s&host=%s",
|
||||
url.PathEscape(dbUser), url.PathEscape(dbPasswd), port, dbName, dbParam, dbsslMode, host)
|
||||
} else {
|
||||
connStr = fmt.Sprintf("postgres://%s:%s@%s:%s/%s%ssslmode=%s",
|
||||
url.PathEscape(dbUser), url.PathEscape(dbPasswd), host, port, dbName, dbParam, dbsslMode)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// ParseMSSQLHostPort splits the host into host and port
|
||||
func ParseMSSQLHostPort(info string) (string, string) {
|
||||
host, port := "127.0.0.1", "1433"
|
||||
if strings.Contains(info, ":") {
|
||||
host = strings.Split(info, ":")[0]
|
||||
port = strings.Split(info, ":")[1]
|
||||
} else if strings.Contains(info, ",") {
|
||||
host = strings.Split(info, ",")[0]
|
||||
port = strings.TrimSpace(strings.Split(info, ",")[1])
|
||||
} else if len(info) > 0 {
|
||||
host = info
|
||||
}
|
||||
return host, port
|
||||
}
|
||||
|
||||
func getEngine() (*xorm.Engine, error) {
|
||||
connStr := ""
|
||||
var Param = "?"
|
||||
if strings.Contains(DbCfg.Name, Param) {
|
||||
Param = "&"
|
||||
}
|
||||
switch DbCfg.Type {
|
||||
case "mysql":
|
||||
connType := "tcp"
|
||||
if DbCfg.Host[0] == '/' { // looks like a unix socket
|
||||
connType = "unix"
|
||||
}
|
||||
tls := DbCfg.SSLMode
|
||||
if tls == "disable" { // allow (Postgres-inspired) default value to work in MySQL
|
||||
tls = "false"
|
||||
}
|
||||
connStr = fmt.Sprintf("%s:%s@%s(%s)/%s%scharset=%s&parseTime=true&tls=%s",
|
||||
DbCfg.User, DbCfg.Passwd, connType, DbCfg.Host, DbCfg.Name, Param, DbCfg.Charset, tls)
|
||||
case "postgres":
|
||||
connStr = getPostgreSQLConnectionString(DbCfg.Host, DbCfg.User, DbCfg.Passwd, DbCfg.Name, Param, DbCfg.SSLMode)
|
||||
case "mssql":
|
||||
host, port := ParseMSSQLHostPort(DbCfg.Host)
|
||||
connStr = fmt.Sprintf("server=%s; port=%s; database=%s; user id=%s; password=%s;", host, port, DbCfg.Name, DbCfg.User, DbCfg.Passwd)
|
||||
case "sqlite3":
|
||||
if !EnableSQLite3 {
|
||||
return nil, errors.New("this binary version does not build support for SQLite3")
|
||||
}
|
||||
if err := os.MkdirAll(path.Dir(DbCfg.Path), os.ModePerm); err != nil {
|
||||
return nil, fmt.Errorf("Failed to create directories: %v", err)
|
||||
}
|
||||
connStr = fmt.Sprintf("file:%s?cache=shared&mode=rwc&_busy_timeout=%d", DbCfg.Path, DbCfg.Timeout)
|
||||
case "tidb":
|
||||
if !EnableTiDB {
|
||||
return nil, errors.New("this binary version does not build support for TiDB")
|
||||
}
|
||||
if err := os.MkdirAll(path.Dir(DbCfg.Path), os.ModePerm); err != nil {
|
||||
return nil, fmt.Errorf("Failed to create directories: %v", err)
|
||||
}
|
||||
connStr = "goleveldb://" + DbCfg.Path
|
||||
default:
|
||||
return nil, fmt.Errorf("Unknown database type: %s", DbCfg.Type)
|
||||
connStr, err := setting.DBConnStr()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return xorm.NewEngine(DbCfg.Type, connStr)
|
||||
return xorm.NewEngine(setting.Database.Type, connStr)
|
||||
}
|
||||
|
||||
// NewTestEngine sets a new test xorm.Engine
|
||||
@@ -262,6 +136,7 @@ func NewTestEngine(x *xorm.Engine) (err error) {
|
||||
return fmt.Errorf("Connect to database: %v", err)
|
||||
}
|
||||
|
||||
x.ShowExecTime(true)
|
||||
x.SetMapper(core.GonicMapper{})
|
||||
x.SetLogger(NewXORMLogger(!setting.ProdMode))
|
||||
x.ShowSQL(!setting.ProdMode)
|
||||
@@ -275,14 +150,15 @@ func SetEngine() (err error) {
|
||||
return fmt.Errorf("Failed to connect to database: %v", err)
|
||||
}
|
||||
|
||||
x.ShowExecTime(true)
|
||||
x.SetMapper(core.GonicMapper{})
|
||||
// WARNING: for serv command, MUST remove the output to os.stdout,
|
||||
// so use log file to instead print to stdout.
|
||||
x.SetLogger(NewXORMLogger(setting.LogSQL))
|
||||
x.ShowSQL(setting.LogSQL)
|
||||
if DbCfg.Type == "mysql" {
|
||||
x.SetMaxIdleConns(0)
|
||||
x.SetConnMaxLifetime(3 * time.Second)
|
||||
x.SetLogger(NewXORMLogger(setting.Database.LogSQL))
|
||||
x.ShowSQL(setting.Database.LogSQL)
|
||||
if setting.Database.UseMySQL {
|
||||
x.SetMaxIdleConns(setting.Database.MaxIdleConns)
|
||||
x.SetConnMaxLifetime(setting.Database.ConnMaxLifetime)
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -374,3 +250,8 @@ func MaxBatchInsertSize(bean interface{}) int {
|
||||
t := x.TableInfo(bean)
|
||||
return 999 / len(t.ColumnsSeq())
|
||||
}
|
||||
|
||||
// Count returns records number according struct's fields as database query conditions
|
||||
func Count(bean interface{}) (int64, error) {
|
||||
return x.Count(bean)
|
||||
}
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
// +build sqlite
|
||||
|
||||
// Copyright 2014 The Gogs 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 models
|
||||
|
||||
import (
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
func init() {
|
||||
EnableSQLite3 = true
|
||||
supportedDatabases = append(supportedDatabases, "sqlite3")
|
||||
}
|
||||
+5
-86
@@ -1,5 +1,4 @@
|
||||
// Copyright 2016 The Gogs Authors. All rights reserved.
|
||||
// Copyright 2018 The Gitea Authors. All rights reserved.
|
||||
// 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.
|
||||
|
||||
@@ -11,99 +10,19 @@ import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func Test_parsePostgreSQLHostPort(t *testing.T) {
|
||||
tests := []struct {
|
||||
HostPort string
|
||||
Host string
|
||||
Port string
|
||||
}{
|
||||
{
|
||||
HostPort: "127.0.0.1:1234",
|
||||
Host: "127.0.0.1",
|
||||
Port: "1234",
|
||||
},
|
||||
{
|
||||
HostPort: "127.0.0.1",
|
||||
Host: "127.0.0.1",
|
||||
Port: "5432",
|
||||
},
|
||||
{
|
||||
HostPort: "[::1]:1234",
|
||||
Host: "[::1]",
|
||||
Port: "1234",
|
||||
},
|
||||
{
|
||||
HostPort: "[::1]",
|
||||
Host: "[::1]",
|
||||
Port: "5432",
|
||||
},
|
||||
{
|
||||
HostPort: "/tmp/pg.sock:1234",
|
||||
Host: "/tmp/pg.sock",
|
||||
Port: "1234",
|
||||
},
|
||||
{
|
||||
HostPort: "/tmp/pg.sock",
|
||||
Host: "/tmp/pg.sock",
|
||||
Port: "5432",
|
||||
},
|
||||
}
|
||||
for _, test := range tests {
|
||||
host, port := parsePostgreSQLHostPort(test.HostPort)
|
||||
assert.Equal(t, test.Host, host)
|
||||
assert.Equal(t, test.Port, port)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_getPostgreSQLConnectionString(t *testing.T) {
|
||||
tests := []struct {
|
||||
Host string
|
||||
Port string
|
||||
User string
|
||||
Passwd string
|
||||
Name string
|
||||
Param string
|
||||
SSLMode string
|
||||
Output string
|
||||
}{
|
||||
{
|
||||
Host: "/tmp/pg.sock",
|
||||
Port: "4321",
|
||||
User: "testuser",
|
||||
Passwd: "space space !#$%^^%^```-=?=",
|
||||
Name: "gitea",
|
||||
Param: "",
|
||||
SSLMode: "false",
|
||||
Output: "postgres://testuser:space%20space%20%21%23$%25%5E%5E%25%5E%60%60%60-=%3F=@:5432/giteasslmode=false&host=/tmp/pg.sock",
|
||||
},
|
||||
{
|
||||
Host: "localhost",
|
||||
Port: "1234",
|
||||
User: "pgsqlusername",
|
||||
Passwd: "I love Gitea!",
|
||||
Name: "gitea",
|
||||
Param: "",
|
||||
SSLMode: "true",
|
||||
Output: "postgres://pgsqlusername:I%20love%20Gitea%21@localhost:5432/giteasslmode=true",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
connStr := getPostgreSQLConnectionString(test.Host, test.User, test.Passwd, test.Name, test.Param, test.SSLMode)
|
||||
assert.Equal(t, test.Output, connStr)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDumpDatabase(t *testing.T) {
|
||||
assert.NoError(t, PrepareTestDatabase())
|
||||
|
||||
dir, err := ioutil.TempDir(os.TempDir(), "dump")
|
||||
assert.NoError(t, err)
|
||||
|
||||
for _, dbType := range supportedDatabases {
|
||||
for _, dbName := range setting.SupportedDatabases {
|
||||
dbType := setting.GetDBTypeByName(dbName)
|
||||
assert.NoError(t, DumpDatabase(filepath.Join(dir, dbType+".sql"), dbType))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ package models
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
)
|
||||
|
||||
type (
|
||||
@@ -52,8 +52,8 @@ type Notification struct {
|
||||
Issue *Issue `xorm:"-"`
|
||||
Repository *Repository `xorm:"-"`
|
||||
|
||||
CreatedUnix util.TimeStamp `xorm:"created INDEX NOT NULL"`
|
||||
UpdatedUnix util.TimeStamp `xorm:"updated INDEX NOT NULL"`
|
||||
CreatedUnix timeutil.TimeStamp `xorm:"created INDEX NOT NULL"`
|
||||
UpdatedUnix timeutil.TimeStamp `xorm:"updated INDEX NOT NULL"`
|
||||
}
|
||||
|
||||
// CreateOrUpdateIssueNotifications creates an issue notification
|
||||
|
||||
+9
-1
@@ -40,10 +40,17 @@ var OAuth2Providers = map[string]OAuth2Provider{
|
||||
ProfileURL: oauth2.GetDefaultProfileURL("gitlab"),
|
||||
},
|
||||
},
|
||||
"gplus": {Name: "gplus", DisplayName: "Google+", Image: "/img/auth/google_plus.png"},
|
||||
"gplus": {Name: "gplus", DisplayName: "Google", Image: "/img/auth/google.png"},
|
||||
"openidConnect": {Name: "openidConnect", DisplayName: "OpenID Connect", Image: "/img/auth/openid_connect.png"},
|
||||
"twitter": {Name: "twitter", DisplayName: "Twitter", Image: "/img/auth/twitter.png"},
|
||||
"discord": {Name: "discord", DisplayName: "Discord", Image: "/img/auth/discord.png"},
|
||||
"gitea": {Name: "gitea", DisplayName: "Gitea", Image: "/img/auth/gitea.png",
|
||||
CustomURLMapping: &oauth2.CustomURLMapping{
|
||||
TokenURL: oauth2.GetDefaultTokenURL("gitea"),
|
||||
AuthURL: oauth2.GetDefaultAuthURL("gitea"),
|
||||
ProfileURL: oauth2.GetDefaultProfileURL("gitea"),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// OAuth2DefaultCustomURLMappings contains the map of default URL's for OAuth2 providers that are allowed to have custom urls
|
||||
@@ -52,6 +59,7 @@ var OAuth2Providers = map[string]OAuth2Provider{
|
||||
var OAuth2DefaultCustomURLMappings = map[string]*oauth2.CustomURLMapping{
|
||||
"github": OAuth2Providers["github"].CustomURLMapping,
|
||||
"gitlab": OAuth2Providers["gitlab"].CustomURLMapping,
|
||||
"gitea": OAuth2Providers["gitea"].CustomURLMapping,
|
||||
}
|
||||
|
||||
// GetActiveOAuth2ProviderLoginSources returns all actived LoginOAuth2 sources
|
||||
|
||||
@@ -11,15 +11,14 @@ import (
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/go-xorm/xorm"
|
||||
uuid "github.com/satori/go.uuid"
|
||||
|
||||
"code.gitea.io/gitea/modules/secret"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
|
||||
"github.com/Unknwon/com"
|
||||
"github.com/dgrijalva/jwt-go"
|
||||
"github.com/go-xorm/xorm"
|
||||
uuid "github.com/satori/go.uuid"
|
||||
"github.com/unknwon/com"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
@@ -36,8 +35,8 @@ type OAuth2Application struct {
|
||||
|
||||
RedirectURIs []string `xorm:"redirect_uris JSON TEXT"`
|
||||
|
||||
CreatedUnix util.TimeStamp `xorm:"INDEX created"`
|
||||
UpdatedUnix util.TimeStamp `xorm:"INDEX updated"`
|
||||
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
|
||||
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
|
||||
}
|
||||
|
||||
// TableName sets the table name to `oauth2_application`
|
||||
@@ -264,7 +263,7 @@ type OAuth2AuthorizationCode struct {
|
||||
CodeChallenge string
|
||||
CodeChallengeMethod string
|
||||
RedirectURI string
|
||||
ValidUntil util.TimeStamp `xorm:"index"`
|
||||
ValidUntil timeutil.TimeStamp `xorm:"index"`
|
||||
}
|
||||
|
||||
// TableName sets the table name to `oauth2_authorization_code`
|
||||
@@ -348,8 +347,8 @@ type OAuth2Grant struct {
|
||||
Application *OAuth2Application `xorm:"-"`
|
||||
ApplicationID int64 `xorm:"INDEX unique(user_application)"`
|
||||
Counter int64 `xorm:"NOT NULL DEFAULT 1"`
|
||||
CreatedUnix util.TimeStamp `xorm:"created"`
|
||||
UpdatedUnix util.TimeStamp `xorm:"updated"`
|
||||
CreatedUnix timeutil.TimeStamp `xorm:"created"`
|
||||
UpdatedUnix timeutil.TimeStamp `xorm:"updated"`
|
||||
}
|
||||
|
||||
// TableName sets the table name to `oauth2_grant`
|
||||
|
||||
+14
-17
@@ -6,24 +6,19 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
"code.gitea.io/gitea/modules/structs"
|
||||
|
||||
"github.com/Unknwon/com"
|
||||
"github.com/go-xorm/xorm"
|
||||
"github.com/unknwon/com"
|
||||
"xorm.io/builder"
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrTeamNotExist team does not exist
|
||||
ErrTeamNotExist = errors.New("Team does not exist")
|
||||
)
|
||||
|
||||
// IsOwnedBy returns true if given user is in the owner team.
|
||||
func (org *User) IsOwnedBy(uid int64) (bool, error) {
|
||||
return IsOrganizationOwner(org.ID, uid)
|
||||
@@ -75,9 +70,12 @@ func (org *User) GetMembers() error {
|
||||
}
|
||||
|
||||
var ids = make([]int64, len(ous))
|
||||
var idsIsPublic = make(map[int64]bool, len(ous))
|
||||
for i, ou := range ous {
|
||||
ids[i] = ou.UID
|
||||
idsIsPublic[ou.UID] = ou.IsPublic
|
||||
}
|
||||
org.MembersIsPublic = idsIsPublic
|
||||
org.Members, err = GetUsersByIDs(ids)
|
||||
return err
|
||||
}
|
||||
@@ -302,15 +300,13 @@ type OrgUser struct {
|
||||
}
|
||||
|
||||
func isOrganizationOwner(e Engine, orgID, uid int64) (bool, error) {
|
||||
ownerTeam := &Team{
|
||||
OrgID: orgID,
|
||||
Name: ownerTeamName,
|
||||
}
|
||||
if has, err := e.Get(ownerTeam); err != nil {
|
||||
ownerTeam, err := getOwnerTeam(e, orgID)
|
||||
if err != nil {
|
||||
if IsErrTeamNotExist(err) {
|
||||
log.Error("Organization does not have owner team: %d", orgID)
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
} else if !has {
|
||||
log.Error("Organization does not have owner team: %d", orgID)
|
||||
return false, nil
|
||||
}
|
||||
return isTeamMember(e, orgID, ownerTeam.ID, uid)
|
||||
}
|
||||
@@ -483,8 +479,9 @@ func AddOrgUser(orgID, uid int64) error {
|
||||
}
|
||||
|
||||
ou := &OrgUser{
|
||||
UID: uid,
|
||||
OrgID: orgID,
|
||||
UID: uid,
|
||||
OrgID: orgID,
|
||||
IsPublic: setting.Service.DefaultOrgMemberVisible,
|
||||
}
|
||||
|
||||
if _, err := sess.Insert(ou); err != nil {
|
||||
|
||||
+75
-5
@@ -15,6 +15,7 @@ import (
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
|
||||
"github.com/go-xorm/xorm"
|
||||
"xorm.io/builder"
|
||||
)
|
||||
|
||||
const ownerTeamName = "Owners"
|
||||
@@ -35,6 +36,67 @@ type Team struct {
|
||||
IncludesAllRepositories bool `xorm:"NOT NULL DEFAULT false"`
|
||||
}
|
||||
|
||||
// SearchTeamOptions holds the search options
|
||||
type SearchTeamOptions struct {
|
||||
UserID int64
|
||||
Keyword string
|
||||
OrgID int64
|
||||
IncludeDesc bool
|
||||
PageSize int
|
||||
Page int
|
||||
}
|
||||
|
||||
// SearchTeam search for teams. Caller is responsible to check permissions.
|
||||
func SearchTeam(opts *SearchTeamOptions) ([]*Team, int64, error) {
|
||||
if opts.Page <= 0 {
|
||||
opts.Page = 1
|
||||
}
|
||||
if opts.PageSize == 0 {
|
||||
// Default limit
|
||||
opts.PageSize = 10
|
||||
}
|
||||
|
||||
var cond = builder.NewCond()
|
||||
|
||||
if len(opts.Keyword) > 0 {
|
||||
lowerKeyword := strings.ToLower(opts.Keyword)
|
||||
var keywordCond builder.Cond = builder.Like{"lower_name", lowerKeyword}
|
||||
if opts.IncludeDesc {
|
||||
keywordCond = keywordCond.Or(builder.Like{"LOWER(description)", lowerKeyword})
|
||||
}
|
||||
cond = cond.And(keywordCond)
|
||||
}
|
||||
|
||||
cond = cond.And(builder.Eq{"org_id": opts.OrgID})
|
||||
|
||||
sess := x.NewSession()
|
||||
defer sess.Close()
|
||||
|
||||
count, err := sess.
|
||||
Where(cond).
|
||||
Count(new(Team))
|
||||
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
sess = sess.Where(cond)
|
||||
if opts.PageSize == -1 {
|
||||
opts.PageSize = int(count)
|
||||
} else {
|
||||
sess = sess.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize)
|
||||
}
|
||||
|
||||
teams := make([]*Team, 0, opts.PageSize)
|
||||
if err = sess.
|
||||
OrderBy("lower_name").
|
||||
Find(&teams); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return teams, count, nil
|
||||
}
|
||||
|
||||
// ColorFormat provides a basic color format for a Team
|
||||
func (t *Team) ColorFormat(s fmt.State) {
|
||||
log.ColorFprintf(s, "%d:%s (OrgID: %d) %-v",
|
||||
@@ -387,7 +449,7 @@ func getTeam(e Engine, orgID int64, name string) (*Team, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if !has {
|
||||
return nil, ErrTeamNotExist
|
||||
return nil, ErrTeamNotExist{orgID, 0, name}
|
||||
}
|
||||
return t, nil
|
||||
}
|
||||
@@ -397,13 +459,18 @@ func GetTeam(orgID int64, name string) (*Team, error) {
|
||||
return getTeam(x, orgID, name)
|
||||
}
|
||||
|
||||
// getOwnerTeam returns team by given team name and organization.
|
||||
func getOwnerTeam(e Engine, orgID int64) (*Team, error) {
|
||||
return getTeam(e, orgID, ownerTeamName)
|
||||
}
|
||||
|
||||
func getTeamByID(e Engine, teamID int64) (*Team, error) {
|
||||
t := new(Team)
|
||||
has, err := e.ID(teamID).Get(t)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if !has {
|
||||
return nil, ErrTeamNotExist
|
||||
return nil, ErrTeamNotExist{0, teamID, ""}
|
||||
}
|
||||
return t, nil
|
||||
}
|
||||
@@ -798,11 +865,14 @@ func IsUserInTeams(userID int64, teamIDs []int64) (bool, error) {
|
||||
}
|
||||
|
||||
// UsersInTeamsCount counts the number of users which are in userIDs and teamIDs
|
||||
func UsersInTeamsCount(userIDs []int64, teamIDs []int64) (count int64, err error) {
|
||||
if count, err = x.In("uid", userIDs).In("team_id", teamIDs).Count(new(TeamUser)); err != nil {
|
||||
func UsersInTeamsCount(userIDs []int64, teamIDs []int64) (int64, error) {
|
||||
var ids []int64
|
||||
if err := x.In("uid", userIDs).In("team_id", teamIDs).
|
||||
Table("team_user").
|
||||
Cols("uid").GroupBy("uid").Find(&ids); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return
|
||||
return int64(len(ids)), nil
|
||||
}
|
||||
|
||||
// ___________ __________
|
||||
|
||||
@@ -373,9 +373,9 @@ func TestUsersInTeamsCount(t *testing.T) {
|
||||
assert.Equal(t, expected, count)
|
||||
}
|
||||
|
||||
test([]int64{2}, []int64{1, 2, 3, 4}, 2)
|
||||
test([]int64{1, 2, 3, 4, 5}, []int64{2, 5}, 2)
|
||||
test([]int64{1, 2, 3, 4, 5}, []int64{2, 3, 5}, 3)
|
||||
test([]int64{2}, []int64{1, 2, 3, 4}, 1) // only userid 2
|
||||
test([]int64{1, 2, 3, 4, 5}, []int64{2, 5}, 2) // userid 2,4
|
||||
test([]int64{1, 2, 3, 4, 5}, []int64{2, 3, 5}, 3) // userid 2,4,5
|
||||
}
|
||||
|
||||
func TestIncludesAllRepositoriesTeams(t *testing.T) {
|
||||
|
||||
+17
-8
@@ -7,6 +7,7 @@ package models
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
"code.gitea.io/gitea/modules/structs"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -63,11 +64,11 @@ func TestUser_GetTeam(t *testing.T) {
|
||||
assert.Equal(t, "team1", team.LowerName)
|
||||
|
||||
_, err = org.GetTeam("does not exist")
|
||||
assert.Equal(t, ErrTeamNotExist, err)
|
||||
assert.True(t, IsErrTeamNotExist(err))
|
||||
|
||||
nonOrg := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
|
||||
_, err = nonOrg.GetTeam("team")
|
||||
assert.Equal(t, ErrTeamNotExist, err)
|
||||
assert.True(t, IsErrTeamNotExist(err))
|
||||
}
|
||||
|
||||
func TestUser_GetOwnerTeam(t *testing.T) {
|
||||
@@ -79,7 +80,7 @@ func TestUser_GetOwnerTeam(t *testing.T) {
|
||||
|
||||
nonOrg := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
|
||||
_, err = nonOrg.GetOwnerTeam()
|
||||
assert.Equal(t, ErrTeamNotExist, err)
|
||||
assert.True(t, IsErrTeamNotExist(err))
|
||||
}
|
||||
|
||||
func TestUser_GetTeams(t *testing.T) {
|
||||
@@ -429,20 +430,28 @@ func TestChangeOrgUserStatus(t *testing.T) {
|
||||
|
||||
func TestAddOrgUser(t *testing.T) {
|
||||
assert.NoError(t, PrepareTestDatabase())
|
||||
testSuccess := func(orgID, userID int64) {
|
||||
testSuccess := func(orgID, userID int64, isPublic bool) {
|
||||
org := AssertExistsAndLoadBean(t, &User{ID: orgID}).(*User)
|
||||
expectedNumMembers := org.NumMembers
|
||||
if !BeanExists(t, &OrgUser{OrgID: orgID, UID: userID}) {
|
||||
expectedNumMembers++
|
||||
}
|
||||
assert.NoError(t, AddOrgUser(orgID, userID))
|
||||
AssertExistsAndLoadBean(t, &OrgUser{OrgID: orgID, UID: userID})
|
||||
ou := &OrgUser{OrgID: orgID, UID: userID}
|
||||
AssertExistsAndLoadBean(t, ou)
|
||||
assert.Equal(t, ou.IsPublic, isPublic)
|
||||
org = AssertExistsAndLoadBean(t, &User{ID: orgID}).(*User)
|
||||
assert.EqualValues(t, expectedNumMembers, org.NumMembers)
|
||||
}
|
||||
testSuccess(3, 5)
|
||||
testSuccess(3, 5)
|
||||
testSuccess(6, 2)
|
||||
|
||||
setting.Service.DefaultOrgMemberVisible = false
|
||||
testSuccess(3, 5, false)
|
||||
testSuccess(3, 5, false)
|
||||
testSuccess(6, 2, false)
|
||||
|
||||
setting.Service.DefaultOrgMemberVisible = true
|
||||
testSuccess(6, 3, true)
|
||||
|
||||
CheckConsistencyFor(t, &User{}, &Team{})
|
||||
}
|
||||
|
||||
|
||||
+57
-46
@@ -23,10 +23,10 @@ import (
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
"code.gitea.io/gitea/modules/sync"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
|
||||
"github.com/Unknwon/com"
|
||||
"github.com/go-xorm/xorm"
|
||||
"github.com/unknwon/com"
|
||||
)
|
||||
|
||||
var pullRequestQueue = sync.NewUniqueQueue(setting.Repository.PullRequestQueueLength)
|
||||
@@ -72,11 +72,11 @@ type PullRequest struct {
|
||||
ProtectedBranch *ProtectedBranch `xorm:"-"`
|
||||
MergeBase string `xorm:"VARCHAR(40)"`
|
||||
|
||||
HasMerged bool `xorm:"INDEX"`
|
||||
MergedCommitID string `xorm:"VARCHAR(40)"`
|
||||
MergerID int64 `xorm:"INDEX"`
|
||||
Merger *User `xorm:"-"`
|
||||
MergedUnix util.TimeStamp `xorm:"updated INDEX"`
|
||||
HasMerged bool `xorm:"INDEX"`
|
||||
MergedCommitID string `xorm:"VARCHAR(40)"`
|
||||
MergerID int64 `xorm:"INDEX"`
|
||||
Merger *User `xorm:"-"`
|
||||
MergedUnix timeutil.TimeStamp `xorm:"updated INDEX"`
|
||||
}
|
||||
|
||||
// Note: don't try to get Issue because will end up recursive querying.
|
||||
@@ -99,6 +99,20 @@ func (pr *PullRequest) LoadAttributes() error {
|
||||
return pr.loadAttributes(x)
|
||||
}
|
||||
|
||||
// LoadBaseRepo loads pull request base repository from database
|
||||
func (pr *PullRequest) LoadBaseRepo() error {
|
||||
if pr.BaseRepo == nil {
|
||||
var repo Repository
|
||||
if has, err := x.ID(pr.BaseRepoID).Get(&repo); err != nil {
|
||||
return err
|
||||
} else if !has {
|
||||
return ErrRepoNotExist{ID: pr.BaseRepoID}
|
||||
}
|
||||
pr.BaseRepo = &repo
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// LoadIssue loads issue information from database
|
||||
func (pr *PullRequest) LoadIssue() (err error) {
|
||||
return pr.loadIssue(x)
|
||||
@@ -339,14 +353,17 @@ func (pr *PullRequest) GetLastCommitStatus() (status *CommitStatus, err error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
repo := pr.HeadRepo
|
||||
lastCommitID, err := headGitRepo.GetBranchCommitID(pr.HeadBranch)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var statusList []*CommitStatus
|
||||
statusList, err = GetLatestCommitStatus(repo, lastCommitID, 0)
|
||||
err = pr.LoadBaseRepo()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
statusList, err := GetLatestCommitStatus(pr.BaseRepo, lastCommitID, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -443,7 +460,7 @@ func (pr *PullRequest) manuallyMerged() bool {
|
||||
}
|
||||
if commit != nil {
|
||||
pr.MergedCommitID = commit.ID.String()
|
||||
pr.MergedUnix = util.TimeStamp(commit.Author.When.Unix())
|
||||
pr.MergedUnix = timeutil.TimeStamp(commit.Author.When.Unix())
|
||||
pr.Status = PullRequestStatusManuallyMerged
|
||||
merger, _ := GetUserByEmail(commit.Author.Email)
|
||||
|
||||
@@ -598,7 +615,7 @@ func (pr *PullRequest) testPatch(e Engine) (err error) {
|
||||
if err != nil {
|
||||
for i := range patchConflicts {
|
||||
if strings.Contains(stderr, patchConflicts[i]) {
|
||||
log.Trace("PullRequest[%d].testPatch (apply): has conflict", pr.ID)
|
||||
log.Trace("PullRequest[%d].testPatch (apply): has conflict: %s", pr.ID, stderr)
|
||||
const prefix = "error: patch failed:"
|
||||
pr.Status = PullRequestStatusConflict
|
||||
pr.ConflictedFiles = make([]string, 0, 5)
|
||||
@@ -640,6 +657,24 @@ func (pr *PullRequest) testPatch(e Engine) (err error) {
|
||||
|
||||
// NewPullRequest creates new pull request with labels for repository.
|
||||
func NewPullRequest(repo *Repository, pull *Issue, labelIDs []int64, uuids []string, pr *PullRequest, patch []byte, assigneeIDs []int64) (err error) {
|
||||
// Retry several times in case INSERT fails due to duplicate key for (repo_id, index); see #7887
|
||||
i := 0
|
||||
for {
|
||||
if err = newPullRequestAttempt(repo, pull, labelIDs, uuids, pr, patch, assigneeIDs); err == nil {
|
||||
return nil
|
||||
}
|
||||
if !IsErrNewIssueInsert(err) {
|
||||
return err
|
||||
}
|
||||
if i++; i == issueMaxDupIndexAttempts {
|
||||
break
|
||||
}
|
||||
log.Error("NewPullRequest: error attempting to insert the new issue; will retry. Original error: %v", err)
|
||||
}
|
||||
return fmt.Errorf("NewPullRequest: too many errors attempting to insert the new issue. Last error was: %v", err)
|
||||
}
|
||||
|
||||
func newPullRequestAttempt(repo *Repository, pull *Issue, labelIDs []int64, uuids []string, pr *PullRequest, patch []byte, assigneeIDs []int64) (err error) {
|
||||
sess := x.NewSession()
|
||||
defer sess.Close()
|
||||
if err = sess.Begin(); err != nil {
|
||||
@@ -654,20 +689,23 @@ func NewPullRequest(repo *Repository, pull *Issue, labelIDs []int64, uuids []str
|
||||
IsPull: true,
|
||||
AssigneeIDs: assigneeIDs,
|
||||
}); err != nil {
|
||||
if IsErrUserDoesNotHaveAccessToRepo(err) {
|
||||
if IsErrUserDoesNotHaveAccessToRepo(err) || IsErrNewIssueInsert(err) {
|
||||
return err
|
||||
}
|
||||
return fmt.Errorf("newIssue: %v", err)
|
||||
}
|
||||
|
||||
pr.Index = pull.Index
|
||||
if err = repo.savePatch(sess, pr.Index, patch); err != nil {
|
||||
return fmt.Errorf("SavePatch: %v", err)
|
||||
}
|
||||
|
||||
pr.BaseRepo = repo
|
||||
if err = pr.testPatch(sess); err != nil {
|
||||
return fmt.Errorf("testPatch: %v", err)
|
||||
pr.Status = PullRequestStatusChecking
|
||||
if len(patch) > 0 {
|
||||
if err = repo.savePatch(sess, pr.Index, patch); err != nil {
|
||||
return fmt.Errorf("SavePatch: %v", err)
|
||||
}
|
||||
|
||||
if err = pr.testPatch(sess); err != nil {
|
||||
return fmt.Errorf("testPatch: %v", err)
|
||||
}
|
||||
}
|
||||
// No conflict appears after test means mergeable.
|
||||
if pr.Status == PullRequestStatusChecking {
|
||||
@@ -683,33 +721,6 @@ func NewPullRequest(repo *Repository, pull *Issue, labelIDs []int64, uuids []str
|
||||
return fmt.Errorf("Commit: %v", err)
|
||||
}
|
||||
|
||||
if err = NotifyWatchers(&Action{
|
||||
ActUserID: pull.Poster.ID,
|
||||
ActUser: pull.Poster,
|
||||
OpType: ActionCreatePullRequest,
|
||||
Content: fmt.Sprintf("%d|%s", pull.Index, pull.Title),
|
||||
RepoID: repo.ID,
|
||||
Repo: repo,
|
||||
IsPrivate: repo.IsPrivate,
|
||||
}); err != nil {
|
||||
log.Error("NotifyWatchers: %v", err)
|
||||
}
|
||||
|
||||
pr.Issue = pull
|
||||
pull.PullRequest = pr
|
||||
mode, _ := AccessLevel(pull.Poster, repo)
|
||||
if err = PrepareWebhooks(repo, HookEventPullRequest, &api.PullRequestPayload{
|
||||
Action: api.HookIssueOpened,
|
||||
Index: pull.Index,
|
||||
PullRequest: pr.APIFormat(),
|
||||
Repository: repo.APIFormat(mode),
|
||||
Sender: pull.Poster.APIFormat(),
|
||||
}); err != nil {
|
||||
log.Error("PrepareWebhooks: %v", err)
|
||||
} else {
|
||||
go HookQueue.Add(repo.ID)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
+30
-180
@@ -1,4 +1,5 @@
|
||||
// Copyright 2014 The Gogs Authors. All rights reserved.
|
||||
// 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.
|
||||
|
||||
@@ -10,11 +11,9 @@ import (
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/process"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
|
||||
"xorm.io/builder"
|
||||
)
|
||||
@@ -27,18 +26,20 @@ type Release struct {
|
||||
PublisherID int64 `xorm:"INDEX"`
|
||||
Publisher *User `xorm:"-"`
|
||||
TagName string `xorm:"INDEX UNIQUE(n)"`
|
||||
OriginalAuthor string
|
||||
OriginalAuthorID int64 `xorm:"index"`
|
||||
LowerTagName string
|
||||
Target string
|
||||
Title string
|
||||
Sha1 string `xorm:"VARCHAR(40)"`
|
||||
NumCommits int64
|
||||
NumCommitsBehind int64 `xorm:"-"`
|
||||
Note string `xorm:"TEXT"`
|
||||
IsDraft bool `xorm:"NOT NULL DEFAULT false"`
|
||||
IsPrerelease bool `xorm:"NOT NULL DEFAULT false"`
|
||||
IsTag bool `xorm:"NOT NULL DEFAULT false"`
|
||||
Attachments []*Attachment `xorm:"-"`
|
||||
CreatedUnix util.TimeStamp `xorm:"INDEX"`
|
||||
NumCommitsBehind int64 `xorm:"-"`
|
||||
Note string `xorm:"TEXT"`
|
||||
IsDraft bool `xorm:"NOT NULL DEFAULT false"`
|
||||
IsPrerelease bool `xorm:"NOT NULL DEFAULT false"`
|
||||
IsTag bool `xorm:"NOT NULL DEFAULT false"`
|
||||
Attachments []*Attachment `xorm:"-"`
|
||||
CreatedUnix timeutil.TimeStamp `xorm:"INDEX"`
|
||||
}
|
||||
|
||||
func (r *Release) loadAttributes(e Engine) error {
|
||||
@@ -65,7 +66,7 @@ func (r *Release) LoadAttributes() error {
|
||||
|
||||
// APIURL the api url for a release. release must have attributes loaded
|
||||
func (r *Release) APIURL() string {
|
||||
return fmt.Sprintf("%sapi/v1/%s/releases/%d",
|
||||
return fmt.Sprintf("%sapi/v1/repos/%s/releases/%d",
|
||||
setting.AppURL, r.Repo.FullName(), r.ID)
|
||||
}
|
||||
|
||||
@@ -112,43 +113,20 @@ func IsReleaseExist(repoID int64, tagName string) (bool, error) {
|
||||
return x.Get(&Release{RepoID: repoID, LowerTagName: strings.ToLower(tagName)})
|
||||
}
|
||||
|
||||
func createTag(gitRepo *git.Repository, rel *Release) error {
|
||||
// Only actual create when publish.
|
||||
if !rel.IsDraft {
|
||||
if !gitRepo.IsTagExist(rel.TagName) {
|
||||
commit, err := gitRepo.GetCommit(rel.Target)
|
||||
if err != nil {
|
||||
return fmt.Errorf("GetCommit: %v", err)
|
||||
}
|
||||
|
||||
// Trim '--' prefix to prevent command line argument vulnerability.
|
||||
rel.TagName = strings.TrimPrefix(rel.TagName, "--")
|
||||
if err = gitRepo.CreateTag(rel.TagName, commit.ID.String()); err != nil {
|
||||
if strings.Contains(err.Error(), "is not a valid tag name") {
|
||||
return ErrInvalidTagName{rel.TagName}
|
||||
}
|
||||
return err
|
||||
}
|
||||
rel.LowerTagName = strings.ToLower(rel.TagName)
|
||||
}
|
||||
commit, err := gitRepo.GetTagCommit(rel.TagName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("GetTagCommit: %v", err)
|
||||
}
|
||||
|
||||
rel.Sha1 = commit.ID.String()
|
||||
rel.CreatedUnix = util.TimeStamp(commit.Author.When.Unix())
|
||||
rel.NumCommits, err = commit.CommitsCount()
|
||||
if err != nil {
|
||||
return fmt.Errorf("CommitsCount: %v", err)
|
||||
}
|
||||
} else {
|
||||
rel.CreatedUnix = util.TimeStampNow()
|
||||
}
|
||||
return nil
|
||||
// InsertRelease inserts a release
|
||||
func InsertRelease(rel *Release) error {
|
||||
_, err := x.Insert(rel)
|
||||
return err
|
||||
}
|
||||
|
||||
func addReleaseAttachments(releaseID int64, attachmentUUIDs []string) (err error) {
|
||||
// UpdateRelease updates all columns of a release
|
||||
func UpdateRelease(rel *Release) error {
|
||||
_, err := x.ID(rel.ID).AllCols().Update(rel)
|
||||
return err
|
||||
}
|
||||
|
||||
// AddReleaseAttachments adds a release attachments
|
||||
func AddReleaseAttachments(releaseID int64, attachmentUUIDs []string) (err error) {
|
||||
// Check attachments
|
||||
var attachments = make([]*Attachment, 0)
|
||||
for _, uuid := range attachmentUUIDs {
|
||||
@@ -173,51 +151,6 @@ func addReleaseAttachments(releaseID int64, attachmentUUIDs []string) (err error
|
||||
return
|
||||
}
|
||||
|
||||
// CreateRelease creates a new release of repository.
|
||||
func CreateRelease(gitRepo *git.Repository, rel *Release, attachmentUUIDs []string) error {
|
||||
isExist, err := IsReleaseExist(rel.RepoID, rel.TagName)
|
||||
if err != nil {
|
||||
return err
|
||||
} else if isExist {
|
||||
return ErrReleaseAlreadyExist{rel.TagName}
|
||||
}
|
||||
|
||||
if err = createTag(gitRepo, rel); err != nil {
|
||||
return err
|
||||
}
|
||||
rel.LowerTagName = strings.ToLower(rel.TagName)
|
||||
|
||||
_, err = x.InsertOne(rel)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = addReleaseAttachments(rel.ID, attachmentUUIDs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !rel.IsDraft {
|
||||
if err := rel.LoadAttributes(); err != nil {
|
||||
log.Error("LoadAttributes: %v", err)
|
||||
} else {
|
||||
mode, _ := AccessLevel(rel.Publisher, rel.Repo)
|
||||
if err := PrepareWebhooks(rel.Repo, HookEventRelease, &api.ReleasePayload{
|
||||
Action: api.HookReleasePublished,
|
||||
Release: rel.APIFormat(),
|
||||
Repository: rel.Repo.APIFormat(mode),
|
||||
Sender: rel.Publisher.APIFormat(),
|
||||
}); err != nil {
|
||||
log.Error("PrepareWebhooks: %v", err)
|
||||
} else {
|
||||
go HookQueue.Add(rel.Repo.ID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetRelease returns release by given ID.
|
||||
func GetRelease(repoID int64, tagName string) (*Release, error) {
|
||||
isExist, err := IsReleaseExist(repoID, tagName)
|
||||
@@ -385,95 +318,12 @@ func SortReleases(rels []*Release) {
|
||||
sort.Sort(sorter)
|
||||
}
|
||||
|
||||
// UpdateRelease updates information of a release.
|
||||
func UpdateRelease(doer *User, gitRepo *git.Repository, rel *Release, attachmentUUIDs []string) (err error) {
|
||||
if err = createTag(gitRepo, rel); err != nil {
|
||||
return err
|
||||
}
|
||||
rel.LowerTagName = strings.ToLower(rel.TagName)
|
||||
|
||||
_, err = x.ID(rel.ID).AllCols().Update(rel)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = rel.loadAttributes(x)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = addReleaseAttachments(rel.ID, attachmentUUIDs)
|
||||
|
||||
mode, _ := AccessLevel(doer, rel.Repo)
|
||||
if err1 := PrepareWebhooks(rel.Repo, HookEventRelease, &api.ReleasePayload{
|
||||
Action: api.HookReleaseUpdated,
|
||||
Release: rel.APIFormat(),
|
||||
Repository: rel.Repo.APIFormat(mode),
|
||||
Sender: rel.Publisher.APIFormat(),
|
||||
}); err1 != nil {
|
||||
log.Error("PrepareWebhooks: %v", err)
|
||||
} else {
|
||||
go HookQueue.Add(rel.Repo.ID)
|
||||
}
|
||||
|
||||
// DeleteReleaseByID deletes a release from database by given ID.
|
||||
func DeleteReleaseByID(id int64) error {
|
||||
_, err := x.ID(id).Delete(new(Release))
|
||||
return err
|
||||
}
|
||||
|
||||
// DeleteReleaseByID deletes a release and corresponding Git tag by given ID.
|
||||
func DeleteReleaseByID(id int64, u *User, delTag bool) error {
|
||||
rel, err := GetReleaseByID(id)
|
||||
if err != nil {
|
||||
return fmt.Errorf("GetReleaseByID: %v", err)
|
||||
}
|
||||
|
||||
repo, err := GetRepositoryByID(rel.RepoID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("GetRepositoryByID: %v", err)
|
||||
}
|
||||
|
||||
if delTag {
|
||||
_, stderr, err := process.GetManager().ExecDir(-1, repo.RepoPath(),
|
||||
fmt.Sprintf("DeleteReleaseByID (git tag -d): %d", rel.ID),
|
||||
git.GitExecutable, "tag", "-d", rel.TagName)
|
||||
if err != nil && !strings.Contains(stderr, "not found") {
|
||||
return fmt.Errorf("git tag -d: %v - %s", err, stderr)
|
||||
}
|
||||
|
||||
if _, err = x.ID(rel.ID).Delete(new(Release)); err != nil {
|
||||
return fmt.Errorf("Delete: %v", err)
|
||||
}
|
||||
} else {
|
||||
rel.IsTag = true
|
||||
rel.IsDraft = false
|
||||
rel.IsPrerelease = false
|
||||
rel.Title = ""
|
||||
rel.Note = ""
|
||||
|
||||
if _, err = x.ID(rel.ID).AllCols().Update(rel); err != nil {
|
||||
return fmt.Errorf("Update: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
rel.Repo = repo
|
||||
if err = rel.LoadAttributes(); err != nil {
|
||||
return fmt.Errorf("LoadAttributes: %v", err)
|
||||
}
|
||||
|
||||
mode, _ := AccessLevel(u, rel.Repo)
|
||||
if err := PrepareWebhooks(rel.Repo, HookEventRelease, &api.ReleasePayload{
|
||||
Action: api.HookReleaseDeleted,
|
||||
Release: rel.APIFormat(),
|
||||
Repository: rel.Repo.APIFormat(mode),
|
||||
Sender: rel.Publisher.APIFormat(),
|
||||
}); err != nil {
|
||||
log.Error("PrepareWebhooks: %v", err)
|
||||
} else {
|
||||
go HookQueue.Add(rel.Repo.ID)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SyncReleasesWithTags synchronizes release table with repository tags
|
||||
func SyncReleasesWithTags(repo *Repository, gitRepo *git.Repository) error {
|
||||
existingRelTags := make(map[string]struct{})
|
||||
@@ -495,8 +345,8 @@ func SyncReleasesWithTags(repo *Repository, gitRepo *git.Repository) error {
|
||||
return fmt.Errorf("GetTagCommitID: %v", err)
|
||||
}
|
||||
if git.IsErrNotExist(err) || commitID != rel.Sha1 {
|
||||
if err := pushUpdateDeleteTag(repo, rel.TagName); err != nil {
|
||||
return fmt.Errorf("pushUpdateDeleteTag: %v", err)
|
||||
if err := PushUpdateDeleteTag(repo, rel.TagName); err != nil {
|
||||
return fmt.Errorf("PushUpdateDeleteTag: %v", err)
|
||||
}
|
||||
} else {
|
||||
existingRelTags[strings.ToLower(rel.TagName)] = struct{}{}
|
||||
@@ -509,7 +359,7 @@ func SyncReleasesWithTags(repo *Repository, gitRepo *git.Repository) error {
|
||||
}
|
||||
for _, tagName := range tags {
|
||||
if _, ok := existingRelTags[strings.ToLower(tagName)]; !ok {
|
||||
if err := pushUpdateAddTag(repo, gitRepo, tagName); err != nil {
|
||||
if err := PushUpdateAddTag(repo, gitRepo, tagName); err != nil {
|
||||
return fmt.Errorf("pushUpdateAddTag: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,153 +0,0 @@
|
||||
// Copyright 2018 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 models
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestRelease_Create(t *testing.T) {
|
||||
assert.NoError(t, PrepareTestDatabase())
|
||||
|
||||
user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
|
||||
repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
|
||||
repoPath := RepoPath(user.Name, repo.Name)
|
||||
|
||||
gitRepo, err := git.OpenRepository(repoPath)
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.NoError(t, CreateRelease(gitRepo, &Release{
|
||||
RepoID: repo.ID,
|
||||
PublisherID: user.ID,
|
||||
TagName: "v0.1",
|
||||
Target: "master",
|
||||
Title: "v0.1 is released",
|
||||
Note: "v0.1 is released",
|
||||
IsDraft: false,
|
||||
IsPrerelease: false,
|
||||
IsTag: false,
|
||||
}, nil))
|
||||
|
||||
assert.NoError(t, CreateRelease(gitRepo, &Release{
|
||||
RepoID: repo.ID,
|
||||
PublisherID: user.ID,
|
||||
TagName: "v0.1.1",
|
||||
Target: "65f1bf27bc3bf70f64657658635e66094edbcb4d",
|
||||
Title: "v0.1.1 is released",
|
||||
Note: "v0.1.1 is released",
|
||||
IsDraft: false,
|
||||
IsPrerelease: false,
|
||||
IsTag: false,
|
||||
}, nil))
|
||||
|
||||
assert.NoError(t, CreateRelease(gitRepo, &Release{
|
||||
RepoID: repo.ID,
|
||||
PublisherID: user.ID,
|
||||
TagName: "v0.1.2",
|
||||
Target: "65f1bf2",
|
||||
Title: "v0.1.2 is released",
|
||||
Note: "v0.1.2 is released",
|
||||
IsDraft: false,
|
||||
IsPrerelease: false,
|
||||
IsTag: false,
|
||||
}, nil))
|
||||
|
||||
assert.NoError(t, CreateRelease(gitRepo, &Release{
|
||||
RepoID: repo.ID,
|
||||
PublisherID: user.ID,
|
||||
TagName: "v0.1.3",
|
||||
Target: "65f1bf2",
|
||||
Title: "v0.1.3 is released",
|
||||
Note: "v0.1.3 is released",
|
||||
IsDraft: true,
|
||||
IsPrerelease: false,
|
||||
IsTag: false,
|
||||
}, nil))
|
||||
|
||||
assert.NoError(t, CreateRelease(gitRepo, &Release{
|
||||
RepoID: repo.ID,
|
||||
PublisherID: user.ID,
|
||||
TagName: "v0.1.4",
|
||||
Target: "65f1bf2",
|
||||
Title: "v0.1.4 is released",
|
||||
Note: "v0.1.4 is released",
|
||||
IsDraft: false,
|
||||
IsPrerelease: true,
|
||||
IsTag: false,
|
||||
}, nil))
|
||||
|
||||
assert.NoError(t, CreateRelease(gitRepo, &Release{
|
||||
RepoID: repo.ID,
|
||||
PublisherID: user.ID,
|
||||
TagName: "v0.1.5",
|
||||
Target: "65f1bf2",
|
||||
Title: "v0.1.5 is released",
|
||||
Note: "v0.1.5 is released",
|
||||
IsDraft: false,
|
||||
IsPrerelease: false,
|
||||
IsTag: true,
|
||||
}, nil))
|
||||
}
|
||||
|
||||
func TestRelease_MirrorDelete(t *testing.T) {
|
||||
assert.NoError(t, PrepareTestDatabase())
|
||||
|
||||
user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
|
||||
repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
|
||||
repoPath := RepoPath(user.Name, repo.Name)
|
||||
migrationOptions := MigrateRepoOptions{
|
||||
Name: "test_mirror",
|
||||
Description: "Test mirror",
|
||||
IsPrivate: false,
|
||||
IsMirror: true,
|
||||
RemoteAddr: repoPath,
|
||||
Wiki: true,
|
||||
SyncReleasesWithTags: true,
|
||||
}
|
||||
mirror, err := MigrateRepository(user, user, migrationOptions)
|
||||
assert.NoError(t, err)
|
||||
|
||||
gitRepo, err := git.OpenRepository(repoPath)
|
||||
assert.NoError(t, err)
|
||||
|
||||
findOptions := FindReleasesOptions{IncludeDrafts: true, IncludeTags: true}
|
||||
initCount, err := GetReleaseCountByRepoID(mirror.ID, findOptions)
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.NoError(t, CreateRelease(gitRepo, &Release{
|
||||
RepoID: repo.ID,
|
||||
PublisherID: user.ID,
|
||||
TagName: "v0.2",
|
||||
Target: "master",
|
||||
Title: "v0.2 is released",
|
||||
Note: "v0.2 is released",
|
||||
IsDraft: false,
|
||||
IsPrerelease: false,
|
||||
IsTag: true,
|
||||
}, nil))
|
||||
|
||||
err = mirror.GetMirror()
|
||||
assert.NoError(t, err)
|
||||
|
||||
_, ok := mirror.Mirror.runSync()
|
||||
assert.True(t, ok)
|
||||
|
||||
count, err := GetReleaseCountByRepoID(mirror.ID, findOptions)
|
||||
assert.EqualValues(t, initCount+1, count)
|
||||
|
||||
release, err := GetRelease(repo.ID, "v0.2")
|
||||
assert.NoError(t, err)
|
||||
assert.NoError(t, DeleteReleaseByID(release.ID, user, true))
|
||||
|
||||
_, ok = mirror.Mirror.runSync()
|
||||
assert.True(t, ok)
|
||||
|
||||
count, err = GetReleaseCountByRepoID(mirror.ID, findOptions)
|
||||
assert.EqualValues(t, initCount, count)
|
||||
}
|
||||
+101
-45
@@ -34,10 +34,10 @@ import (
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
"code.gitea.io/gitea/modules/sync"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
|
||||
"github.com/Unknwon/com"
|
||||
"github.com/go-xorm/xorm"
|
||||
"github.com/unknwon/com"
|
||||
ini "gopkg.in/ini.v1"
|
||||
"xorm.io/builder"
|
||||
)
|
||||
@@ -129,14 +129,14 @@ func NewRepoContext() {
|
||||
// Repository represents a git repository.
|
||||
type Repository struct {
|
||||
ID int64 `xorm:"pk autoincr"`
|
||||
OwnerID int64 `xorm:"UNIQUE(s)"`
|
||||
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
|
||||
Website string
|
||||
OriginalURL string
|
||||
Description string `xorm:"TEXT"`
|
||||
Website string `xorm:"VARCHAR(2048)"`
|
||||
OriginalURL string `xorm:"VARCHAR(2048)"`
|
||||
DefaultBranch string
|
||||
|
||||
NumWatches int
|
||||
@@ -175,8 +175,8 @@ type Repository struct {
|
||||
// Avatar: ID(10-20)-md5(32) - must fit into 64 symbols
|
||||
Avatar string `xorm:"VARCHAR(64)"`
|
||||
|
||||
CreatedUnix util.TimeStamp `xorm:"INDEX created"`
|
||||
UpdatedUnix util.TimeStamp `xorm:"INDEX updated"`
|
||||
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
|
||||
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
|
||||
}
|
||||
|
||||
// ColorFormat returns a colored string to represent this repo
|
||||
@@ -275,12 +275,35 @@ func (repo *Repository) innerAPIFormat(e Engine, mode AccessMode, isParent bool)
|
||||
}
|
||||
}
|
||||
hasIssues := false
|
||||
if _, err := repo.getUnit(e, UnitTypeIssues); err == nil {
|
||||
var externalTracker *api.ExternalTracker
|
||||
var internalTracker *api.InternalTracker
|
||||
if unit, err := repo.getUnit(e, UnitTypeIssues); err == nil {
|
||||
config := unit.IssuesConfig()
|
||||
hasIssues = true
|
||||
internalTracker = &api.InternalTracker{
|
||||
EnableTimeTracker: config.EnableTimetracker,
|
||||
AllowOnlyContributorsToTrackTime: config.AllowOnlyContributorsToTrackTime,
|
||||
EnableIssueDependencies: config.EnableDependencies,
|
||||
}
|
||||
} else if unit, err := repo.getUnit(e, UnitTypeExternalTracker); err == nil {
|
||||
config := unit.ExternalTrackerConfig()
|
||||
hasIssues = true
|
||||
externalTracker = &api.ExternalTracker{
|
||||
ExternalTrackerURL: config.ExternalTrackerURL,
|
||||
ExternalTrackerFormat: config.ExternalTrackerFormat,
|
||||
ExternalTrackerStyle: config.ExternalTrackerStyle,
|
||||
}
|
||||
}
|
||||
hasWiki := false
|
||||
var externalWiki *api.ExternalWiki
|
||||
if _, err := repo.getUnit(e, UnitTypeWiki); err == nil {
|
||||
hasWiki = true
|
||||
} else if unit, err := repo.getUnit(e, UnitTypeExternalWiki); err == nil {
|
||||
hasWiki = true
|
||||
config := unit.ExternalWikiConfig()
|
||||
externalWiki = &api.ExternalWiki{
|
||||
ExternalWikiURL: config.ExternalWikiURL,
|
||||
}
|
||||
}
|
||||
hasPullRequests := false
|
||||
ignoreWhitespaceConflicts := false
|
||||
@@ -324,7 +347,10 @@ func (repo *Repository) innerAPIFormat(e Engine, mode AccessMode, isParent bool)
|
||||
Updated: repo.UpdatedUnix.AsTime(),
|
||||
Permissions: permission,
|
||||
HasIssues: hasIssues,
|
||||
ExternalTracker: externalTracker,
|
||||
InternalTracker: internalTracker,
|
||||
HasWiki: hasWiki,
|
||||
ExternalWiki: externalWiki,
|
||||
HasPullRequests: hasPullRequests,
|
||||
IgnoreWhitespaceConflicts: ignoreWhitespaceConflicts,
|
||||
AllowMerge: allowMerge,
|
||||
@@ -508,8 +534,9 @@ func (repo *Repository) mustOwnerName(e Engine) string {
|
||||
func (repo *Repository) ComposeMetas() map[string]string {
|
||||
if repo.ExternalMetas == nil {
|
||||
repo.ExternalMetas = map[string]string{
|
||||
"user": repo.MustOwner().Name,
|
||||
"repo": repo.Name,
|
||||
"user": repo.MustOwner().Name,
|
||||
"repo": repo.Name,
|
||||
"repoPath": repo.RepoPath(),
|
||||
}
|
||||
unit, err := repo.GetUnit(UnitTypeExternalTracker)
|
||||
if err != nil {
|
||||
@@ -970,7 +997,7 @@ func MigrateRepository(doer, u *User, opts MigrateRepoOptions) (*Repository, err
|
||||
RepoID: repo.ID,
|
||||
Interval: setting.Mirror.DefaultInterval,
|
||||
EnablePrune: true,
|
||||
NextUpdateUnix: util.TimeStampNow().AddDuration(setting.Mirror.DefaultInterval),
|
||||
NextUpdateUnix: timeutil.TimeStampNow().AddDuration(setting.Mirror.DefaultInterval),
|
||||
}); err != nil {
|
||||
return repo, fmt.Errorf("InsertOne: %v", err)
|
||||
}
|
||||
@@ -1097,6 +1124,7 @@ type CreateRepoOptions struct {
|
||||
Description string
|
||||
OriginalURL string
|
||||
Gitignores string
|
||||
IssueLabels string
|
||||
License string
|
||||
Readme string
|
||||
IsPrivate bool
|
||||
@@ -1306,13 +1334,17 @@ func createRepository(e *xorm.Session, doer, u *User, repo *Repository) (err err
|
||||
return err
|
||||
}
|
||||
|
||||
u.NumRepos++
|
||||
// Remember visibility preference.
|
||||
u.LastRepoVisibility = repo.IsPrivate
|
||||
if err = updateUser(e, u); err != nil {
|
||||
if err = updateUserCols(e, u, "last_repo_visibility"); err != nil {
|
||||
return fmt.Errorf("updateUser: %v", err)
|
||||
}
|
||||
|
||||
if _, err = e.Incr("num_repos").ID(u.ID).Update(new(User)); err != nil {
|
||||
return fmt.Errorf("increment user total_repos: %v", err)
|
||||
}
|
||||
u.NumRepos++
|
||||
|
||||
// Give access to all members in teams with access to all repositories.
|
||||
if u.IsOrganization() {
|
||||
if err := u.GetTeams(); err != nil {
|
||||
@@ -1333,7 +1365,6 @@ func createRepository(e *xorm.Session, doer, u *User, repo *Repository) (err err
|
||||
}); err != nil {
|
||||
return fmt.Errorf("prepareWebhooks: %v", err)
|
||||
}
|
||||
go HookQueue.Add(repo.ID)
|
||||
} else if err = repo.recalculateAccesses(e); err != nil {
|
||||
// Organization automatically called this in addRepository method.
|
||||
return fmt.Errorf("recalculateAccesses: %v", err)
|
||||
@@ -1395,6 +1426,13 @@ func CreateRepository(doer, u *User, opts CreateRepoOptions) (_ *Repository, err
|
||||
return nil, fmt.Errorf("initRepository: %v", err)
|
||||
}
|
||||
|
||||
// Initialize Issue Labels if selected
|
||||
if len(opts.IssueLabels) > 0 {
|
||||
if err = initalizeLabels(sess, repo.ID, opts.IssueLabels); err != nil {
|
||||
return nil, fmt.Errorf("initalizeLabels: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
_, stderr, err := process.GetManager().ExecDir(-1,
|
||||
repoPath, fmt.Sprintf("CreateRepository(git update-server-info): %s", repoPath),
|
||||
git.GitExecutable, "update-server-info")
|
||||
@@ -1403,7 +1441,16 @@ func CreateRepository(doer, u *User, opts CreateRepoOptions) (_ *Repository, err
|
||||
}
|
||||
}
|
||||
|
||||
return repo, sess.Commit()
|
||||
if err = sess.Commit(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Add to hook queue for created repo after session commit.
|
||||
if u.IsOrganization() {
|
||||
go HookQueue.Add(repo.ID)
|
||||
}
|
||||
|
||||
return repo, err
|
||||
}
|
||||
|
||||
func countRepositories(userID int64, private bool) int64 {
|
||||
@@ -1708,6 +1755,12 @@ func UpdateRepository(repo *Repository, visibilityChanged bool) (err error) {
|
||||
return sess.Commit()
|
||||
}
|
||||
|
||||
// UpdateRepositoryUpdatedTime updates a repository's updated time
|
||||
func UpdateRepositoryUpdatedTime(repoID int64, updateTime time.Time) error {
|
||||
_, err := x.Exec("UPDATE repository SET updated_unix = ? WHERE id = ?", updateTime.Unix(), repoID)
|
||||
return err
|
||||
}
|
||||
|
||||
// UpdateRepositoryUnits updates a repository's units
|
||||
func UpdateRepositoryUnits(repo *Repository, units []RepoUnit) (err error) {
|
||||
sess := x.NewSession()
|
||||
@@ -1797,6 +1850,8 @@ func DeleteRepository(doer *User, uid, repoID int64) error {
|
||||
&HookTask{RepoID: repoID},
|
||||
&Notification{RepoID: repoID},
|
||||
&CommitStatus{RepoID: repoID},
|
||||
&RepoIndexerStatus{RepoID: repoID},
|
||||
&Comment{RefRepoID: repoID},
|
||||
); err != nil {
|
||||
return fmt.Errorf("deleteBeans: %v", err)
|
||||
}
|
||||
@@ -1943,8 +1998,12 @@ func DeleteRepository(doer *User, uid, repoID int64) error {
|
||||
|
||||
// GetRepositoryByOwnerAndName returns the repository by given ownername and reponame.
|
||||
func GetRepositoryByOwnerAndName(ownerName, repoName string) (*Repository, error) {
|
||||
return getRepositoryByOwnerAndName(x, ownerName, repoName)
|
||||
}
|
||||
|
||||
func getRepositoryByOwnerAndName(e Engine, ownerName, repoName string) (*Repository, error) {
|
||||
var repo Repository
|
||||
has, err := x.Select("repository.*").
|
||||
has, err := e.Table("repository").Select("repository.*").
|
||||
Join("INNER", "`user`", "`user`.id = repository.owner_id").
|
||||
Where("repository.lower_name = ?", strings.ToLower(repoName)).
|
||||
And("`user`.lower_name = ?", strings.ToLower(ownerName)).
|
||||
@@ -2065,11 +2124,6 @@ func DeleteRepositoryArchives() error {
|
||||
|
||||
// DeleteOldRepositoryArchives deletes old repository archives.
|
||||
func DeleteOldRepositoryArchives() {
|
||||
if !taskStatusTable.StartIfNotRunning(archiveCleanup) {
|
||||
return
|
||||
}
|
||||
defer taskStatusTable.Stop(archiveCleanup)
|
||||
|
||||
log.Trace("Doing: ArchiveCleanup")
|
||||
|
||||
if err := x.Where("id > 0").Iterate(new(Repository), deleteOldRepositoryArchives); err != nil {
|
||||
@@ -2196,27 +2250,12 @@ func SyncRepositoryHooks() error {
|
||||
})
|
||||
}
|
||||
|
||||
// Prevent duplicate running tasks.
|
||||
var taskStatusTable = sync.NewStatusTable()
|
||||
|
||||
const (
|
||||
mirrorUpdate = "mirror_update"
|
||||
gitFsck = "git_fsck"
|
||||
checkRepos = "check_repos"
|
||||
archiveCleanup = "archive_cleanup"
|
||||
)
|
||||
|
||||
// GitFsck calls 'git fsck' to check repository health.
|
||||
func GitFsck() {
|
||||
if !taskStatusTable.StartIfNotRunning(gitFsck) {
|
||||
return
|
||||
}
|
||||
defer taskStatusTable.Stop(gitFsck)
|
||||
|
||||
log.Trace("Doing: GitFsck")
|
||||
|
||||
if err := x.
|
||||
Where("id>0 AND is_fsck_enabled=?", true).BufferSize(setting.IterateBufferSize).
|
||||
Where("id>0 AND is_fsck_enabled=?", true).BufferSize(setting.Database.IterateBufferSize).
|
||||
Iterate(new(Repository),
|
||||
func(idx int, bean interface{}) error {
|
||||
repo := bean.(*Repository)
|
||||
@@ -2240,7 +2279,7 @@ func GitFsck() {
|
||||
func GitGcRepos() error {
|
||||
args := append([]string{"gc"}, setting.Git.GCArgs...)
|
||||
return x.
|
||||
Where("id > 0").BufferSize(setting.IterateBufferSize).
|
||||
Where("id > 0").BufferSize(setting.Database.IterateBufferSize).
|
||||
Iterate(new(Repository),
|
||||
func(idx int, bean interface{}) error {
|
||||
repo := bean.(*Repository)
|
||||
@@ -2281,11 +2320,6 @@ func repoStatsCheck(checker *repoChecker) {
|
||||
|
||||
// CheckRepoStats checks the repository stats
|
||||
func CheckRepoStats() {
|
||||
if !taskStatusTable.StartIfNotRunning(checkRepos) {
|
||||
return
|
||||
}
|
||||
defer taskStatusTable.Stop(checkRepos)
|
||||
|
||||
log.Trace("Doing: CheckRepoStats")
|
||||
|
||||
checkers := []*repoChecker{
|
||||
@@ -2341,6 +2375,23 @@ func CheckRepoStats() {
|
||||
}
|
||||
// ***** END: Repository.NumClosedIssues *****
|
||||
|
||||
// ***** START: Repository.NumClosedPulls *****
|
||||
desc = "repository count 'num_closed_pulls'"
|
||||
results, err = x.Query("SELECT repo.id FROM `repository` repo WHERE repo.num_closed_pulls!=(SELECT COUNT(*) FROM `issue` WHERE repo_id=repo.id AND is_closed=? AND is_pull=?)", true, true)
|
||||
if err != nil {
|
||||
log.Error("Select %s: %v", desc, err)
|
||||
} else {
|
||||
for _, result := range results {
|
||||
id := com.StrTo(result["id"]).MustInt64()
|
||||
log.Trace("Updating %s: %d", desc, id)
|
||||
_, err = x.Exec("UPDATE `repository` SET num_closed_pulls=(SELECT COUNT(*) FROM `issue` WHERE repo_id=? AND is_closed=? AND is_pull=?) WHERE id=?", id, true, true, id)
|
||||
if err != nil {
|
||||
log.Error("Update %s[%d]: %v", desc, id, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
// ***** END: Repository.NumClosedPulls *****
|
||||
|
||||
// FIXME: use checker when stop supporting old fork repo format.
|
||||
// ***** START: Repository.NumForks *****
|
||||
results, err = x.Query("SELECT repo.id FROM `repository` repo WHERE repo.num_forks!=(SELECT COUNT(*) FROM `repository` WHERE fork_id=repo.id)")
|
||||
@@ -2475,6 +2526,11 @@ func ForkRepository(doer, u *User, oldRepo *Repository, name, desc string) (_ *R
|
||||
go HookQueue.Add(oldRepo.ID)
|
||||
}
|
||||
|
||||
// Add to hook queue for created repo after session commit.
|
||||
if u.IsOrganization() {
|
||||
go HookQueue.Add(repo.ID)
|
||||
}
|
||||
|
||||
if err = repo.UpdateSize(); err != nil {
|
||||
log.Error("Failed to update size for repository: %v", err)
|
||||
}
|
||||
@@ -2566,7 +2622,7 @@ func (repo *Repository) generateRandomAvatar(e Engine) error {
|
||||
// RemoveRandomAvatars removes the randomly generated avatars that were created for repositories
|
||||
func RemoveRandomAvatars() error {
|
||||
return x.
|
||||
Where("id > 0").BufferSize(setting.IterateBufferSize).
|
||||
Where("id > 0").BufferSize(setting.Database.IterateBufferSize).
|
||||
Iterate(new(Repository),
|
||||
func(idx int, bean interface{}) error {
|
||||
repository := bean.(*Repository)
|
||||
|
||||
@@ -16,20 +16,6 @@ type Collaboration struct {
|
||||
Mode AccessMode `xorm:"DEFAULT 2 NOT NULL"`
|
||||
}
|
||||
|
||||
// ModeI18nKey returns the collaboration mode I18n Key
|
||||
func (c *Collaboration) ModeI18nKey() string {
|
||||
switch c.Mode {
|
||||
case AccessModeRead:
|
||||
return "repo.settings.collaboration.read"
|
||||
case AccessModeWrite:
|
||||
return "repo.settings.collaboration.write"
|
||||
case AccessModeAdmin:
|
||||
return "repo.settings.collaboration.admin"
|
||||
default:
|
||||
return "repo.settings.collaboration.undefined"
|
||||
}
|
||||
}
|
||||
|
||||
// AddCollaborator adds new collaboration to a repository with default access mode.
|
||||
func (repo *Repository) AddCollaborator(u *User) error {
|
||||
collaboration := &Collaboration{
|
||||
@@ -183,3 +169,17 @@ func (repo *Repository) DeleteCollaboration(uid int64) (err error) {
|
||||
|
||||
return sess.Commit()
|
||||
}
|
||||
|
||||
func (repo *Repository) getRepoTeams(e Engine) (teams []*Team, err error) {
|
||||
return teams, e.
|
||||
Join("INNER", "team_repo", "team_repo.team_id = team.id").
|
||||
Where("team.org_id = ?", repo.OwnerID).
|
||||
And("team_repo.repo_id=?", repo.ID).
|
||||
OrderBy("CASE WHEN name LIKE '" + ownerTeamName + "' THEN '' ELSE name END").
|
||||
Find(&teams)
|
||||
}
|
||||
|
||||
// GetRepoTeams gets the list of teams that has access to the repository
|
||||
func (repo *Repository) GetRepoTeams() ([]*Team, error) {
|
||||
return repo.getRepoTeams(x)
|
||||
}
|
||||
|
||||
@@ -10,17 +10,6 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestCollaboration_ModeI18nKey(t *testing.T) {
|
||||
assert.Equal(t, "repo.settings.collaboration.read",
|
||||
(&Collaboration{Mode: AccessModeRead}).ModeI18nKey())
|
||||
assert.Equal(t, "repo.settings.collaboration.write",
|
||||
(&Collaboration{Mode: AccessModeWrite}).ModeI18nKey())
|
||||
assert.Equal(t, "repo.settings.collaboration.admin",
|
||||
(&Collaboration{Mode: AccessModeAdmin}).ModeI18nKey())
|
||||
assert.Equal(t, "repo.settings.collaboration.undefined",
|
||||
(&Collaboration{Mode: AccessModeNone}).ModeI18nKey())
|
||||
}
|
||||
|
||||
func TestRepository_AddCollaborator(t *testing.T) {
|
||||
assert.NoError(t, PrepareTestDatabase())
|
||||
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user