Skip to content

Commit 351dfb4

Browse files
authored
Merge branch 'main' into message-blank-line
2 parents d296520 + 74858dc commit 351dfb4

File tree

97 files changed

+1247
-1147
lines changed

Some content is hidden

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

97 files changed

+1247
-1147
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/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 (

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/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/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() {

models/git/commit_status_test.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func TestGetCommitStatuses(t *testing.T) {
2626

2727
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
2828

29-
sha1 := "1234123412341234123412341234123412341234"
29+
sha1 := "1234123412341234123412341234123412341234" // the mocked commit ID in test fixtures
3030

3131
statuses, maxResults, err := db.FindAndCount[git_model.CommitStatus](db.DefaultContext, &git_model.CommitStatusOptions{
3232
ListOptions: db.ListOptions{Page: 1, PageSize: 50},
@@ -256,3 +256,26 @@ func TestCommitStatusesHideActionsURL(t *testing.T) {
256256
assert.Empty(t, statuses[0].TargetURL)
257257
assert.Equal(t, "https://mycicd.org/1", statuses[1].TargetURL)
258258
}
259+
260+
func TestGetCountLatestCommitStatus(t *testing.T) {
261+
assert.NoError(t, unittest.PrepareTestDatabase())
262+
263+
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
264+
265+
sha1 := "1234123412341234123412341234123412341234" // the mocked commit ID in test fixtures
266+
267+
commitStatuses, err := git_model.GetLatestCommitStatus(db.DefaultContext, repo1.ID, sha1, db.ListOptions{
268+
Page: 1,
269+
PageSize: 2,
270+
})
271+
assert.NoError(t, err)
272+
assert.Len(t, commitStatuses, 2)
273+
assert.Equal(t, structs.CommitStatusFailure, commitStatuses[0].State)
274+
assert.Equal(t, "ci/awesomeness", commitStatuses[0].Context)
275+
assert.Equal(t, structs.CommitStatusError, commitStatuses[1].State)
276+
assert.Equal(t, "deploy/awesomeness", commitStatuses[1].Context)
277+
278+
count, err := git_model.CountLatestCommitStatus(db.DefaultContext, repo1.ID, sha1)
279+
assert.NoError(t, err)
280+
assert.EqualValues(t, 3, count)
281+
}

0 commit comments

Comments
 (0)