Skip to content

Commit 33a7de7

Browse files
committed
Merge branch 'main' into tooldeps2
2 parents cb77de6 + 2ff0c12 commit 33a7de7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+1589
-944
lines changed

.eslintrc.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ plugins:
2222
- eslint-plugin-wc
2323

2424
env:
25-
es2022: true
25+
es2024: true
2626
node: true
2727

2828
overrides:
@@ -156,7 +156,7 @@ rules:
156156
import/no-restricted-paths: [0]
157157
import/no-self-import: [2]
158158
import/no-unassigned-import: [0]
159-
import/no-unresolved: [2, {commonjs: true, ignore: ["\\?.+$"]}]
159+
import/no-unresolved: [2, {commonjs: true, ignore: [\?.+$, ^vitest/]}]
160160
import/no-unused-modules: [2, {unusedExports: true}]
161161
import/no-useless-path-segments: [2, {commonjs: true}]
162162
import/no-webpack-loader-syntax: [2]
@@ -693,7 +693,7 @@ rules:
693693
unicorn/prefer-dom-node-remove: [2]
694694
unicorn/prefer-dom-node-text-content: [2]
695695
unicorn/prefer-event-target: [2]
696-
unicorn/prefer-export-from: [2, {ignoreUsedVariables: true}]
696+
unicorn/prefer-export-from: [0]
697697
unicorn/prefer-includes: [2]
698698
unicorn/prefer-json-parse-buffer: [0]
699699
unicorn/prefer-keyboard-event-key: [2]

MAINTAINERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,4 @@ wxiaoguang <[email protected]> (@wxiaoguang)
5353
Gary Moon <[email protected]> (@garymoon)
5454
Philip Peterson <[email protected]> (@philip-peterson)
5555
Denys Konovalov <[email protected]> (@denyskon)
56+
Punit Inani <[email protected]> (@puni9869)

models/git/branch.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515
"code.gitea.io/gitea/modules/log"
1616
"code.gitea.io/gitea/modules/timeutil"
1717
"code.gitea.io/gitea/modules/util"
18+
19+
"xorm.io/builder"
1820
)
1921

2022
// ErrBranchNotExist represents an error that branch with such name does not exist.
@@ -378,3 +380,22 @@ func RenameBranch(ctx context.Context, repo *repo_model.Repository, from, to str
378380

379381
return committer.Commit()
380382
}
383+
384+
// FindRecentlyPushedNewBranches return at most 2 new branches pushed by the user in 6 hours which has no opened PRs created
385+
func FindRecentlyPushedNewBranches(ctx context.Context, repoID, userID int64) (BranchList, error) {
386+
branches := make(BranchList, 0, 2)
387+
subQuery := builder.Select("head_branch").From("pull_request").
388+
InnerJoin("issue", "issue.id = pull_request.issue_id").
389+
Where(builder.Eq{
390+
"pull_request.head_repo_id": repoID,
391+
"issue.is_closed": false,
392+
})
393+
err := db.GetEngine(ctx).
394+
Where("pusher_id=? AND is_deleted=?", userID, false).
395+
And("updated_unix >= ?", time.Now().Add(-time.Hour*6).Unix()).
396+
NotIn("name", subQuery).
397+
OrderBy("branch.updated_unix DESC").
398+
Limit(2).
399+
Find(&branches)
400+
return branches, err
401+
}

models/repo/repo.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,18 @@ func (repo *Repository) ComposeCompareURL(oldCommitID, newCommitID string) strin
528528
return fmt.Sprintf("%s/%s/compare/%s...%s", url.PathEscape(repo.OwnerName), url.PathEscape(repo.Name), util.PathEscapeSegments(oldCommitID), util.PathEscapeSegments(newCommitID))
529529
}
530530

