Skip to content

Commit 8e42c13

Browse files
authored
Merge branch 'main' into lunny/remove_repo_ref_in_commit
2 parents 23750dc + 7bf2972 commit 8e42c13

Some content is hidden

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

42 files changed

+758
-441
lines changed

models/issues/pull.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -417,10 +417,6 @@ func (pr *PullRequest) GetGitHeadRefName() string {
417417
return fmt.Sprintf("%s%d/head", git.PullPrefix, pr.Index)
418418
}
419419

420-
func (pr *PullRequest) GetGitHeadBranchRefName() string {
421-
return fmt.Sprintf("%s%s", git.BranchPrefix, pr.HeadBranch)
422-
}
423-
424420
// GetReviewCommentsCount returns the number of review comments made on the diff of a PR review (not including comments on commits or issues in a PR)
425421
func (pr *PullRequest) GetReviewCommentsCount(ctx context.Context) int {
426422
opts := FindCommentsOptions{
@@ -646,9 +642,8 @@ func (pr *PullRequest) UpdateCols(ctx context.Context, cols ...string) error {
646642
}
647643

648644
// UpdateColsIfNotMerged updates specific fields of a pull request if it has not been merged
649-
func (pr *PullRequest) UpdateColsIfNotMerged(ctx context.Context, cols ...string) error {
650-
_, err := db.GetEngine(ctx).Where("id = ? AND has_merged = ?", pr.ID, false).Cols(cols...).Update(pr)
651-
return err
645+
func (pr *PullRequest) UpdateColsIfNotMerged(ctx context.Context, cols ...string) (int64, error) {
646+
return db.GetEngine(ctx).Where("id = ? AND has_merged = ?", pr.ID, false).Cols(cols...).Update(pr)
652647
}
653648

654649
// IsWorkInProgress determine if the Pull Request is a Work In Progress by its title

models/migrations/v1_12/v136.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@ package v1_12
66
import (
77
"fmt"
88
"math"
9-
"path/filepath"
10-
"strings"
119
"time"
1210

13-
"code.gitea.io/gitea/modules/git"
11+
repo_model "code.gitea.io/gitea/models/repo"
12+
"code.gitea.io/gitea/modules/gitrepo"
1413
"code.gitea.io/gitea/modules/graceful"
1514
"code.gitea.io/gitea/modules/log"
1615
"code.gitea.io/gitea/modules/setting"
@@ -85,12 +84,9 @@ func AddCommitDivergenceToPulls(x *xorm.Engine) error {
8584
log.Error("Missing base repo with id %d for PR ID %d", pr.BaseRepoID, pr.ID)
8685
continue
8786
}
88-
userPath := filepath.Join(setting.RepoRootPath, strings.ToLower(baseRepo.OwnerName))
89-
repoPath := filepath.Join(userPath, strings.ToLower(baseRepo.Name)+".git")
90-
87+
repoStore := repo_model.StorageRepo(repo_model.RelativePath(baseRepo.OwnerName, baseRepo.Name))
9188
gitRefName := fmt.Sprintf("refs/pull/%d/head", pr.Index)
92-
93-
divergence, err := git.GetDivergingCommits(graceful.GetManager().HammerContext(), repoPath, pr.BaseBranch, gitRefName)
89+
divergence, err := gitrepo.GetDivergingCommits(graceful.GetManager().HammerContext(), repoStore, pr.BaseBranch, gitRefName)
9490
if err != nil {
9591
log.Warn("Could not recalculate Divergence for pull: %d", pr.ID)
9692
pr.CommitsAhead = 0

modules/actions/workflows.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -377,20 +377,28 @@ func matchIssuesEvent(issuePayload *api.IssuePayload, evt *jobparser.Event) bool
377377
// Actions with the same name:
378378
// opened, edited, closed, reopened, assigned, unassigned, milestoned, demilestoned
379379
// Actions need to be converted:
380-
// label_updated -> labeled
380+
// label_updated -> labeled (when adding) or unlabeled (when removing)
381381
// label_cleared -> unlabeled
382382
// Unsupported activity types:
383383
// deleted, transferred, pinned, unpinned, locked, unlocked
384384

385-
action := issuePayload.Action
386-
switch action {
385+
actions := []string{}
386+
switch issuePayload.Action {
387387
case api.HookIssueLabelUpdated:
388-
action = "labeled"
388+
if len(issuePayload.Changes.AddedLabels) > 0 {
389+
actions = append(actions, "labeled")
390+
}
391+
if len(issuePayload.Changes.RemovedLabels) > 0 {
392+
actions = append(actions, "unlabeled")
393+
}
389394
case api.HookIssueLabelCleared:
390-
action = "unlabeled"
395+
actions = append(actions, "unlabeled")
396+
default:
397+
actions = append(actions, string(issuePayload.Action))
391398
}
399+
392400
for _, val := range vals {
393-
if glob.MustCompile(val, '/').Match(string(action)) {
401+
if slices.ContainsFunc(actions, glob.MustCompile(val, '/').Match) {
394402
matchTimes++
395403
break
396404
}

modules/actions/workflows_test.go

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,184 @@ func TestDetectMatched(t *testing.T) {
154154
})
155155
}
156156
}
157+
158+
func TestMatchIssuesEvent(t *testing.T) {
159+
testCases := []struct {
160+
desc string
161+
payload *api.IssuePayload
162+
yamlOn string
163+
expected bool
164+
eventType string
165+
}{
166+
{
167+
desc: "Label deletion should trigger unlabeled event",
168+
payload: &api.IssuePayload{
169+
Action: api.HookIssueLabelUpdated,
170+
Issue: &api.Issue{
171+
Labels: []*api.Label{},
172+
},
173+
Changes: &api.ChangesPayload{
174+
RemovedLabels: []*api.Label{
175+
{ID: 123, Name: "deleted-label"},
176+
},
177+
},
178+
},
179+
yamlOn: "on:\n issues:\n types: [unlabeled]",
180+
expected: true,
181+
eventType: "unlabeled",
182+
},
183+
{
184+
desc: "Label deletion with existing labels should trigger unlabeled event",
185+
payload: &api.IssuePayload{
186+
Action: api.HookIssueLabelUpdated,
187+
Issue: &api.Issue{
188+
Labels: []*api.Label{
189+
{ID: 456, Name: "existing-label"},
190+
},
191+
},
192+
Changes: &api.ChangesPayload{
193+
AddedLabels: nil,
194+
RemovedLabels: []*api.Label{
195+
{ID: 123, Name: "deleted-label"},
196+
},
197+
},
198+
},
199+
yamlOn: "on:\n issues:\n types: [unlabeled]",
200+
expected: true,
201+
eventType: "unlabeled",
202+
},
203+
{
204+
desc: "Label addition should trigger labeled event",
205+
payload: &api.IssuePayload{
206+
Action: api.HookIssueLabelUpdated,
207+
Issue: &api.Issue{
208+
Labels: []*api.Label{
209+
{ID: 123, Name: "new-label"},
210+
},
211+
},
212+
Changes: &api.ChangesPayload{
213+
AddedLabels: []*api.Label{
214+
{ID: 123, Name: "new-label"},
215+
},
216+
RemovedLabels: []*api.Label{}, // Empty array, no labels removed
217+
},
218+
},
219+
yamlOn: "on:\n issues:\n types: [labeled]",
220+
expected: true,
221+
eventType: "labeled",
222+
},
223+
{
224+
desc: "Label clear should trigger unlabeled event",
225+
payload: &api.IssuePayload{
226+
Action: api.HookIssueLabelCleared,
227+
Issue: &api.Issue{
228+
Labels: []*api.Label{},
229+
},
230+
},
231+
yamlOn: "on:\n issues:\n types: [unlabeled]",
232+
expected: true,
233+
eventType: "unlabeled",
234+
},
235+
{
236+
desc: "Both adding and removing labels should trigger labeled event",
237+
payload: &api.IssuePayload{
238+
Action: api.HookIssueLabelUpdated,
239+
Issue: &api.Issue{
240+
Labels: []*api.Label{
241+
{ID: 789, Name: "new-label"},
242+
},
243+
},
244+
Changes: &api.ChangesPayload{
245+
AddedLabels: []*api.Label{
246+
{ID: 789, Name: "new-label"},
247+
},
248+
RemovedLabels: []*api.Label{
249+
{ID: 123, Name: "deleted-label"},
250+
},
251+
},
252+
},
253+
yamlOn: "on:\n issues:\n types: [labeled]",
254+
expected: true,
255+
eventType: "labeled",
256+
},
257+
{
258+
desc: "Both adding and removing labels should trigger unlabeled event",
259+
payload: &api.IssuePayload{
260+
Action: api.HookIssueLabelUpdated,
261+
Issue: &api.Issue{
262+
Labels: []*api.Label{
263+
{ID: 789, Name: "new-label"},
264+
},
265+
},
266+
Changes: &api.ChangesPayload{
267+
AddedLabels: []*api.Label{
268+
{ID: 789, Name: "new-label"},
269+
},
270+
RemovedLabels: []*api.Label{
271+
{ID: 123, Name: "deleted-label"},
272+
},
273+
},
274+
},
275+
yamlOn: "on:\n issues:\n types: [unlabeled]",
276+
expected: true,
277+
eventType: "unlabeled",
278+
},
279+
{
280+
desc: "Both adding and removing labels should trigger both events",
281+
payload: &api.IssuePayload{
282+
Action: api.HookIssueLabelUpdated,
283+
Issue: &api.Issue{
284+
Labels: []*api.Label{
285+
{ID: 789, Name: "new-label"},
286+
},
287+
},
288+
Changes: &api.ChangesPayload{
289+
AddedLabels: []*api.Label{
290+
{ID: 789, Name: "new-label"},
291+
},
292+
RemovedLabels: []*api.Label{
293+
{ID: 123, Name: "deleted-label"},
294+
},
295+
},
296+
},
297+
yamlOn: "on:\n issues:\n types: [labeled, unlabeled]",
298+
expected: true,
299+
eventType: "multiple",
300+
},
301+
}
302+
303+
for _, tc := range testCases {
304+
t.Run(tc.desc, func(t *testing.T) {
305+
evts, err := GetEventsFromContent([]byte(tc.yamlOn))
306+
assert.NoError(t, err)
307+
assert.Len(t, evts, 1)
308+
309+
// Test if the event matches as expected
310+
assert.Equal(t, tc.expected, matchIssuesEvent(tc.payload, evts[0]))
311+
312+
// For extra validation, check that action mapping works correctly
313+
if tc.eventType == "multiple" {
314+
// Skip direct action mapping validation for multiple events case
315+
// as one action can map to multiple event types
316+
return
317+
}
318+
319+
// Determine expected action for single event case
320+
var expectedAction string
321+
switch tc.payload.Action {
322+
case api.HookIssueLabelUpdated:
323+
if tc.eventType == "labeled" {
324+
expectedAction = "labeled"
325+
} else if tc.eventType == "unlabeled" {
326+
expectedAction = "unlabeled"
327+
}
328+
case api.HookIssueLabelCleared:
329+
expectedAction = "unlabeled"
330+
default:
331+
expectedAction = string(tc.payload.Action)
332+
}
333+
334+
assert.Equal(t, expectedAction, tc.eventType, "Event type should match expected")
335+
})
336+
}
337+
}

modules/git/repo.go

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -243,36 +243,6 @@ func GetLatestCommitTime(ctx context.Context, repoPath string) (time.Time, error
243243
return time.Parse("Mon Jan _2 15:04:05 2006 -0700", commitTime)
244244
}
245245

246-
// DivergeObject represents commit count diverging commits
247-
type DivergeObject struct {
248-
Ahead int
249-
Behind int
250-
}
251-
252-
// GetDivergingCommits returns the number of commits a targetBranch is ahead or behind a baseBranch
253-
func GetDivergingCommits(ctx context.Context, repoPath, baseBranch, targetBranch string) (do DivergeObject, err error) {
254-
cmd := gitcmd.NewCommand("rev-list", "--count", "--left-right").
255-
AddDynamicArguments(baseBranch + "..." + targetBranch).AddArguments("--")
256-
stdout, _, err := cmd.RunStdString(ctx, &gitcmd.RunOpts{Dir: repoPath})
257-
if err != nil {
258-
return do, err
259-
}
260-
left, right, found := strings.Cut(strings.Trim(stdout, "\n"), "\t")
261-
if !found {
262-
return do, fmt.Errorf("git rev-list output is missing a tab: %q", stdout)
263-
}
264-
265-
do.Behind, err = strconv.Atoi(left)
266-
if err != nil {
267-
return do, err
268-
}
269-
do.Ahead, err = strconv.Atoi(right)
270-
if err != nil {
271-
return do, err
272-
}
273-
return do, nil
274-
}
275-
276246
// CreateBundle create bundle content to the target path
277247
func (repo *Repository) CreateBundle(ctx context.Context, commit string, out io.Writer) error {
278248
tmp, cleanup, err := setting.AppDataTempDir("git-repo-content").MkdirTempRandom("gitea-bundle")

modules/git/repo_commit_gogit.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,6 @@ func (repo *Repository) GetRefCommitID(name string) (string, error) {
3838
return ref.Hash().String(), nil
3939
}
4040

41-
// SetReference sets the commit ID string of given reference (e.g. branch or tag).
42-
func (repo *Repository) SetReference(name, commitID string) error {
43-
return repo.gogitRepo.Storer.SetReference(plumbing.NewReferenceFromStrings(name, commitID))
44-
}
45-
46-
// RemoveReference removes the given reference (e.g. branch or tag).
47-
func (repo *Repository) RemoveReference(name string) error {
48-
return repo.gogitRepo.Storer.RemoveReference(plumbing.ReferenceName(name))
49-
}
50-
5141
// ConvertToHash returns a Hash object from a potential ID string
5242
func (repo *Repository) ConvertToGitID(commitID string) (ObjectID, error) {
5343
objectFormat, err := repo.GetObjectFormat()

modules/git/repo_commit_nogogit.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,6 @@ func (repo *Repository) GetRefCommitID(name string) (string, error) {
5151
return string(shaBs), nil
5252
}
5353

54-
// SetReference sets the commit ID string of given reference (e.g. branch or tag).
55-
func (repo *Repository) SetReference(name, commitID string) error {
56-
_, _, err := gitcmd.NewCommand("update-ref").AddDynamicArguments(name, commitID).RunStdString(repo.Ctx, &gitcmd.RunOpts{Dir: repo.Path})
57-
return err
58-
}
59-
60-
// RemoveReference removes the given reference (e.g. branch or tag).
61-
func (repo *Repository) RemoveReference(name string) error {
62-
_, _, err := gitcmd.NewCommand("update-ref", "--no-deref", "-d").AddDynamicArguments(name).RunStdString(repo.Ctx, &gitcmd.RunOpts{Dir: repo.Path})
63-
return err
64-
}
65-
6654
// IsCommitExist returns true if given commit exists in current repository.
6755
func (repo *Repository) IsCommitExist(name string) bool {
6856
if err := ensureValidGitRepository(repo.Ctx, repo.Path); err != nil {

modules/git/repo_compare_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"path/filepath"
1010
"testing"
1111

12+
"code.gitea.io/gitea/modules/git/gitcmd"
13+
1214
"github.com/stretchr/testify/assert"
1315
)
1416

@@ -99,7 +101,9 @@ func TestReadWritePullHead(t *testing.T) {
99101

100102
// Write a fake sha1 with only 40 zeros
101103
newCommit := "feaf4ba6bc635fec442f46ddd4512416ec43c2c2"
102-
err = repo.SetReference(PullPrefix+"1/head", newCommit)
104+
_, _, err = gitcmd.NewCommand("update-ref").
105+
AddDynamicArguments(PullPrefix+"1/head", newCommit).
106+
RunStdString(t.Context(), &gitcmd.RunOpts{Dir: repo.Path})
103107
if err != nil {
104108
assert.NoError(t, err)
105109
return
@@ -116,7 +120,9 @@ func TestReadWritePullHead(t *testing.T) {
116120
assert.Equal(t, headContents, newCommit)
117121

118122
// Remove file after the test
119-
err = repo.RemoveReference(PullPrefix + "1/head")
123+
_, _, err = gitcmd.NewCommand("update-ref", "--no-deref", "-d").
124+
AddDynamicArguments(PullPrefix+"1/head").
125+
RunStdString(t.Context(), &gitcmd.RunOpts{Dir: repo.Path})
120126
assert.NoError(t, err)
121127
}
122128

0 commit comments

Comments
 (0)