@@ -17,7 +17,9 @@ import (
17
17
"code.gitea.io/gitea/models/unittest"
18
18
user_model "code.gitea.io/gitea/models/user"
19
19
api "code.gitea.io/gitea/modules/structs"
20
+ "code.gitea.io/gitea/modules/timeutil"
20
21
"code.gitea.io/gitea/modules/util"
22
+ webhook_module "code.gitea.io/gitea/modules/webhook"
21
23
actions_service "code.gitea.io/gitea/services/actions"
22
24
23
25
"github.com/stretchr/testify/assert"
@@ -470,7 +472,7 @@ jobs:
470
472
"appVersion" : "v1.22" ,
471
473
})
472
474
session .MakeRequest (t , req , http .StatusSeeOther )
473
- runner .fetchNoTask (t ) // cannot fetch task since task2 is not completed
475
+ runner .fetchNoTask (t ) // cannot fetch task because task2 is not completed
474
476
475
477
// run the workflow with appVersion=v1.22 and cancel=true
476
478
req = NewRequestWithValues (t , "POST" , urlStr , map [string ]string {
@@ -499,39 +501,95 @@ func TestScheduleConcurrency(t *testing.T) {
499
501
500
502
apiRepo := createActionsTestRepo (t , token , "actions-concurrency" , false )
501
503
repo := unittest .AssertExistsAndLoadBean (t , & repo_model.Repository {ID : apiRepo .ID })
504
+ httpContext := NewAPITestContext (t , user2 .Name , repo .Name , auth_model .AccessTokenScopeWriteRepository )
505
+ defer doAPIDeleteRepository (httpContext )(t )
502
506
runner := newMockRunner ()
503
507
runner .registerAsRepoRunner (t , user2 .Name , repo .Name , "mock-runner" , []string {"ubuntu-latest" })
504
508
505
- // add a variable for test
506
- req := NewRequestWithJSON (t , "POST" ,
507
- fmt .Sprintf ("/api/v1/repos/%s/%s/actions/variables/cancel_schedule" , user2 .Name , repo .Name ), & api.CreateVariableOption {
508
- Value : "false" ,
509
- }).
510
- AddTokenAuth (token )
511
- MakeRequest (t , req , http .StatusNoContent )
512
-
513
509
wf1TreePath := ".gitea/workflows/schedule-concurrency.yml"
514
510
wf1FileContent := `name: schedule-concurrency
515
511
on:
512
+ push:
516
513
schedule:
517
- - cron: '0/5 * * * * '
514
+ - cron: '@every 1m '
518
515
concurrency:
519
516
group: schedule-concurrency
520
- cancel-in-progress: ${{ vars.cancel_schedule == 'false ' }}
517
+ cancel-in-progress: ${{ gitea.event_name == 'push ' }}
521
518
jobs:
522
519
job:
523
520
runs-on: ubuntu-latest
524
521
steps:
525
- - run: echo 'workflow dispatch job '
522
+ - run: echo 'schedule workflow '
526
523
`
527
524
528
525
opts1 := getWorkflowCreateFileOptions (user2 , repo .DefaultBranch , fmt .Sprintf ("create %s" , wf1TreePath ), wf1FileContent )
529
526
createWorkflowFile (t , token , user2 .Name , repo .Name , wf1TreePath , opts1 )
530
527
528
+ // fetch the task triggered by push
529
+ task1 := runner .fetchTask (t )
530
+ _ , _ , run1 := getTaskAndJobAndRunByTaskID (t , task1 .Id )
531
+ assert .Equal (t , "schedule-concurrency" , run1 .ConcurrencyGroup )
532
+ assert .True (t , run1 .ConcurrencyCancel )
533
+ assert .Equal (t , string (webhook_module .HookEventPush ), run1 .TriggerEvent )
534
+ assert .Equal (t , actions_model .StatusRunning , run1 .Status )
535
+
536
+ // trigger the task by schedule
537
+ spec := unittest .AssertExistsAndLoadBean (t , & actions_model.ActionScheduleSpec {RepoID : repo .ID })
538
+ spec .Next = timeutil .TimeStampNow () // manually update "Next"
539
+ assert .NoError (t , actions_model .UpdateScheduleSpec (context .Background (), spec , "next" ))
540
+ assert .NoError (t , actions_service .StartScheduleTasks (context .Background ()))
541
+ runner .fetchNoTask (t ) // cannot fetch because task1 is not completed
542
+ runner .execTask (t , task1 , & mockTaskOutcome {
543
+ result : runnerv1 .Result_RESULT_SUCCESS ,
544
+ })
545
+ _ , _ , run1 = getTaskAndJobAndRunByTaskID (t , task1 .Id )
546
+ assert .Equal (t , actions_model .StatusSuccess , run1 .Status )
547
+ task2 := runner .fetchTask (t )
548
+ _ , _ , run2 := getTaskAndJobAndRunByTaskID (t , task2 .Id )
549
+ assert .Equal (t , "schedule-concurrency" , run2 .ConcurrencyGroup )
550
+ assert .False (t , run2 .ConcurrencyCancel )
551
+ assert .Equal (t , string (webhook_module .HookEventSchedule ), run2 .TriggerEvent )
552
+ assert .Equal (t , actions_model .StatusRunning , run2 .Status )
553
+
554
+ // trigger the task by schedule again
555
+ spec = unittest .AssertExistsAndLoadBean (t , & actions_model.ActionScheduleSpec {RepoID : repo .ID })
556
+ spec .Next = timeutil .TimeStampNow () // manually update "Next"
557
+ assert .NoError (t , actions_model .UpdateScheduleSpec (context .Background (), spec , "next" ))
531
558
assert .NoError (t , actions_service .StartScheduleTasks (context .Background ()))
559
+ runner .fetchNoTask (t ) // cannot fetch because task2 is not completed
560
+ run3 := unittest .AssertExistsAndLoadBean (t , & actions_model.ActionRun {RepoID : repo .ID , Status : actions_model .StatusBlocked })
561
+ assert .Equal (t , "schedule-concurrency" , run3 .ConcurrencyGroup )
562
+ assert .False (t , run3 .ConcurrencyCancel )
563
+ assert .Equal (t , string (webhook_module .HookEventSchedule ), run3 .TriggerEvent )
564
+
565
+ // trigger the task by push
566
+ doAPICreateFile (httpContext , "doc.txt" , & api.CreateFileOptions {
567
+ FileOptions : api.FileOptions {
568
+ NewBranchName : "main" ,
569
+ Message : "create doc.txt" ,
570
+ Author : api.Identity {
571
+ Name : user2 .Name ,
572
+ Email : user2 .Email ,
573
+ },
574
+ Committer : api.Identity {
575
+ Name : user2 .Name ,
576
+ Email : user2 .Email ,
577
+ },
578
+ Dates : api.CommitDateOptions {
579
+ Author : time .Now (),
580
+ Committer : time .Now (),
581
+ },
582
+ },
583
+ ContentBase64 : base64 .StdEncoding .EncodeToString ([]byte ("doc" )),
584
+ })(t )
532
585
533
- httpContext := NewAPITestContext (t , user2 .Name , repo .Name , auth_model .AccessTokenScopeWriteRepository )
534
- doAPIDeleteRepository (httpContext )(t )
586
+ task4 := runner .fetchTask (t )
587
+ _ , _ , run4 := getTaskAndJobAndRunByTaskID (t , task4 .Id )
588
+ assert .Equal (t , "schedule-concurrency" , run4 .ConcurrencyGroup )
589
+ assert .True (t , run4 .ConcurrencyCancel )
590
+ assert .Equal (t , string (webhook_module .HookEventPush ), run4 .TriggerEvent )
591
+ run3 = unittest .AssertExistsAndLoadBean (t , & actions_model.ActionRun {ID : run3 .ID })
592
+ assert .Equal (t , actions_model .StatusCancelled , run3 .Status )
535
593
})
536
594
}
537
595
0 commit comments