531+
func (repo *Repository) ComposeBranchCompareURL(baseRepo *Repository, branchName string) string {
532+
if baseRepo == nil {
533+
baseRepo = repo
534+
}
535+
var cmpBranchEscaped string
536+
if repo.ID != baseRepo.ID {
537+
cmpBranchEscaped = fmt.Sprintf("%s/%s:", url.PathEscape(repo.OwnerName), url.PathEscape(repo.Name))
538+
}
539+
cmpBranchEscaped = fmt.Sprintf("%s%s", cmpBranchEscaped, util.PathEscapeSegments(branchName))
540+
return fmt.Sprintf("%s/compare/%s...%s", baseRepo.Link(), util.PathEscapeSegments(baseRepo.DefaultBranch), cmpBranchEscaped)
541+
}
542+
531543
// IsOwnedBy returns true when user owns this repository
532544
func (repo *Repository) IsOwnedBy(userID int64) bool {
533545
return repo.OwnerID == userID

modules/actions/workflows.go

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ import (
2323
type DetectedWorkflow struct {
2424
EntryName string
2525
TriggerEvent string
26-
Commit *git.Commit
27-
Ref string
2826
Content []byte
2927
}
3028

@@ -120,7 +118,6 @@ func DetectWorkflows(commit *git.Commit, triggedEvent webhook_module.HookEventTy
120118
dwf := &DetectedWorkflow{
121119
EntryName: entry.Name(),
122120
TriggerEvent: evt.Name,
123-
Commit: commit,
124121
Content: content,
125122
}
126123
workflows = append(workflows, dwf)
@@ -335,44 +332,47 @@ func matchIssuesEvent(commit *git.Commit, issuePayload *api.IssuePayload, evt *j
335332
}
336333

337334
func matchPullRequestEvent(commit *git.Commit, prPayload *api.PullRequestPayload, evt *jobparser.Event) bool {
338-
// with no special filter parameters
339-
if len(evt.Acts()) == 0 {
335+
acts := evt.Acts()
336+
activityTypeMatched := false
337+
matchTimes := 0
338+
339+
if vals, ok := acts["types"]; !ok {
340340
// defaultly, only pull request `opened`, `reopened` and `synchronized` will trigger workflow
341341
// See https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request
342-
return prPayload.Action == api.HookIssueSynchronized || prPayload.Action == api.HookIssueOpened || prPayload.Action == api.HookIssueReOpened
342+
activityTypeMatched = prPayload.Action == api.HookIssueSynchronized || prPayload.Action == api.HookIssueOpened || prPayload.Action == api.HookIssueReOpened
343+
} else {
344+
// See https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request
345+
// Actions with the same name:
346+
// opened, edited, closed, reopened, assigned, unassigned
347+
// Actions need to be converted:
348+
// synchronized -> synchronize
349+
// label_updated -> labeled
350+
// label_cleared -> unlabeled
351+
// Unsupported activity types:
352+
// converted_to_draft, ready_for_review, locked, unlocked, review_requested, review_request_removed, auto_merge_enabled, auto_merge_disabled
353+
354+
action := prPayload.Action
355+
switch action {
356+
case api.HookIssueSynchronized:
357+
action = "synchronize"
358+
case api.HookIssueLabelUpdated:
359+
action = "labeled"
360+
case api.HookIssueLabelCleared:
361+
action = "unlabeled"
362+
}
363+
log.Trace("matching pull_request %s with %v", action, vals)
364+
for _, val := range vals {
365+
if glob.MustCompile(val, '/').Match(string(action)) {
366+
activityTypeMatched = true
367+
matchTimes++
368+
break
369+
}
370+
}
343371
}
344372

345-
matchTimes := 0
346373
// all acts conditions should be satisfied
347-
for cond, vals := range evt.Acts() {
374+
for cond, vals := range acts {
348375
switch cond {
349-
case "types":
350-
// See https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request
351-
// Actions with the same name:
352-
// opened, edited, closed, reopened, assigned, unassigned
353-
// Actions need to be converted:
354-
// synchronized -> synchronize
355-
// label_updated -> labeled
356-
// label_cleared -> unlabeled
357-
// Unsupported activity types:
358-
// converted_to_draft, ready_for_review, locked, unlocked, review_requested, review_request_removed, auto_merge_enabled, auto_merge_disabled
359-
360-
action := prPayload.Action
361-
switch action {
362-
case api.HookIssueSynchronized:
363-
action = "synchronize"
364-
case api.HookIssueLabelUpdated:
365-
action = "labeled"
366-
case api.HookIssueLabelCleared:
367-
action = "unlabeled"
368-
}
369-
log.Trace("matching pull_request %s with %v", action, vals)
370-
for _, val := range vals {
371-
if glob.MustCompile(val, '/').Match(string(action)) {
372-
matchTimes++
373-
break
374-
}
375-
}
376376
case "branches":
377377
refName := git.RefName(prPayload.PullRequest.Base.Ref)
378378
patterns, err := workflowpattern.CompilePatterns(vals...)
@@ -421,7 +421,7 @@ func matchPullRequestEvent(commit *git.Commit, prPayload *api.PullRequestPayload
421421
log.Warn("pull request event unsupported condition %q", cond)
422422
}
423423
}
424-
return matchTimes == len(evt.Acts())
424+
return activityTypeMatched && matchTimes == len(evt.Acts())
425425
}
426426

427427
func matchIssueCommentEvent(commit *git.Commit, issueCommentPayload *api.IssueCommentPayload, evt *jobparser.Event) bool {

modules/actions/workflows_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,25 @@ func TestDetectMatched(t *testing.T) {
5757
yamlOn: "on: pull_request",
5858
expected: false,
5959
},
60+
{
61+
desc: "HookEventPullRequest(pull_request) `closed` action doesn't match GithubEventPullRequest(pull_request) with no activity type",
62+
triggedEvent: webhook_module.HookEventPullRequest,
63+
payload: &api.PullRequestPayload{Action: api.HookIssueClosed},
64+
yamlOn: "on: pull_request",
65+
expected: false,
66+
},
67+
{
68+
desc: "HookEventPullRequest(pull_request) `closed` action doesn't match GithubEventPullRequest(pull_request) with branches",
69+
triggedEvent: webhook_module.HookEventPullRequest,
70+
payload: &api.PullRequestPayload{
71+
Action: api.HookIssueClosed,
72+
PullRequest: &api.PullRequest{
73+
Base: &api.PRBranchInfo{},
74+
},
75+
},
76+
yamlOn: "on:\n pull_request:\n branches: [main]",
77+
expected: false,
78+
},
6079
{
6180
desc: "HookEventPullRequest(pull_request) `label_updated` action matches GithubEventPullRequest(pull_request) with `label` activity type",
6281
triggedEvent: webhook_module.HookEventPullRequest,

modules/indexer/issues/bleve/bleve.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func (b *Indexer) Delete(_ context.Context, ids ...int64) error {
138138

139139
// Search searches for issues by given conditions.
140140
// Returns the matching issue IDs
141-
func (b *Indexer) Search(ctx context.Context, keyword string, repoIDs []int64, limit, start int) (*internal.SearchResult, error) {
141+
func (b *Indexer) Search(ctx context.Context, keyword string, repoIDs []int64, limit, start int, state string) (*internal.SearchResult, error) {
142142
var repoQueriesP []*query.NumericRangeQuery
143143
for _, repoID := range repoIDs {
144144
repoQueriesP = append(repoQueriesP, numericEqualityQuery(repoID, "repo_id"))

modules/indexer/issues/bleve/bleve_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func TestBleveIndexAndSearch(t *testing.T) {
7777
}
7878

7979
for _, kw := range keywords {
80-
res, err := indexer.Search(context.TODO(), kw.Keyword, []int64{2}, 10, 0)
80+
res, err := indexer.Search(context.TODO(), kw.Keyword, []int64{2}, 10, 0, "")
8181
assert.NoError(t, err)
8282

8383
ids := make([]int64, 0, len(res.Hits))

modules/indexer/issues/db/db.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func (i *Indexer) Delete(_ context.Context, _ ...int64) error {
3636
}
3737

3838
// Search searches for issues
39-
func (i *Indexer) Search(ctx context.Context, kw string, repoIDs []int64, limit, start int) (*internal.SearchResult, error) {
39+
func (i *Indexer) Search(ctx context.Context, kw string, repoIDs []int64, limit, start int, state string) (*internal.SearchResult, error) {
4040
total, ids, err := issues_model.SearchIssueIDsByKeyword(ctx, kw, repoIDs, limit, start)
4141
if err != nil {
4242
return nil, err

modules/indexer/issues/elasticsearch/elasticsearch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func (b *Indexer) Delete(ctx context.Context, ids ...int64) error {
140140

141141
// Search searches for issues by given conditions.
142142
// Returns the matching issue IDs
143-
func (b *Indexer) Search(ctx context.Context, keyword string, repoIDs []int64, limit, start int) (*internal.SearchResult, error) {
143+
func (b *Indexer) Search(ctx context.Context, keyword string, repoIDs []int64, limit, start int, state string) (*internal.SearchResult, error) {
144144
kwQuery := elastic.NewMultiMatchQuery(keyword, "title", "content", "comments")
145145
query := elastic.NewBoolQuery()
146146
query = query.Must(kwQuery)

modules/indexer/issues/indexer.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -242,12 +242,18 @@ func UpdateIssueIndexer(issue *issues_model.Issue) {
242242
comments = append(comments, comment.Content)
243243
}
244244
}
245+
issueType := "issue"
246+
if issue.IsPull {
247+
issueType = "pull"
248+
}
245249
indexerData := &internal.IndexerData{
246-
ID: issue.ID,
247-
RepoID: issue.RepoID,
248-
Title: issue.Title,
249-
Content: issue.Content,
250-
Comments: comments,
250+
ID: issue.ID,
251+
RepoID: issue.RepoID,
252+
State: string(issue.State()),
253+
IssueType: issueType,
254+
Title: issue.Title,
255+
Content: issue.Content,
256+
Comments: comments,
251257
}
252258
log.Debug("Adding to channel: %v", indexerData)
253259
if err := issueIndexerQueue.Push(indexerData); err != nil {
@@ -278,10 +284,10 @@ func DeleteRepoIssueIndexer(ctx context.Context, repo *repo_model.Repository) {
278284

279285
// SearchIssuesByKeyword search issue ids by keywords and repo id
280286
// WARNNING: You have to ensure user have permission to visit repoIDs' issues
281-
func SearchIssuesByKeyword(ctx context.Context, repoIDs []int64, keyword string) ([]int64, error) {
287+
func SearchIssuesByKeyword(ctx context.Context, repoIDs []int64, keyword, state string) ([]int64, error) {
282288
var issueIDs []int64
283289
indexer := *globalIndexer.Load()
284-
res, err := indexer.Search(ctx, keyword, repoIDs, 50, 0)
290+
res, err := indexer.Search(ctx, keyword, repoIDs, 50, 0, state)
285291
if err != nil {
286292
return nil, err
287293
}

modules/indexer/issues/indexer_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,19 @@ func TestBleveSearchIssues(t *testing.T) {
5050

5151
time.Sleep(5 * time.Second)
5252

53-
ids, err := SearchIssuesByKeyword(context.TODO(), []int64{1}, "issue2")
53+
ids, err := SearchIssuesByKeyword(context.TODO(), []int64{1}, "issue2", "")
5454
assert.NoError(t, err)
5555
assert.EqualValues(t, []int64{2}, ids)
5656

57-
ids, err = SearchIssuesByKeyword(context.TODO(), []int64{1}, "first")
57+
ids, err = SearchIssuesByKeyword(context.TODO(), []int64{1}, "first", "")
5858
assert.NoError(t, err)
5959
assert.EqualValues(t, []int64{1}, ids)
6060

61-
ids, err = SearchIssuesByKeyword(context.TODO(), []int64{1}, "for")
61+
ids, err = SearchIssuesByKeyword(context.TODO(), []int64{1}, "for", "")
6262
assert.NoError(t, err)
6363
assert.ElementsMatch(t, []int64{1, 2, 3, 5, 11}, ids)
6464

65-
ids, err = SearchIssuesByKeyword(context.TODO(), []int64{1}, "good")
65+
ids, err = SearchIssuesByKeyword(context.TODO(), []int64{1}, "good", "")
6666
assert.NoError(t, err)
6767
assert.EqualValues(t, []int64{1}, ids)
6868
}
@@ -73,19 +73,19 @@ func TestDBSearchIssues(t *testing.T) {
7373
setting.Indexer.IssueType = "db"
7474
InitIssueIndexer(true)
7575

76-
ids, err := SearchIssuesByKeyword(context.TODO(), []int64{1}, "issue2")
76+
ids, err := SearchIssuesByKeyword(context.TODO(), []int64{1}, "issue2", "")
7777
assert.NoError(t, err)
7878
assert.EqualValues(t, []int64{2}, ids)
7979

80-
ids, err = SearchIssuesByKeyword(context.TODO(), []int64{1}, "first")
80+
ids, err = SearchIssuesByKeyword(context.TODO(), []int64{1}, "first", "")
8181
assert.NoError(t, err)
8282
assert.EqualValues(t, []int64{1}, ids)
8383

84-
ids, err = SearchIssuesByKeyword(context.TODO(), []int64{1}, "for")
84+
ids, err = SearchIssuesByKeyword(context.TODO(), []int64{1}, "for", "")
8585
assert.NoError(t, err)
8686
assert.ElementsMatch(t, []int64{1, 2, 3, 5, 11}, ids)
8787

88-
ids, err = SearchIssuesByKeyword(context.TODO(), []int64{1}, "good")
88+
ids, err = SearchIssuesByKeyword(context.TODO(), []int64{1}, "good", "")
8989
assert.NoError(t, err)
9090
assert.EqualValues(t, []int64{1}, ids)
9191
}

modules/indexer/issues/internal/indexer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type Indexer interface {
1515
internal.Indexer
1616
Index(ctx context.Context, issue []*IndexerData) error
1717
Delete(ctx context.Context, ids ...int64) error
18-
Search(ctx context.Context, kw string, repoIDs []int64, limit, start int) (*SearchResult, error)
18+
Search(ctx context.Context, kw string, repoIDs []int64, limit, start int, state string) (*SearchResult, error)
1919
}
2020

2121
// NewDummyIndexer returns a dummy indexer
@@ -37,6 +37,6 @@ func (d *dummyIndexer) Delete(ctx context.Context, ids ...int64) error {
3737
return fmt.Errorf("indexer is not ready")
3838
}
3939

40-
func (d *dummyIndexer) Search(ctx context.Context, kw string, repoIDs []int64, limit, start int) (*SearchResult, error) {
40+
func (d *dummyIndexer) Search(ctx context.Context, kw string, repoIDs []int64, limit, start int, state string) (*SearchResult, error) {
4141
return nil, fmt.Errorf("indexer is not ready")
4242
}

modules/indexer/issues/internal/model.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ package internal
55

66
// IndexerData data stored in the issue indexer
77
type IndexerData struct {
8-
ID int64 `json:"id"`
9-
RepoID int64 `json:"repo_id"`
10-
Title string `json:"title"`
11-
Content string `json:"content"`
12-
Comments []string `json:"comments"`
13-
IsDelete bool `json:"is_delete"`
14-
IDs []int64 `json:"ids"`
8+
ID int64 `json:"id"`
9+
RepoID int64 `json:"repo_id"`
10+
State string `json:"state"` // open, closed, all
11+
IssueType string `json:"type"` // issue or pull
12+
Title string `json:"title"`
13+
Content string `json:"content"`
14+
Comments []string `json:"comments"`
15+
IsDelete bool `json:"is_delete"`
16+
IDs []int64 `json:"ids"`
1517
}
1618

1719
// Match represents on search result

0 commit comments

Comments
 (0)