Skip to content

Commit c228842

Browse files
committed
Merge branch 'master' of github.com:go-gitea/gitea into delete_releases_attachments_if_release_is_deleted
2 parents c1908f4 + 3c0e6d1 commit c228842

File tree

25 files changed

+519
-194
lines changed

25 files changed

+519
-194
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ Values containing `#` or `;` must be quoted using `` ` `` or `"""`.
293293
- `HOST`: **\<empty\>**: Connection string for `redis` and `memcache`.
294294
- Redis: `network=tcp,addr=127.0.0.1:6379,password=macaron,db=0,pool_size=100,idle_timeout=180`
295295
- Memache: `127.0.0.1:9090;127.0.0.1:9091`
296+
- `ITEM_TTL`: **16h**: Time to keep items in cache if not used, Setting it to 0 disables caching.
296297

297298
## Session (`session`)
298299

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,12 @@ menu:
144144

145145
## Cache (`cache`)
146146

147-
- `ADAPTER`: 缓存引擎,可以为 `memory`, `redis``memcache`
148-
- `INTERVAL`: 只对内存缓存有效,GC间隔,单位秒。
149-
- `HOST`: 针对redis和memcache有效,主机地址和端口。
147+
- `ADAPTER`: **memory**: 缓存引擎,可以为 `memory`, `redis``memcache`
148+
- `INTERVAL`: **60**: 只对内存缓存有效,GC间隔,单位秒。
149+
- `HOST`: **\<empty\>**: 针对redis和memcache有效,主机地址和端口。
150150
- Redis: `network=tcp,addr=127.0.0.1:6379,password=macaron,db=0,pool_size=100,idle_timeout=180`
151151
- Memache: `127.0.0.1:9090;127.0.0.1:9091`
152+
- `ITEM_TTL`: **16h**: 缓存项目失效时间,设置为 0 则禁用缓存。
152153

153154
## Session (`session`)
154155

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ require (
8080
github.com/nfnt/resize v0.0.0-20160724205520-891127d8d1b5
8181
github.com/oliamb/cutter v0.2.2
8282
github.com/philhofer/fwd v1.0.0 // indirect
83+
github.com/pkg/errors v0.8.1
8384
github.com/pquerna/otp v0.0.0-20160912161815-54653902c20e
8485
github.com/prometheus/client_golang v1.1.0
8586
github.com/prometheus/procfs v0.0.4 // indirect

models/branches.go

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ type ProtectedBranch struct {
3636
EnableMergeWhitelist bool `xorm:"NOT NULL DEFAULT false"`
3737
MergeWhitelistUserIDs []int64 `xorm:"JSON TEXT"`
3838
MergeWhitelistTeamIDs []int64 `xorm:"JSON TEXT"`
39+
EnableStatusCheck bool `xorm:"NOT NULL DEFAULT false"`
40+
StatusCheckContexts []string `xorm:"JSON TEXT"`
3941
ApprovalsWhitelistUserIDs []int64 `xorm:"JSON TEXT"`
4042
ApprovalsWhitelistTeamIDs []int64 `xorm:"JSON TEXT"`
4143
RequiredApprovals int64 `xorm:"NOT NULL DEFAULT 0"`

models/commit_status.go

+22
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"crypto/sha1"
1010
"fmt"
1111
"strings"
12+
"time"
1213

1314
"code.gitea.io/gitea/modules/log"
1415
"code.gitea.io/gitea/modules/setting"
@@ -205,6 +206,27 @@ func GetLatestCommitStatus(repo *Repository, sha string, page int) ([]*CommitSta
205206
return statuses, x.In("id", ids).Find(&statuses)
206207
}
207208

209+
// FindRepoRecentCommitStatusContexts returns repository's recent commit status contexts
210+
func FindRepoRecentCommitStatusContexts(repoID int64, before time.Duration) ([]string, error) {
211+
start := timeutil.TimeStampNow().AddDuration(-before)
212+
ids := make([]int64, 0, 10)
213+
if err := x.Table("commit_status").
214+
Where("repo_id = ?", repoID).
215+
And("updated_unix >= ?", start).
216+
Select("max( id ) as id").
217+
GroupBy("context_hash").OrderBy("max( id ) desc").
218+
Find(&ids); err != nil {
219+
return nil, err
220+
}
221+
222+
var contexts = make([]string, 0, len(ids))
223+
if len(ids) == 0 {
224+
return contexts, nil
225+
}
226+
return contexts, x.Select("context").Table("commit_status").In("id", ids).Find(&contexts)
227+
228+
}
229+
208230
// NewCommitStatusOptions holds options for creating a CommitStatus
209231
type NewCommitStatusOptions struct {
210232
Repo *Repository

models/issue.go

+23
Original file line numberDiff line numberDiff line change
@@ -1682,6 +1682,21 @@ func GetUserIssueStats(opts UserIssueStatsOptions) (*IssueStats, error) {
16821682
if err != nil {
16831683
return nil, err
16841684
}
1685+
case FilterModeMention:
1686+
stats.OpenCount, err = x.Where(cond).And("is_closed = ?", false).
1687+
Join("INNER", "issue_user", "issue.id = issue_user.issue_id and issue_user.is_mentioned = ?", true).
1688+
And("issue_user.uid = ?", opts.UserID).
1689+
Count(new(Issue))
1690+
if err != nil {
1691+
return nil, err
1692+
}
1693+
stats.ClosedCount, err = x.Where(cond).And("is_closed = ?", true).
1694+
Join("INNER", "issue_user", "issue.id = issue_user.issue_id and issue_user.is_mentioned = ?", true).
1695+
And("issue_user.uid = ?", opts.UserID).
1696+
Count(new(Issue))
1697+
if err != nil {
1698+
return nil, err
1699+
}
16851700
}
16861701

16871702
cond = cond.And(builder.Eq{"issue.is_closed": opts.IsClosed})
@@ -1700,6 +1715,14 @@ func GetUserIssueStats(opts UserIssueStatsOptions) (*IssueStats, error) {
17001715
return nil, err
17011716
}
17021717

1718+
stats.MentionCount, err = x.Where(cond).
1719+
Join("INNER", "issue_user", "issue.id = issue_user.issue_id and issue_user.is_mentioned = ?", true).
1720+
And("issue_user.uid = ?", opts.UserID).
1721+
Count(new(Issue))
1722+
if err != nil {
1723+
return nil, err
1724+
}
1725+
17031726
stats.YourRepositoriesCount, err = x.Where(cond).
17041727
And(builder.In("issue.repo_id", opts.UserRepoIDs)).
17051728
Count(new(Issue))

models/issue_milestone.go

-41
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package models
77
import (
88
"fmt"
99

10-
"code.gitea.io/gitea/modules/log"
1110
"code.gitea.io/gitea/modules/setting"
1211
api "code.gitea.io/gitea/modules/structs"
1312
"code.gitea.io/gitea/modules/timeutil"
@@ -394,46 +393,6 @@ func ChangeMilestoneAssign(issue *Issue, doer *User, oldMilestoneID int64) (err
394393
if err = sess.Commit(); err != nil {
395394
return fmt.Errorf("Commit: %v", err)
396395
}
397-
398-
var hookAction api.HookIssueAction
399-
if issue.MilestoneID > 0 {
400-
hookAction = api.HookIssueMilestoned
401-
} else {
402-
hookAction = api.HookIssueDemilestoned
403-
}
404-
405-
if err = issue.LoadAttributes(); err != nil {
406-
return err
407-
}
408-
409-
mode, _ := AccessLevel(doer, issue.Repo)
410-
if issue.IsPull {
411-
err = issue.PullRequest.LoadIssue()
412-
if err != nil {
413-
log.Error("LoadIssue: %v", err)
414-
return
415-
}
416-
err = PrepareWebhooks(issue.Repo, HookEventPullRequest, &api.PullRequestPayload{
417-
Action: hookAction,
418-
Index: issue.Index,
419-
PullRequest: issue.PullRequest.APIFormat(),
420-
Repository: issue.Repo.APIFormat(mode),
421-
Sender: doer.APIFormat(),
422-
})
423-
} else {
424-
err = PrepareWebhooks(issue.Repo, HookEventIssues, &api.IssuePayload{
425-
Action: hookAction,
426-
Index: issue.Index,
427-
Issue: issue.APIFormat(),
428-
Repository: issue.Repo.APIFormat(mode),
429-
Sender: doer.APIFormat(),
430-
})
431-
}
432-
if err != nil {
433-
log.Error("PrepareWebhooks [is_pull: %v]: %v", issue.IsPull, err)
434-
} else {
435-
go HookQueue.Add(issue.RepoID)
436-
}
437396
return nil
438397
}
439398

models/migrations/migrations.go

+2
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,8 @@ var migrations = []Migration{
244244
NewMigration("add email notification enabled preference to user", addEmailNotificationEnabledToUser),
245245
// v94 -> v95
246246
NewMigration("delete orphaned attachments", deleteOrphanedAttachments),
247+
// v95 -> v96
248+
NewMigration("add enable_status_check, status_check_contexts to protected_branch", addStatusCheckColumnsForProtectedBranches),
247249
}
248250

249251
// Migrate database to current version

models/migrations/v94.go

+10-31
Original file line numberDiff line numberDiff line change
@@ -5,43 +5,22 @@
55
package migrations
66

77
import (
8-
"os"
9-
10-
"code.gitea.io/gitea/models"
11-
"code.gitea.io/gitea/modules/setting"
128
"github.com/go-xorm/xorm"
139
)
1410

15-
func deleteOrphanedAttachments(x *xorm.Engine) error {
16-
17-
type Attachment struct {
18-
ID int64 `xorm:"pk autoincr"`
19-
UUID string `xorm:"uuid UNIQUE"`
20-
IssueID int64 `xorm:"INDEX"`
21-
ReleaseID int64 `xorm:"INDEX"`
22-
CommentID int64
11+
func addStatusCheckColumnsForProtectedBranches(x *xorm.Engine) error {
12+
type ProtectedBranch struct {
13+
EnableStatusCheck bool `xorm:"NOT NULL DEFAULT false"`
14+
StatusCheckContexts []string `xorm:"JSON TEXT"`
2315
}
2416

25-
sess := x.NewSession()
26-
defer sess.Close()
27-
28-
err := sess.BufferSize(setting.IterateBufferSize).
29-
Where("`comment_id` = 0 and (`release_id` = 0 or `release_id` not in (select `id` from `releases`))").Cols("uuid").
30-
Iterate(new(Attachment),
31-
func(idx int, bean interface{}) error {
32-
attachment := bean.(*Attachment)
33-
34-
if err := os.RemoveAll(models.AttachmentLocalPath(attachment.UUID)); err != nil {
35-
return err
36-
}
37-
38-
_, err := sess.Delete(attachment)
39-
return err
40-
})
41-
42-
if err != nil {
17+
if err := x.Sync2(new(ProtectedBranch)); err != nil {
4318
return err
4419
}
4520

46-
return sess.Commit()
21+
_, err := x.Cols("enable_status_check", "status_check_contexts").Update(&ProtectedBranch{
22+
EnableStatusCheck: false,
23+
StatusCheckContexts: []string{},
24+
})
25+
return err
4726
}

models/migrations/v95.go

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright 2019 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package migrations
6+
7+
import (
8+
"os"
9+
10+
"code.gitea.io/gitea/models"
11+
"code.gitea.io/gitea/modules/setting"
12+
"github.com/go-xorm/xorm"
13+
)
14+
15+
func deleteOrphanedAttachments(x *xorm.Engine) error {
16+
17+
type Attachment struct {
18+
ID int64 `xorm:"pk autoincr"`
19+
UUID string `xorm:"uuid UNIQUE"`
20+
IssueID int64 `xorm:"INDEX"`
21+
ReleaseID int64 `xorm:"INDEX"`
22+
CommentID int64
23+
}
24+
25+
sess := x.NewSession()
26+
defer sess.Close()
27+
28+
err := sess.BufferSize(setting.IterateBufferSize).
29+
Where("`comment_id` = 0 and (`release_id` = 0 or `release_id` not in (select `id` from `releases`))").Cols("uuid").
30+
Iterate(new(Attachment),
31+
func(idx int, bean interface{}) error {
32+
attachment := bean.(*Attachment)
33+
34+
if err := os.RemoveAll(models.AttachmentLocalPath(attachment.UUID)); err != nil {
35+
return err
36+
}
37+
38+
_, err := sess.Delete(attachment)
39+
return err
40+
})
41+
42+
if err != nil {
43+
return err
44+
}
45+
46+
return sess.Commit()
47+
}

models/pull.go

+14
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,20 @@ func (pr *PullRequest) LoadAttributes() error {
9999
return pr.loadAttributes(x)
100100
}
101101

102+
// LoadBaseRepo loads pull request base repository from database
103+
func (pr *PullRequest) LoadBaseRepo() error {
104+
if pr.BaseRepo == nil {
105+
var repo Repository
106+
if has, err := x.ID(pr.BaseRepoID).Get(&repo); err != nil {
107+
return err
108+
} else if !has {
109+
return ErrRepoNotExist{ID: pr.BaseRepoID}
110+
}
111+
pr.BaseRepo = &repo
112+
}
113+
return nil
114+
}
115+
102116
// LoadIssue loads issue information from database
103117
func (pr *PullRequest) LoadIssue() (err error) {
104118
return pr.loadIssue(x)

modules/auth/repo_form.go

+2
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ type ProtectBranchForm struct {
155155
EnableMergeWhitelist bool
156156
MergeWhitelistUsers string
157157
MergeWhitelistTeams string
158+
EnableStatusCheck bool `xorm:"NOT NULL DEFAULT false"`
159+
StatusCheckContexts []string
158160
RequiredApprovals int64
159161
ApprovalsWhitelistUsers string
160162
ApprovalsWhitelistTeams string

modules/pull/commit_status.go

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright 2019 The Gitea Authors.
2+
// All rights reserved.
3+
// Use of this source code is governed by a MIT-style
4+
// license that can be found in the LICENSE file.
5+
6+
package pull
7+
8+
import (
9+
"code.gitea.io/gitea/models"
10+
"code.gitea.io/gitea/modules/git"
11+
12+
"github.com/pkg/errors"
13+
)
14+
15+
// IsCommitStatusContextSuccess returns true if all required status check contexts succeed.
16+
func IsCommitStatusContextSuccess(commitStatuses []*models.CommitStatus, requiredContexts []string) bool {
17+
for _, ctx := range requiredContexts {
18+
var found bool
19+
for _, commitStatus := range commitStatuses {
20+
if commitStatus.Context == ctx {
21+
if commitStatus.State != models.CommitStatusSuccess {
22+
return false
23+
}
24+
25+
found = true
26+
break
27+
}
28+
}
29+
if !found {
30+
return false
31+
}
32+
}
33+
return true
34+
}
35+
36+
// IsPullCommitStatusPass returns if all required status checks PASS
37+
func IsPullCommitStatusPass(pr *models.PullRequest) (bool, error) {
38+
if err := pr.LoadProtectedBranch(); err != nil {
39+
return false, errors.Wrap(err, "GetLatestCommitStatus")
40+
}
41+
if pr.ProtectedBranch == nil || !pr.ProtectedBranch.EnableStatusCheck {
42+
return true, nil
43+
}
44+
45+
// check if all required status checks are successful
46+
headGitRepo, err := git.OpenRepository(pr.HeadRepo.RepoPath())
47+
if err != nil {
48+
return false, errors.Wrap(err, "OpenRepository")
49+
}
50+
51+
if !headGitRepo.IsBranchExist(pr.HeadBranch) {
52+
return false, errors.New("Head branch does not exist, can not merge")
53+
}
54+
55+
sha, err := headGitRepo.GetBranchCommitID(pr.HeadBranch)
56+
if err != nil {
57+
return false, errors.Wrap(err, "GetBranchCommitID")
58+
}
59+
60+
if err := pr.LoadBaseRepo(); err != nil {
61+
return false, errors.Wrap(err, "LoadBaseRepo")
62+
}
63+
64+
commitStatuses, err := models.GetLatestCommitStatus(pr.BaseRepo, sha, 0)
65+
if err != nil {
66+
return false, errors.Wrap(err, "GetLatestCommitStatus")
67+
}
68+
69+
return IsCommitStatusContextSuccess(commitStatuses, pr.ProtectedBranch.StatusCheckContexts), nil
70+
}

0 commit comments

Comments
 (0)