Skip to content

Commit 829cced

Browse files
Merge branch 'main' into feature/fix-tag-push-paths
2 parents 2e52d74 + 79cc369 commit 829cced

File tree

167 files changed

+1926
-1504
lines changed

Some content is hidden

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

167 files changed

+1926
-1504
lines changed

.github/workflows/pull-db-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
runs-on: ubuntu-latest
1818
services:
1919
pgsql:
20-
image: postgres:12
20+
image: postgres:14
2121
env:
2222
POSTGRES_DB: test
2323
POSTGRES_PASSWORD: postgres

cmd/admin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func runRepoSyncReleases(_ *cli.Context) error {
107107

108108
log.Trace("Synchronizing repository releases (this may take a while)")
109109
for page := 1; ; page++ {
110-
repos, count, err := repo_model.SearchRepositoryByName(ctx, &repo_model.SearchRepoOptions{
110+
repos, count, err := repo_model.SearchRepositoryByName(ctx, repo_model.SearchRepoOptions{
111111
ListOptions: db.ListOptions{
112112
PageSize: repo_model.RepositoryListDefaultPageSize,
113113
Page: page,

cmd/admin_auth_ldap_test.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"testing"
99

1010
"code.gitea.io/gitea/models/auth"
11+
"code.gitea.io/gitea/modules/test"
1112
"code.gitea.io/gitea/services/auth/source/ldap"
1213

1314
"github.com/stretchr/testify/assert"
@@ -16,9 +17,7 @@ import (
1617

1718
func TestAddLdapBindDn(t *testing.T) {
1819
// Mock cli functions to do not exit on error
19-
osExiter := cli.OsExiter
20-
defer func() { cli.OsExiter = osExiter }()
21-
cli.OsExiter = func(code int) {}
20+
defer test.MockVariableValue(&cli.OsExiter, func(code int) {})()
2221

2322
// Test cases
2423
cases := []struct {
@@ -256,9 +255,7 @@ func TestAddLdapBindDn(t *testing.T) {
256255

257256
func TestAddLdapSimpleAuth(t *testing.T) {
258257
// Mock cli functions to do not exit on error
259-
osExiter := cli.OsExiter
260-
defer func() { cli.OsExiter = osExiter }()
261-
cli.OsExiter = func(code int) {}
258+
defer test.MockVariableValue(&cli.OsExiter, func(code int) {})()
262259

263260
// Test cases
264261
cases := []struct {
@@ -487,9 +484,7 @@ func TestAddLdapSimpleAuth(t *testing.T) {
487484

488485
func TestUpdateLdapBindDn(t *testing.T) {
489486
// Mock cli functions to do not exit on error
490-
osExiter := cli.OsExiter
491-
defer func() { cli.OsExiter = osExiter }()
492-
cli.OsExiter = func(code int) {}
487+
defer test.MockVariableValue(&cli.OsExiter, func(code int) {})()
493488

494489
// Test cases
495490
cases := []struct {
@@ -964,9 +959,7 @@ func TestUpdateLdapBindDn(t *testing.T) {
964959

965960
func TestUpdateLdapSimpleAuth(t *testing.T) {
966961
// Mock cli functions to do not exit on error
967-
osExiter := cli.OsExiter
968-
defer func() { cli.OsExiter = osExiter }()
969-
cli.OsExiter = func(code int) {}
962+
defer test.MockVariableValue(&cli.OsExiter, func(code int) {})()
970963

971964
// Test cases
972965
cases := []struct {

cmd/hook.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
)
2525

2626
const (
27-
hookBatchSize = 30
27+
hookBatchSize = 500
2828
)
2929

3030
var (

cmd/web_graceful.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,6 @@ func NoHTTPRedirector() {
2323
graceful.GetManager().InformCleanup()
2424
}
2525

26-
// NoMainListener tells our cleanup routine that we will not be using a possibly provided listener
27-
// for our main HTTP/HTTPS service
28-
func NoMainListener() {
29-
graceful.GetManager().InformCleanup()
30-
}
31-
3226
// NoInstallListener tells our cleanup routine that we will not be using a possibly provided listener
3327
// for our install HTTP/HTTPS service
3428
func NoInstallListener() {

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ replace github.com/hashicorp/go-version => github.com/6543/go-version v1.3.1
317317

318318
replace github.com/shurcooL/vfsgen => github.com/lunny/vfsgen v0.0.0-20220105142115-2c99e1ffdfa0
319319

320-
replace github.com/nektos/act => gitea.com/gitea/act v0.261.4
320+
replace github.com/nektos/act => gitea.com/gitea/act v0.261.6
321321

322322
// TODO: the only difference is in `PutObject`: the fork doesn't use `NewVerifyingReader(r, sha256.New(), oid, expectedSize)`, need to figure out why
323323
replace github.com/charmbracelet/git-lfs-transfer => gitea.com/gitea/git-lfs-transfer v0.2.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ dario.cat/mergo v1.0.1 h1:Ra4+bf83h2ztPIQYNP99R6m+Y7KfnARDfID+a+vLl4s=
1414
dario.cat/mergo v1.0.1/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk=
1515
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
1616
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
17-
gitea.com/gitea/act v0.261.4 h1:Tf9eLlvsYFtKcpuxlMvf9yT3g4Hshb2Beqw6C1STuH8=
18-
gitea.com/gitea/act v0.261.4/go.mod h1:Pg5C9kQY1CEA3QjthjhlrqOC/QOT5NyWNjOjRHw23Ok=
17+
gitea.com/gitea/act v0.261.6 h1:CjZwKOyejonNFDmsXOw3wGm5Vet573hHM6VMLsxtvPY=
18+
gitea.com/gitea/act v0.261.6/go.mod h1:Pg5C9kQY1CEA3QjthjhlrqOC/QOT5NyWNjOjRHw23Ok=
1919
gitea.com/gitea/git-lfs-transfer v0.2.0 h1:baHaNoBSRaeq/xKayEXwiDQtlIjps4Ac/Ll4KqLMB40=
2020
gitea.com/gitea/git-lfs-transfer v0.2.0/go.mod h1:UrXUCm3xLQkq15fu7qlXHUMlrhdlXHoi13KH2Dfiits=
2121
gitea.com/gitea/go-xsd-duration v0.0.0-20220703122237-02e73435a078 h1:BAFmdZpRW7zMQZQDClaCWobRj9uL1MR3MzpCVJvc5s4=

models/actions/runner.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package actions
55

66
import (
77
"context"
8+
"errors"
89
"fmt"
910
"strings"
1011
"time"
@@ -298,6 +299,23 @@ func DeleteRunner(ctx context.Context, id int64) error {
298299
return err
299300
}
300301

302+
// DeleteEphemeralRunner deletes a ephemeral runner by given ID.
303+
func DeleteEphemeralRunner(ctx context.Context, id int64) error {
304+
runner, err := GetRunnerByID(ctx, id)
305+
if err != nil {
306+
if errors.Is(err, util.ErrNotExist) {
307+
return nil
308+
}
309+
return err
310+
}
311+
if !runner.Ephemeral {
312+
return nil
313+
}
314+
315+
_, err = db.DeleteByID[ActionRunner](ctx, id)
316+
return err
317+
}
318+
301319
// CreateRunner creates new runner.
302320
func CreateRunner(ctx context.Context, t *ActionRunner) error {
303321
if t.OwnerID != 0 && t.RepoID != 0 {

models/actions/task.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,11 @@ func UpdateTask(ctx context.Context, task *ActionTask, cols ...string) error {
336336
sess.Cols(cols...)
337337
}
338338
_, err := sess.Update(task)
339+
340+
// Automatically delete the ephemeral runner if the task is done
341+
if err == nil && task.Status.IsDone() && util.SliceContainsString(cols, "status") {
342+
return DeleteEphemeralRunner(ctx, task.RunnerID)
343+
}
339344
return err
340345
}
341346

models/actions/utils.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,22 @@ func calculateDuration(started, stopped timeutil.TimeStamp, status Status) time.
8282
}
8383
return timeSince(s).Truncate(time.Second)
8484
}
85+
86+
// best effort function to convert an action schedule to action run, to be used in GenerateGiteaContext
87+
func (s *ActionSchedule) ToActionRun() *ActionRun {
88+
return &ActionRun{
89+
Title: s.Title,
90+
RepoID: s.RepoID,
91+
Repo: s.Repo,
92+
OwnerID: s.OwnerID,
93+
WorkflowID: s.WorkflowID,
94+
TriggerUserID: s.TriggerUserID,
95+
TriggerUser: s.TriggerUser,
96+
Ref: s.Ref,
97+
CommitSHA: s.CommitSHA,
98+
Event: s.Event,
99+
EventPayload: s.EventPayload,
100+
Created: s.Created,
101+
Updated: s.Updated,
102+
}
103+
}

models/activities/user_heatmap.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func getUserHeatmapData(ctx context.Context, user *user_model.User, team *organi
6666
Select(groupBy+" AS timestamp, count(user_id) as contributions").
6767
Table("action").
6868
Where(cond).
69-
And("created_unix > ?", timeutil.TimeStampNow()-31536000).
69+
And("created_unix > ?", timeutil.TimeStampNow()-(366+7)*86400). // (366+7) days to include the first week for the heatmap
7070
GroupBy(groupByName).
7171
OrderBy("timestamp").
7272
Find(&hdata)

models/fixtures/action_runner.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,14 @@
3838
repo_id: 0
3939
description: "This runner is going to be deleted"
4040
agent_labels: '["runner_to_be_deleted","linux"]'
41+
-
42+
id: 34350
43+
name: runner_to_be_deleted-org-ephemeral
44+
uuid: 3FF231BD-FBB7-4E4B-9602-E6F28363EF20
45+
token_hash: 3FF231BD-FBB7-4E4B-9602-E6F28363EF20
46+
ephemeral: true
47+
version: "1.0.0"
48+
owner_id: 3
49+
repo_id: 0
50+
description: "This runner is going to be deleted"
51+
agent_labels: '["runner_to_be_deleted","linux"]'

models/fixtures/action_task.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,26 @@
117117
log_length: 707
118118
log_size: 90179
119119
log_expired: 0
120+
-
121+
id: 52
122+
job_id: 196
123+
attempt: 1
124+
runner_id: 34350
125+
status: 6 # running
126+
started: 1683636528
127+
stopped: 1683636626
128+
repo_id: 4
129+
owner_id: 1
130+
commit_sha: c2d72f548424103f01ee1dc02889c1e2bff816b0
131+
is_fork_pull_request: 0
132+
token_hash: f8d3962425466b6709b9ac51446f93260c54afe8e7b6d3686e34f991fb8a8953822b0deed86fe41a103f34bc48dbc4784222
133+
token_salt: ffffffffff
134+
token_last_eight: ffffffff
135+
log_filename: artifact-test2/2f/47.log
136+
log_in_storage: 1
137+
log_length: 707
138+
log_size: 90179
139+
log_expired: 0
120140
-
121141
id: 53
122142
job_id: 198

models/fixtures/commit_status.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
target_url: https://example.com/builds/
88
description: My awesome CI-service
99
context: ci/awesomeness
10+
context_hash: c65f4d64a3b14a3eced0c9b36799e66e1bd5ced7
1011
creator_id: 2
1112

1213
-
@@ -18,6 +19,7 @@
1819
target_url: https://example.com/converage/
1920
description: My awesome Coverage service
2021
context: cov/awesomeness
22+
context_hash: 3929ac7bccd3fa1bf9b38ddedb77973b1b9a8cfe
2123
creator_id: 2
2224

2325
-
@@ -29,6 +31,7 @@
2931
target_url: https://example.com/converage/
3032
description: My awesome Coverage service
3133
context: cov/awesomeness
34+
context_hash: 3929ac7bccd3fa1bf9b38ddedb77973b1b9a8cfe
3235
creator_id: 2
3336

3437
-
@@ -40,6 +43,7 @@
4043
target_url: https://example.com/builds/
4144
description: My awesome CI-service
4245
context: ci/awesomeness
46+
context_hash: c65f4d64a3b14a3eced0c9b36799e66e1bd5ced7
4347
creator_id: 2
4448

4549
-
@@ -51,4 +55,5 @@
5155
target_url: https://example.com/builds/
5256
description: My awesome deploy service
5357
context: deploy/awesomeness
58+
context_hash: ae9547713a6665fc4261d0756904932085a41cf2
5459
creator_id: 2

models/fixtures/hook_task.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
id: 2
1919
hook_id: 1
2020
uuid: uuid2
21-
is_delivered: false
21+
is_delivered: true
2222

2323
-
2424
id: 3

models/git/branch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ func FindRecentlyPushedNewBranches(ctx context.Context, doer *user_model.User, o
487487
ForkFrom: opts.BaseRepo.ID,
488488
Archived: optional.Some(false),
489489
}
490-
repoCond := repo_model.SearchRepositoryCondition(&repoOpts).And(repo_model.AccessibleRepositoryCondition(doer, unit.TypeCode))
490+
repoCond := repo_model.SearchRepositoryCondition(repoOpts).And(repo_model.AccessibleRepositoryCondition(doer, unit.TypeCode))
491491
if opts.Repo.ID == opts.BaseRepo.ID {
492492
// should also include the base repo's branches
493493
repoCond = repoCond.Or(builder.Eq{"id": opts.BaseRepo.ID})

models/git/commit_status.go

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -230,18 +230,24 @@ func (status *CommitStatus) HideActionsURL(ctx context.Context) {
230230

231231
// CalcCommitStatus returns commit status state via some status, the commit statues should order by id desc
232232
func CalcCommitStatus(statuses []*CommitStatus) *CommitStatus {
233+
// This function is widely used, but it is not quite right.
234+
// Ideally it should return something like "CommitStatusSummary" with properly aggregated state.
235+
// GitHub's behavior: if all statuses are "skipped", GitHub will return "success" as the combined status.
233236
var lastStatus *CommitStatus
234237
state := api.CommitStatusSuccess
235238
for _, status := range statuses {
236-
if status.State.NoBetterThan(state) {
239+
if state == status.State || status.State.HasHigherPriorityThan(state) {
237240
state = status.State
238241
lastStatus = status
239242
}
240243
}
241244
if lastStatus == nil {
242245
if len(statuses) > 0 {
246+
// FIXME: a bad case: Gitea just returns the first commit status, its status is "skipped" in this case.
243247
lastStatus = statuses[0]
244248
} else {
249+
// FIXME: another bad case: if the "statuses" slice is empty, the returned value is an invalid CommitStatus, all its fields are empty.
250+
// Frontend code (tmpl&vue) sometimes depend on the empty fields to skip rendering commit status elements (need to double check in the future)
245251
lastStatus = &CommitStatus{}
246252
}
247253
}
@@ -298,27 +304,37 @@ type CommitStatusIndex struct {
298304
MaxIndex int64 `xorm:"index"`
299305
}
300306

307+
func makeRepoCommitQuery(ctx context.Context, repoID int64, sha string) *xorm.Session {
308+
return db.GetEngine(ctx).Table(&CommitStatus{}).
309+
Where("repo_id = ?", repoID).And("sha = ?", sha)
310+
}
311+
301312
// GetLatestCommitStatus returns all statuses with a unique context for a given commit.
302-
func GetLatestCommitStatus(ctx context.Context, repoID int64, sha string, listOptions db.ListOptions) ([]*CommitStatus, int64, error) {
303-
getBase := func() *xorm.Session {
304-
return db.GetEngine(ctx).Table(&CommitStatus{}).
305-
Where("repo_id = ?", repoID).And("sha = ?", sha)
306-
}
313+
func GetLatestCommitStatus(ctx context.Context, repoID int64, sha string, listOptions db.ListOptions) ([]*CommitStatus, error) {
307314
indices := make([]int64, 0, 10)
308-
sess := getBase().Select("max( `index` ) as `index`").
309-
GroupBy("context_hash").OrderBy("max( `index` ) desc")
315+
sess := makeRepoCommitQuery(ctx, repoID, sha).
316+
Select("max( `index` ) as `index`").
317+
GroupBy("context_hash").
318+
OrderBy("max( `index` ) desc")
310319
if !listOptions.IsListAll() {
311320
sess = db.SetSessionPagination(sess, &listOptions)
312321
}
313-
count, err := sess.FindAndCount(&indices)
314-
if err != nil {
315-
return nil, count, err
322+
if err := sess.Find(&indices); err != nil {
323+
return nil, err
316324
}
317325
statuses := make([]*CommitStatus, 0, len(indices))
318326
if len(indices) == 0 {
319-
return statuses, count, nil
327+
return statuses, nil
320328
}
321-
return statuses, count, getBase().And(builder.In("`index`", indices)).Find(&statuses)
329+
err := makeRepoCommitQuery(ctx, repoID, sha).And(builder.In("`index`", indices)).Find(&statuses)
330+
return statuses, err
331+
}
332+
333+
func CountLatestCommitStatus(ctx context.Context, repoID int64, sha string) (int64, error) {
334+
return makeRepoCommitQuery(ctx, repoID, sha).
335+
Select("count(context_hash)").
336+
GroupBy("context_hash").
337+
Count()
322338
}
323339

324340
// GetLatestCommitStatusForPairs returns all statuses with a unique context for a given list of repo-sha pairs

models/git/commit_status_summary.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,15 @@ func GetLatestCommitStatusForRepoAndSHAs(ctx context.Context, repoSHAs []RepoSHA
5555
}
5656

5757
func UpdateCommitStatusSummary(ctx context.Context, repoID int64, sha string) error {
58-
commitStatuses, _, err := GetLatestCommitStatus(ctx, repoID, sha, db.ListOptionsAll)
58+
commitStatuses, err := GetLatestCommitStatus(ctx, repoID, sha, db.ListOptionsAll)
5959
if err != nil {
6060
return err
6161
}
62-
state := CalcCommitStatus(commitStatuses)
62+
// it guarantees that commitStatuses is not empty because this function is always called after a commit status is created
63+
if len(commitStatuses) == 0 {
64+
setting.PanicInDevOrTesting("no commit statuses found for repo %d and sha %s", repoID, sha)
65+
}
66+
state := CalcCommitStatus(commitStatuses) // non-empty commitStatuses is guaranteed
6367
// mysql will return 0 when update a record which state hasn't been changed which behaviour is different from other database,
6468
// so we need to use insert in on duplicate
6569
if setting.Database.Type.IsMySQL() {

0 commit comments

Comments
 (0)